Skip to content

Commit

Permalink
fix (jkube-kit/generator) : Do not add build timestamp to build date …
Browse files Browse the repository at this point in the history
…label (eclipse-jkube#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 <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Sep 28, 2023
1 parent 1005190 commit e0bd130
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Usage:
* Fix #2356: Helm values.yaml parameter names preserve case
* 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 #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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buil
String docURL = project.getDocumentationUrl();
Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<ImageConfiguration> 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<ImageConfiguration> 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());
}
}

0 comments on commit e0bd130

Please sign in to comment.