Skip to content

Commit

Permalink
feat (#553): Allow decorating resource by kind.
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Jul 11, 2020
1 parent f33c098 commit 0de15f4
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public AddAnnotationDecorator(Annotation annotation) {
}

public AddAnnotationDecorator(String name, Annotation annotation) {
super(name);
this(ANY, name, annotation);
}

public AddAnnotationDecorator(String kind, String name, Annotation annotation) {
super(kind, name);
this.annotation = annotation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public AddLabelDecorator(Label label) {
}

public AddLabelDecorator(String name, Label label) {
super(name);
this(ANY, name, label);
}

public AddLabelDecorator(String kind, String name, Label label) {
super(kind, name);
this.label = label;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Optional;

import static io.dekorate.utils.Metadata.getKind;
import static io.dekorate.utils.Metadata.getMetadata;

public abstract class NamedResourceDecorator<T> extends Decorator<VisitableBuilder> {
Expand All @@ -32,28 +33,45 @@ public abstract class NamedResourceDecorator<T> extends Decorator<VisitableBuild
*/
public static final String ANY = null;

protected final String kind;
protected final String name;

private final ResourceVisitor visitor = new ResourceVisitor(null);
private final ResourceVisitor visitor = new ResourceVisitor(null, null);

public NamedResourceDecorator() {
this(ANY);
this(ANY, ANY);
}

public NamedResourceDecorator(String name) {
this(ANY, name);
}

public NamedResourceDecorator(String kind, String name) {
this.kind = kind;
this.name = name;
}

@Override
public void visit(VisitableBuilder builder) {
Optional<String> resourceKind = getKind(builder);
Optional<ObjectMeta> objectMeta = getMetadata(builder);
if (!objectMeta.isPresent()) {
if (!resourceKind.isPresent() || !objectMeta.isPresent()) {
return;
}
if (Strings.isNullOrEmpty(name)) {
builder.accept(visitor.withMetadata(objectMeta.get()));
} else if (objectMeta.map(m -> m.getName()).filter(s -> s.equals(name)).isPresent()) {
builder.accept(visitor.withMetadata(objectMeta.get()));

System.out.println("kind: " + resourceKind.get() + "name:" + objectMeta.get());
if (Strings.isNullOrEmpty(kind)) {
if (Strings.isNullOrEmpty(name)) {
builder.accept(visitor.withKind(resourceKind.get()).withMetadata(objectMeta.get()));
} else if (objectMeta.map(m -> m.getName()).filter(s -> s.equals(name)).isPresent()) {
builder.accept(visitor.withKind(resourceKind.get()).withMetadata(objectMeta.get()));
}
} else if (resourceKind.filter(k -> k.equals(kind)).isPresent()) {
if (Strings.isNullOrEmpty(name)) {
builder.accept(visitor.withKind(resourceKind.get()).withMetadata(objectMeta.get()));
} else if (objectMeta.map(m -> m.getName()).filter(s -> s.equals(name)).isPresent()) {
builder.accept(visitor.withKind(resourceKind.get()).withMetadata(objectMeta.get()));
}
}
}

Expand All @@ -64,21 +82,37 @@ public void visit(VisitableBuilder builder) {
*/
public abstract void andThenVisit(T item, ObjectMeta resourceMeta);

/**
* Visit a part of a Resource.
* @param item the visited item
* @param item toplevel resource kind
* @param the {@link ObjectMeta} of the current resource.
*/
public void andThenVisit(T item, String kind, ObjectMeta resourceMeta) {
andThenVisit(item, resourceMeta);
}

private class ResourceVisitor extends TypedVisitor<T> {

private final String kind;
private final ObjectMeta metadata;

public ResourceVisitor(ObjectMeta metadata) {
public ResourceVisitor(String kind, ObjectMeta metadata) {
this.kind = kind;
this.metadata = metadata;
}

@Override
public void visit(T item) {
andThenVisit(item, metadata);
andThenVisit(item, kind, metadata);
}

public ResourceVisitor withKind(String kind) {
return new ResourceVisitor(kind, this.metadata);
}

public ResourceVisitor withMetadata(ObjectMeta metadata) {
return new ResourceVisitor(metadata);
return new ResourceVisitor(this.kind, metadata);
}

public Class<T> getType() {
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/io/dekorate/utils/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@

public class Metadata {

public static Optional<String> getKind(Builder builder) {
try {
Method method = builder.getClass().getMethod("getKind");
Object o = method.invoke(builder);
if (o instanceof String) {
return Optional.of((String)o);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//ignore
}
return Optional.empty();
}

public static Optional<ObjectMeta> getMetadata(Builder builder) {
try {
Method method = builder.getClass().getMethod("buildMetadata");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
*/
package io.dekorate.kubernetes.decorator;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.dekorate.kubernetes.config.Annotation;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;

class AddAnnotationDecoratorTest {

@Test
public void shouldAddAnnotationToResources() {
public void shouldAddAnnotationToPod() {
assertEquals("Pod", new PodBuilder().getKind());

Pod expected = new PodBuilder()
.withNewMetadata()
.withName("pod")
Expand All @@ -42,4 +47,41 @@ public void shouldAddAnnotationToResources() {

assertEquals(expected, actual);
}

@Test
public void shouldAddAnnotationToKind() {
assertEquals("Pod", new PodBuilder().getKind());

Pod expectedPod = new PodBuilder()
.withNewMetadata()
.withName("pod")
.addToAnnotations("key1","value1")
.endMetadata()
.build();

Service expectedService = new ServiceBuilder()
.withNewMetadata()
.withName("my-service")
.endMetadata()
.build();


Pod actualPod = new PodBuilder()
.withNewMetadata()
.withName("pod")
.endMetadata()
.accept(new AddAnnotationDecorator("Pod", null, new Annotation("key1", "value1")))
.build();

assertEquals(expectedPod, actualPod);

Service actualService = new ServiceBuilder()
.withNewMetadata()
.withName("my-service")
.endMetadata()
.accept(new AddAnnotationDecorator("Pod", null, new Annotation("key1", "value1")))
.build();

assertEquals(expectedService, actualService);
}
}

0 comments on commit 0de15f4

Please sign in to comment.