-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (#609): Allow adding/removing labels to/from selectors
- Loading branch information
Showing
18 changed files
with
446 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ain/java/io/dekorate/openshift/decorator/AddLabelToDeploymentConfigSelectorDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...c/main/java/io/dekorate/openshift/decorator/DeploymentConfigSelectorDecoratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ava/io/dekorate/openshift/decorator/RemoveLabelFromDeploymentConfigSelectorDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ift-annotations/src/main/resources/META-INF/services/io.dekorate.SelectorDecoratorFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.dekorate.openshift.decorator.DeploymentConfigSelectorDecoratorFactory |
37 changes: 37 additions & 0 deletions
37
core/src/main/java/io/dekorate/SelectorDecoratorFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
core/src/main/java/io/dekorate/SelectorDecoratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/src/main/java/io/dekorate/kubernetes/decorator/AddLabelToServiceSelectorDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
core/src/main/java/io/dekorate/kubernetes/decorator/AddToMatchingLabelsDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
core/src/main/java/io/dekorate/kubernetes/decorator/AddToSelectorDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromMatchingLabelsDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
core/src/main/java/io/dekorate/kubernetes/decorator/RemoveFromSelectorDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.