-
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.
Add nodeSelector capabilities to DeploymentSpec
Signed-off-by: Vincent Sourin <sourin-v@bridgestone-bae.com> Signed-off-by: Vinche <sourin-v@bridgestone-bae.com>
- Loading branch information
Showing
25 changed files
with
714 additions
and
7 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
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
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
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
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
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
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
23 changes: 23 additions & 0 deletions
23
core/src/main/java/io/dekorate/kubernetes/annotation/NodeSelector.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 @@ | ||
/** | ||
* 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 ""; | ||
} |
57 changes: 57 additions & 0 deletions
57
core/src/main/java/io/dekorate/kubernetes/decorator/AddNodeSelectorDecorator.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,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); | ||
} | ||
} |
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,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 |
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,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> |
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,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 | ||
``` | ||
|
||
|
Oops, something went wrong.