Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When cr annotation update,sts annotations will not updated! #398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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