Skip to content

Commit

Permalink
Add nodeSelector capabilities to DeploymentSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Sourin <[email protected]>
  • Loading branch information
Vinche59 committed Oct 27, 2024
1 parent 8bb8c28 commit b7514dd
Show file tree
Hide file tree
Showing 27 changed files with 791 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
@BuildableReference(io.dekorate.kubernetes.config.HostAlias.class),
@BuildableReference(io.dekorate.kubernetes.config.Container.class),
@BuildableReference(io.dekorate.kubernetes.config.Job.class),
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class)
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class),
@BuildableReference(io.dekorate.kubernetes.config.NodeSelector.class)
})
@Pojo(name = "KindConfig", relativePath = "../config", autobox = true, mutable = true, superClass = BaseConfig.class, withStaticBuilderMethod = true, withStaticAdapterMethod = false, adapter = @Adapter(name = "KindConfigAdapter", relativePath = "../adapter", withMapAdapterMethod = true))
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.dekorate.kubernetes.annotation.ServiceType;
import io.dekorate.kubernetes.config.BaseConfig;
import io.dekorate.kubernetes.config.HostAlias;
import io.dekorate.kubernetes.config.NodeSelector;
import io.dekorate.kubernetes.config.RollingUpdate;
import io.dekorate.project.BuildInfo;
import io.dekorate.project.Project;
Expand All @@ -57,7 +58,8 @@
@BuildableReference(Project.class),
@BuildableReference(BuildInfo.class),
@BuildableReference(HostAlias.class),
@BuildableReference(RollingUpdate.class)
@BuildableReference(RollingUpdate.class),
@BuildableReference(NodeSelector.class)
})
@Pojo(name = "KnativeConfig", autobox = true, mutable = true, superClass = BaseConfig.class, relativePath = "../config", withStaticAdapterMethod = false, adapter = @Adapter(relativePath = "../adapter", withMapAdapterMethod = true))
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2018 The 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.knative.decorator;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import io.dekorate.kubernetes.config.NodeSelector;
import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
import io.dekorate.utils.Strings;
import io.fabric8.knative.serving.v1.RevisionSpecFluent;
import io.fabric8.kubernetes.api.model.ObjectMeta;

