Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide a way to set labels on images defined by Generators (#2… #2956

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Usage:
* Fix #2470: Add configuration option for overriding buildpack builder image
* Fix #2662: Sanitize VCS remote URL used in `jkube.eclipse.org/git-url` annotation
* Fix #2860: Correctly pass Docker build-arg from the build configuration to the Openshift build strategy
* Fix #2885: Provide a way to set labels on images defined by Generators
* Fix #2901: Ensure Docker build arguments from properties are used during images pre-pulling

### 1.16.2 (2024-03-27)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ or already added by a generator which has been run previously.
The mode takes only effect when running in OpenShift mode.
| `jkube.generator.fromMode`

| *labels*
| A comma separated list of additional labels you want to set on your image with
| `jkube.generator.labels`

| *name*
| The Docker image name used when doing Docker builds. For OpenShift S2I builds its the name of the image stream. This
can be a pattern as described in <<image-name, Name Placeholders>>. The default is `%g/%a:%l`. Note that this flag would only work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ enum Config implements Configs.Config {
FROM_MODE("fromMode", null),
BUILDPACKS_BUILDER_IMAGE("buildpacksBuilderImage", null),

// Labels
LABELS("labels", null),

// Optional registry
REGISTRY("registry", null),

Expand Down Expand Up @@ -254,6 +257,18 @@ private boolean containsBuildConfiguration(List<ImageConfiguration> configs) {
return false;
}


protected void addLabelsFromConfig(Map<String, String> labels) {
String commaSeparatedLabels = getConfigWithFallback(Config.LABELS, "jkube.generator.labels", null);
if (StringUtils.isNotBlank(commaSeparatedLabels)) {
Map<String,String> configLabels = Arrays.stream(commaSeparatedLabels.split(","))
.map(envNameValue -> envNameValue.split("="))
.filter(e -> e.length == 2)
.collect(Collectors.toMap(e -> e[0].trim(), e -> e[1].trim()));
labels.putAll(configLabels);
}
}

protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buildBuilder, PrefixedLogger log) {
final JavaProject project = getProject();
String docURL = project.getDocumentationUrl();
Expand All @@ -274,6 +289,8 @@ protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buil
labels.put(BuildLabelAnnotations.VERSION.value(), project.getVersion());
labels.put(BuildLabelAnnotations.SCHEMA_VERSION.value(), LABEL_SCHEMA_VERSION);

addLabelsFromConfig(labels);

try {
Repository repository = GitUtil.getGitRepository(project.getBaseDirectory());
if (repository != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.LinkedHashMap;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.eclipse.jkube.generator.api.FromSelector;
Expand Down Expand Up @@ -420,6 +422,23 @@ void addTagsFromProperty() {
.containsExactlyInAnyOrder("tag-1", "tag-2", "other-tag");
}

@Test
@DisplayName("add labels from property")
void addLabelsFromProperty() {
when(ctx.getProject()).thenReturn(project);
BuildConfiguration.BuildConfigurationBuilder builder = BuildConfiguration.builder();
properties.put("jkube.generator.labels", " label-1=a, label-2=b , invalid-label");
Map<String, String> extractedLabels = new LinkedHashMap<>();
BaseGenerator generator = createGenerator(null);
generator.addLabelsFromConfig(extractedLabels);
assertThat(extractedLabels)
.hasSize(2)
.contains(
entry("label-1", "a"),
entry("label-2", "b")
);
}

private void inKubernetes() {
when(ctx.getRuntimeMode()).thenReturn(RuntimeMode.KUBERNETES);
}
Expand Down