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 <sourin-v@bridgestone-bae.com>
Signed-off-by: Vinche <sourin-v@bridgestone-bae.com>
  • Loading branch information
Vinche59 committed Oct 26, 2024
1 parent 8bb8c28 commit 0b447b7
Show file tree
Hide file tree
Showing 25 changed files with 714 additions and 7 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
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
94 changes: 94 additions & 0 deletions examples/kubernetes-with-nodeselectors-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.dekorate</groupId>
<artifactId>kubernetes-with-nodeselectors-example</artifactId>
<version>999-SNAPSHOT</version>
<name>Dekorate :: Examples :: Kubernetes with nodeSelectors</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<version.spring-boot>2.5.12</version.spring-boot>
<version.maven-compiler-plugin>3.8.0</version.maven-compiler-plugin>
<version.maven-surefire-plugin>3.0.0-M3</version.maven-surefire-plugin>
</properties>

<dependencies>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-annotations</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.spring-boot}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-junit-starter</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.maven-compiler-plugin}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${version.spring-boot}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
<inherited>true</inherited>
<configuration>
<trimStackTrace>false</trimStackTrace>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>


</project>
71 changes: 71 additions & 0 deletions examples/kubernetes-with-nodeselectors-example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Add nodeSelector example

An example that demonstrates the use of `@KubernetesApplication` in order to add nodeSelector property to a deployment.
To access the `@KubernetesApplication` annotation you just need to have the following dependency in your
class path:

<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-annotations</artifactId>
<version>${project.version}</version>
</dependency>

So as to add the nodeSelector section of the Deployment specification you need pass the `nodeSelector` parameter containing they key and the value of the `nodeSelector` `@KubernetesApplication` in the Spring Boot annotated class. The code would look as follow:

```
@KubernetesApplication(nodeSelector = @NodeSelector(key = "diskType", value = "ssd"))
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
```
Check, if necessary, the [Main.java](src/main/java/io/dekorate/example/Main.java).

Compile the project using:

mvn clean install

You can find the generated deployment under: `target/classes/META-INF/dekorate/kubernetes.yml` that should look like:
```---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
app.dekorate.io/vcs-url: <<unknown>>
app.dekorate.io/commit-id: 31df9b1860543a31fc710f8461b160ea65ce7c7a
labels:
app.kubernetes.io/name: kubernetes-with-nodeselectors-example
app.kubernetes.io/version: 999-SNAPSHOT
name: kubernetes-with-nodeselectors-example
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: kubernetes-with-nodeselectors-example
app.kubernetes.io/version: 999-SNAPSHOT
template:
metadata:
annotations:
app.dekorate.io/vcs-url: <<unknown>>
app.dekorate.io/commit-id: 31df9b1860543a31fc710f8461b160ea65ce7c7a
labels:
app.kubernetes.io/name: kubernetes-with-nodeselectors-example
app.kubernetes.io/version: 999-SNAPSHOT
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: vinche/kubernetes-with-nodeselectors-example:999-SNAPSHOT
imagePullPolicy: IfNotPresent
name: kubernetes-with-nodeselectors-example
nodeSelector:
diskType: ssd
```


Loading

0 comments on commit 0b447b7

Please sign in to comment.