Skip to content

Commit

Permalink
Automated cherry pick of #1054: fix stability test, add v1 to state…
Browse files Browse the repository at this point in the history
…fulset apiVersions (#1056)

* fix stability test, add `v1` to statefulset apiVersions
  • Loading branch information
weekface authored Oct 30, 2019
1 parent 987c229 commit 34e4916
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion manifests/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ webhooks:
rules:
- operations: [ "UPDATE" ]
apiGroups: [ "apps", "" ]
apiVersions: ["v1"]
apiVersions: ["v1beta1", "v1"]
resources: ["statefulsets"]
42 changes: 30 additions & 12 deletions pkg/webhook/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb-operator/pkg/webhook/util"
"k8s.io/api/admission/v1beta1"
apps "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand All @@ -45,10 +46,11 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
namespace := ar.Request.Namespace
glog.Infof("admit statefulsets [%s/%s]", name, namespace)

setResource := metav1.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}
if ar.Request.Resource != setResource {
err := fmt.Errorf("expect resource to be %s", setResource)
glog.Errorf("%v", err)
apiVersion := ar.Request.Resource.Version
setResource := metav1.GroupVersionResource{Group: "apps", Version: apiVersion, Resource: "statefulsets"}
if ar.Request.Resource.Group != "apps" || ar.Request.Resource.Resource != "statefulsets" {
err := fmt.Errorf("expect resource to be %s instead of %s", setResource, ar.Request.Resource)
glog.Error(err)
return util.ARFail(err)
}

Expand All @@ -66,20 +68,20 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
}
}

raw := ar.Request.OldObject.Raw
set := apps.StatefulSet{}
if _, _, err := deserializer.Decode(raw, nil, &set); err != nil {
glog.Errorf("deseriralizer fail to decode request %v", err)
stsObjectMeta, stsPartition, err := getStsAttributes(ar.Request.OldObject.Raw, apiVersion)
if err != nil {
err = fmt.Errorf("statefulset %s/%s, decode request failed, err: %v", namespace, name, err)
glog.Error(err)
return util.ARFail(err)
}

tc, err := versionCli.PingcapV1alpha1().TidbClusters(namespace).Get(set.Labels[label.InstanceLabelKey], metav1.GetOptions{})
tc, err := versionCli.PingcapV1alpha1().TidbClusters(namespace).Get(stsObjectMeta.Labels[label.InstanceLabelKey], metav1.GetOptions{})
if err != nil {
glog.Errorf("fail to fetch tidbcluster info namespace %s clustername(instance) %s err %v", namespace, set.Labels[label.InstanceLabelKey], err)
glog.Errorf("fail to fetch tidbcluster info namespace %s clustername(instance) %s err %v", namespace, stsObjectMeta.Labels[label.InstanceLabelKey], err)
return util.ARFail(err)
}

if set.Labels[label.ComponentLabelKey] == label.TiDBLabelVal {
if stsObjectMeta.Labels[label.ComponentLabelKey] == label.TiDBLabelVal {
protect, ok := tc.Annotations[label.AnnTiDBPartition]

if ok {
Expand All @@ -89,7 +91,7 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
return util.ARFail(err)
}

if (*set.Spec.UpdateStrategy.RollingUpdate.Partition) <= int32(partition) && tc.Status.TiDB.Phase == v1alpha1.UpgradePhase {
if *stsPartition <= int32(partition) && tc.Status.TiDB.Phase == v1alpha1.UpgradePhase {
glog.Infof("set has been protect by annotations name %s namespace %s", name, namespace)
return util.ARFail(errors.New("protect by annotation"))
}
Expand All @@ -98,3 +100,19 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

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
}

set := appsv1beta1.StatefulSet{}
if _, _, err := deserializer.Decode(data, nil, &set); err != nil {
return nil, nil, err
}
return &(set.ObjectMeta), set.Spec.UpdateStrategy.RollingUpdate.Partition, nil
}
2 changes: 1 addition & 1 deletion tests/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (oa *operatorActions) DeployOperator(info *OperatorConfig) error {
}

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

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

0 comments on commit 34e4916

Please sign in to comment.