diff --git a/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/adapter/ComponentConfigAdapter.java b/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/adapter/ComponentConfigAdapter.java index 9041495b8..85eca26e2 100644 --- a/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/adapter/ComponentConfigAdapter.java +++ b/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/adapter/ComponentConfigAdapter.java @@ -53,8 +53,8 @@ public static ComponentConfigBuilder newBuilder(HalkyonComponent instance) { i.secret(), i.configmap(), i.field())).collect(Collectors.toList()).toArray(new io.dekorate.kubernetes.config.Env[0]), - Arrays.asList(instance.labels()).stream().map(i -> new io.dekorate.kubernetes.config.Label(i.key(), - i.value())).collect(Collectors.toList()).toArray(new io.dekorate.kubernetes.config.Label[0]), + Arrays.asList(instance.labels()).stream().map(i -> new io.dekorate.kubernetes.config.Label(i.key(), i.value(), + i.kinds())).collect(Collectors.toList()).toArray(new io.dekorate.kubernetes.config.Label[0]), instance.buildType(), instance.remote(), Arrays.asList(instance.provides()).stream().map(i -> new io.dekorate.halkyon.config.CapabilityConfig(null, @@ -121,7 +121,8 @@ private static Parameter getParameter(Map j) { private static Label getLabel(Map i) { return new Label( (String) i.getOrDefault("key", null), - (String) i.getOrDefault("value", null)); + (String) i.getOrDefault("value", null), + (String[]) i.getOrDefault("kinds", new String[0])); } private static Env getEnv(Map i) { diff --git a/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/handler/ComponentHandler.java b/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/handler/ComponentHandler.java index 728e60191..c547713dc 100644 --- a/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/handler/ComponentHandler.java +++ b/annotations/halkyon-annotations/src/main/java/io/dekorate/halkyon/handler/ComponentHandler.java @@ -128,14 +128,14 @@ private void addVisitors(ComponentConfig config) { allLabels.put(l.getKey(), l.getValue()); }); - Labels.createLabels(kubernetesConfig).forEach((k, v) -> { + Labels.createLabelsAsMap(kubernetesConfig, "Component").forEach((k, v) -> { if (!allLabels.containsKey(k)) { allLabels.put(k, v); } }); allLabels.forEach((k, v) -> { - resources.decorateCustom(ResourceGroup.NAME, new AddLabelDecorator(new Label(k, v))); + resources.decorateCustom(ResourceGroup.NAME, new AddLabelDecorator(new Label(k, v, new String[]{"Component"}))); resources.decorateCustom(ResourceGroup.NAME, new AddToSelectorDecorator(k, v)); }); diff --git a/annotations/knative-annotations/src/test/java/io/dekorate/knative/config/KnativeConfigTest.java b/annotations/knative-annotations/src/test/java/io/dekorate/knative/config/KnativeConfigTest.java index c9e8cfda4..b96544792 100644 --- a/annotations/knative-annotations/src/test/java/io/dekorate/knative/config/KnativeConfigTest.java +++ b/annotations/knative-annotations/src/test/java/io/dekorate/knative/config/KnativeConfigTest.java @@ -25,9 +25,7 @@ public void testGeneratedCode() { .withName("myapp") .withVersion("1.0.0") .withExpose(true) - .addNewLabel("key1", "val1") + .addNewLabel("key1", "val1", new String[0]) .build(); - } - } diff --git a/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/handler/KubernetesHandler.java b/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/handler/KubernetesHandler.java index ebafa8777..a74a240cd 100644 --- a/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/handler/KubernetesHandler.java +++ b/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/handler/KubernetesHandler.java @@ -153,7 +153,7 @@ protected void addDecorators(String group, KubernetesConfig config) { } Ports.getHttpPort(config).ifPresent(p -> { - resources.decorate(group, new AddIngressDecorator(config, Labels.createLabels(config))); + resources.decorate(group, new AddIngressDecorator(config, Labels.createLabelsAsMap(config, "Ingress"))); resources.decorate(group, new AddIngressRuleDecorator(config.getName(), config.getHost(), p)); }); @@ -169,27 +169,14 @@ public Deployment createDeployment(KubernetesConfig appConfig, ImageConfiguratio return new DeploymentBuilder() .withNewMetadata() .withName(appConfig.getName()) - .withLabels(Labels.createLabels(appConfig)) .endMetadata() .withNewSpec() .withReplicas(1) .withTemplate(createPodTemplateSpec(appConfig, imageConfig)) - .withSelector(createSelector(appConfig)) .endSpec() .build(); } - /** - * Creates a {@link LabelSelector} that matches the labels for the {@link KubernetesConfig}. - * - * @return A labels selector. - */ - public LabelSelector createSelector(KubernetesConfig config) { - return new LabelSelectorBuilder() - .withMatchLabels(Labels.createLabels(config)) - .build(); - } - /** * Creates a {@link PodTemplateSpec} for the {@link KubernetesConfig}. * @@ -200,7 +187,6 @@ public static PodTemplateSpec createPodTemplateSpec(KubernetesConfig appConfig, return new PodTemplateSpecBuilder() .withSpec(createPodSpec(appConfig, imageConfig)) .withNewMetadata() - .withLabels(createLabels(appConfig)) .endMetadata() .build(); } diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddRouteDecorator.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddRouteDecorator.java index 3e5e304c1..ea4ffbbb7 100644 --- a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddRouteDecorator.java +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddRouteDecorator.java @@ -51,7 +51,7 @@ public void visit(KubernetesListBuilder list) { list.addNewRouteItem() .withNewMetadata() .withName(config.getName()) - .withLabels(Labels.createLabels(config)) + .withLabels(Labels.createLabelsAsMap(config, "Route")) .endMetadata() .withNewSpec() .withHost(config.getHost()) diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/handler/OpenshiftHandler.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/handler/OpenshiftHandler.java index 694d98b7a..73fbfb859 100644 --- a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/handler/OpenshiftHandler.java +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/handler/OpenshiftHandler.java @@ -148,14 +148,14 @@ protected void addDecorators(String group, OpenshiftConfig config, ImageConfigur if (config.hasAttribute(RUNTIME_TYPE)) { resources.decorate(group, - new AddLabelDecorator(new Label(OpenshiftLabels.RUNTIME, config.getAttribute(RUNTIME_TYPE)))); + new AddLabelDecorator(new Label(OpenshiftLabels.RUNTIME, config.getAttribute(RUNTIME_TYPE), new String[0]))); } resources.decorate(group, new RemoveAnnotationDecorator(Annotations.VCS_URL)); Project p = getProject(); String vcsUrl = p.getScmInfo() != null && Strings.isNotNullOrEmpty(p.getScmInfo().getUrl()) ? p.getScmInfo().getUrl() : Labels.UNKNOWN; - resources.decorate(group, new AddAnnotationDecorator(new Annotation(OpenshiftAnnotations.VCS_URL, vcsUrl))); + resources.decorate(group, new AddAnnotationDecorator(new Annotation(OpenshiftAnnotations.VCS_URL, vcsUrl, new String[0]))); } public boolean canHandle(Class type) { diff --git a/annotations/openshift-annotations/src/test/java/io/dekorate/openshift/config/OpenshiftConfigTest.java b/annotations/openshift-annotations/src/test/java/io/dekorate/openshift/config/OpenshiftConfigTest.java index f4ebbe225..789ad9538 100644 --- a/annotations/openshift-annotations/src/test/java/io/dekorate/openshift/config/OpenshiftConfigTest.java +++ b/annotations/openshift-annotations/src/test/java/io/dekorate/openshift/config/OpenshiftConfigTest.java @@ -25,7 +25,7 @@ public void testGeneratedCode() { .withName("myapp") .withVersion("1.0.0") .withExpose(true) - .addNewLabel("key1", "val1") + .addNewLabel("key1", "val1", new String[0]) .build(); } diff --git a/core/src/main/java/io/dekorate/AbstractKubernetesHandler.java b/core/src/main/java/io/dekorate/AbstractKubernetesHandler.java index f2cce4ad5..5e019c003 100644 --- a/core/src/main/java/io/dekorate/AbstractKubernetesHandler.java +++ b/core/src/main/java/io/dekorate/AbstractKubernetesHandler.java @@ -25,7 +25,6 @@ import io.dekorate.kubernetes.config.Container; import io.dekorate.kubernetes.config.Env; import io.dekorate.kubernetes.config.HostAlias; -import io.dekorate.kubernetes.config.Label; import io.dekorate.kubernetes.config.Mount; import io.dekorate.kubernetes.config.PersistentVolumeClaimVolume; import io.dekorate.kubernetes.config.Port; @@ -107,9 +106,9 @@ protected void addDecorators(String group, C config) { resources.decorate(new AddVcsUrlAnnotationDecorator()); resources.decorate(new AddCommitIdAnnotationDecorator()); - Labels.createLabels(config).forEach((k, v) -> { - resources.decorate(group, new AddLabelDecorator(new Label(k, v))); - resources.decorate(group, new AddToSelectorDecorator(k, v)); + Labels.createLabels(config).forEach(l -> { + resources.decorate(group, new AddLabelDecorator(l)); + resources.decorate(group, new AddToSelectorDecorator(l.getKey(), l.getValue())); }); for (Annotation annotation : config.getAnnotations()) { diff --git a/core/src/main/java/io/dekorate/kubernetes/annotation/Annotation.java b/core/src/main/java/io/dekorate/kubernetes/annotation/Annotation.java index 1bc7ba11b..537e9fc8d 100644 --- a/core/src/main/java/io/dekorate/kubernetes/annotation/Annotation.java +++ b/core/src/main/java/io/dekorate/kubernetes/annotation/Annotation.java @@ -16,7 +16,10 @@ package io.dekorate.kubernetes.annotation; public @interface Annotation { + String key(); String value(); + + String[] kinds() default {}; } diff --git a/core/src/main/java/io/dekorate/kubernetes/annotation/Label.java b/core/src/main/java/io/dekorate/kubernetes/annotation/Label.java index 4d6806ceb..1dca35974 100644 --- a/core/src/main/java/io/dekorate/kubernetes/annotation/Label.java +++ b/core/src/main/java/io/dekorate/kubernetes/annotation/Label.java @@ -16,7 +16,10 @@ package io.dekorate.kubernetes.annotation; public @interface Label { + String key(); String value(); + + String[] kinds() default {}; } diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddAnnotationDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddAnnotationDecorator.java index 01df61673..f0a68ab74 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddAnnotationDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddAnnotationDecorator.java @@ -15,6 +15,7 @@ */ package io.dekorate.kubernetes.decorator; +import java.util.Arrays; import java.util.Objects; import io.dekorate.doc.Description; @@ -32,14 +33,18 @@ public AddAnnotationDecorator(Annotation annotation) { } public AddAnnotationDecorator(String name, Annotation annotation) { - this(ANY, name, annotation); + super(ANY, name); + this.annotation = annotation; } - public AddAnnotationDecorator(String kind, String name, Annotation annotation) { - super(kind, name); - this.annotation = annotation; + @Override + public void andThenVisit(ObjectMetaBuilder builder, String kind, ObjectMeta resourceMeta) { + if (annotation.getKinds() == null || annotation.getKinds().length == 0 || Arrays.asList(annotation.getKinds()).contains(kind)) { + andThenVisit(builder, resourceMeta); + } } + @Override public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) { builder.addToAnnotations(annotation.getKey(), annotation.getValue()); diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java index 9204023d6..270c1591f 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java @@ -15,6 +15,8 @@ */ package io.dekorate.kubernetes.decorator; +import java.util.Arrays; + import io.dekorate.doc.Description; import io.dekorate.kubernetes.config.Label; import io.dekorate.utils.Metadata; @@ -34,12 +36,15 @@ public AddLabelDecorator(Label label) { } public AddLabelDecorator(String name, Label label) { - this(ANY, name, label); + super(ANY, name); + this.label = label; } - public AddLabelDecorator(String kind, String name, Label label) { - super(kind, name); - this.label = label; + @Override + public void andThenVisit(VisitableBuilder builder, String kind, ObjectMeta resourceMeta) { + if (label.getKinds() == null || label.getKinds().length == 0 || Arrays.asList(label.getKinds()).contains(kind)) { + andThenVisit(builder, resourceMeta); + } } @Override diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddServiceResourceDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddServiceResourceDecorator.java index e278cd85c..0e4d67aa3 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddServiceResourceDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddServiceResourceDecorator.java @@ -50,14 +50,15 @@ public void visit(KubernetesListBuilder list) { return; } + Map labels = Labels.createLabelsAsMap(config, "Service"); list.addNewServiceItem() .withNewMetadata() .withName(config.getName()) - .withLabels(Labels.createLabels(config)) + .withLabels(labels) .endMetadata() .withNewSpec() .withType(config.getServiceType().name()) - .withSelector(Labels.createLabels(config)) + .withSelector(labels) .withPorts(Arrays.asList(config.getPorts()).stream() .filter(distinct(p -> p.getName())).map(this::toServicePort).collect(Collectors.toList())) .endSpec() diff --git a/core/src/main/java/io/dekorate/utils/Labels.java b/core/src/main/java/io/dekorate/utils/Labels.java index e49536a33..ece1a97d9 100644 --- a/core/src/main/java/io/dekorate/utils/Labels.java +++ b/core/src/main/java/io/dekorate/utils/Labels.java @@ -16,8 +16,11 @@ package io.dekorate.utils; -import java.util.HashMap; +import java.util.Arrays; +import java.util.HashSet; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import io.dekorate.kubernetes.config.BaseConfig; import io.dekorate.kubernetes.config.Label; @@ -35,20 +38,25 @@ public class Labels { * @param config The config. * @return A map containing the lables. */ - public static Map createLabels(BaseConfig config) { - Map result = new HashMap() { + public static Set