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

Fix sts webhook error #2291

Merged
merged 9 commits into from
Apr 26, 2020
Merged
Changes from 8 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
28 changes: 12 additions & 16 deletions pkg/webhook/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -110,27 +110,23 @@ func (sc *StatefulSetAdmissionControl) AdmitStatefulSets(ar *admission.Admission
return util.ARFail(err)
}

setPartition := *(stsPartition)
if setPartition > 0 && setPartition <= 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"))
}
klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, *stsPartition, partition)
}
klog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, setPartition, partition)
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
}
return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, 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
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code seems identical no matter what apiVersion is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

}