18
18
import io .dekorate .doc .Description ;
19
19
import io .dekorate .kubernetes .adapter .ContainerAdapter ;
20
20
import io .dekorate .kubernetes .config .Container ;
21
+ import io .fabric8 .kubernetes .api .model .ContainerBuilder ;
21
22
import io .fabric8 .kubernetes .api .model .ObjectMeta ;
22
23
import io .fabric8 .kubernetes .api .model .PodSpecBuilder ;
23
24
@@ -36,6 +37,54 @@ public AddInitContainerDecorator(String deployment, Container container) {
36
37
37
38
@ Override
38
39
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 ());
40
89
}
41
90
}
0 commit comments