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 <[email protected]>
  • Loading branch information
rohanKanojia committed Sep 29, 2023
1 parent 2836467 commit 447c420
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -256,7 +259,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().format(DateTimeFormatter.ISO_DATE));
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,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<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
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())));
}
}

0 comments on commit 447c420

Please sign in to comment.