diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddInitContainerDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddInitContainerDecorator.java
index 6e5fec71d..540c84ff8 100644
--- a/core/src/main/java/io/dekorate/kubernetes/decorator/AddInitContainerDecorator.java
+++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddInitContainerDecorator.java
@@ -18,6 +18,7 @@
import io.dekorate.doc.Description;
import io.dekorate.kubernetes.adapter.ContainerAdapter;
import io.dekorate.kubernetes.config.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.PodSpecBuilder;
@@ -36,6 +37,54 @@ public AddInitContainerDecorator(String deployment, Container container) {
@Override
public void andThenVisit(PodSpecBuilder podSpec, ObjectMeta resourceMeta) {
- podSpec.addToInitContainers(ContainerAdapter.adapt(container));
+ var resource = ContainerAdapter.adapt(container);
+ if (podSpec.hasMatchingInitContainer(this::existsContainerByName)) {
+ update(podSpec, resource);
+ } else {
+ add(podSpec, resource);
+ }
+ }
+
+ private void add(PodSpecBuilder podSpec, io.fabric8.kubernetes.api.model.Container resource) {
+ podSpec.addToInitContainers(resource);
+ }
+
+ private void update(PodSpecBuilder podSpec, io.fabric8.kubernetes.api.model.Container resource) {
+ var matching = podSpec.editMatchingInitContainer(this::existsContainerByName);
+
+ if (resource.getImage() != null) {
+ matching.withImage(resource.getImage());
+ }
+
+ if (resource.getWorkingDir() != null) {
+ matching.withWorkingDir(resource.getWorkingDir());
+ }
+
+ if (resource.getCommand() != null && !resource.getCommand().isEmpty()) {
+ matching.withCommand(resource.getCommand());
+ }
+
+ if (resource.getArgs() != null && !resource.getArgs().isEmpty()) {
+ matching.withArgs(resource.getArgs());
+ }
+
+ if (resource.getReadinessProbe() != null) {
+ matching.withReadinessProbe(resource.getReadinessProbe());
+ }
+
+ if (resource.getLivenessProbe() != null) {
+ matching.withLivenessProbe(resource.getLivenessProbe());
+ }
+
+ matching.addAllToEnv(resource.getEnv());
+ if (resource.getPorts() != null && !resource.getPorts().isEmpty()) {
+ matching.withPorts(resource.getPorts());
+ }
+
+ matching.endInitContainer();
+ }
+
+ private boolean existsContainerByName(ContainerBuilder containerBuilder) {
+ return containerBuilder.getName().equals(container.getName());
}
}
diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/ApplyRequestsCpuDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/ApplyRequestsCpuDecorator.java
index 3e25969c0..9d4e2a744 100644
--- a/core/src/main/java/io/dekorate/kubernetes/decorator/ApplyRequestsCpuDecorator.java
+++ b/core/src/main/java/io/dekorate/kubernetes/decorator/ApplyRequestsCpuDecorator.java
@@ -37,7 +37,6 @@ public ApplyRequestsCpuDecorator(String deploymentName, String containerName, St
@Override
public void andThenVisit(ContainerFluent> container) {
- System.out.println("Apply requests cpu to:" + container.getName());
container.editOrNewResources().addToRequests(CPU, new Quantity(amount)).endResources();
}
}
diff --git a/tests/feat-overwrite-init-containers/pom.xml b/tests/feat-overwrite-init-containers/pom.xml
new file mode 100644
index 000000000..70217728c
--- /dev/null
+++ b/tests/feat-overwrite-init-containers/pom.xml
@@ -0,0 +1,72 @@
+
+
+ 4.0.0
+
+
+ dekorate-tests
+ io.dekorate
+ 3.6-SNAPSHOT
+ ../
+
+
+ io.dekorate
+ feat-overwrite-init-containers
+ Dekorate :: Tests :: Annotations :: Allow to overwrite existing init container
+
+
+
+ io.dekorate
+ kubernetes-annotations
+ ${project.version}
+
+
+ io.dekorate
+ dekorate-spring-boot
+ ${project.version}
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+ ${version.spring-boot}
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ ${version.junit-jupiter}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ ${version.junit-jupiter}
+ test
+
+
+
+
+
+
+ src/main/resources
+ true
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ true
+
+ false
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ ${version.spring-boot}
+
+
+
+
diff --git a/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/DemoApplication.java b/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/DemoApplication.java
new file mode 100644
index 000000000..8bd220c62
--- /dev/null
+++ b/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/DemoApplication.java
@@ -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.annotationless;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class DemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoApplication.class, args);
+ }
+
+}
diff --git a/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/HelloController.java b/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/HelloController.java
new file mode 100644
index 000000000..1d952335a
--- /dev/null
+++ b/tests/feat-overwrite-init-containers/src/main/java/io/dekorate/annotationless/HelloController.java
@@ -0,0 +1,32 @@
+/**
+ * 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.annotationless;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class HelloController {
+
+ private static final String HELLO = "hello world!";
+
+ @RequestMapping("/")
+ public String hello() {
+ return HELLO;
+ }
+}
diff --git a/tests/feat-overwrite-init-containers/src/main/resources/application.properties b/tests/feat-overwrite-init-containers/src/main/resources/application.properties
new file mode 100644
index 000000000..492e0a0e6
--- /dev/null
+++ b/tests/feat-overwrite-init-containers/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+dekorate.kubernetes.init-containers[0].name=foo
+dekorate.kubernetes.init-containers[1].name=foo
diff --git a/tests/feat-overwrite-init-containers/src/test/java/io/dekorate/example/FeatOverwriteInitContainersTest.java b/tests/feat-overwrite-init-containers/src/test/java/io/dekorate/example/FeatOverwriteInitContainersTest.java
new file mode 100644
index 000000000..8518d2ccb
--- /dev/null
+++ b/tests/feat-overwrite-init-containers/src/test/java/io/dekorate/example/FeatOverwriteInitContainersTest.java
@@ -0,0 +1,50 @@
+/**
+ * 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.example;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import java.util.Optional;
+
+import org.junit.jupiter.api.Test;
+
+import io.dekorate.utils.Serialization;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.KubernetesList;
+import io.fabric8.kubernetes.api.model.apps.Deployment;
+
+public class FeatOverwriteInitContainersTest {
+
+ @Test
+ public void shouldContainOnlyOneInitContainer() {
+ KubernetesList list = Serialization
+ .unmarshalAsList(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml"));
+ assertNotNull(list);
+ Deployment s = findFirst(list, Deployment.class).orElseThrow(() -> new IllegalStateException());
+ assertEquals("feat-overwrite-init-containers", s.getMetadata().getName());
+ assertEquals(1, s.getSpec().getTemplate().getSpec().getInitContainers().size());
+ assertEquals("foo", s.getSpec().getTemplate().getSpec().getInitContainers().get(0).getName());
+ }
+
+ Optional findFirst(KubernetesList list, Class t) {
+ return (Optional) list.getItems().stream()
+ .filter(i -> t.isInstance(i))
+ .findFirst();
+ }
+}
diff --git a/tests/pom.xml b/tests/pom.xml
index 05c710e60..7801e2c9f 100644
--- a/tests/pom.xml
+++ b/tests/pom.xml
@@ -95,6 +95,7 @@
issue-1158-spring-boot-certmanager-with-annotations
feat-openshift-secure-routes
issue-knative-probes-helm
+ feat-overwrite-init-containers