From ce5897e97b82a79861949ec760aece81654d0560 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 | 2 +- .../javaexec/JavaExecGeneratorTest.java | 43 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2671d000a..dbfe48ad05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ 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 + +_**Note**_: +- Container Images generated using jkube opinionated defaults would no longer contain full timestamp in `org.label-schema.build-date` label. It would only contain build date. ### 1.14.0 (2023-08-31) * Fix #1674: SpringBootGenerator utilizes the layered jar if present and use it as Docker layers 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..5e4d50e8cc 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 @@ -256,7 +256,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().toLocalDate().toString()); 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..d6060d4cd9 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,43 @@ 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 + assertThat(result) + .singleElement(InstanceOfAssertFactories.type(ImageConfiguration.class)) + .extracting(ImageConfiguration::getBuildConfiguration) + .extracting(BuildConfiguration::getLabels) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("org.label-schema.build-date", LocalDateTime.now().toLocalDate().toString()); + } }