Skip to content

Commit 50a2f18

Browse files
Vinche59iocanel
authored andcommitted
Add nodeSelector capabilities to DeploymentSpec
Signed-off-by: Vincent Sourin <[email protected]>
1 parent 0e3883c commit 50a2f18

File tree

27 files changed

+791
-8
lines changed

27 files changed

+791
-8
lines changed

annotations/kind-annotations/src/main/java/io/dekorate/kind/annotation/Kind.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
@BuildableReference(io.dekorate.kubernetes.config.HostAlias.class),
5151
@BuildableReference(io.dekorate.kubernetes.config.Container.class),
5252
@BuildableReference(io.dekorate.kubernetes.config.Job.class),
53-
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class)
53+
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class),
54+
@BuildableReference(io.dekorate.kubernetes.config.NodeSelector.class)
5455
})
5556
@Pojo(name = "KindConfig", relativePath = "../config", autobox = true, mutable = true, superClass = BaseConfig.class, withStaticBuilderMethod = true, withStaticAdapterMethod = false, adapter = @Adapter(name = "KindConfigAdapter", relativePath = "../adapter", withMapAdapterMethod = true))
5657
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })

annotations/knative-annotations/src/main/java/io/dekorate/knative/annotation/KnativeApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.dekorate.kubernetes.annotation.ServiceType;
4646
import io.dekorate.kubernetes.config.BaseConfig;
4747
import io.dekorate.kubernetes.config.HostAlias;
48+
import io.dekorate.kubernetes.config.NodeSelector;
4849
import io.dekorate.kubernetes.config.RollingUpdate;
4950
import io.dekorate.project.BuildInfo;
5051
import io.dekorate.project.Project;
@@ -57,7 +58,8 @@
5758
@BuildableReference(Project.class),
5859
@BuildableReference(BuildInfo.class),
5960
@BuildableReference(HostAlias.class),
60-
@BuildableReference(RollingUpdate.class)
61+
@BuildableReference(RollingUpdate.class),
62+
@BuildableReference(NodeSelector.class)
6163
})
6264
@Pojo(name = "KnativeConfig", autobox = true, mutable = true, superClass = BaseConfig.class, relativePath = "../config", withStaticAdapterMethod = false, adapter = @Adapter(relativePath = "../adapter", withMapAdapterMethod = true))
6365
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright 2018 The original authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
**/
17+
18+
package io.dekorate.knative.decorator;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
import io.dekorate.kubernetes.config.NodeSelector;
25+
import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
26+
import io.dekorate.utils.Strings;
27+
import io.fabric8.knative.serving.v1.RevisionSpecFluent;
28+
import io.fabric8.kubernetes.api.model.ObjectMeta;
29+
30+
public class AddNodeSelectorToRevisionDecorator extends NamedResourceDecorator<RevisionSpecFluent<?>> {
31+
32+
private final NodeSelector nodeSelector;
33+
34+
public AddNodeSelectorToRevisionDecorator(NodeSelector nodeSelector) {
35+
this(ANY, nodeSelector);
36+
}
37+
38+
public AddNodeSelectorToRevisionDecorator(String deploymentName, NodeSelector nodeSelector) {
39+
super(deploymentName);
40+
this.nodeSelector = nodeSelector;
41+
}
42+
43+
public void andThenVisit(RevisionSpecFluent<?> revisionSpec, ObjectMeta resourceMeta) {
44+
if (Strings.isNotNullOrEmpty(nodeSelector.getKey()) && Strings.isNotNullOrEmpty(nodeSelector.getValue())) {
45+
Map<String, String> existing = revisionSpec.getNodeSelector();
46+
47+
if (existing == null)
48+
existing = new HashMap<>();
49+
else
50+
existing.clear();
51+
52+
existing.put(nodeSelector.getKey(), nodeSelector.getValue());
53+
revisionSpec.withNodeSelector(existing);
54+
}
55+
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o)
60+
return true;
61+
if (o == null || getClass() != o.getClass())
62+
return false;
63+
AddNodeSelectorToRevisionDecorator that = (AddNodeSelectorToRevisionDecorator) o;
64+
return Objects.equals(nodeSelector, that.nodeSelector);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(nodeSelector);
70+
}
71+
}

annotations/knative-annotations/src/main/java/io/dekorate/knative/manifest/KnativeManifestGenerator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.dekorate.knative.decorator.AddConfigMapVolumeToRevisionDecorator;
3737
import io.dekorate.knative.decorator.AddEmptyDirVolumeToRevisionDecorator;
3838
import io.dekorate.knative.decorator.AddHostAliasesToRevisionDecorator;
39+
import io.dekorate.knative.decorator.AddNodeSelectorToRevisionDecorator;
3940
import io.dekorate.knative.decorator.AddPvcVolumeToRevisionDecorator;
4041
import io.dekorate.knative.decorator.AddSecretVolumeToRevisionDecorator;
4142
import io.dekorate.knative.decorator.AddSidecarToRevisionDecorator;
@@ -292,6 +293,10 @@ public void generate(KnativeConfig config) {
292293
resourceRegistry.decorate(KNATIVE, new AddHostAliasesToRevisionDecorator(hostAlias));
293294
}
294295

296+
if (config.getNodeSelector() != null) {
297+
resourceRegistry.decorate(KNATIVE, new AddNodeSelectorToRevisionDecorator(config.getNodeSelector()));
298+
}
299+
295300
}
296301

