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 ;
24
+ import io .fabric8 .kubernetes .api .model .PodSpecFluent ;
23
25
24
26
/**
25
27
* A decorator that adds an init container to a pod template.
@@ -40,7 +42,59 @@ public AddSidecarDecorator(String deployment, Container container) {
40
42
41
43
@ Override
42
44
public void andThenVisit (PodSpecBuilder podSpec , ObjectMeta resourceMeta ) {
43
- podSpec .addToContainers (ContainerAdapter .adapt (container ));
45
+ io .fabric8 .kubernetes .api .model .Container resource = ContainerAdapter .adapt (container );
46
+ if (podSpec .hasMatchingContainer (this ::existsContainerByName )) {
47
+ update (podSpec , resource );
48
+ } else {
49
+ add (podSpec , resource );
50
+ }
51
+ }
52
+
53
+ private void add (PodSpecBuilder podSpec , io .fabric8 .kubernetes .api .model .Container resource ) {
54
+ podSpec .addToContainers (resource );
55
+ }
56
+
57
+ private void update (PodSpecBuilder podSpec , io .fabric8 .kubernetes .api .model .Container resource ) {
58
+ PodSpecFluent .ContainersNested <PodSpecBuilder > matching = podSpec .editMatchingContainer (this ::existsContainerByName );
59
+
60
+ if (resource .getImage () != null ) {
61
+ matching .withImage (resource .getImage ());
62
+ }
63
+
64
+ if (resource .getImagePullPolicy () != null ) {
65
+ matching .withImagePullPolicy (resource .getImagePullPolicy ());
66
+ }
67
+
68
+ if (resource .getWorkingDir () != null ) {
69
+ matching .withWorkingDir (resource .getWorkingDir ());
70
+ }
71
+
72
+ if (resource .getCommand () != null && !resource .getCommand ().isEmpty ()) {
73
+ matching .withCommand (resource .getCommand ());
74
+ }
75
+
76
+ if (resource .getArgs () != null && !resource .getArgs ().isEmpty ()) {
77
+ matching .withArgs (resource .getArgs ());
78
+ }
79
+
80
+ if (resource .getReadinessProbe () != null ) {
81
+ matching .withReadinessProbe (resource .getReadinessProbe ());
82
+ }
83
+
84
+ if (resource .getLivenessProbe () != null ) {
85
+ matching .withLivenessProbe (resource .getLivenessProbe ());
86
+ }
87
+
88
+ matching .addAllToEnv (resource .getEnv ());
89
+ if (resource .getPorts () != null && !resource .getPorts ().isEmpty ()) {
90
+ matching .withPorts (resource .getPorts ());
91
+ }
92
+
93
+ matching .endContainer ();
94
+ }
95
+
96
+ private boolean existsContainerByName (ContainerBuilder containerBuilder ) {
97
+ return containerBuilder .getName ().equals (container .getName ());
44
98
}
45
99
46
100
public Class <? extends Decorator >[] after () {
0 commit comments