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 stability test, add v1 to statefulset apiVersions #1054

Merged
merged 4 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
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"]
39 changes: 28 additions & 11 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.V(4).Infof("admit statefulsets [%s/%s]", namespace, name)

setResource := metav1.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}
if ar.Request.Resource != setResource {
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.Errorf(err.Error())
glog.Error(err)
return util.ARFail(err)
}

Expand All @@ -68,22 +70,21 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
}
}

raw := ar.Request.OldObject.Raw
set := apps.StatefulSet{}
if _, _, err := deserializer.Decode(raw, nil, &set); err != nil {
err := fmt.Errorf("statefulset %s/%s, decode request failed, err: %v", namespace, name, err)
glog.Errorf(err.Error())
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)
weekface marked this conversation as resolved.
Show resolved Hide resolved
}

l := label.Label(set.Labels)
l := label.Label(stsObjectMeta.Labels)

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

controllerRef := metav1.GetControllerOf(&set)
controllerRef := metav1.GetControllerOf(stsObjectMeta)
if controllerRef == nil || controllerRef.Kind != controller.ControllerKind.Kind {
// In this case, we can't tell if this statefulset is controlled by tidb-operator,
// so we don't block this statefulset upgrade, return directly.
Expand Down Expand Up @@ -116,11 +117,27 @@ func AdmitStatefulSets(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
return util.ARFail(err)
}

setPartition := *set.Spec.UpdateStrategy.RollingUpdate.Partition
setPartition := *(stsPartition)
if setPartition > 0 && setPartition <= int32(partition) {
glog.V(4).Infof("statefulset %s/%s has been protect by partition %s annotations", namespace, name, partitionStr)
return util.ARFail(errors.New("protect by partition annotation"))
}
glog.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
}

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 @@ -447,7 +447,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))
Copy link
Contributor

Choose a reason for hiding this comment

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

manifests/webhook.yaml has been modified, so can you omit this operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, v1.0.1 or prior only has one v1beta1. We must change them too in the stability test case.

glog.Info(cmd)

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