297302
@Override
@@ -331,7 +336,7 @@ public boolean accepts(Class<? extends Configuration> type) {
331336

332337
/**
333338
* Creates a {@link Service} for the {@link KnativeConfig}.
334-
*
339+
*
335340
* @param config The sesssion.
336341
* @return The deployment config.
337342
*/

annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/annotation/KubernetesApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@
228228
*/
229229
HostAlias[] hostAliases() default {};
230230

231+
/**
232+
* The nodeSelector
233+
*/
234+
NodeSelector nodeSelector() default @NodeSelector();
235+
231236
/**
232237
* The liveness probe.
233238
*

annotations/minikube-annotations/src/main/java/io/dekorate/minikube/annotation/Minikube.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
@BuildableReference(io.dekorate.kubernetes.config.HostAlias.class),
4949
@BuildableReference(io.dekorate.kubernetes.config.Container.class),
5050
@BuildableReference(io.dekorate.kubernetes.config.Job.class),
51-
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class)
51+
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class),
52+
@BuildableReference(io.dekorate.kubernetes.config.NodeSelector.class)
5253
})
5354
@Pojo(name = "MinikubeConfig", relativePath = "../config", autobox = true, mutable = true, superClass = BaseConfig.class, withStaticBuilderMethod = true, withStaticAdapterMethod = false, adapter = @Adapter(suffix = "Adapter", relativePath = "../adapter", withMapAdapterMethod = true))
5455
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })

annotations/openshift-annotations/src/main/java/io/dekorate/openshift/annotation/OpenshiftApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import io.dekorate.kubernetes.config.BaseConfig;
4545
import io.dekorate.kubernetes.config.DeploymentStrategy;
4646
import io.dekorate.kubernetes.config.HostAlias;
47+
import io.dekorate.kubernetes.config.NodeSelector;
4748
import io.dekorate.project.BuildInfo;
4849
import io.dekorate.project.Project;
4950
import io.sundr.builder.annotations.Adapter;
@@ -54,7 +55,8 @@
5455
@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder", refs = {
5556
@BuildableReference(Project.class),
5657
@BuildableReference(BuildInfo.class),
57-
@BuildableReference(HostAlias.class)
58+
@BuildableReference(HostAlias.class),
59+
@BuildableReference(NodeSelector.class)
5860
})
5961
@Pojo(name = "OpenshiftConfig", autobox = true, mutable = true, superClass = BaseConfig.class, relativePath = "../config", withStaticAdapterMethod = false, adapter = @Adapter(relativePath = "../adapter", withMapAdapterMethod = true))
6062
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })

core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import io.dekorate.kubernetes.decorator.AddLivenessProbeDecorator;
5555
import io.dekorate.kubernetes.decorator.AddMetadataToTemplateDecorator;
5656
import io.dekorate.kubernetes.decorator.AddMountDecorator;
57+
import io.dekorate.kubernetes.decorator.AddNodeSelectorDecorator;
5758
import io.dekorate.kubernetes.decorator.AddPortDecorator;
5859
import io.dekorate.kubernetes.decorator.AddPvcVolumeDecorator;
5960
import io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator;
@@ -80,7 +81,7 @@
8081
/**
8182
* An abstract generator.
8283
* A generator is meant to populate the initial resources to the {@link Session} as well as adding decorator etc.
83-
*
84+
*
8485
* @param <C> The config type (its expected to vary between processors).
8586
*/
8687
public abstract class AbstractKubernetesManifestGenerator<C extends BaseConfig> implements ManifestGenerator<C>, WithProject {
@@ -97,7 +98,7 @@ public AbstractKubernetesManifestGenerator(ResourceRegistry resources, Configura
9798

9899
/**
99100
* Generate / populate the resources.
100-
*
101+
*
101102
* @param config
102103
*/
103104
public abstract void generate(C config);
@@ -106,7 +107,7 @@ public AbstractKubernetesManifestGenerator(ResourceRegistry resources, Configura
106107
* Add all decorator to the resources.
107108
* This method will read the config and then add all the required decorator to the resources.
108109
* The method is intended to be called from the generate method and thus marked as protected.
109-
*
110+
*
110111
* @param group The group.
111112
* @param config The config.
112113
*/
@@ -150,15 +151,22 @@ protected void addDecorators(String group, C config) {
150151
resourceRegistry.decorate(new AddHostAliasesDecorator(config.getName(), hostAlias));
151152
}
152153

154+
if (config.getNodeSelector() != null) {
155+
resourceRegistry.decorate(new AddNodeSelectorDecorator(config.getName(), config.getNodeSelector()));
156+
}
157+
153158
for (Container container : config.getSidecars()) {
154159
resourceRegistry.decorate(group, new AddSidecarDecorator(config.getName(), container));
155160
}
161+
156162
for (Env env : config.getEnvVars()) {
157163
resourceRegistry.decorate(group, new AddEnvVarDecorator(config.getName(), config.getName(), env));
158164
}
165+
159166
for (Port port : config.getPorts()) {
160167
resourceRegistry.decorate(group, new AddPortDecorator(config.getName(), config.getName(), port));
161168
}
169+
162170
for (Mount mount : config.getMounts()) {
163171
resourceRegistry.decorate(group, new AddMountDecorator(config.getName(), config.getName(), mount));
164172
}

core/src/main/java/io/dekorate/kubernetes/annotation/Base.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@
195195
*/
196196
HostAlias[] hostAliases() default {};
197197

198+
/**
199+
* Node Selector
200+
*
201+
* @return The nodeSelector
202+
*/
203+
NodeSelector nodeSelector();
204+
198205
/**
199206
* The liveness probe.
200207
*
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2018 The original authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.dekorate.kubernetes.annotation;
17+
18+
public @interface NodeSelector {
19+
20+
String key() default "";
21+
22+
String value() default "";
23+
}

0 commit comments

Comments
 (0)