forked from dekorateio/dekorate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the generation of hostAliases property from Deployment Signed-off-by: Vincent Sourtin <[email protected]>
- Loading branch information
Showing
11 changed files
with
374 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
core/src/main/java/io/dekorate/kubernetes/annotation/HostAlias.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 @@ | ||
/** | ||
* 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 HostAlias { | ||
|
||
/** | ||
* Ip address to resolve to. | ||
* @return the ip address. | ||
*/ | ||
String ip() default ""; | ||
|
||
/** | ||
* List of hostnames (comma separated) | ||
* @return the hostnames | ||
*/ | ||
String hostnames() default ""; | ||
} |
68 changes: 68 additions & 0 deletions
68
core/src/main/java/io/dekorate/kubernetes/decorator/AddHostAliasesDecorator.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,68 @@ | ||
/** | ||
* 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 io.dekorate.deps.kubernetes.api.builder.Predicate; | ||
import io.dekorate.deps.kubernetes.api.model.HostAliasBuilder; | ||
import io.dekorate.deps.kubernetes.api.model.ObjectMeta; | ||
import io.dekorate.deps.kubernetes.api.model.PodSpecFluent; | ||
import io.dekorate.kubernetes.config.HostAlias; | ||
import io.dekorate.utils.Strings; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class AddHostAliasesDecorator extends NamedResourceDecorator<PodSpecFluent<?>> { | ||
|
||
private final HostAlias hostAlias; | ||
|
||
public AddHostAliasesDecorator(String deploymentName, HostAlias hostAlias) { | ||
super(deploymentName); | ||
this.hostAlias = hostAlias; | ||
} | ||
|
||
public void andThenVisit(PodSpecFluent<?> podSpec, ObjectMeta resourceMeta) { | ||
if (Strings.isNotNullOrEmpty(hostAlias.getIp()) && Strings.isNotNullOrEmpty(hostAlias.getHostnames())) { | ||
Predicate<HostAliasBuilder> matchingHostAlias = host -> { | ||
if (host.getIp() != null) | ||
return host.getIp().equals(hostAlias.getIp()); | ||
return false; | ||
}; | ||
|
||
podSpec.removeMatchingFromHostAliases(matchingHostAlias); | ||
|
||
podSpec.addNewHostAlias() | ||
.withIp(hostAlias.getIp()) | ||
.withHostnames(Arrays.asList(hostAlias.getHostnames().split(","))) | ||
.endHostAlias(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AddHostAliasesDecorator that = (AddHostAliasesDecorator) o; | ||
return Objects.equals(hostAlias, that.hostAlias); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(hostAlias); | ||
} | ||
} |
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-example-with-hostaliases.jar | ||
CMD java ${JAVA_OPTS} -jar kubernetes-example-with-hostaliases.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,78 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>examples</artifactId> | ||
<groupId>io.dekorate</groupId> | ||
<version>0.12-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.dekorate</groupId> | ||
<artifactId>kubernetes-example-with-hostaliases</artifactId> | ||
<name>Dekorate :: Examples :: Kubernetes with hostAliases</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.1.13.RELEASE</version.spring-boot> | ||
</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> | ||
<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> | ||
</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,60 @@ | ||
# Add hostAliases property Example | ||
|
||
An example that demonstrates the use of `@KubernetesApplication` in order to add hostAliases 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 hostAliases section of the Deployment specification you need pass the `hostAliases` parameter containing the ip address and the list of hostnames (comma separated values) to the `@KubernetesApplication` in the Spring Boot annotated class. The code would look as follow: | ||
|
||
``` | ||
@KubernetesApplication(hostAliases = {@HostAlias(ip = "127.0.0.1", hostnames = "foo.org,bar.com")}) | ||
@SpringBootApplication | ||
public class Main { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Main.class, args); | ||
} | ||
} | ||
``` | ||
You can pass multiple `@HostAlias` annotation depending of your needs. | ||
|
||
Check, if necessary, the [Main.java](src/main/java/io/dekorate/examples/kubernetes/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: | ||
labels: | ||
app.kubernetes.io/name: kubernetes-example-with-hostaliases | ||
app.kubernetes.io/version: 0.12-SNAPSHOT | ||
name: kubernetes-example-with-hostaliases | ||
spec: | ||
replicas: 1 | ||
template: | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: kubernetes-example-with-hostaliases | ||
app.kubernetes.io/version: 0.12-SNAPSHOT | ||
spec: | ||
hostAliases: | ||
- hostnames: | ||
- foo.org | ||
- bar.com | ||
ip: 127.0.0.1 | ||
- hostnames: | ||
- test.com | ||
ip: 10.0.0.1 | ||
``` | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
...es-example-with-hostaliases/src/main/java/io/dekorate/examples/kubernetes/Controller.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,28 @@ | ||
/** | ||
* 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.examples.kubernetes; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class Controller { | ||
|
||
@RequestMapping("/") | ||
public String hello() { | ||
return "Hello world"; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...bernetes-example-with-hostaliases/src/main/java/io/dekorate/examples/kubernetes/Main.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 @@ | ||
/** | ||
* 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.examples.kubernetes; | ||
|
||
import io.dekorate.kubernetes.annotation.HostAlias; | ||
import io.dekorate.kubernetes.annotation.KubernetesApplication; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@KubernetesApplication(hostAliases = {@HostAlias(ip = "127.0.0.1", hostnames = "foo.org,bar.com"), | ||
@HostAlias(ip = "10.0.0.1", hostnames = "test.com")}) | ||
@SpringBootApplication | ||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Main.class, args); | ||
} | ||
} |
Oops, something went wrong.