Skip to content

Commit

Permalink
Merge pull request nacos-group#396 from Accelerator96/update-sts
Browse files Browse the repository at this point in the history
修复当修改nacos cr部分参数不会自动生效的问题
  • Loading branch information
paderlol authored Apr 11, 2023
2 parents 17ca8e6 + 0ba4931 commit 1ee8ea2
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions operator/pkg/service/k8s/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ func (s *StatefulSetService) CreateOrUpdateStatefulSet(namespace string, statefu
// namespace is our spec(https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#concurrency-control-and-consistency),
// we will replace the current namespace state.

dAtA, _ := json.Marshal(storedStatefulSet.Spec.Template.Spec.Containers[0].Resources)
dAtB, _ := json.Marshal(statefulSet.Spec.Template.Spec.Containers[0].Resources)
if !bytes.Equal(dAtA, dAtB) ||
*statefulSet.Spec.Replicas != *storedStatefulSet.Spec.Replicas {
switch checkSts(storedStatefulSet, statefulSet) {
case Update:
statefulSet.ResourceVersion = storedStatefulSet.ResourceVersion
return s.UpdateStatefulSet(namespace, statefulSet)
//updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden
case Delete:
return s.DeleteStatefulSet(namespace, storedStatefulSet.Name)
}
return nil
}
Expand All @@ -141,3 +142,55 @@ func (s *StatefulSetService) DeleteStatefulSet(namespace, name string) error {
func (s *StatefulSetService) ListStatefulSets(namespace string) (*appsv1.StatefulSetList, error) {
return s.kubeClient.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
}

type operator int

const (
None operator = iota
Update
Delete
)

// check whether delete sts
func checkSts(old *appsv1.StatefulSet, new *appsv1.StatefulSet) operator {

rsA, _ := json.Marshal(old.Spec.Template.Spec.Containers[0].Resources)
rsB, _ := json.Marshal(new.Spec.Template.Spec.Containers[0].Resources)

envA, _ := json.Marshal(old.Spec.Template.Spec.Containers[0].Env)
envB, _ := json.Marshal(new.Spec.Template.Spec.Containers[0].Env)

if checkVolumeClaimTemplates(old, new) {
return Delete
}

if !bytes.Equal(rsA, rsB) || *old.Spec.Replicas != *new.Spec.Replicas || !bytes.Equal(envA, envB) {
return Update
}

return None
}

// check whether delete sts
func checkVolumeClaimTemplates(old *appsv1.StatefulSet, new *appsv1.StatefulSet) bool {
ov := old.Spec.VolumeClaimTemplates
nv := new.Spec.VolumeClaimTemplates
if len(ov) == 0 && len(nv) == 0 {
return false
}

if len(ov) != len(nv) {
return true
}

if len(ov) > 0 && len(nv) > 0 {
vmA, _ := json.Marshal(old.Spec.VolumeClaimTemplates[0].Spec.Resources)
vmB, _ := json.Marshal(new.Spec.VolumeClaimTemplates[0].Spec.Resources)

oscn := old.Spec.VolumeClaimTemplates[0].Spec.StorageClassName
nscn := new.Spec.VolumeClaimTemplates[0].Spec.StorageClassName
return !bytes.Equal(vmA, vmB) || !strings.EqualFold(*oscn, *nscn)
}

return false
}

0 comments on commit 1ee8ea2

Please sign in to comment.