Skip to content

Commit

Permalink
Fix pd deleting admission webhook bug (#1568) (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Jan 16, 2020
1 parent 6cbf92e commit 82a41cd
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 36 deletions.
6 changes: 5 additions & 1 deletion pkg/webhook/pod/pd_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
operatorUtils "github.com/pingcap/tidb-operator/pkg/util"
"github.com/pingcap/tidb-operator/pkg/webhook/util"
admission "k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
Expand Down Expand Up @@ -121,9 +122,12 @@ func (pc *PodAdmissionControl) admitDeleteNonPDMemberPod(payload *admitPayload)
// it would be existed an pd-3 instance with its deferDeleting label Annotations PVC.
// And the pvc can be deleted during upgrading if we use create pod webhook in future.
if !isInOrdinal {
pvcName := operatorUtils.OrdinalPVCName(v1alpha1.TiKVMemberType, payload.ownerStatefulSet.Name, ordinal)
pvcName := operatorUtils.OrdinalPVCName(v1alpha1.PDMemberType, payload.ownerStatefulSet.Name, ordinal)
pvc, err := pc.kubeCli.CoreV1().PersistentVolumeClaims(namespace).Get(pvcName, meta.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return util.ARSuccess()
}
return util.ARFail(err)
}
err = addDeferDeletingToPVC(pvc, pc.kubeCli, payload.tc)
Expand Down
56 changes: 47 additions & 9 deletions pkg/webhook/pod/pd_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package pod

import (
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -29,6 +32,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"
k8sTesting "k8s.io/client-go/testing"
)

var (
Expand All @@ -49,6 +53,7 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading bool
isLeader bool
UpdatePVCErr bool
PVCNotFound bool
expectFn func(g *GomegaWithT, response *admission.AdmissionResponse)
}

Expand All @@ -65,7 +70,20 @@ func TestPDDeleterDelete(t *testing.T) {
tc := newTidbClusterForPodAdmissionControl()
kubeCli := kubefake.NewSimpleClientset()

podAdmissionControl := newPodAdmissionControl()
if test.UpdatePVCErr {

if test.PVCNotFound {
kubeCli.PrependReactor("get", "persistentvolumeclaims", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, errors.NewNotFound(action.GetResource().GroupResource(), "name")
})
} else {
kubeCli.PrependReactor("get", "persistentvolumeclaims", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("some errors")
})
}
}

podAdmissionControl := newPodAdmissionControl(kubeCli)
pdControl := pdapi.NewFakePDControl(kubeCli)
fakePDClient := controller.NewFakePDClient(pdControl, tc)

Expand Down Expand Up @@ -159,8 +177,9 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: true,
isLeader: false,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, false)
g.Expect(response.Allowed).Should(Equal(false))
},
},
{
Expand All @@ -171,8 +190,9 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: true,
isLeader: false,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, true)
g.Expect(response.Allowed).Should(Equal(true))
},
},
{
Expand All @@ -183,8 +203,9 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: true,
isLeader: false,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, false)
g.Expect(response.Allowed).Should(Equal(false))
},
},
{
Expand All @@ -195,8 +216,9 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: true,
isLeader: true,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, false)
g.Expect(response.Allowed).Should(Equal(false))
},
},
{
Expand All @@ -206,8 +228,10 @@ func TestPDDeleterDelete(t *testing.T) {
isOutOfOrdinal: true,
isStatefulSetUpgrading: false,
isLeader: false,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, false)
g.Expect(response.Allowed).Should(Equal(false))
},
},
{
Expand All @@ -218,8 +242,9 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: false,
isLeader: false,
UpdatePVCErr: false,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, true)
g.Expect(response.Allowed).Should(Equal(true))
},
},
{
Expand All @@ -230,9 +255,22 @@ func TestPDDeleterDelete(t *testing.T) {
isStatefulSetUpgrading: false,
isLeader: false,
UpdatePVCErr: true,
PVCNotFound: false,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed).Should(Equal(false))
},
},
{
name: "final scale in,update pvc error,pvc not found",
isMember: false,
isDeferDeleting: true,
isOutOfOrdinal: true,
isStatefulSetUpgrading: false,
isLeader: false,
UpdatePVCErr: true,
PVCNotFound: true,
expectFn: func(g *GomegaWithT, response *admission.AdmissionResponse) {
g.Expect(response.Allowed, false)
g.Expect(response.Result.Message, "update pvc error")
g.Expect(response.Allowed).Should(Equal(true))
},
},
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/webhook/pod/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package pod
import (
"testing"

"k8s.io/client-go/kubernetes"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
operatorClifake "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -53,7 +55,8 @@ func TestAdmitPod(t *testing.T) {
testFn := func(test *testcase) {
t.Log(test.name)

podAdmissionControl := newPodAdmissionControl()
kubeCli := kubefake.NewSimpleClientset()
podAdmissionControl := newPodAdmissionControl(kubeCli)
ar := newAdmissionRequest()
pod := newNormalPod()
if test.isDelete {
Expand Down Expand Up @@ -124,8 +127,7 @@ func newAdmissionRequest() *admission.AdmissionRequest {
return &request
}

func newPodAdmissionControl() *PodAdmissionControl {
kubeCli := kubefake.NewSimpleClientset()
func newPodAdmissionControl(kubeCli kubernetes.Interface) *PodAdmissionControl {
operatorCli := operatorClifake.NewSimpleClientset()
pdControl := pdapi.NewFakePDControl(kubeCli)
return &PodAdmissionControl{
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/pod/tikv_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package pod

import (
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -131,6 +132,9 @@ func (pc *PodAdmissionControl) admitDeleteUselessTiKVPod(payload *admitPayload)
pvcName := operatorUtils.OrdinalPVCName(v1alpha1.TiKVMemberType, payload.ownerStatefulSet.Name, ordinal)
pvc, err := pc.kubeCli.CoreV1().PersistentVolumeClaims(namespace).Get(pvcName, meta.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return util.ARSuccess()
}
return util.ARFail(err)
}
err = addDeferDeletingToPVC(pvc, pc.kubeCli, payload.tc)
Expand Down
Loading

0 comments on commit 82a41cd

Please sign in to comment.