diff --git a/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java b/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java index 8559cd327..0928c9b69 100644 --- a/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java +++ b/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java @@ -52,11 +52,13 @@ import io.dekorate.kubernetes.decorator.AddJobDecorator; import io.dekorate.kubernetes.decorator.AddLabelDecorator; import io.dekorate.kubernetes.decorator.AddLivenessProbeDecorator; +import io.dekorate.kubernetes.decorator.AddMetadataToTemplateDecorator; import io.dekorate.kubernetes.decorator.AddMountDecorator; import io.dekorate.kubernetes.decorator.AddPortDecorator; import io.dekorate.kubernetes.decorator.AddPvcVolumeDecorator; import io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator; import io.dekorate.kubernetes.decorator.AddSecretVolumeDecorator; +import io.dekorate.kubernetes.decorator.AddSelectorToDeploymentSpecDecorator; import io.dekorate.kubernetes.decorator.AddSidecarDecorator; import io.dekorate.kubernetes.decorator.AddStartupProbeDecorator; import io.dekorate.kubernetes.decorator.AddToSelectorDecorator; @@ -124,7 +126,12 @@ protected void addDecorators(String group, C config) { //Metadata handling Labels.createLabels(config).forEach(l -> { resourceRegistry.decorate(group, new AddLabelDecorator(config.getName(), l)); + // Ensure that metadata exists + resourceRegistry.decorate(group, new AddMetadataToTemplateDecorator()); + resourceRegistry.decorate(group, new AddToSelectorDecorator(config.getName(), l.getKey(), l.getValue())); + // Selectors have some additional requirements + resourceRegistry.decorate(group, new AddSelectorToDeploymentSpecDecorator()); }); for (Annotation annotation : config.getAnnotations()) { diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddMetadataToTemplateDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddMetadataToTemplateDecorator.java new file mode 100644 index 000000000..dd979a183 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddMetadataToTemplateDecorator.java @@ -0,0 +1,19 @@ +package io.dekorate.kubernetes.decorator; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.PodTemplateSpecFluent; + +public class AddMetadataToTemplateDecorator extends NamedResourceDecorator> { + + @Override + public void andThenVisit(PodTemplateSpecFluent item, ObjectMeta resourceMeta) { + if (!item.hasMetadata()) { + item.withNewMetadata().endMetadata(); + } + } + + @Override + public Class[] before() { + return new Class[] { AddLabelDecorator.class, RemoveLabelDecorator.class }; + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddSelectorToDeploymentSpecDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddSelectorToDeploymentSpecDecorator.java new file mode 100644 index 000000000..3e285459c --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddSelectorToDeploymentSpecDecorator.java @@ -0,0 +1,23 @@ +package io.dekorate.kubernetes.decorator; + +import java.util.HashMap; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.apps.DeploymentSpecFluent; + +public class AddSelectorToDeploymentSpecDecorator extends NamedResourceDecorator> { + + @Override + public void andThenVisit(DeploymentSpecFluent item, ObjectMeta resourceMeta) { + if (!item.hasSelector()) { + item.withNewSelector() + .withMatchLabels(new HashMap<>()) + .endSelector(); + } + } + + @Override + public Class[] before() { + return new Class[] { AddToSelectorDecorator.class }; + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java index 6263f006d..956986f5d 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java @@ -61,8 +61,8 @@ public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) { } @Override - public Class[] before() { - return new Class[] { AddToSelectorDecorator.class }; + public Class[] after() { + return new Class[] { AddSelectorToDeploymentSpecDecorator.class }; } } diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/DeploymentResourceFactory.java b/core/src/main/java/io/dekorate/kubernetes/decorator/DeploymentResourceFactory.java index f5d00ebf6..92d682b2e 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/DeploymentResourceFactory.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/DeploymentResourceFactory.java @@ -15,14 +15,11 @@ */ package io.dekorate.kubernetes.decorator; -import java.util.HashMap; - import io.dekorate.AbstractKubernetesManifestGenerator; import io.dekorate.ResourceFactory; import io.dekorate.kubernetes.config.BaseConfig; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.PodSpecBuilder; -import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder; import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; public class DeploymentResourceFactory implements ResourceFactory { @@ -42,12 +39,9 @@ public HasMetadata create(AbstractKubernetesManifestGenerator generator, Base .endMetadata() .withNewSpec() .withReplicas(1) - .withNewSelector() //We need to have at least an empty selector so that the decorator can work with it. - .withMatchLabels(new HashMap()) - .endSelector() - .withTemplate(new PodTemplateSpecBuilder() - .withSpec(new PodSpecBuilder().build()) - .withNewMetadata().endMetadata().build()) + .withNewTemplate() + .withSpec(new PodSpecBuilder().build()) + .endTemplate() .endSpec().build(); } } diff --git a/examples/pom.xml b/examples/pom.xml index f4efeee78..ad8e58610 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -3,7 +3,7 @@ dekorate-project io.dekorate - 3.4-SNAPSHOT + 3.5-SNAPSHOT 4.0.0 diff --git a/tests/pom.xml b/tests/pom.xml index 9d859b428..62aa50450 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -5,7 +5,7 @@ io.dekorate dekorate-project - 3.4-SNAPSHOT + 3.5-SNAPSHOT .. 4.0.0