From b7fab91b6f33c11f42db2b400339827dc800b08d Mon Sep 17 00:00:00 2001 From: jiuker Date: Fri, 9 Dec 2022 22:27:45 +0800 Subject: [PATCH 1/4] When cr annotation update,sts annotations will not updated! Signed-off-by: jiuker --- k8sutils/statefulset.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index 2ff34b52f..ba87ec564 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -3,6 +3,7 @@ package k8sutils import ( "context" "fmt" + "github.com/google/go-cmp/cmp" "path" redisv1beta1 "redis-operator/api/v1beta1" "sort" @@ -100,6 +101,8 @@ 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. + di := cmp.Diff(newStateful.Spec.VolumeClaimTemplates, storedStateful.Spec.VolumeClaimTemplates) + fmt.Println(di) if !apiequality.Semantic.DeepEqual(newStateful.Spec.VolumeClaimTemplates, storedStateful.Spec.VolumeClaimTemplates) { // resize pvc // 1.Get the data already stored internally @@ -161,7 +164,7 @@ func patchStatefulSet(storedStateful *appsv1.StatefulSet, newStateful *appsv1.St } } // set stored.volumeClaimTemplates - newStateful.Annotations = storedStateful.Annotations + newStateful.Annotations["storageCapacity"] = storedStateful.Annotations["storageCapacity"] newStateful.Spec.VolumeClaimTemplates = storedStateful.Spec.VolumeClaimTemplates } From 38efd25d78599d65959b421e5d07cc46c5f2e524 Mon Sep 17 00:00:00 2001 From: jiuker Date: Fri, 9 Dec 2022 22:37:51 +0800 Subject: [PATCH 2/4] only the spec can be equal Signed-off-by: jiuker --- k8sutils/statefulset.go | 111 ++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index ba87ec564..beea3b12e 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -3,7 +3,6 @@ package k8sutils import ( "context" "fmt" - "github.com/google/go-cmp/cmp" "path" redisv1beta1 "redis-operator/api/v1beta1" "sort" @@ -100,65 +99,65 @@ 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. - di := cmp.Diff(newStateful.Spec.VolumeClaimTemplates, storedStateful.Spec.VolumeClaimTemplates) - fmt.Println(di) - 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)) + } } } } From 089477db03cd3d93c9c705d1630d2db21671a77c Mon Sep 17 00:00:00 2001 From: jiuker Date: Fri, 9 Dec 2022 22:46:01 +0800 Subject: [PATCH 3/4] only the spec can be equal Signed-off-by: jiuker --- k8sutils/statefulset.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index beea3b12e..7635ace88 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -162,8 +162,8 @@ func patchStatefulSet(storedStateful *appsv1.StatefulSet, newStateful *appsv1.St } } } - // set stored.volumeClaimTemplates newStateful.Annotations["storageCapacity"] = storedStateful.Annotations["storageCapacity"] + // set stored.volumeClaimTemplates newStateful.Spec.VolumeClaimTemplates = storedStateful.Spec.VolumeClaimTemplates } From 4603d9d8a7f19ddc1a6acad9e3c222f56f96fc0e Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Fri, 9 Dec 2022 15:14:44 +0000 Subject: [PATCH 4/4] Update CHANGELOG.md Signed-off-by: jiuker --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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