From 7ceedead3564a874abd112c60cccef142db14f1d Mon Sep 17 00:00:00 2001 From: seeker Date: Thu, 4 Jan 2024 18:05:15 +0800 Subject: [PATCH] Fix VolumeClaimTemplates index out of range problem (#2493) when the desired statefulset has different numbers of volume claim template with current cluster, will be panic because of index out of range --- pkg/cluster/cluster.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 992d665b2..69fcb895c 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -480,23 +480,24 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *appsv1.StatefulSet) *compa if len(c.Statefulset.Spec.VolumeClaimTemplates) != len(statefulSet.Spec.VolumeClaimTemplates) { needsReplace = true reasons = append(reasons, "new statefulset's volumeClaimTemplates contains different number of volumes to the old one") - } - for i := 0; i < len(c.Statefulset.Spec.VolumeClaimTemplates); i++ { - name := c.Statefulset.Spec.VolumeClaimTemplates[i].Name - // Some generated fields like creationTimestamp make it not possible to use DeepCompare on ObjectMeta - if name != statefulSet.Spec.VolumeClaimTemplates[i].Name { - needsReplace = true - reasons = append(reasons, fmt.Sprintf("new statefulset's name for volume %d does not match the current one", i)) - continue - } - if !reflect.DeepEqual(c.Statefulset.Spec.VolumeClaimTemplates[i].Annotations, statefulSet.Spec.VolumeClaimTemplates[i].Annotations) { - needsReplace = true - reasons = append(reasons, fmt.Sprintf("new statefulset's annotations for volume %q does not match the current one", name)) - } - if !reflect.DeepEqual(c.Statefulset.Spec.VolumeClaimTemplates[i].Spec, statefulSet.Spec.VolumeClaimTemplates[i].Spec) { + } else { + for i := 0; i < len(c.Statefulset.Spec.VolumeClaimTemplates); i++ { name := c.Statefulset.Spec.VolumeClaimTemplates[i].Name - needsReplace = true - reasons = append(reasons, fmt.Sprintf("new statefulset's volumeClaimTemplates specification for volume %q does not match the current one", name)) + // Some generated fields like creationTimestamp make it not possible to use DeepCompare on ObjectMeta + if name != statefulSet.Spec.VolumeClaimTemplates[i].Name { + needsReplace = true + reasons = append(reasons, fmt.Sprintf("new statefulset's name for volume %d does not match the current one", i)) + continue + } + if !reflect.DeepEqual(c.Statefulset.Spec.VolumeClaimTemplates[i].Annotations, statefulSet.Spec.VolumeClaimTemplates[i].Annotations) { + needsReplace = true + reasons = append(reasons, fmt.Sprintf("new statefulset's annotations for volume %q does not match the current one", name)) + } + if !reflect.DeepEqual(c.Statefulset.Spec.VolumeClaimTemplates[i].Spec, statefulSet.Spec.VolumeClaimTemplates[i].Spec) { + name := c.Statefulset.Spec.VolumeClaimTemplates[i].Name + needsReplace = true + reasons = append(reasons, fmt.Sprintf("new statefulset's volumeClaimTemplates specification for volume %q does not match the current one", name)) + } } }