From 53f2b590d5f5084aa19c465bed7f6f68ae7dc487 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Sat, 25 Apr 2020 15:05:25 +0800 Subject: [PATCH 1/5] Update statefulset.go --- pkg/webhook/statefulset/statefulset.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/webhook/statefulset/statefulset.go b/pkg/webhook/statefulset/statefulset.go index 568d1bf2f2..d56c64ce91 100644 --- a/pkg/webhook/statefulset/statefulset.go +++ b/pkg/webhook/statefulset/statefulset.go @@ -110,12 +110,11 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission return util.ARFail(err) } - setPartition := *(stsPartition) - if setPartition > 0 && setPartition <= int32(partition) { + if stsPartition != nil && *stsPartition > 0 && *stsPartition <= int32(partition) { klog.Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr) return util.ARFail(errors.New("protect by partition annotation")) } - klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, setPartition, partition) + klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) return util.ARSuccess() } @@ -125,12 +124,18 @@ func getStsAttributes(data []byte, apiVersion string) (*metav1.ObjectMeta, *int3 if _, _, err := deserializer.Decode(data, nil, &set); err != nil { return nil, nil, err } - return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil + if set.Spec.UpdateStrategy.RollingUpdate != nil { + return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil + } + return &(set.ObjectMeta), nil, nil } set := apps.StatefulSet{} if _, _, err := deserializer.Decode(data, nil, &set); err != nil { return nil, nil, err } - return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil + if set.Spec.UpdateStrategy.RollingUpdate != nil { + return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil + } + return &(set.ObjectMeta), nil, nil } From 863200217357c77dd6fc6ddfd7701856dfc05410 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Sun, 26 Apr 2020 11:13:08 +0800 Subject: [PATCH 2/5] fix nil problem --- pkg/webhook/statefulset/statefulset.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/webhook/statefulset/statefulset.go b/pkg/webhook/statefulset/statefulset.go index d56c64ce91..b1ff0d9f42 100644 --- a/pkg/webhook/statefulset/statefulset.go +++ b/pkg/webhook/statefulset/statefulset.go @@ -110,12 +110,17 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission return util.ARFail(err) } - if stsPartition != nil && *stsPartition > 0 && *stsPartition <= int32(partition) { - klog.Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr) - return util.ARFail(errors.New("protect by partition annotation")) + if stsPartition != nil { + if *stsPartition > 0 && *stsPartition <= int32(partition) { + klog.Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr) + return util.ARFail(errors.New("protect by partition annotation")) + } else { + klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) + return util.ARSuccess() + } + } else { + return util.ARSuccess() } - klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) - return util.ARSuccess() } func getStsAttributes(data []byte, apiVersion string) (*metav1.ObjectMeta, *int32, error) { From 88ed71ebea290d6c0e70fe59946339b53fbfd713 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Sun, 26 Apr 2020 11:17:32 +0800 Subject: [PATCH 3/5] fix lint --- pkg/webhook/statefulset/statefulset.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/webhook/statefulset/statefulset.go b/pkg/webhook/statefulset/statefulset.go index b1ff0d9f42..e82973c672 100644 --- a/pkg/webhook/statefulset/statefulset.go +++ b/pkg/webhook/statefulset/statefulset.go @@ -118,9 +118,8 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) return util.ARSuccess() } - } else { - return util.ARSuccess() } + return util.ARSuccess() } func getStsAttributes(data []byte, apiVersion string) (*metav1.ObjectMeta, *int32, error) { From 847bcf54724edd6dcf7ffcaa20afee9ff6e17ccd Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Sun, 26 Apr 2020 11:39:23 +0800 Subject: [PATCH 4/5] fix lint --- pkg/webhook/statefulset/statefulset.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/webhook/statefulset/statefulset.go b/pkg/webhook/statefulset/statefulset.go index e82973c672..4afd583dde 100644 --- a/pkg/webhook/statefulset/statefulset.go +++ b/pkg/webhook/statefulset/statefulset.go @@ -114,10 +114,8 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission if *stsPartition > 0 && *stsPartition <= int32(partition) { klog.Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr) return util.ARFail(errors.New("protect by partition annotation")) - } else { - klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) - return util.ARSuccess() } + klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition) } return util.ARSuccess() } From cda1c4c6bbf95dc039c55006f1b912cc89632dbe Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Sun, 26 Apr 2020 15:05:30 +0800 Subject: [PATCH 5/5] remove redundent code --- pkg/webhook/statefulset/statefulset.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pkg/webhook/statefulset/statefulset.go b/pkg/webhook/statefulset/statefulset.go index 4afd583dde..87cbd9dd0c 100644 --- a/pkg/webhook/statefulset/statefulset.go +++ b/pkg/webhook/statefulset/statefulset.go @@ -63,7 +63,7 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission klog.Infof("admit %s [%s/%s]", setResource, namespace, name) - stsObjectMeta, stsPartition, err := getStsAttributes(ar.OldObject.Raw, apiVersion) + stsObjectMeta, stsPartition, err := getStsAttributes(ar.OldObject.Raw) if err != nil { err = fmt.Errorf("statefulset %s/%s, decode request failed, err: %v", namespace, name, err) klog.Error(err) @@ -120,18 +120,7 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission return util.ARSuccess() } -func getStsAttributes(data []byte, apiVersion string) (*metav1.ObjectMeta, *int32, error) { - if apiVersion == "v1" { - set := apps.StatefulSet{} - if _, _, err := deserializer.Decode(data, nil, &set); err != nil { - return nil, nil, err - } - if set.Spec.UpdateStrategy.RollingUpdate != nil { - return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil - } - return &(set.ObjectMeta), nil, nil - } - +func getStsAttributes(data []byte) (*metav1.ObjectMeta, *int32, error) { set := apps.StatefulSet{} if _, _, err := deserializer.Decode(data, nil, &set); err != nil { return nil, nil, err