Skip to content

Commit 95d86c7

Browse files
author
Dmitrii Dolgov
committed
Move container comparison logic to a separate function
1 parent c4aab50 commit 95d86c7

File tree

1 file changed

+61
-27
lines changed

1 file changed

+61
-27
lines changed

pkg/cluster/cluster.go

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -309,33 +309,7 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *v1beta1.StatefulSet) *comp
309309
needsRollUpdate = true
310310
reasons = append(reasons, "new statefulset's container specification doesn't match the current one")
311311
} else {
312-
for index, container1 := range c.Statefulset.Spec.Template.Spec.Containers {
313-
container2 := statefulSet.Spec.Template.Spec.Containers[index]
314-
if container1.Name != container2.Name {
315-
needsRollUpdate = true
316-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d name doesn't match the current one", index))
317-
}
318-
if container1.Image != container2.Image {
319-
needsRollUpdate = true
320-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d image doesn't match the current one", index))
321-
}
322-
if !reflect.DeepEqual(container1.Ports, container2.Ports) {
323-
needsRollUpdate = true
324-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d ports don't match the current one", index))
325-
}
326-
if !compareResources(&container1.Resources, &container2.Resources) {
327-
needsRollUpdate = true
328-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d resources don't match the current ones", index))
329-
}
330-
if !reflect.DeepEqual(container1.Env, container2.Env) {
331-
needsRollUpdate = true
332-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d environment doesn't match the current one", index))
333-
}
334-
if !reflect.DeepEqual(container1.EnvFrom, container2.EnvFrom) {
335-
needsRollUpdate = true
336-
reasons = append(reasons, fmt.Sprintf("new statefulset's container %d environment sources don't match the current one", index))
337-
}
338-
}
312+
needsRollUpdate, reasons = c.compareContainers(c.Statefulset, statefulSet)
339313
}
340314
if len(c.Statefulset.Spec.Template.Spec.Containers) == 0 {
341315
c.logger.Warningf("statefulset %q has no container", util.NameFromMeta(c.Statefulset.ObjectMeta))
@@ -403,6 +377,66 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *v1beta1.StatefulSet) *comp
403377
return &compareStatefulsetResult{match: match, reasons: reasons, rollingUpdate: needsRollUpdate, replace: needsReplace}
404378
}
405379

380+
type ContainerCheck struct {
381+
condition func(a, b v1.Container) bool
382+
reason string
383+
}
384+
385+
// compareContainers: compare containers from two stateful sets
386+
// and return:
387+
// * whether or not roll update is needed
388+
// * a list of reasons in a human readable format
389+
func (c *Cluster) compareContainers(setA, setB *v1beta1.StatefulSet) (bool, []string) {
390+
reasons := make([]string, 0)
391+
needsRollUpdate := false
392+
checks := map[string]ContainerCheck{
393+
"name": ContainerCheck{
394+
condition: func(a, b v1.Container) bool { return a.Name != b.Name },
395+
reason: "new statefulset's container %d name doesn't match the current one",
396+
},
397+
"image": ContainerCheck{
398+
condition: func(a, b v1.Container) bool { return a.Image != b.Image },
399+
reason: "new statefulset's container %d image doesn't match the current one",
400+
},
401+
"ports": ContainerCheck{
402+
condition: func(a, b v1.Container) bool {
403+
return !reflect.DeepEqual(a.Ports, b.Ports)
404+
},
405+
reason: "new statefulset's container %d ports don't match the current one",
406+
},
407+
"resourses": ContainerCheck{
408+
condition: func(a, b v1.Container) bool {
409+
return !compareResources(&a.Resources, &b.Resources)
410+
},
411+
reason: "new statefulset's container %d resources don't match the current ones",
412+
},
413+
"env": ContainerCheck{
414+
condition: func(a, b v1.Container) bool {
415+
return !reflect.DeepEqual(a.Env, b.Env)
416+
},
417+
reason: "new statefulset's container %d environment doesn't match the current one",
418+
},
419+
"env_from": ContainerCheck{
420+
condition: func(a, b v1.Container) bool {
421+
return !reflect.DeepEqual(a.EnvFrom, b.EnvFrom)
422+
},
423+
reason: "new statefulset's container %d environment sources don't match the current one",
424+
},
425+
}
426+
427+
for index, containerA := range setA.Spec.Template.Spec.Containers {
428+
containerB := setB.Spec.Template.Spec.Containers[index]
429+
for _, check := range checks {
430+
if check.condition(containerA, containerB) {
431+
needsRollUpdate = true
432+
reasons = append(reasons, fmt.Sprintf(check.reason, index))
433+
}
434+
}
435+
}
436+
437+
return needsRollUpdate, reasons
438+
}
439+
406440
func compareResources(a *v1.ResourceRequirements, b *v1.ResourceRequirements) (equal bool) {
407441
equal = true
408442
if a != nil {

0 commit comments

Comments
 (0)