From 447c42012d5fa3249b989acc0580f9bba5197a56 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 27 Sep 2023 23:04:41 +0530 Subject: [PATCH] fix (jkube-kit/generator) : Do not add build timestamp to build date label (#2393) We add `org.label-schema.build-date` LABEL to image while creating opinionated container images. This causes Dockerfile to be different for every build and it doesn't utilize docker cache properly. Signed-off-by: Rohan Kumar --- CHANGELOG.md | 4 ++ .../generator/api/support/BaseGenerator.java | 5 ++- .../javaexec/JavaExecGeneratorTest.java | 45 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a089a07575..cc8a745bbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,8 +27,12 @@ Usage: * Fix #2369: Helm chart apiVersion can be configured * Fix #2386: Helm icon inferred from annotations in independent resource files (not aggregated kubernetes/openshift.yaml) * Fix #2397: Default JKube work directory (`jkube.workDir`) changed from `${project.build.directory}/jkube` to `${project.build.directory}/jkube-temp` +* Fix #2393: Remove timestamp from `org.label-schema.build-date` LABEL to utilize docker cache * Fix #2399: Helm no longer generates default function; broadens support for different value types +_**Note**_: +- Container Images generated using jkube opinionated defaults no longer contain full timestamp in `org.label-schema.build-date` label. The label contains the build date in the format `yyyy-MM-dd`. + ### 1.14.0 (2023-08-31) * Fix #1674: SpringBootGenerator utilizes the layered jar if present and use it as Docker layers * Fix #1713: Add HelidonHealthCheckEnricher to add Kubernetes health checks for Helidon applications diff --git a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/support/BaseGenerator.java b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/support/BaseGenerator.java index af60134735..d2e9b95883 100644 --- a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/support/BaseGenerator.java +++ b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/support/BaseGenerator.java @@ -15,6 +15,9 @@ import java.io.IOException; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -256,7 +259,7 @@ protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buil String docURL = project.getDocumentationUrl(); Map labels = new HashMap<>(); - labels.put(BuildLabelAnnotations.BUILD_DATE.value(), LocalDateTime.now().toString()); + labels.put(BuildLabelAnnotations.BUILD_DATE.value(), LocalDateTime.now().format(DateTimeFormatter.ISO_DATE)); labels.put(BuildLabelAnnotations.NAME.value(), project.getName()); labels.put(BuildLabelAnnotations.DESCRIPTION.value(), project.getDescription()); if (docURL != null) { diff --git a/jkube-kit/generator/java-exec/src/test/java/org/eclipse/jkube/generator/javaexec/JavaExecGeneratorTest.java b/jkube-kit/generator/java-exec/src/test/java/org/eclipse/jkube/generator/javaexec/JavaExecGeneratorTest.java index 3771b9df71..0e7be14f2b 100644 --- a/jkube-kit/generator/java-exec/src/test/java/org/eclipse/jkube/generator/javaexec/JavaExecGeneratorTest.java +++ b/jkube-kit/generator/java-exec/src/test/java/org/eclipse/jkube/generator/javaexec/JavaExecGeneratorTest.java @@ -14,6 +14,7 @@ package org.eclipse.jkube.generator.javaexec; import java.io.File; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -24,6 +25,8 @@ import org.eclipse.jkube.kit.common.AssemblyConfiguration; import org.eclipse.jkube.kit.common.Plugin; import org.assertj.core.api.InstanceOfAssertFactories; +import org.eclipse.jkube.kit.config.image.ImageConfiguration; +import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.MockedConstruction; @@ -46,6 +49,7 @@ void setUp() { generatorContext = mock(GeneratorContext.class, RETURNS_DEEP_STUBS); properties = new Properties(); plugins = new ArrayList<>(); + when(generatorContext.getProject().getVersion()).thenReturn("0.0.1"); when(generatorContext.getProject().getProperties()).thenReturn(properties); when(generatorContext.getProject().getPlugins()).thenReturn(plugins); } @@ -120,4 +124,45 @@ void createAssemblyWithFatJarShouldAddDefaultFileSetsAndFatJar() { ); } } + + @Test + void customize_whenInvoked_shouldAddLabelsToBuildConfiguration() { + // Given + properties.put("jkube.generator.java-exec.mainClass", "org.example.Foo"); + JavaExecGenerator javaExecGenerator = new JavaExecGenerator(generatorContext); + List result = new ArrayList<>(); + + // When + result = javaExecGenerator.customize(result, false); + + // Then + assertThat(result) + .singleElement(InstanceOfAssertFactories.type(ImageConfiguration.class)) + .extracting(ImageConfiguration::getBuildConfiguration) + .extracting(BuildConfiguration::getLabels) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsKeys("org.label-schema.build-date", "org.label-schema.description", "org.label-schema.version", + "org.label-schema.schema-version", "org.label-schema.build-date", "org.label-schema.name"); + } + + @Test + void customize_whenInvoked_shouldNotAddBuildTimestampToBuildDateLabel() { + // Given + properties.put("jkube.generator.java-exec.mainClass", "org.example.Foo"); + JavaExecGenerator javaExecGenerator = new JavaExecGenerator(generatorContext); + List result = new ArrayList<>(); + + // When + result = javaExecGenerator.customize(result, false); + + // Then + LocalDateTime currentTime = LocalDateTime.now(); + assertThat(result) + .singleElement(InstanceOfAssertFactories.type(ImageConfiguration.class)) + .extracting(ImageConfiguration::getBuildConfiguration) + .extracting(BuildConfiguration::getLabels) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("org.label-schema.build-date", String.format("%d-%s-%s", + currentTime.getYear(), String.format("%02d", currentTime.getMonthValue()), String.format("%02d", currentTime.getDayOfMonth()))); + } }