Skip to content

Commit

Permalink
When minikube is being used, never push an container-image
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and gsmet committed Jul 17, 2020
1 parent c50ee6e commit 2750309
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.kubernetes.deployment;

public class DeploymentTargetEntry {
private final String name;
private final String kind;
private final int priority;

public DeploymentTargetEntry(String name, String kind, int priority) {
this.name = name;
this.kind = kind;
this.priority = priority;
}

public String getName() {
return name;
}

public String getKind() {
return kind;
}

public int getPriority() {
return priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,17 @@

public final class EnabledKubernetesDeploymentTargetsBuildItem extends SimpleBuildItem {

private final List<Entry> entriesSortedByPriority;
private final List<DeploymentTargetEntry> entriesSortedByPriority;

public EnabledKubernetesDeploymentTargetsBuildItem(List<Entry> entriesSortedByPriority) {
public EnabledKubernetesDeploymentTargetsBuildItem(List<DeploymentTargetEntry> entriesSortedByPriority) {
if (entriesSortedByPriority.isEmpty()) {
throw new IllegalArgumentException("At least one enabled entry must be active");
}
this.entriesSortedByPriority = entriesSortedByPriority;
}

public List<Entry> getEntriesSortedByPriority() {
public List<DeploymentTargetEntry> getEntriesSortedByPriority() {
return entriesSortedByPriority;
}

public static class Entry {
private final String name;
private final String kind;
private final int priority;

public Entry(String name, String kind, int priority) {
this.name = name;
this.kind = kind;
this.priority = priority;
}

public String getName() {
return name;
}

public String getKind() {
return kind;
}

public int getPriority() {
return priority;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,29 @@ public class KubernetesDeployer {
.map(s -> "\"" + s + "\"").collect(Collectors.joining(", "));

@BuildStep(onlyIf = IsNormal.class)
public void deploy(KubernetesClientBuildItem kubernetesClient,
ContainerImageInfoBuildItem containerImageInfo,
public void selectDeploymentTarget(ContainerImageInfoBuildItem containerImageInfo,
EnabledKubernetesDeploymentTargetsBuildItem targets,
OutputTargetBuildItem outputTarget,
Capabilities capabilities,
BuildProducer<SelectedKubernetesDeploymentTargetBuildItem> selectedDeploymentTarget) {

Optional<String> activeContainerImageCapability = ContainerImageCapabilitiesUtil
.getActiveContainerImageCapability(capabilities);

if (!activeContainerImageCapability.isPresent()) {
// we can't thrown an exception here, because it could prevent the Kubernetes resources from being generated
return;
}

final DeploymentTargetEntry selectedTarget = determineDeploymentTarget(
containerImageInfo, targets, activeContainerImageCapability.get());
selectedDeploymentTarget.produce(new SelectedKubernetesDeploymentTargetBuildItem(selectedTarget));
}

@BuildStep(onlyIf = IsNormal.class)
public void deploy(KubernetesClientBuildItem kubernetesClient,
Capabilities capabilities,
Optional<SelectedKubernetesDeploymentTargetBuildItem> selectedDeploymentTarget,
OutputTargetBuildItem outputTarget,
BuildProducer<DeploymentResultBuildItem> deploymentResult,
// needed to ensure that this step runs after the container image has been built
@SuppressWarnings("unused") List<ArtifactResultBuildItem> artifactResults) {
Expand All @@ -62,21 +80,21 @@ public void deploy(KubernetesClientBuildItem kubernetesClient,
return;
}

Optional<String> activeContainerImageCapability = ContainerImageCapabilitiesUtil
.getActiveContainerImageCapability(capabilities);
if (!selectedDeploymentTarget.isPresent()) {

if (!activeContainerImageCapability.isPresent()) {
throw new RuntimeException(
"A Kubernetes deployment was requested but no extension was found to build a container image. Consider adding one of following extensions: "
+ CONTAINER_IMAGE_EXTENSIONS_STR + ".");
}
if (!ContainerImageCapabilitiesUtil
.getActiveContainerImageCapability(capabilities).isPresent()) {
throw new RuntimeException(
"A Kubernetes deployment was requested but no extension was found to build a container image. Consider adding one of following extensions: "
+ CONTAINER_IMAGE_EXTENSIONS_STR + ".");
}

final EnabledKubernetesDeploymentTargetsBuildItem.Entry selectedTarget = determineDeploymentTarget(
containerImageInfo, targets, activeContainerImageCapability.get());
return;
}

final KubernetesClient client = Clients.fromConfig(kubernetesClient.getClient().getConfiguration());
deploymentResult
.produce(deploy(selectedTarget, client, outputTarget.getOutputDirectory()));
.produce(deploy(selectedDeploymentTarget.get().getEntry(), client, outputTarget.getOutputDirectory()));
}

/**
Expand All @@ -90,10 +108,10 @@ public void deploy(KubernetesClientBuildItem kubernetesClient,
*
* If the user specifies deployment targets, pick the first one
*/
private EnabledKubernetesDeploymentTargetsBuildItem.Entry determineDeploymentTarget(
private DeploymentTargetEntry determineDeploymentTarget(
ContainerImageInfoBuildItem containerImageInfo,
EnabledKubernetesDeploymentTargetsBuildItem targets, String activeContainerImageCapability) {
final EnabledKubernetesDeploymentTargetsBuildItem.Entry selectedTarget;
final DeploymentTargetEntry selectedTarget;

boolean checkForMissingRegistry = true;
List<String> userSpecifiedDeploymentTargets = KubernetesConfigUtil.getUserSpecifiedDeploymentTargets();
Expand Down Expand Up @@ -139,7 +157,7 @@ private Optional<KubernetesDeploymentTargetBuildItem> getOptionalDeploymentTarge
return deploymentTargets.stream().filter(d -> name.equals(d.getName())).findFirst();
}

private DeploymentResultBuildItem deploy(EnabledKubernetesDeploymentTargetsBuildItem.Entry deploymentTarget,
private DeploymentResultBuildItem deploy(DeploymentTargetEntry deploymentTarget,
KubernetesClient client, Path outputDir) {
String namespace = Optional.ofNullable(client.getNamespace()).orElse("default");
log.info("Deploying to " + deploymentTarget.getName().toLowerCase() + " server: " + client.getMasterUrl()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.kubernetes.deployment;

import java.util.Optional;

import io.quarkus.container.spi.ContainerImageBuildRequestBuildItem;
import io.quarkus.container.spi.ContainerImageInfoBuildItem;
import io.quarkus.container.spi.ContainerImagePushRequestBuildItem;
Expand All @@ -11,18 +13,23 @@ public class KubernetesDeployerPrerequisite {

@BuildStep(onlyIf = IsNormal.class)
public void prepare(ContainerImageInfoBuildItem containerImage,
Optional<SelectedKubernetesDeploymentTargetBuildItem> selectedDeploymentTarget,
BuildProducer<ContainerImageBuildRequestBuildItem> buildRequestProducer,
BuildProducer<ContainerImagePushRequestBuildItem> pushRequestProducer) {

// we don't want to throw an exception at this step and fail the build because it could prevent
// the Kubernetes resources from being generated
if (!KubernetesDeploy.INSTANCE.checkSilently()) {
if (!KubernetesDeploy.INSTANCE.checkSilently() || !selectedDeploymentTarget.isPresent()) {
return;
}

//Let's communicate to the container-image plugin that we need an image build and an image push.
buildRequestProducer.produce(new ContainerImageBuildRequestBuildItem());
if (containerImage.getRegistry().isPresent()) {
// When a registry is present, we want to push the image
// However we need to make sure we don't push to the registry when deploying to Minikube
// since all updates are meant to find the image from the docker daemon
if (containerImage.getRegistry().isPresent() &&
!selectedDeploymentTarget.get().getEntry().getName().equals(Constants.MINIKUBE)) {
pushRequestProducer.produce(new ContainerImagePushRequestBuildItem());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTa
List<KubernetesDeploymentTargetBuildItem> mergedDeploymentTargets = mergeList(allDeploymentTargets);
Collections.sort(mergedDeploymentTargets);

List<EnabledKubernetesDeploymentTargetsBuildItem.Entry> entries = new ArrayList<>(mergedDeploymentTargets.size());
List<DeploymentTargetEntry> entries = new ArrayList<>(mergedDeploymentTargets.size());
for (KubernetesDeploymentTargetBuildItem deploymentTarget : mergedDeploymentTargets) {
if (deploymentTarget.isEnabled()) {
entries.add(new EnabledKubernetesDeploymentTargetsBuildItem.Entry(deploymentTarget.getName(),
entries.add(new DeploymentTargetEntry(deploymentTarget.getName(),
deploymentTarget.getKind(), deploymentTarget.getPriority()));
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public void build(ApplicationInfoBuildItem applicationInfo,

Map<String, Object> config = KubernetesConfigUtil.toMap(kubernetesConfig, openshiftConfig, knativeConfig);
Set<String> deploymentTargets = kubernetesDeploymentTargets.getEntriesSortedByPriority().stream()
.map(EnabledKubernetesDeploymentTargetsBuildItem.Entry::getName)
.map(DeploymentTargetEntry::getName)
.collect(Collectors.toSet());

Path artifactPath = outputTarget.getOutputDirectory().resolve(
Expand All @@ -283,7 +283,7 @@ public void build(ApplicationInfoBuildItem applicationInfo,
final SessionReader sessionReader = new SimpleFileReader(
project.getRoot().resolve("src").resolve("main").resolve("kubernetes"), kubernetesDeploymentTargets
.getEntriesSortedByPriority().stream()
.map(EnabledKubernetesDeploymentTargetsBuildItem.Entry::getName).collect(Collectors.toSet()));
.map(DeploymentTargetEntry::getName).collect(Collectors.toSet()));
sessionWriter.setProject(project);
final Session session = Session.getSession(new NoopLogger());
session.setWriter(sessionWriter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.kubernetes.deployment;

import io.quarkus.builder.item.SimpleBuildItem;

public final class SelectedKubernetesDeploymentTargetBuildItem extends SimpleBuildItem {

private final DeploymentTargetEntry entry;

public SelectedKubernetesDeploymentTargetBuildItem(DeploymentTargetEntry entry) {
this.entry = entry;
}

public DeploymentTargetEntry getEntry() {
return entry;
}
}

0 comments on commit 2750309

Please sign in to comment.