Skip to content

Commit e3282c4

Browse files
committed
Support overwrite values from existing init-containers
Related to quarkusio/quarkus#33097 (comment)
1 parent 3d1b808 commit e3282c4

File tree

8 files changed

+235
-2
lines changed

8 files changed

+235
-2
lines changed

core/src/main/java/io/dekorate/kubernetes/decorator/AddInitContainerDecorator.java

+50-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.dekorate.doc.Description;
1919
import io.dekorate.kubernetes.adapter.ContainerAdapter;
2020
import io.dekorate.kubernetes.config.Container;
21+
import io.fabric8.kubernetes.api.model.ContainerBuilder;
2122
import io.fabric8.kubernetes.api.model.ObjectMeta;
2223
import io.fabric8.kubernetes.api.model.PodSpecBuilder;
2324

@@ -36,6 +37,54 @@ public AddInitContainerDecorator(String deployment, Container container) {
3637

3738
@Override
3839
public void andThenVisit(PodSpecBuilder podSpec, ObjectMeta resourceMeta) {
39-
podSpec.addToInitContainers(ContainerAdapter.adapt(container));
40+
var resource = ContainerAdapter.adapt(container);
41+
if (podSpec.hasMatchingInitContainer(this::existsContainerByName)) {
42+
update(podSpec, resource);
43+
} else {
44+
add(podSpec, resource);
45+
}
46+
}
47+
48+
private void add(PodSpecBuilder podSpec, io.fabric8.kubernetes.api.model.Container resource) {
49+
podSpec.addToInitContainers(resource);
50+
}
51+
52+
private void update(PodSpecBuilder podSpec, io.fabric8.kubernetes.api.model.Container resource) {
53+
var matching = podSpec.editMatchingInitContainer(this::existsContainerByName);
54+
55+
if (resource.getImage() != null) {
56+
matching.withImage(resource.getImage());
57+
}
58+
59+
if (resource.getWorkingDir() != null) {
60+
matching.withWorkingDir(resource.getWorkingDir());
61+
}
62+
63+
if (resource.getCommand() != null && !resource.getCommand().isEmpty()) {
64+
matching.withCommand(resource.getCommand());
65+
}
66+
67+
if (resource.getArgs() != null && !resource.getArgs().isEmpty()) {
68+
matching.withArgs(resource.getArgs());
69+
}
70+
71+
if (resource.getReadinessProbe() != null) {
72+
matching.withReadinessProbe(resource.getReadinessProbe());
73+
}
74+
75+
if (resource.getLivenessProbe() != null) {
76+
matching.withLivenessProbe(resource.getLivenessProbe());
77+
}
78+
79+
matching.addAllToEnv(resource.getEnv());
80+
if (resource.getPorts() != null && !resource.getPorts().isEmpty()) {
81+
matching.withPorts(resource.getPorts());
82+
}
83+
84+
matching.endInitContainer();
85+
}
86+
87+
private boolean existsContainerByName(ContainerBuilder containerBuilder) {
88+
return containerBuilder.getName().equals(container.getName());
4089
}
4190
}

core/src/main/java/io/dekorate/kubernetes/decorator/ApplyRequestsCpuDecorator.java

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public ApplyRequestsCpuDecorator(String deploymentName, String containerName, St
3737

3838
@Override
3939
public void andThenVisit(ContainerFluent<?> container) {
40-
System.out.println("Apply requests cpu to:" + container.getName());
4140
container.editOrNewResources().addToRequests(CPU, new Quantity(amount)).endResources();
4241
}
4342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<artifactId>dekorate-tests</artifactId>
8+
<groupId>io.dekorate</groupId>
9+
<version>3.6-SNAPSHOT</version>
10+
<relativePath>../</relativePath>
11+
</parent>
12+
13+
<groupId>io.dekorate</groupId>
14+
<artifactId>feat-overwrite-init-containers</artifactId>
15+
<name>Dekorate :: Tests :: Annotations :: Allow to overwrite existing init container</name>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.dekorate</groupId>
20+
<artifactId>kubernetes-annotations</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.dekorate</groupId>
25+
<artifactId>dekorate-spring-boot</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
<version>${version.spring-boot}</version>
32+
</dependency>
33+
34+
<!-- Testing -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-api</artifactId>
38+
<version>${version.junit-jupiter}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-engine</artifactId>
44+
<version>${version.junit-jupiter}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<resources>
51+
<resource>
52+
<directory>src/main/resources</directory>
53+
<filtering>true</filtering>
54+
</resource>
55+
</resources>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<inherited>true</inherited>
61+
<configuration>
62+
<useSystemClassLoader>false</useSystemClassLoader>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
<version>${version.spring-boot}</version>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.annotationless;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
@SpringBootApplication
22+
public class DemoApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(DemoApplication.class, args);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.annotationless;
19+
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
import org.springframework.web.bind.annotation.RestController;
22+
23+
@RestController
24+
public class HelloController {
25+
26+
private static final String HELLO = "hello world!";
27+
28+
@RequestMapping("/")
29+
public String hello() {
30+
return HELLO;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dekorate.kubernetes.init-containers[0].name=foo
2+
dekorate.kubernetes.init-containers[1].name=foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.example;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
import java.util.Optional;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
import io.dekorate.utils.Serialization;
28+
import io.fabric8.kubernetes.api.model.HasMetadata;
29+
import io.fabric8.kubernetes.api.model.KubernetesList;
30+
import io.fabric8.kubernetes.api.model.apps.Deployment;
31+
32+
public class FeatOverwriteInitContainersTest {
33+
34+
@Test
35+
public void shouldContainOnlyOneInitContainer() {
36+
KubernetesList list = Serialization
37+
.unmarshalAsList(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml"));
38+
assertNotNull(list);
39+
Deployment s = findFirst(list, Deployment.class).orElseThrow(() -> new IllegalStateException());
40+
assertEquals("feat-overwrite-init-containers", s.getMetadata().getName());
41+
assertEquals(1, s.getSpec().getTemplate().getSpec().getInitContainers().size());
42+
assertEquals("foo", s.getSpec().getTemplate().getSpec().getInitContainers().get(0).getName());
43+
}
44+
45+
<T extends HasMetadata> Optional<T> findFirst(KubernetesList list, Class<T> t) {
46+
return (Optional<T>) list.getItems().stream()
47+
.filter(i -> t.isInstance(i))
48+
.findFirst();
49+
}
50+
}

tests/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<module>issue-1158-spring-boot-certmanager-with-annotations</module>
9696
<module>feat-openshift-secure-routes</module>
9797
<module>issue-knative-probes-helm</module>
98+
<module>feat-overwrite-init-containers</module>
9899
</modules>
99100

100101
</project>

0 commit comments

Comments
 (0)