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

fix: don't require selectors / metadata from existing resources #1163

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PodTemplateSpecFluent<?>> {

@Override
public void andThenVisit(PodTemplateSpecFluent<?> item, ObjectMeta resourceMeta) {
if (!item.hasMetadata()) {
item.withNewMetadata().endMetadata();
}
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddLabelDecorator.class, RemoveLabelDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -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<DeploymentSpecFluent<?>> {

@Override
public void andThenVisit(DeploymentSpecFluent<?> item, ObjectMeta resourceMeta) {
if (!item.hasSelector()) {
item.withNewSelector()
.withMatchLabels(new HashMap<>())
.endSelector();
}
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddToSelectorDecorator.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) {
}

@Override
public Class<? extends Decorator>[] before() {
return new Class[] { AddToSelectorDecorator.class };
public Class<? extends Decorator>[] after() {
return new Class[] { AddSelectorToDeploymentSpecDecorator.class };
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String>())
.endSelector()
.withTemplate(new PodTemplateSpecBuilder()
.withSpec(new PodSpecBuilder().build())
.withNewMetadata().endMetadata().build())
.withNewTemplate()
.withSpec(new PodSpecBuilder().build())
.endTemplate()
.endSpec().build();
}
}
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>dekorate-project</artifactId>
<groupId>io.dekorate</groupId>
<version>3.4-SNAPSHOT</version>
<version>3.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.dekorate</groupId>
<artifactId>dekorate-project</artifactId>
<version>3.4-SNAPSHOT</version>
<version>3.5-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down