Skip to content

Commit 1a8614a

Browse files
committed
fix stability test, add v1 to statefulset apiVersions
1 parent 39c63b9 commit 1a8614a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

manifests/webhook.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ webhooks:
105105
rules:
106106
- operations: [ "UPDATE" ]
107107
apiGroups: [ "apps", "" ]
108-
apiVersions: ["v1beta1"]
108+
apiVersions: ["v1beta1", "v1"]
109109
resources: ["statefulsets"]

pkg/webhook/statefulset/statefulset.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
"github.com/pingcap/tidb-operator/pkg/label"
2525
"github.com/pingcap/tidb-operator/pkg/webhook/util"
2626
"k8s.io/api/admission/v1beta1"
27-
apps "k8s.io/api/apps/v1beta1"
27+
apps "k8s.io/api/apps/v1"
28+
appsv1beta1 "k8s.io/api/apps/v1beta1"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
"k8s.io/client-go/rest"
@@ -45,8 +46,9 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
4546
namespace := ar.Request.Namespace
4647
glog.V(4).Infof("admit statefulsets [%s/%s]", namespace, name)
4748

48-
setResource := metav1.GroupVersionResource{Group: "apps", Version: "v1beta1", Resource: "statefulsets"}
49-
if ar.Request.Resource != setResource {
49+
apiVersion := ar.Request.Resource.Version
50+
setResource := metav1.GroupVersionResource{Group: "apps", Version: apiVersion, Resource: "statefulsets"}
51+
if ar.Request.Resource.Group != "apps" || ar.Request.Resource.Resource != "statefulsets" {
5052
err := fmt.Errorf("expect resource to be %s instead of %s", setResource, ar.Request.Resource)
5153
glog.Errorf("%v", err)
5254
return util.ARFail(err)
@@ -66,21 +68,20 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
6668
}
6769
}
6870

69-
raw := ar.Request.OldObject.Raw
70-
set := apps.StatefulSet{}
71-
if _, _, err := deserializer.Decode(raw, nil, &set); err != nil {
71+
stsObjectMeta, stsPartition, err := getStsAttributes(ar.Request.OldObject.Raw, apiVersion)
72+
if err != nil {
7273
glog.Errorf("statefulset %s/%s, decode request failed, err: %v", namespace, name, err)
7374
return util.ARFail(err)
7475
}
7576

76-
l := label.Label(set.Labels)
77+
l := label.Label(stsObjectMeta.Labels)
7778

7879
if !(l.IsTiDB() || l.IsTiKV()) {
7980
// If it is not statefulset of tikv and tidb, return quickly.
8081
return util.ARSuccess()
8182
}
8283

83-
controllerRef := metav1.GetControllerOf(&set)
84+
controllerRef := metav1.GetControllerOf(stsObjectMeta)
8485
if controllerRef == nil || controllerRef.Kind != controller.ControllerKind.Kind {
8586
// In this case, we can't tell if this statefulset is controlled by tidb-operator,
8687
// so we don't block this statefulset upgrade, return directly.
@@ -111,11 +112,27 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
111112
return util.ARFail(err)
112113
}
113114

114-
setPartition := *set.Spec.UpdateStrategy.RollingUpdate.Partition
115+
setPartition := *(stsPartition)
115116
if setPartition > 0 && setPartition <= int32(partition) {
116117
glog.V(4).Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr)
117118
return util.ARFail(errors.New("protect by partition annotation"))
118119
}
119120
glog.Infof("admit statefulset %s/%s update partition to %d, protect partition is %d", namespace, name, setPartition, partition)
120121
return util.ARSuccess()
121122
}
123+
124+
func getStsAttributes(data []byte, apiVersion string) (*metav1.ObjectMeta, *int32, error) {
125+
if apiVersion == "v1" {
126+
set := apps.StatefulSet{}
127+
if _, _, err := deserializer.Decode(data, nil, &set); err != nil {
128+
return nil, nil, err
129+
}
130+
return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil
131+
}
132+
133+
set := appsv1beta1.StatefulSet{}
134+
if _, _, err := deserializer.Decode(data, nil, &set); err != nil {
135+
return nil, nil, err
136+
}
137+
return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil
138+
}

tests/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func (oa *operatorActions) DeployOperator(info *OperatorConfig) error {
448448
}
449449

450450
// deploy statefulset webhook and configuration to hijack update statefulset opeartion
451-
cmd = fmt.Sprintf("kubectl apply -f %s/webhook.yaml", oa.manifestPath(info.Tag))
451+
cmd = fmt.Sprintf(`sed 's/apiVersions: \["v1beta1"\]/apiVersions: ["v1", "v1beta1"]/' %s/webhook.yaml | kubectl apply -f -`, oa.manifestPath(info.Tag))
452452
glog.Info(cmd)
453453

454454
res, err = exec.Command("/bin/sh", "-c", cmd).CombinedOutput()

0 commit comments

Comments
 (0)