public class AddNodeSelectorToRevisionDecorator extends NamedResourceDecorator<RevisionSpecFluent<?>> {

private final NodeSelector nodeSelector;

public AddNodeSelectorToRevisionDecorator(NodeSelector nodeSelector) {
this(ANY, nodeSelector);
}

public AddNodeSelectorToRevisionDecorator(String deploymentName, NodeSelector nodeSelector) {
super(deploymentName);
this.nodeSelector = nodeSelector;
}

public void andThenVisit(RevisionSpecFluent<?> revisionSpec, ObjectMeta resourceMeta) {
if (Strings.isNotNullOrEmpty(nodeSelector.getKey()) && Strings.isNotNullOrEmpty(nodeSelector.getValue())) {
Map<String, String> existing = revisionSpec.getNodeSelector();

if (existing == null)
existing = new HashMap<>();
else
existing.clear();

existing.put(nodeSelector.getKey(), nodeSelector.getValue());
revisionSpec.withNodeSelector(existing);
}
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AddNodeSelectorToRevisionDecorator that = (AddNodeSelectorToRevisionDecorator) o;
return Objects.equals(nodeSelector, that.nodeSelector);
}

@Override
public int hashCode() {
return Objects.hash(nodeSelector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.dekorate.knative.decorator.AddConfigMapVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddEmptyDirVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddHostAliasesToRevisionDecorator;
import io.dekorate.knative.decorator.AddNodeSelectorToRevisionDecorator;
import io.dekorate.knative.decorator.AddPvcVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddSecretVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddSidecarToRevisionDecorator;
Expand Down Expand Up @@ -292,6 +293,10 @@ public void generate(KnativeConfig config) {
resourceRegistry.decorate(KNATIVE, new AddHostAliasesToRevisionDecorator(hostAlias));
}

if (config.getNodeSelector() != null) {
resourceRegistry.decorate(KNATIVE, new AddNodeSelectorToRevisionDecorator(config.getNodeSelector()));
}

}

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

/**
* Creates a {@link Service} for the {@link KnativeConfig}.
*
*
* @param config The sesssion.
* @return The deployment config.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
*/
HostAlias[] hostAliases() default {};

/**
* The nodeSelector
*/
NodeSelector nodeSelector() default @NodeSelector();

/**
* The liveness probe.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
@BuildableReference(io.dekorate.kubernetes.config.HostAlias.class),
@BuildableReference(io.dekorate.kubernetes.config.Container.class),
@BuildableReference(io.dekorate.kubernetes.config.Job.class),
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class)
@BuildableReference(io.dekorate.kubernetes.config.CronJob.class),
@BuildableReference(io.dekorate.kubernetes.config.NodeSelector.class)
})
@Pojo(name = "MinikubeConfig", relativePath = "../config", autobox = true, mutable = true, superClass = BaseConfig.class, withStaticBuilderMethod = true, withStaticAdapterMethod = false, adapter = @Adapter(suffix = "Adapter", relativePath = "../adapter", withMapAdapterMethod = true))
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.dekorate.kubernetes.config.BaseConfig;
import io.dekorate.kubernetes.config.DeploymentStrategy;
import io.dekorate.kubernetes.config.HostAlias;
import io.dekorate.kubernetes.config.NodeSelector;
import io.dekorate.project.BuildInfo;
import io.dekorate.project.Project;
import io.sundr.builder.annotations.Adapter;
Expand All @@ -54,7 +55,8 @@
@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder", refs = {
@BuildableReference(Project.class),
@BuildableReference(BuildInfo.class),
@BuildableReference(HostAlias.class)
@BuildableReference(HostAlias.class),
@BuildableReference(NodeSelector.class)
})
@Pojo(name = "OpenshiftConfig", autobox = true, mutable = true, superClass = BaseConfig.class, relativePath = "../config", withStaticAdapterMethod = false, adapter = @Adapter(relativePath = "../adapter", withMapAdapterMethod = true))
@Target({ ElementType.CONSTRUCTOR, ElementType.TYPE })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import io.dekorate.kubernetes.decorator.AddLivenessProbeDecorator;
import io.dekorate.kubernetes.decorator.AddMetadataToTemplateDecorator;
import io.dekorate.kubernetes.decorator.AddMountDecorator;
import io.dekorate.kubernetes.decorator.AddNodeSelectorDecorator;
import io.dekorate.kubernetes.decorator.AddPortDecorator;
import io.dekorate.kubernetes.decorator.AddPvcVolumeDecorator;
import io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator;
Expand All @@ -80,7 +81,7 @@
/**
* An abstract generator.
* A generator is meant to populate the initial resources to the {@link Session} as well as adding decorator etc.
*
*
* @param <C> The config type (its expected to vary between processors).
*/
public abstract class AbstractKubernetesManifestGenerator<C extends BaseConfig> implements ManifestGenerator<C>, WithProject {
Expand All @@ -97,7 +98,7 @@ public AbstractKubernetesManifestGenerator(ResourceRegistry resources, Configura

/**
* Generate / populate the resources.
*
*
* @param config
*/
public abstract void generate(C config);
Expand All @@ -106,7 +107,7 @@ public AbstractKubernetesManifestGenerator(ResourceRegistry resources, Configura
* Add all decorator to the resources.
* This method will read the config and then add all the required decorator to the resources.
* The method is intended to be called from the generate method and thus marked as protected.
*
*
* @param group The group.
* @param config The config.
*/
Expand Down Expand Up @@ -150,15 +151,22 @@ protected void addDecorators(String group, C config) {
resourceRegistry.decorate(new AddHostAliasesDecorator(config.getName(), hostAlias));
}

if (config.getNodeSelector() != null) {
resourceRegistry.decorate(new AddNodeSelectorDecorator(config.getName(), config.getNodeSelector()));
}

for (Container container : config.getSidecars()) {
resourceRegistry.decorate(group, new AddSidecarDecorator(config.getName(), container));
}

for (Env env : config.getEnvVars()) {
resourceRegistry.decorate(group, new AddEnvVarDecorator(config.getName(), config.getName(), env));
}

for (Port port : config.getPorts()) {
resourceRegistry.decorate(group, new AddPortDecorator(config.getName(), config.getName(), port));
}

for (Mount mount : config.getMounts()) {
resourceRegistry.decorate(group, new AddMountDecorator(config.getName(), config.getName(), mount));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@
*/
HostAlias[] hostAliases() default {};

/**
* Node Selector
*
* @return The nodeSelector
*/
NodeSelector nodeSelector();

/**
* The liveness probe.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2018 The 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.annotation;

public @interface NodeSelector {

String key() default "";

String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2018 The 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.Objects;

import io.dekorate.kubernetes.config.NodeSelector;
import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.PodSpecFluent;

public class AddNodeSelectorDecorator extends NamedResourceDecorator<PodSpecFluent<?>> {

private final NodeSelector nodeSelector;

public AddNodeSelectorDecorator(String deploymentName, NodeSelector nodeSelector) {
super(deploymentName);
this.nodeSelector = nodeSelector;
}

public void andThenVisit(PodSpecFluent<?> podSpec, ObjectMeta resourceMeta) {
if (Strings.isNotNullOrEmpty(nodeSelector.getKey()) && Strings.isNotNullOrEmpty(nodeSelector.getValue())) {
podSpec.removeFromNodeSelector(nodeSelector.getKey());
podSpec.addToNodeSelector(nodeSelector.getKey(), nodeSelector.getValue());
}
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AddNodeSelectorDecorator that = (AddNodeSelectorDecorator) o;
return Objects.equals(nodeSelector, that.nodeSelector);
}

@Override
public int hashCode() {
return Objects.hash(nodeSelector);
}
}
4 changes: 4 additions & 0 deletions examples/kubernetes-with-nodeselectors-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:8u171-alpine3.7
RUN apk --no-cache add curl
COPY target/*.jar kubernetes-with-nodeselectors-example.jar
CMD java ${JAVA_OPTS} -jar kubernetes-with-nodeselectors-example.jar
Loading

0 comments on commit b7514dd

Please sign in to comment.