diff --git a/CHANGELOG.md b/CHANGELOG.md index a77877022..fdf2139d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ #### :beetle: Bug Fixes - Fixed multiple follower logic for redis cluster +- Fixed CR's annotations updated,sts annotations will not updated #### :tada: Features diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index 2ff34b52f..7635ace88 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -99,69 +99,71 @@ func patchStatefulSet(storedStateful *appsv1.StatefulSet, newStateful *appsv1.St } if !patchResult.IsEmpty() { logger.Info("Changes in statefulset Detected, Updating...", "patch", string(patchResult.Patch)) - // Field is immutable therefore we MUST keep it as is. - if !apiequality.Semantic.DeepEqual(newStateful.Spec.VolumeClaimTemplates, storedStateful.Spec.VolumeClaimTemplates) { - // resize pvc - // 1.Get the data already stored internally - // 2.Get the desired data - // 3.Start querying the pvc list when you find data inconsistencies - // 3.1 Comparison using real pvc capacity and desired data - // 3.1.1 Update if you find inconsistencies - // 3.2 Writing successful updates to internal - // 4. Set to old VolumeClaimTemplates to update.Prevent update error reporting - // 5. Set to old annotations to update - annotations := storedStateful.Annotations - if annotations == nil { - annotations = map[string]string{ - "storageCapacity": "0", - } - } - storedCapacity, _ := strconv.ParseInt(annotations["storageCapacity"], 0, 64) - if len(newStateful.Spec.VolumeClaimTemplates) != 0 { - stateCapacity := newStateful.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests.Storage().Value() - if storedCapacity != stateCapacity { - listOpt := metav1.ListOptions{ - LabelSelector: labels.FormatLabels( - map[string]string{ - "app": storedStateful.Name, - "app.kubernetes.io/component": "redis", - "app.kubernetes.io/name": storedStateful.Name, - }, - ), + if len(newStateful.Spec.VolumeClaimTemplates) >= 1 && len(newStateful.Spec.VolumeClaimTemplates) == len(storedStateful.Spec.VolumeClaimTemplates) { + // Field is immutable therefore we MUST keep it as is. + if !apiequality.Semantic.DeepEqual(newStateful.Spec.VolumeClaimTemplates[0].Spec, storedStateful.Spec.VolumeClaimTemplates[0].Spec) { + // resize pvc + // 1.Get the data already stored internally + // 2.Get the desired data + // 3.Start querying the pvc list when you find data inconsistencies + // 3.1 Comparison using real pvc capacity and desired data + // 3.1.1 Update if you find inconsistencies + // 3.2 Writing successful updates to internal + // 4. Set to old VolumeClaimTemplates to update.Prevent update error reporting + // 5. Set to old annotations to update + annotations := storedStateful.Annotations + if annotations == nil { + annotations = map[string]string{ + "storageCapacity": "0", } - pvcs, err := generateK8sClient().CoreV1().PersistentVolumeClaims(storedStateful.Namespace).List(context.Background(), listOpt) - if err != nil { - return err - } - updateFailed := false - realUpdate := false - for _, pvc := range pvcs.Items { - realCapacity := pvc.Spec.Resources.Requests.Storage().Value() - if realCapacity != stateCapacity { - realUpdate = true - pvc.Spec.Resources.Requests = newStateful.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests - _, err = generateK8sClient().CoreV1().PersistentVolumeClaims(storedStateful.Namespace).Update(context.Background(), &pvc, metav1.UpdateOptions{}) - if err != nil { - if !updateFailed { - updateFailed = true + } + storedCapacity, _ := strconv.ParseInt(annotations["storageCapacity"], 0, 64) + if len(newStateful.Spec.VolumeClaimTemplates) != 0 { + stateCapacity := newStateful.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests.Storage().Value() + if storedCapacity != stateCapacity { + listOpt := metav1.ListOptions{ + LabelSelector: labels.FormatLabels( + map[string]string{ + "app": storedStateful.Name, + "app.kubernetes.io/component": "redis", + "app.kubernetes.io/name": storedStateful.Name, + }, + ), + } + pvcs, err := generateK8sClient().CoreV1().PersistentVolumeClaims(storedStateful.Namespace).List(context.Background(), listOpt) + if err != nil { + return err + } + updateFailed := false + realUpdate := false + for _, pvc := range pvcs.Items { + realCapacity := pvc.Spec.Resources.Requests.Storage().Value() + if realCapacity != stateCapacity { + realUpdate = true + pvc.Spec.Resources.Requests = newStateful.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests + _, err = generateK8sClient().CoreV1().PersistentVolumeClaims(storedStateful.Namespace).Update(context.Background(), &pvc, metav1.UpdateOptions{}) + if err != nil { + if !updateFailed { + updateFailed = true + } + logger.Error(fmt.Errorf("redis:%s resize pvc failed:%s", storedStateful.Name, err.Error()), "") } - logger.Error(fmt.Errorf("redis:%s resize pvc failed:%s", storedStateful.Name, err.Error()), "") } } - } - if !updateFailed && len(pvcs.Items) != 0 { - annotations["storageCapacity"] = fmt.Sprintf("%d", stateCapacity) - storedStateful.Annotations = annotations - if realUpdate { - logger.Info(fmt.Sprintf("redis:%s resize pvc from %d to %d", storedStateful.Name, storedCapacity, stateCapacity)) - } else { - logger.Info(fmt.Sprintf("redis:%s resize noting,just set annotations", storedStateful.Name)) + if !updateFailed && len(pvcs.Items) != 0 { + annotations["storageCapacity"] = fmt.Sprintf("%d", stateCapacity) + storedStateful.Annotations = annotations + if realUpdate { + logger.Info(fmt.Sprintf("redis:%s resize pvc from %d to %d", storedStateful.Name, storedCapacity, stateCapacity)) + } else { + logger.Info(fmt.Sprintf("redis:%s resize noting,just set annotations", storedStateful.Name)) + } } } } } + newStateful.Annotations["storageCapacity"] = storedStateful.Annotations["storageCapacity"] // set stored.volumeClaimTemplates - newStateful.Annotations = storedStateful.Annotations newStateful.Spec.VolumeClaimTemplates = storedStateful.Spec.VolumeClaimTemplates }