Skip to content

Commit

Permalink
feat: provide a way to set labels on images defined by Generators (ec…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecavestro committed Apr 23, 2024
1 parent c38d3e2 commit 2975e56
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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
Expand Up @@ -79,6 +79,9 @@ enum Config implements Configs.Config {
// Base image mode (only relevant for OpenShift)
FROM_MODE("fromMode", null),

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

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

Expand Down Expand Up @@ -248,6 +251,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 @@ -268,6 +283,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
Expand Up @@ -21,6 +21,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 @@ -402,6 +404,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

0 comments on commit 2975e56

Please sign in to comment.