Skip to content

Commit ee19cf3

Browse files
xstefankmetacosm
authored andcommitted
fix: allow to add labels in CSVMetadata
Fixes #1088 Signed-off-by: xstefank <xstefank122@gmail.com>
1 parent f73d5f2 commit ee19cf3

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

annotations/src/main/java/io/quarkiverse/operatorsdk/annotations/CSVMetadata.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
*/
5050
Annotations annotations() default @Annotations;
5151

52+
/**
53+
* Extra labels that should be added to the CSV metadata.
54+
*/
55+
Label[] labels() default {};
56+
5257
String description() default "";
5358

5459
String displayName() default "";
@@ -101,6 +106,12 @@
101106
}
102107
}
103108

109+
@interface Label {
110+
String name();
111+
112+
String value();
113+
}
114+
104115
@interface Icon {
105116
String DEFAULT_MEDIA_TYPE = "image/svg+xml";
106117

bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/BundleProcessor.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.IOException;
66
import java.nio.file.Path;
77
import java.util.ArrayList;
8-
import java.util.Collections;
98
import java.util.HashMap;
109
import java.util.LinkedList;
1110
import java.util.List;
@@ -330,15 +329,7 @@ private CSVMetadataHolder createMetadataHolder(AnnotationInstance csvMetadata, C
330329
if (annotationsField != null) {
331330
final var annotationsAnn = annotationsField.asNested();
332331

333-
final var othersAnn = annotationsAnn.value("others");
334-
Map<String, String> others = Collections.emptyMap();
335-
if (othersAnn != null) {
336-
final var othersArray = othersAnn.asNestedArray();
337-
others = new HashMap<>(othersArray.length);
338-
for (AnnotationInstance other : othersArray) {
339-
others.put(other.value("name").asString(), other.value("value").asString());
340-
}
341-
}
332+
Map<String, String> others = extractNameValueMap(annotationsAnn.value("others"));
342333
annotations = new CSVMetadataHolder.Annotations(
343334
ConfigurationUtils.annotationValueOrDefault(annotationsAnn, "containerImage",
344335
AnnotationValue::asString, () -> null),
@@ -362,6 +353,8 @@ private CSVMetadataHolder createMetadataHolder(AnnotationInstance csvMetadata, C
362353
annotations = CSVMetadataHolder.Annotations.override(annotations, bundleConfig.annotations());
363354
}
364355

356+
Map<String, String> labels = extractNameValueMap(csvMetadata.value("labels"));
357+
365358
final var maintainersField = csvMetadata.value("maintainers");
366359
CSVMetadataHolder.Maintainer[] maintainers;
367360
if (maintainersField != null) {
@@ -472,7 +465,7 @@ private CSVMetadataHolder createMetadataHolder(AnnotationInstance csvMetadata, C
472465
AnnotationValue::asString, () -> mh.description),
473466
ConfigurationUtils.annotationValueOrDefault(csvMetadata, "displayName",
474467
AnnotationValue::asString, () -> mh.displayName),
475-
annotations,
468+
annotations, labels,
476469
ConfigurationUtils.annotationValueOrDefault(csvMetadata, "keywords",
477470
AnnotationValue::asStringArray, () -> mh.keywords),
478471
providerName, providerURL,
@@ -495,6 +488,21 @@ private CSVMetadataHolder createMetadataHolder(AnnotationInstance csvMetadata, C
495488
origin);
496489
}
497490

491+
private static Map<String, String> extractNameValueMap(AnnotationValue annotationValue) {
492+
Map<String, String> map;
493+
if (annotationValue != null) {
494+
final var array = annotationValue.asNestedArray();
495+
map = new HashMap<>(array.length);
496+
for (AnnotationInstance instance : array) {
497+
map.put(instance.value("name").asString(), instance.value("value").asString());
498+
}
499+
} else {
500+
map = new HashMap<>();
501+
}
502+
503+
return map;
504+
}
505+
498506
private static class IsGenerationEnabled implements BooleanSupplier {
499507

500508
private BundleGenerationConfiguration config;

bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/builders/CsvManifestsBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public CsvManifestsBuilder(CSVMetadataHolder metadata, BuildTimeOperatorConfigur
102102
metadata.annotations.others.forEach(metadataBuilder::addToAnnotations);
103103
}
104104
}
105+
if (metadata.labels != null) {
106+
metadata.labels.forEach(metadataBuilder::addToLabels);
107+
}
105108
csvBuilder = metadataBuilder.endMetadata();
106109

107110
final var csvSpecBuilder = csvBuilder

bundle-generator/deployment/src/test/java/io/quarkiverse/operatorsdk/bundle/MultipleOperatorsBundleTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public void shouldWriteBundleForTheOperators() throws IOException {
117117
assertEquals(OVERRIDEN_REPO_ANNOTATION, annotations.get(BundleConfiguration.REPOSITORY_ANNOTATION));
118118
assertEquals(OVERRIDEN_BY_THIRD_ANNOTATION_VALUE, annotations.get(OVERRIDDEN_DEFAULT_ANNOTATION_NAME));
119119
assertEquals("bar", annotations.get("foo"));
120+
var labels = metadata.getLabels();
121+
assertEquals(2, labels.size());
122+
assertEquals("test-value", labels.get("test-label"));
123+
assertEquals("test-value2", labels.get("test-label2"));
120124
// version should be the default application's version since it's not provided for this reconciler
121125
assertEquals(VERSION, spec.getVersion());
122126

bundle-generator/deployment/src/test/java/io/quarkiverse/operatorsdk/bundle/sources/ThirdReconciler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.quarkiverse.operatorsdk.annotations.CSVMetadata;
1010
import io.quarkiverse.operatorsdk.annotations.CSVMetadata.Annotations;
1111
import io.quarkiverse.operatorsdk.annotations.CSVMetadata.Annotations.Annotation;
12+
import io.quarkiverse.operatorsdk.annotations.CSVMetadata.Label;
1213
import io.quarkiverse.operatorsdk.annotations.CSVMetadata.RequiredCRD;
1314

1415
@Workflow(dependents = {
@@ -17,7 +18,9 @@
1718
@Dependent(name = "pod2", type = PodDependentResource.class)
1819
})
1920
@CSVMetadata(bundleName = ThirdReconciler.BUNDLE_NAME, requiredCRDs = @RequiredCRD(kind = SecondExternal.KIND, name = "externalagains."
20-
+ SecondExternal.GROUP, version = SecondExternal.VERSION), replaces = "1.0.0", annotations = @Annotations(skipRange = ">=1.0.0 <1.0.3", capabilities = "Test", repository = "should be overridden by property", others = @Annotation(name = "foo", value = "bar")))
21+
+ SecondExternal.GROUP, version = SecondExternal.VERSION), replaces = "1.0.0", annotations = @Annotations(skipRange = ">=1.0.0 <1.0.3", capabilities = "Test", repository = "should be overridden by property", others = @Annotation(name = "foo", value = "bar")), labels = {
22+
@Label(name = "test-label", value = "test-value"),
23+
@Label(name = "test-label2", value = "test-value2") })
2124
@ControllerConfiguration(name = ThirdReconciler.NAME)
2225
public class ThirdReconciler implements Reconciler<Third> {
2326

bundle-generator/runtime/src/main/java/io/quarkiverse/operatorsdk/bundle/runtime/CSVMetadataHolder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class CSVMetadataHolder {
1414
public final String description;
1515
public final String displayName;
1616
public final Annotations annotations;
17+
public final Map<String, String> labels;
1718
public final String[] keywords;
1819
public final String providerName;
1920
public final String providerURL;
@@ -152,14 +153,15 @@ public RequiredCRD(String kind, String name, String version) {
152153
}
153154

154155
public CSVMetadataHolder(String bundleName, String version, String replaces, String providerName, String origin) {
155-
this(bundleName, null, null, null, null, null, providerName, null, replaces, null, version, null, null, null,
156+
this(bundleName, null, null, null, null, null, null, providerName, null, replaces, null, version, null, null, null,
156157
null, null,
157158
null, null,
158159
null,
159160
origin);
160161
}
161162

162163
public CSVMetadataHolder(String bundleName, String csvName, String description, String displayName, Annotations annotations,
164+
Map<String, String> labels,
163165
String[] keywords,
164166
String providerName,
165167
String providerURL, String replaces, String[] skips, String version, String maturity,
@@ -173,6 +175,7 @@ public CSVMetadataHolder(String bundleName, String csvName, String description,
173175
this.description = description;
174176
this.displayName = displayName;
175177
this.annotations = annotations;
178+
this.labels = labels;
176179
this.keywords = keywords;
177180
this.providerURL = providerURL;
178181
this.replaces = replaces;

0 commit comments

Comments
 (0)