Skip to content

Commit

Permalink
feat (dekorateio#533): Support Annotations & Labels per resource kind.
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel authored and aureamunoz committed Dec 4, 2020
1 parent c00ecf3 commit 29f49ae
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand All @@ -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}.
*
Expand All @@ -200,7 +187,6 @@ public static PodTemplateSpec createPodTemplateSpec(KubernetesConfig appConfig,
return new PodTemplateSpecBuilder()
.withSpec(createPodSpec(appConfig, imageConfig))
.withNewMetadata()
.withLabels(createLabels(appConfig))
.endMetadata()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Configuration> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/io/dekorate/AbstractKubernetesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package io.dekorate.kubernetes.annotation;

public @interface Annotation {

String key();

String value();

String[] kinds() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package io.dekorate.kubernetes.annotation;

public @interface Label {

String key();

String value();

String[] kinds() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.dekorate.kubernetes.decorator;

import java.util.Arrays;
import java.util.Objects;

import io.dekorate.doc.Description;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ public void visit(KubernetesListBuilder list) {
return;
}

Map<String, String> 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()
Expand Down
22 changes: 15 additions & 7 deletions core/src/main/java/io/dekorate/utils/Labels.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,20 +38,25 @@ public class Labels {
* @param config The config.
* @return A map containing the lables.
*/
public static Map<String, String> createLabels(BaseConfig config) {
Map<String, String> result = new HashMap<String, String>() {
public static Set<Label> createLabels(BaseConfig config) {
Set<Label> result = new HashSet<Label>() {
{
put(NAME, config.getName());
put(VERSION, config.getVersion());
add(new Label(NAME, config.getName(), null));
add(new Label(VERSION, config.getVersion(), null));
if (Strings.isNotNullOrEmpty(config.getPartOf())) {
put(PART_OF, config.getPartOf());
add(new Label(PART_OF, config.getPartOf(), null));
}
}
};

for (Label label : config.getLabels()) {
result.put(label.getKey(), label.getValue());
result.add(label);
}
return result;
}


public static Map<String, String> createLabelsAsMap(BaseConfig config, String kind) {
return createLabels(config).stream().filter(l -> l.getKinds().length == 0 || Arrays.asList(l.getKinds()).contains(kind)).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void shouldAddAnnotationToPod() {
.withNewMetadata()
.withName("pod")
.endMetadata()
.accept(new AddAnnotationDecorator(new Annotation("key1", "value1")))
.accept(new AddAnnotationDecorator(new Annotation("key1", "value1", new String[0])))
.build();

assertEquals(expected, actual);
Expand All @@ -69,7 +69,7 @@ public void shouldAddAnnotationToKind() {
.withNewMetadata()
.withName("pod")
.endMetadata()
.accept(new AddAnnotationDecorator("Pod", null, new Annotation("key1", "value1")))
.accept(new AddAnnotationDecorator(null, new Annotation("key1", "value1", new String[]{"Pod"})))
.build();

assertEquals(expectedPod, actualPod);
Expand All @@ -78,7 +78,7 @@ public void shouldAddAnnotationToKind() {
.withNewMetadata()
.withName("my-service")
.endMetadata()
.accept(new AddAnnotationDecorator("Pod", null, new Annotation("key1", "value1")))
.accept(new AddAnnotationDecorator(null, new Annotation("key1", "value1", new String[]{"Pod"})))
.build();

assertEquals(expectedService, actualService);
Expand Down
4 changes: 4 additions & 0 deletions tests/feat-553-ingress-annotations/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:8u171-alpine3.7
RUN apk --no-cache add curl
COPY target/*.jar example.jar
CMD java ${JAVA_OPTS} -jar example.jar
Loading

0 comments on commit 29f49ae

Please sign in to comment.