Skip to content

Commit

Permalink
When cr annotation update,sts annotations will not updated! (#398)
Browse files Browse the repository at this point in the history
* When cr annotation update,sts annotations will not updated!

Signed-off-by: jiuker <guozhi.li@daocloud.io>

* only the spec can be equal

Signed-off-by: jiuker <guozhi.li@daocloud.io>

* only the spec can be equal

Signed-off-by: jiuker <guozhi.li@daocloud.io>

* Update CHANGELOG.md

Signed-off-by: jiuker <guozhi.li@daocloud.io>

Signed-off-by: jiuker <guozhi.li@daocloud.io>
Co-authored-by: jiuker <guozhi.li@daocloud.io>
  • Loading branch information
jiuker and jiuker authored Dec 28, 2022
1 parent 3202785 commit 0e975e1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
110 changes: 56 additions & 54 deletions k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0e975e1

Please sign in to comment.