Skip to content

Commit

Permalink
feat (#609): Allow adding/removing labels to/from selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Sep 9, 2020
1 parent aa1a413 commit 14d71d0
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 9 deletions.
2 changes: 1 addition & 1 deletion annotations/openshift-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ limitations under the License.
<include>io/dekorate/openshift/listener/**</include>
<include>io/dekorate/openshift/util/**</include>
<include>io/dekorate/openshift/*</include>
<include>META-INF/services/io.dekorate.Generator</include>
<include>META-INF/services/io.dekorate.*</include>
<include>META-INF/MANIFEST.MF</include>
</includes>
</filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DeploymentConfigSpecFluent<?>> {

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<? extends Decorator>[] before() {
return new Class[] { RemoveLabelFromDeploymentConfigSelectorDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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<DeploymentConfigSpecFluent<?>> {

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<? extends Decorator>[] before() {
return new Class[] { AddLabelToDeploymentConfigSelectorDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.dekorate.openshift.decorator.DeploymentConfigSelectorDecoratorFactory
37 changes: 37 additions & 0 deletions core/src/main/java/io/dekorate/SelectorDecoratorFactories.java
Original file line number Diff line number Diff line change
@@ -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<SelectorDecoratorFactory> find(String kind) {
return stream().filter(s -> s.kind().equalsIgnoreCase(kind)).findFirst();
}

private static Stream<SelectorDecoratorFactory> stream() {
ServiceLoader<SelectorDecoratorFactory> loader = ServiceLoader.load(SelectorDecoratorFactory.class, SelectorDecoratorFactory.class.getClassLoader());
return StreamSupport.stream(loader.spliterator(), false);
}


}
17 changes: 17 additions & 0 deletions core/src/main/java/io/dekorate/SelectorDecoratorFactory.java
Original file line number Diff line number Diff line change
@@ -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 extends NamedResourceDecorator<?>> D createAddToSelectorDecorator(String name, String key, String value);

<D extends NamedResourceDecorator<?>> D createRemoveFromSelectorDecorator(String name, String key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectMetaBuilder> {
public class AddLabelDecorator extends NamedResourceDecorator<VisitableBuilder> {

private final Label label;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServiceSpecFluent<?>> {

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<? extends Decorator>[] before() {
return new Class[] { RemoveLabelFromServiceSelectorDecorator.class };
}

}
Original file line number Diff line number Diff line change
@@ -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<LabelSelectorFluent<?>> {

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<? extends Decorator>[] after() {
return new Class[] { ApplyLabelSelectorDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -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<VisitableBuilder> {

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<SelectorDecoratorFactory> factory = SelectorDecoratorFactories.find(kind);
factory.ifPresent(f -> f.createAddToSelectorDecorator(resourceMeta.getName(), key, value));
}

@Override
public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) {
//Not needed
}
}
Original file line number Diff line number Diff line change
@@ -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<LabelSelectorFluent<?>> {

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<? extends Decorator>[] after() {
return new Class[] { ApplyLabelSelectorDecorator.class };
}
}
Original file line number Diff line number Diff line change
@@ -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<VisitableBuilder> {

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<SelectorDecoratorFactory> factory = SelectorDecoratorFactories.find(kind);
factory.ifPresent(f -> f.createRemoveFromSelectorDecorator(resourceMeta.getName(), key));
}

@Override
public void andThenVisit(VisitableBuilder item, ObjectMeta resourceMeta) {
//Not needed
}
}
Loading

0 comments on commit 14d71d0

Please sign in to comment.