Skip to content

Commit

Permalink
Merge pull request #12865 from ioforks/fix-12858-kubernetes-remote-dev
Browse files Browse the repository at this point in the history
Catch and log when project and container image info can't be created
  • Loading branch information
gsmet authored Oct 27, 2020
2 parents 569016c + 4ff066e commit b79b432
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import java.util.Collections;
import java.util.Optional;

import org.jboss.logging.Logger;

import io.quarkus.container.spi.ContainerImageInfoBuildItem;
import io.quarkus.container.spi.ImageReference;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;

public class ContainerImageProcessor {

private static final String UNKNOWN_USER = "?";
private static final Logger log = Logger.getLogger(ContainerImageProcessor.class);

@BuildStep
public ContainerImageInfoBuildItem publishImageInfo(ApplicationInfoBuildItem app,
ContainerImageConfig containerImageConfig, Capabilities capabilities) {
public void publishImageInfo(ApplicationInfoBuildItem app,
ContainerImageConfig containerImageConfig, Capabilities capabilities,
BuildProducer<ContainerImageInfoBuildItem> containerImage) {

ensureSingleContainerImageExtension(capabilities);

Expand All @@ -27,12 +34,21 @@ public ContainerImageInfoBuildItem publishImageInfo(ApplicationInfoBuildItem app
}
}

String effectiveGroup = containerImageConfig.getEffectiveGroup().orElse("");
//This can be the case when running inside kubernetes/minikube in dev-mode. Instead of failing, we should just catch and log.
if (UNKNOWN_USER.equals(effectiveGroup)) {
log.warn(
"Can't get the current user name, which is used as default for container image group. Can't publish container image info.");
return;
}

// if the user supplied the entire image string, use it
if (containerImageConfig.image.isPresent()) {
ImageReference imageReference = ImageReference.parse(containerImageConfig.image.get());
String repository = imageReference.getRepository();
return new ContainerImageInfoBuildItem(Optional.of(imageReference.getRegistry()), repository,
imageReference.getTag(), containerImageConfig.additionalTags.orElse(Collections.emptyList()));
containerImage.produce(new ContainerImageInfoBuildItem(Optional.of(imageReference.getRegistry()), repository,
imageReference.getTag(), containerImageConfig.additionalTags.orElse(Collections.emptyList())));
return;
}

String registry = containerImageConfig.registry.orElse(null);
Expand All @@ -44,18 +60,18 @@ public ContainerImageInfoBuildItem publishImageInfo(ApplicationInfoBuildItem app
String repository = (containerImageConfig.getEffectiveGroup().map(s -> s + "/").orElse("")) + effectiveName;
if (!ImageReference.isValidRepository(repository)) {
throw new IllegalArgumentException("The supplied combination of container-image group '"
+ containerImageConfig.getEffectiveGroup().orElse("") + "' and name '" + effectiveName + "' is invalid");
+ effectiveGroup + "' and name '" + effectiveName + "' is invalid");
}

final String effectiveTag = containerImageConfig.tag.orElse(app.getVersion());
if (!ImageReference.isValidTag(effectiveTag)) {
throw new IllegalArgumentException("The supplied container-image tag '" + effectiveTag + "' is invalid");
}

return new ContainerImageInfoBuildItem(containerImageConfig.registry,
containerImage.produce(new ContainerImageInfoBuildItem(containerImageConfig.registry,
containerImageConfig.getEffectiveGroup(),
effectiveName, effectiveTag,
containerImageConfig.additionalTags.orElse(Collections.emptyList()));
containerImageConfig.additionalTags.orElse(Collections.emptyList())));
}

private void ensureSingleContainerImageExtension(Capabilities capabilities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
List<DecoratorBuildItem> result = new ArrayList<>();
String name = ResourceNameUtil.getResourceName(config, applicationInfo);

Project project = KubernetesCommonHelper.createProject(applicationInfo, outputTarget, packageConfig);
Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, outputTarget, packageConfig);
result.addAll(KubernetesCommonHelper.createDecorators(project, MINIKUBE, name, config, metricsConfiguration,
annotations, labels, command,
ports, livenessPath, readinessPath, roles, roleBindings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
List<DecoratorBuildItem> result = new ArrayList<>();
String name = ResourceNameUtil.getResourceName(config, applicationInfo);

Project project = KubernetesCommonHelper.createProject(applicationInfo, outputTarget, packageConfig);
Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, outputTarget, packageConfig);
result.addAll(KubernetesCommonHelper.createDecorators(project, KNATIVE, name, config, metricsConfiguration, annotations,
labels, command,
ports, livenessPath, readinessPath, roles, roleBindings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,28 @@ public class KubernetesCommonHelper {

private static final String OUTPUT_ARTIFACT_FORMAT = "%s%s.jar";

public static Project createProject(ApplicationInfoBuildItem app, OutputTargetBuildItem outputTarget,
public static Optional<Project> createProject(ApplicationInfoBuildItem app, OutputTargetBuildItem outputTarget,
PackageConfig packageConfig) {
return createProject(app, outputTarget.getOutputDirectory()
.resolve(String.format(OUTPUT_ARTIFACT_FORMAT, outputTarget.getBaseName(), packageConfig.runnerSuffix)));
}

public static Project createProject(ApplicationInfoBuildItem app, Path artifactPath) {
public static Optional<Project> createProject(ApplicationInfoBuildItem app, Path artifactPath) {
//Let dekorate create a Project instance and then override with what is found in ApplicationInfoBuildItem.
Project project = FileProjectFactory.create(artifactPath.toFile());
BuildInfo buildInfo = new BuildInfo(app.getName(), app.getVersion(),
"jar", project.getBuildInfo().getBuildTool(),
project.getBuildInfo().getBuildToolVersion(),
artifactPath.toAbsolutePath(),
project.getBuildInfo().getClassOutputDir(),
project.getBuildInfo().getResourceDir());

return new Project(project.getRoot(), buildInfo, project.getScmInfo());
try {
Project project = FileProjectFactory.create(artifactPath.toFile());
BuildInfo buildInfo = new BuildInfo(app.getName(), app.getVersion(),
"jar", project.getBuildInfo().getBuildTool(),
project.getBuildInfo().getBuildToolVersion(),
artifactPath.toAbsolutePath(),
project.getBuildInfo().getClassOutputDir(),
project.getBuildInfo().getResourceDir());

return Optional.of(new Project(project.getRoot(), buildInfo, project.getScmInfo()));

} catch (Exception e) {
return Optional.empty();
}
}

/**
Expand All @@ -112,7 +117,7 @@ public static List<ConfiguratorBuildItem> createPlatformConfigurators(PlatformCo
/**
* Creates the common decorator build items.
*/
public static List<DecoratorBuildItem> createDecorators(Project project, String target, String name,
public static List<DecoratorBuildItem> createDecorators(Optional<Project> project, String target, String name,
PlatformConfiguration config,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
List<KubernetesAnnotationBuildItem> annotations,
Expand Down Expand Up @@ -177,7 +182,7 @@ public static List<DecoratorBuildItem> createDecorators(Project project, String
* @param name The name of the resource to accept the configuration
* @param config The {@link PlatformConfiguration} instance
*/
private static List<DecoratorBuildItem> createContainerDecorators(Project project, String target, String name,
private static List<DecoratorBuildItem> createContainerDecorators(Optional<Project> project, String target, String name,
PlatformConfiguration config) {
List<DecoratorBuildItem> result = new ArrayList<>();
if (config.getNamespace().isPresent()) {
Expand Down Expand Up @@ -206,7 +211,7 @@ private static List<DecoratorBuildItem> createContainerDecorators(Project projec
* @param name The name of the resource to accept the configuration
* @param config The {@link PlatformConfiguration} instance
*/
private static List<DecoratorBuildItem> createPodDecorators(Project project, String target, String name,
private static List<DecoratorBuildItem> createPodDecorators(Optional<Project> project, String target, String name,
PlatformConfiguration config) {
List<DecoratorBuildItem> result = new ArrayList<>();
config.getImagePullSecrets().ifPresent(l -> {
Expand Down Expand Up @@ -248,7 +253,8 @@ private static List<DecoratorBuildItem> createPodDecorators(Project project, Str
return result;
}

private static List<DecoratorBuildItem> createMountAndVolumeDecorators(Project project, String target, String name,
private static List<DecoratorBuildItem> createMountAndVolumeDecorators(Optional<Project> project, String target,
String name,
PlatformConfiguration config) {
List<DecoratorBuildItem> result = new ArrayList<>();
config.getMounts().entrySet().forEach(e -> {
Expand Down Expand Up @@ -282,30 +288,32 @@ private static List<DecoratorBuildItem> createMountAndVolumeDecorators(Project p
return result;
}

private static List<DecoratorBuildItem> createAnnotationDecorators(Project project, String target, String name,
private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Project> project, String target, String name,
PlatformConfiguration config,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
List<KubernetesPortBuildItem> ports) {
List<DecoratorBuildItem> result = new ArrayList<>();
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);

ScmInfo scm = project.getScmInfo();
String vcsUrl = scm != null ? scm.getUrl() : null;
String commitId = scm != null ? scm.getCommit() : null;
project.ifPresent(p -> {
ScmInfo scm = p.getScmInfo();
String vcsUrl = scm != null ? scm.getUrl() : null;
String commitId = scm != null ? scm.getCommit() : null;

//Dekorate uses its own annotations. Let's replace them with the quarkus ones.
result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.VCS_URL)));
result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.COMMIT_ID)));
//Dekorate uses its own annotations. Let's replace them with the quarkus ones.
result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.VCS_URL)));
result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.COMMIT_ID)));

//Add quarkus vcs annotations
if (commitId != null) {
result.add(new DecoratorBuildItem(target,
new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_COMMIT_ID, commitId, new String[0]))));
}
if (vcsUrl != null) {
result.add(new DecoratorBuildItem(target,
new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_VCS_URL, vcsUrl, new String[0]))));
}
//Add quarkus vcs annotations
if (commitId != null) {
result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name,
new Annotation(QUARKUS_ANNOTATIONS_COMMIT_ID, commitId, new String[0]))));
}
if (vcsUrl != null) {
result.add(new DecoratorBuildItem(target,
new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_VCS_URL, vcsUrl, new String[0]))));
}
});

if (config.isAddBuildTimestamp()) {
result.add(new DecoratorBuildItem(target,
Expand Down
Loading

0 comments on commit b79b432

Please sign in to comment.