@@ -104,23 +104,7 @@ func ensureContainer(modified *bool, existing *corev1.Container, required corev1
104
104
ensureEnvFromSource (modified , & existing .EnvFrom , required .EnvFrom )
105
105
setStringIfSet (modified , & existing .WorkingDir , required .WorkingDir )
106
106
ensureResourceRequirements (modified , & existing .Resources , required .Resources )
107
-
108
- // any port we specify, we require
109
- for _ , required := range required .Ports {
110
- var existingCurr * corev1.ContainerPort
111
- for j , curr := range existing .Ports {
112
- if curr .Name == required .Name {
113
- existingCurr = & existing .Ports [j ]
114
- break
115
- }
116
- }
117
- if existingCurr == nil {
118
- * modified = true
119
- existing .Ports = append (existing .Ports , corev1.ContainerPort {})
120
- existingCurr = & existing .Ports [len (existing .Ports )- 1 ]
121
- }
122
- ensureContainerPort (modified , existingCurr , required )
123
- }
107
+ ensureContainerPorts (modified , & existing .Ports , required .Ports )
124
108
125
109
// any volume mount we specify, we require
126
110
for _ , required := range required .VolumeMounts {
@@ -191,6 +175,37 @@ func ensureProbeHandler(modified *bool, existing *corev1.Handler, required corev
191
175
}
192
176
}
193
177
178
+ func ensureContainerPorts (modified * bool , existing * []corev1.ContainerPort , required []corev1.ContainerPort ) {
179
+ for i := len (* existing ) - 1 ; i >= 0 ; i -- {
180
+ existingContainerPort := & (* existing )[i ]
181
+ var existingCurr * corev1.ContainerPort
182
+ for _ , requiredContainerPort := range required {
183
+ if existingContainerPort .Name == requiredContainerPort .Name {
184
+ existingCurr = & (* existing )[i ]
185
+ ensureContainerPort (modified , existingCurr , requiredContainerPort )
186
+ break
187
+ }
188
+ }
189
+ if existingCurr == nil {
190
+ * modified = true
191
+ * existing = append ((* existing )[:i ], (* existing )[i + 1 :]... )
192
+ }
193
+ }
194
+ for _ , requiredContainerPort := range required {
195
+ match := false
196
+ for _ , existingContainerPort := range * existing {
197
+ if existingContainerPort .Name == requiredContainerPort .Name {
198
+ match = true
199
+ break
200
+ }
201
+ }
202
+ if ! match {
203
+ * modified = true
204
+ * existing = append (* existing , requiredContainerPort )
205
+ }
206
+ }
207
+ }
208
+
194
209
func ensureContainerPort (modified * bool , existing * corev1.ContainerPort , required corev1.ContainerPort ) {
195
210
if ! equality .Semantic .DeepEqual (required , * existing ) {
196
211
* modified = true
0 commit comments