diff --git a/annotations/openshift-annotations/pom.xml b/annotations/openshift-annotations/pom.xml index bb4a32e58..328f452b8 100644 --- a/annotations/openshift-annotations/pom.xml +++ b/annotations/openshift-annotations/pom.xml @@ -170,7 +170,7 @@ limitations under the License. io/dekorate/openshift/listener/** io/dekorate/openshift/util/** io/dekorate/openshift/* - META-INF/services/io.dekorate.Generator + META-INF/services/io.dekorate.* META-INF/MANIFEST.MF diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddLabelToDeploymentConfigSelectorDecorator.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddLabelToDeploymentConfigSelectorDecorator.java new file mode 100644 index 000000000..a697a763f --- /dev/null +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/AddLabelToDeploymentConfigSelectorDecorator.java @@ -0,0 +1,35 @@ + +package io.dekorate.openshift.decorator; + +import io.dekorate.kubernetes.decorator.Decorator; +import io.dekorate.kubernetes.decorator.NamedResourceDecorator; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.openshift.api.model.DeploymentConfigSpecFluent; + +public class AddLabelToDeploymentConfigSelectorDecorator extends NamedResourceDecorator> { + + private String key; + private String value; + + public AddLabelToDeploymentConfigSelectorDecorator(String name, String key, String value) { + super(name); + this.key = key; + this.value = value; + } + + public AddLabelToDeploymentConfigSelectorDecorator(String kind, String name, String key, String value) { + super(kind, name); + this.key = key; + this.value = value; + } + + @Override + public void andThenVisit(DeploymentConfigSpecFluent spec, ObjectMeta resourceMeta) { + spec.addToSelector(key, value); + } + + @Override + public Class[] before() { + return new Class[] { RemoveLabelFromDeploymentConfigSelectorDecorator.class }; + } +} diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/DeploymentConfigSelectorDecoratorFactory.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/DeploymentConfigSelectorDecoratorFactory.java new file mode 100644 index 000000000..cfa9cf12a --- /dev/null +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/DeploymentConfigSelectorDecoratorFactory.java @@ -0,0 +1,23 @@ + +package io.dekorate.openshift.decorator; + +import io.dekorate.SelectorDecoratorFactory; + +public class DeploymentConfigSelectorDecoratorFactory implements SelectorDecoratorFactory { + + @Override + public AddLabelToDeploymentConfigSelectorDecorator createAddToSelectorDecorator(String name, String key, + String value) { + return new AddLabelToDeploymentConfigSelectorDecorator(name, key, value); + } + + @Override + public RemoveLabelFromDeploymentConfigSelectorDecorator createRemoveFromSelectorDecorator(String name, String key) { + return new RemoveLabelFromDeploymentConfigSelectorDecorator(name, key); + } + + @Override + public String kind() { + return "DeploymentConfig"; + } +} diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/RemoveLabelFromDeploymentConfigSelectorDecorator.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/RemoveLabelFromDeploymentConfigSelectorDecorator.java new file mode 100644 index 000000000..017a9439d --- /dev/null +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/decorator/RemoveLabelFromDeploymentConfigSelectorDecorator.java @@ -0,0 +1,31 @@ +package io.dekorate.openshift.decorator; + +import io.dekorate.kubernetes.decorator.Decorator; +import io.dekorate.kubernetes.decorator.NamedResourceDecorator; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.openshift.api.model.DeploymentConfigSpecFluent; + +public class RemoveLabelFromDeploymentConfigSelectorDecorator extends NamedResourceDecorator> { + + private String key; + + public RemoveLabelFromDeploymentConfigSelectorDecorator(String name, String key) { + super(name); + this.key = key; + } + + public RemoveLabelFromDeploymentConfigSelectorDecorator(String kind, String name, String key) { + super(kind, name); + this.key = key; + } + + @Override + public void andThenVisit(DeploymentConfigSpecFluent spec, ObjectMeta resourceMeta) { + spec.removeFromSelector(key); + } + + @Override + public Class[] before() { + return new Class[] { AddLabelToDeploymentConfigSelectorDecorator.class }; + } +} diff --git a/annotations/openshift-annotations/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory b/annotations/openshift-annotations/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory new file mode 100644 index 000000000..8f447860a --- /dev/null +++ b/annotations/openshift-annotations/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory @@ -0,0 +1 @@ +io.dekorate.openshift.decorator.DeploymentConfigSelectorDecoratorFactory diff --git a/core/src/main/java/io/dekorate/SelectorDecoratorFactories.java b/core/src/main/java/io/dekorate/SelectorDecoratorFactories.java new file mode 100644 index 000000000..527f6aca3 --- /dev/null +++ b/core/src/main/java/io/dekorate/SelectorDecoratorFactories.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2020 Original Authors + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +**/ + +package io.dekorate; + +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +public class SelectorDecoratorFactories { + + public static Optional find(String kind) { + return stream().filter(s -> s.kind().equalsIgnoreCase(kind)).findFirst(); + } + + private static Stream stream() { + ServiceLoader loader = ServiceLoader.load(SelectorDecoratorFactory.class, SelectorDecoratorFactory.class.getClassLoader()); + return StreamSupport.stream(loader.spliterator(), false); + } + + +} diff --git a/core/src/main/java/io/dekorate/SelectorDecoratorFactory.java b/core/src/main/java/io/dekorate/SelectorDecoratorFactory.java new file mode 100644 index 000000000..2fbda5b5f --- /dev/null +++ b/core/src/main/java/io/dekorate/SelectorDecoratorFactory.java @@ -0,0 +1,17 @@ + +package io.dekorate; + +import io.dekorate.kubernetes.decorator.NamedResourceDecorator; + +/** + * A factory for creating {@link SelectorDecorator} instances. + */ +public interface SelectorDecoratorFactory { + + String kind(); + + > D createAddToSelectorDecorator(String name, String key, String value); + + > D createRemoveFromSelectorDecorator(String name, String key); + +} 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 29d56e679..8e61aa90f 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelDecorator.java @@ -15,16 +15,17 @@ */ package io.dekorate.kubernetes.decorator; +import io.fabric8.kubernetes.api.builder.VisitableBuilder; import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.dekorate.doc.Description; import io.dekorate.kubernetes.config.Label; +import io.dekorate.utils.Metadata; /** * A decorator that adds a label to resources. */ @Description("Add a label to the all metadata.") -public class AddLabelDecorator extends NamedResourceDecorator { +public class AddLabelDecorator extends NamedResourceDecorator { private final Label label; @@ -42,8 +43,8 @@ public AddLabelDecorator(String kind, String name, Label label) { } @Override - public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) { - builder.addToLabels(label.getKey(), label.getValue()); + public void andThenVisit(VisitableBuilder builder, ObjectMeta resourceMeta) { + Metadata.addToLabels(builder, label.getKey(), label.getValue()); } public Label getLabel() { diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelToServiceSelectorDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelToServiceSelectorDecorator.java new file mode 100644 index 000000000..20dcf2458 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelToServiceSelectorDecorator.java @@ -0,0 +1,34 @@ + +package io.dekorate.kubernetes.decorator; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ServiceSpecFluent; + +public class AddLabelToServiceSelectorDecorator extends NamedResourceDecorator> { + + private String key; + private String value; + + public AddLabelToServiceSelectorDecorator(String name, String key, String value) { + super(name); + this.key = key; + this.value = value; + } + + public AddLabelToServiceSelectorDecorator(String kind, String name, String key, String value) { + super(kind, name); + this.key = key; + this.value = value; + } + + @Override + public void andThenVisit(ServiceSpecFluent spec, ObjectMeta resourceMeta) { + spec.addToSelector(key, value); + } + + @Override + public Class[] before() { + return new Class[] { RemoveLabelFromServiceSelectorDecorator.class }; + } + +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddToMatchingLabelsDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddToMatchingLabelsDecorator.java new file mode 100644 index 000000000..bade175b6 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddToMatchingLabelsDecorator.java @@ -0,0 +1,33 @@ + +package io.dekorate.kubernetes.decorator; + +import io.fabric8.kubernetes.api.model.LabelSelectorFluent; +import io.fabric8.kubernetes.api.model.ObjectMeta; + +public class AddToMatchingLabelsDecorator extends NamedResourceDecorator> { + + private String key; + private String value; + + public AddToMatchingLabelsDecorator(String name, String key, String value) { + super(name); + this.key = key; + this.value = value; + } + + public AddToMatchingLabelsDecorator(String kind, String name, String key, String value) { + super(kind, name); + this.key = key; + this.value = value; + } + + @Override + public void andThenVisit(LabelSelectorFluent selector, ObjectMeta resourceMeta) { + selector.addToMatchLabels(key, value); + } + + @Override + public Class[] after() { + return new Class[] { ApplyLabelSelectorDecorator.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 new file mode 100644 index 000000000..8cf3fd467 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java @@ -0,0 +1,55 @@ +/** + * Copyright (C) 2020 Original Authors + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +**/ + +package io.dekorate.kubernetes.decorator; + +import java.util.Optional; + +import io.dekorate.SelectorDecoratorFactories; +import io.dekorate.SelectorDecoratorFactory; +import io.fabric8.kubernetes.api.builder.VisitableBuilder; +import io.fabric8.kubernetes.api.model.ObjectMeta; + +public class AddToSelectorDecorator extends NamedResourceDecorator { + + private final String key; + private final String value; + + public AddToSelectorDecorator(String name, String key, String value) { + super(name); + this.key = key; + this.value = value; + } + + public AddToSelectorDecorator(String kind, String name, String key, String value) { + super(kind, name); + this.key = key; + this.value = value; + } + + + @Override + public void andThenVisit(VisitableBuilder builder ,String kind, ObjectMeta resourceMeta) { + Optional factory = SelectorDecoratorFactories.find(kind); + factory.ifPresent(f -> f.createAddToSelectorDecorator(resourceMeta.getName(), key, value)); + } + + @Override + public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) { + //Not needed + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromMatchingLabelsDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromMatchingLabelsDecorator.java new file mode 100644 index 000000000..1057be6ec --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromMatchingLabelsDecorator.java @@ -0,0 +1,29 @@ +package io.dekorate.kubernetes.decorator; + +import io.fabric8.kubernetes.api.model.LabelSelectorFluent; +import io.fabric8.kubernetes.api.model.ObjectMeta; + +public class RemoveFromMatchingLabelsDecorator extends NamedResourceDecorator> { + + private String key; + + public RemoveFromMatchingLabelsDecorator(String name, String key) { + super(name); + this.key = key; + } + + public RemoveFromMatchingLabelsDecorator(String kind, String name, String key) { + super(kind, name); + this.key = key; + } + + @Override + public void andThenVisit(LabelSelectorFluent selector, ObjectMeta resourceMeta) { + selector.removeFromMatchLabels(key); + } + + @Override + public Class[] after() { + return new Class[] { ApplyLabelSelectorDecorator.class }; + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromSelectorDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromSelectorDecorator.java new file mode 100644 index 000000000..b99270d56 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromSelectorDecorator.java @@ -0,0 +1,52 @@ +/** + * Copyright (C) 2020 Original Authors + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +**/ + +package io.dekorate.kubernetes.decorator; + +import java.util.Optional; + +import io.dekorate.SelectorDecoratorFactories; +import io.dekorate.SelectorDecoratorFactory; +import io.fabric8.kubernetes.api.builder.VisitableBuilder; +import io.fabric8.kubernetes.api.model.ObjectMeta; + +public class RemoveFromSelectorDecorator extends NamedResourceDecorator { + + private final String key; + + public RemoveFromSelectorDecorator(String name, String key) { + super(name); + this.key = key; + } + + public RemoveFromSelectorDecorator(String kind, String name, String key) { + super(kind, name); + this.key = key; + } + + + @Override + public void andThenVisit(VisitableBuilder builder ,String kind, ObjectMeta resourceMeta) { + Optional factory = SelectorDecoratorFactories.find(kind); + factory.ifPresent(f -> f.createRemoveFromSelectorDecorator(resourceMeta.getName(), key)); + } + + @Override + public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) { + //Not needed + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelDecorator.java index d873ac0dd..203f20b66 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelDecorator.java @@ -15,15 +15,16 @@ */ package io.dekorate.kubernetes.decorator; +import io.fabric8.kubernetes.api.builder.VisitableBuilder; import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.dekorate.doc.Description; +import io.dekorate.utils.Metadata; /** * A decorator that removes a label. */ @Description("Remove a label from the all metadata.") -public class RemoveLabelDecorator extends NamedResourceDecorator { +public class RemoveLabelDecorator extends NamedResourceDecorator { private final String labelKey; @@ -37,8 +38,8 @@ public RemoveLabelDecorator(String name, String labelKey) { } @Override - public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) { - builder.removeFromLabels(labelKey); + public void andThenVisit(VisitableBuilder builder, ObjectMeta resourceMeta) { + Metadata.removeFromLabels(builder, labelKey); } public String getLabelKey() { diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelFromServiceSelectorDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelFromServiceSelectorDecorator.java new file mode 100644 index 000000000..f4600c46d --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveLabelFromServiceSelectorDecorator.java @@ -0,0 +1,30 @@ +package io.dekorate.kubernetes.decorator; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ServiceSpecFluent; + +public class RemoveLabelFromServiceSelectorDecorator extends NamedResourceDecorator> { + + private String key; + + public RemoveLabelFromServiceSelectorDecorator(String name, String key) { + super(name); + this.key = key; + } + + public RemoveLabelFromServiceSelectorDecorator(String kind, String name, String key) { + super(kind, name); + this.key = key; + } + + @Override + public void andThenVisit(ServiceSpecFluent spec, ObjectMeta resourceMeta) { + spec.removeFromSelector(key); + } + + @Override + public Class[] after() { + return new Class[] { AddLabelToServiceSelectorDecorator.class }; + } + +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/ServiceSelectorDecoratorFactory.java b/core/src/main/java/io/dekorate/kubernetes/decorator/ServiceSelectorDecoratorFactory.java new file mode 100644 index 000000000..44214d3df --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/ServiceSelectorDecoratorFactory.java @@ -0,0 +1,22 @@ + +package io.dekorate.kubernetes.decorator; + +import io.dekorate.SelectorDecoratorFactory; + +public class ServiceSelectorDecoratorFactory implements SelectorDecoratorFactory { + + @Override + public AddLabelToServiceSelectorDecorator createAddToSelectorDecorator(String name, String key, String value) { + return new AddLabelToServiceSelectorDecorator(name, key, value); + } + + @Override + public RemoveLabelFromServiceSelectorDecorator createRemoveFromSelectorDecorator(String name, String key) { + return new RemoveLabelFromServiceSelectorDecorator(name, key); + } + + @Override + public String kind() { + return "Service"; + } +} diff --git a/core/src/main/java/io/dekorate/utils/Metadata.java b/core/src/main/java/io/dekorate/utils/Metadata.java index c4e0a6c2e..0be409208 100644 --- a/core/src/main/java/io/dekorate/utils/Metadata.java +++ b/core/src/main/java/io/dekorate/utils/Metadata.java @@ -21,6 +21,7 @@ import io.fabric8.kubernetes.api.builder.VisitableBuilder; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ObjectMetaFluent; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -54,6 +55,40 @@ public static Optional getMetadata(Builder builder) { return Optional.empty(); } + public static boolean addToLabels(Builder builder, String key, String value) { + try { + Method editMethod = builder.getClass().getMethod("editOrNewMetadata"); + Object o = editMethod.invoke(builder); + if (o instanceof ObjectMetaFluent) { + ObjectMetaFluent fluent = (ObjectMetaFluent) o; + fluent.addToLabels(key, value); + Method endMethod = fluent.getClass().getMethod("endMetadata"); + endMethod.invoke(fluent); + return true; + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + //ignore + } + return false; + } + + public static boolean removeFromLabels(Builder builder, String key) { + try { + Method editMethod = builder.getClass().getMethod("editOrNewMetadata"); + Object o = editMethod.invoke(builder); + if (o instanceof ObjectMetaFluent) { + ObjectMetaFluent fluent = (ObjectMetaFluent) o; + fluent.removeFromLabels(key); + Method endMethod = fluent.getClass().getMethod("endMetadata"); + endMethod.invoke(fluent); + return true; + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + //ignore + } + return false; + } + /** * Create a {@link Predicate} that checks that a resource builder doesn't match the name and kind. * @param HasMetadata The specified resource. diff --git a/core/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory b/core/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory new file mode 100644 index 000000000..b18c65268 --- /dev/null +++ b/core/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory @@ -0,0 +1 @@ +io.dekorate.kubernetes.dekorator.ServiceSelectorDecoratorFactory