From 66e722765a216db5288498b0a21bcededf8d9236 Mon Sep 17 00:00:00 2001 From: Morten Torkildsen Date: Mon, 8 Mar 2021 22:00:47 -0800 Subject: [PATCH] Update e2e tests to use the policy v1 api --- test/e2e/apps/disruption.go | 63 ++++++++++--------- .../autoscaling/cluster_size_autoscaling.go | 18 +++--- test/e2e/framework/service/jig.go | 16 ++--- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/test/e2e/apps/disruption.go b/test/e2e/apps/disruption.go index 235b485ecb4..a425ce46a60 100644 --- a/test/e2e/apps/disruption.go +++ b/test/e2e/apps/disruption.go @@ -27,6 +27,7 @@ import ( appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" + policyv1 "k8s.io/api/policy/v1" policyv1beta1 "k8s.io/api/policy/v1beta1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -103,7 +104,7 @@ var _ = SIGDescribe("DisruptionController", func() { // Since disruptionAllowed starts out 0, if we see it ever become positive, // that means the controller is working. err := wait.PollImmediate(framework.Poll, timeout, func() (bool, error) { - pdb, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), defaultName, metav1.GetOptions{}) + pdb, err := cs.PolicyV1().PodDisruptionBudgets(ns).Get(context.TODO(), defaultName, metav1.GetOptions{}) if err != nil { return false, err } @@ -121,17 +122,17 @@ var _ = SIGDescribe("DisruptionController", func() { createPodsOrDie(cs, ns, 1) waitForPodsOrDie(cs, ns, 1) pod, _ := locateRunningPod(cs, ns) - updatePDBOrDie(cs, ns, defaultName, func(old *policyv1beta1.PodDisruptionBudget) *policyv1beta1.PodDisruptionBudget { + updatePDBOrDie(cs, ns, defaultName, func(old *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget { old.Status.DisruptedPods = make(map[string]metav1.Time) old.Status.DisruptedPods[pod.Name] = metav1.NewTime(time.Now()) return old - }, cs.PolicyV1beta1().PodDisruptionBudgets(ns).UpdateStatus) + }, cs.PolicyV1().PodDisruptionBudgets(ns).UpdateStatus) // fetch again to make sure the update from API was effective updated := getPDBStatusOrDie(dc, ns, defaultName) framework.ExpectHaveKey(updated.Status.DisruptedPods, pod.Name, "Expecting the DisruptedPods have %s", pod.Name) ginkgo.By("Patching PodDisruptionBudget status") - patched := patchPDBOrDie(cs, dc, ns, defaultName, func(old *policyv1beta1.PodDisruptionBudget) (bytes []byte, err error) { + patched := patchPDBOrDie(cs, dc, ns, defaultName, func(old *policyv1.PodDisruptionBudget) (bytes []byte, err error) { oldBytes, err := json.Marshal(old) framework.ExpectNoError(err, "failed to marshal JSON for old data") old.Status.DisruptedPods = make(map[string]metav1.Time) @@ -290,11 +291,11 @@ var _ = SIGDescribe("DisruptionController", func() { gomega.Expect(err).Should(gomega.MatchError("Cannot evict pod as it would violate the pod's disruption budget.")) ginkgo.By("Updating the pdb to allow a pod to be evicted") - updatePDBOrDie(cs, ns, defaultName, func(pdb *policyv1beta1.PodDisruptionBudget) *policyv1beta1.PodDisruptionBudget { + updatePDBOrDie(cs, ns, defaultName, func(pdb *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget { newMinAvailable := intstr.FromInt(2) pdb.Spec.MinAvailable = &newMinAvailable return pdb - }, cs.PolicyV1beta1().PodDisruptionBudgets(ns).Update) + }, cs.PolicyV1().PodDisruptionBudgets(ns).Update) ginkgo.By("Trying to evict the same pod we tried earlier which should now be evictable") waitForPodsOrDie(cs, ns, 3) @@ -303,7 +304,7 @@ var _ = SIGDescribe("DisruptionController", func() { framework.ExpectNoError(err) // the eviction is now allowed ginkgo.By("Patching the pdb to disallow a pod to be evicted") - patchPDBOrDie(cs, dc, ns, defaultName, func(old *policyv1beta1.PodDisruptionBudget) (bytes []byte, err error) { + patchPDBOrDie(cs, dc, ns, defaultName, func(old *policyv1.PodDisruptionBudget) (bytes []byte, err error) { oldData, err := json.Marshal(old) framework.ExpectNoError(err, "failed to marshal JSON for old data") old.Spec.MinAvailable = nil @@ -338,45 +339,45 @@ var _ = SIGDescribe("DisruptionController", func() { }) func createPDBMinAvailableOrDie(cs kubernetes.Interface, ns string, name string, minAvailable intstr.IntOrString, labels map[string]string) { - pdb := policyv1beta1.PodDisruptionBudget{ + pdb := policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: ns, Labels: labels, }, - Spec: policyv1beta1.PodDisruptionBudgetSpec{ + Spec: policyv1.PodDisruptionBudgetSpec{ Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, MinAvailable: &minAvailable, }, } - _, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Create(context.TODO(), &pdb, metav1.CreateOptions{}) + _, err := cs.PolicyV1().PodDisruptionBudgets(ns).Create(context.TODO(), &pdb, metav1.CreateOptions{}) framework.ExpectNoError(err, "Waiting for the pdb to be created with minAvailable %d in namespace %s", minAvailable.IntVal, ns) waitForPdbToBeProcessed(cs, ns, name) } func createPDBMaxUnavailableOrDie(cs kubernetes.Interface, ns string, name string, maxUnavailable intstr.IntOrString) { - pdb := policyv1beta1.PodDisruptionBudget{ + pdb := policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: ns, }, - Spec: policyv1beta1.PodDisruptionBudgetSpec{ + Spec: policyv1.PodDisruptionBudgetSpec{ Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, MaxUnavailable: &maxUnavailable, }, } - _, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Create(context.TODO(), &pdb, metav1.CreateOptions{}) + _, err := cs.PolicyV1().PodDisruptionBudgets(ns).Create(context.TODO(), &pdb, metav1.CreateOptions{}) framework.ExpectNoError(err, "Waiting for the pdb to be created with maxUnavailable %d in namespace %s", maxUnavailable.IntVal, ns) waitForPdbToBeProcessed(cs, ns, name) } -type updateFunc func(pdb *policyv1beta1.PodDisruptionBudget) *policyv1beta1.PodDisruptionBudget -type updateRestAPI func(ctx context.Context, podDisruptionBudget *policyv1beta1.PodDisruptionBudget, opts metav1.UpdateOptions) (*policyv1beta1.PodDisruptionBudget, error) -type patchFunc func(pdb *policyv1beta1.PodDisruptionBudget) ([]byte, error) +type updateFunc func(pdb *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget +type updateRestAPI func(ctx context.Context, podDisruptionBudget *policyv1.PodDisruptionBudget, opts metav1.UpdateOptions) (*policyv1.PodDisruptionBudget, error) +type patchFunc func(pdb *policyv1.PodDisruptionBudget) ([]byte, error) -func updatePDBOrDie(cs kubernetes.Interface, ns string, name string, f updateFunc, api updateRestAPI) (updated *policyv1beta1.PodDisruptionBudget) { +func updatePDBOrDie(cs kubernetes.Interface, ns string, name string, f updateFunc, api updateRestAPI) (updated *policyv1.PodDisruptionBudget) { err := retry.RetryOnConflict(retry.DefaultRetry, func() error { - old, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) + old, err := cs.PolicyV1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { return err } @@ -392,12 +393,12 @@ func updatePDBOrDie(cs kubernetes.Interface, ns string, name string, f updateFun return updated } -func patchPDBOrDie(cs kubernetes.Interface, dc dynamic.Interface, ns string, name string, f patchFunc, subresources ...string) (updated *policyv1beta1.PodDisruptionBudget) { +func patchPDBOrDie(cs kubernetes.Interface, dc dynamic.Interface, ns string, name string, f patchFunc, subresources ...string) (updated *policyv1.PodDisruptionBudget) { err := retry.RetryOnConflict(retry.DefaultRetry, func() error { old := getPDBStatusOrDie(dc, ns, name) patchBytes, err := f(old) framework.ExpectNoError(err) - if updated, err = cs.PolicyV1beta1().PodDisruptionBudgets(ns).Patch(context.TODO(), old.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, subresources...); err != nil { + if updated, err = cs.PolicyV1().PodDisruptionBudgets(ns).Patch(context.TODO(), old.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, subresources...); err != nil { return err } framework.ExpectNoError(err) @@ -410,13 +411,13 @@ func patchPDBOrDie(cs kubernetes.Interface, dc dynamic.Interface, ns string, nam } func deletePDBOrDie(cs kubernetes.Interface, ns string, name string) { - err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}) + err := cs.PolicyV1().PodDisruptionBudgets(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}) framework.ExpectNoError(err, "Deleting pdb in namespace %s", ns) waitForPdbToBeDeleted(cs, ns, name) } func listPDBs(cs kubernetes.Interface, ns string, labelSelector string, count int, expectedPDBNames []string) { - pdbList, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) + pdbList, err := cs.PolicyV1().PodDisruptionBudgets(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) framework.ExpectNoError(err, "Listing PDB set in namespace %s", ns) framework.ExpectEqual(len(pdbList.Items), count, "Expecting %d PDBs returned in namespace %s", count, ns) @@ -429,7 +430,7 @@ func listPDBs(cs kubernetes.Interface, ns string, labelSelector string, count in func deletePDBCollection(cs kubernetes.Interface, ns string) { ginkgo.By("deleting a collection of PDBs") - err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{}) + err := cs.PolicyV1().PodDisruptionBudgets(ns).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{}) framework.ExpectNoError(err, "Deleting PDB set in namespace %s", ns) waitForPDBCollectionToBeDeleted(cs, ns) @@ -438,7 +439,7 @@ func deletePDBCollection(cs kubernetes.Interface, ns string) { func waitForPDBCollectionToBeDeleted(cs kubernetes.Interface, ns string) { ginkgo.By("Waiting for the PDB collection to be deleted") err := wait.PollImmediate(framework.Poll, schedulingTimeout, func() (bool, error) { - pdbList, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).List(context.TODO(), metav1.ListOptions{}) + pdbList, err := cs.PolicyV1().PodDisruptionBudgets(ns).List(context.TODO(), metav1.ListOptions{}) if err != nil { return false, err } @@ -564,7 +565,7 @@ func locateRunningPod(cs kubernetes.Interface, ns string) (pod *v1.Pod, err erro func waitForPdbToBeProcessed(cs kubernetes.Interface, ns string, name string) { ginkgo.By("Waiting for the pdb to be processed") err := wait.PollImmediate(framework.Poll, schedulingTimeout, func() (bool, error) { - pdb, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) + pdb, err := cs.PolicyV1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { return false, err } @@ -579,7 +580,7 @@ func waitForPdbToBeProcessed(cs kubernetes.Interface, ns string, name string) { func waitForPdbToBeDeleted(cs kubernetes.Interface, ns string, name string) { ginkgo.By("Waiting for the pdb to be deleted") err := wait.PollImmediate(framework.Poll, schedulingTimeout, func() (bool, error) { - _, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) + _, err := cs.PolicyV1().PodDisruptionBudgets(ns).Get(context.TODO(), name, metav1.GetOptions{}) if apierrors.IsNotFound(err) { return true, nil // done } @@ -594,7 +595,7 @@ func waitForPdbToBeDeleted(cs kubernetes.Interface, ns string, name string) { func waitForPdbToObserveHealthyPods(cs kubernetes.Interface, ns string, healthyCount int32) { ginkgo.By("Waiting for the pdb to observed all healthy pods") err := wait.PollImmediate(framework.Poll, wait.ForeverTestTimeout, func() (bool, error) { - pdb, err := cs.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), "foo", metav1.GetOptions{}) + pdb, err := cs.PolicyV1().PodDisruptionBudgets(ns).Get(context.TODO(), "foo", metav1.GetOptions{}) if err != nil { return false, err } @@ -606,8 +607,8 @@ func waitForPdbToObserveHealthyPods(cs kubernetes.Interface, ns string, healthyC framework.ExpectNoError(err, "Waiting for the pdb in namespace %s to observed %d healthy pods", ns, healthyCount) } -func getPDBStatusOrDie(dc dynamic.Interface, ns string, name string) *policyv1beta1.PodDisruptionBudget { - pdbStatusResource := policyv1beta1.SchemeGroupVersion.WithResource("poddisruptionbudgets") +func getPDBStatusOrDie(dc dynamic.Interface, ns string, name string) *policyv1.PodDisruptionBudget { + pdbStatusResource := policyv1.SchemeGroupVersion.WithResource("poddisruptionbudgets") unstruct, err := dc.Resource(pdbStatusResource).Namespace(ns).Get(context.TODO(), name, metav1.GetOptions{}, "status") framework.ExpectNoError(err) pdb, err := unstructuredToPDB(unstruct) @@ -615,12 +616,12 @@ func getPDBStatusOrDie(dc dynamic.Interface, ns string, name string) *policyv1be return pdb } -func unstructuredToPDB(obj *unstructured.Unstructured) (*policyv1beta1.PodDisruptionBudget, error) { +func unstructuredToPDB(obj *unstructured.Unstructured) (*policyv1.PodDisruptionBudget, error) { json, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj) if err != nil { return nil, err } - pdb := &policyv1beta1.PodDisruptionBudget{} + pdb := &policyv1.PodDisruptionBudget{} err = runtime.DecodeInto(clientscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), json, pdb) pdb.Kind = "" pdb.APIVersion = "" diff --git a/test/e2e/autoscaling/cluster_size_autoscaling.go b/test/e2e/autoscaling/cluster_size_autoscaling.go index 6aa1655f424..1d17c2bebb4 100644 --- a/test/e2e/autoscaling/cluster_size_autoscaling.go +++ b/test/e2e/autoscaling/cluster_size_autoscaling.go @@ -30,7 +30,7 @@ import ( "time" v1 "k8s.io/api/core/v1" - policyv1beta1 "k8s.io/api/policy/v1beta1" + policyv1 "k8s.io/api/policy/v1" schedulingv1 "k8s.io/api/scheduling/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -1028,20 +1028,20 @@ func runDrainTest(f *framework.Framework, migSizes map[string]int, namespace str ginkgo.By("Create a PodDisruptionBudget") minAvailable := intstr.FromInt(numPods - pdbSize) - pdb := &policyv1beta1.PodDisruptionBudget{ + pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: "test_pdb", Namespace: namespace, }, - Spec: policyv1beta1.PodDisruptionBudgetSpec{ + Spec: policyv1.PodDisruptionBudgetSpec{ Selector: &metav1.LabelSelector{MatchLabels: labelMap}, MinAvailable: &minAvailable, }, } - _, err = f.ClientSet.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{}) + _, err = f.ClientSet.PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{}) defer func() { - f.ClientSet.PolicyV1beta1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdb.Name, metav1.DeleteOptions{}) + f.ClientSet.PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdb.Name, metav1.DeleteOptions{}) }() framework.ExpectNoError(err) @@ -1881,7 +1881,7 @@ func addKubeSystemPdbs(f *framework.Framework) (func(), error) { var finalErr error for _, newPdbName := range newPdbs { ginkgo.By(fmt.Sprintf("Delete PodDisruptionBudget %v", newPdbName)) - err := f.ClientSet.PolicyV1beta1().PodDisruptionBudgets("kube-system").Delete(context.TODO(), newPdbName, metav1.DeleteOptions{}) + err := f.ClientSet.PolicyV1().PodDisruptionBudgets("kube-system").Delete(context.TODO(), newPdbName, metav1.DeleteOptions{}) if err != nil { // log error, but attempt to remove other pdbs klog.Errorf("Failed to delete PodDisruptionBudget %v, err: %v", newPdbName, err) @@ -1909,17 +1909,17 @@ func addKubeSystemPdbs(f *framework.Framework) (func(), error) { labelMap := map[string]string{"k8s-app": pdbData.label} pdbName := fmt.Sprintf("test-pdb-for-%v", pdbData.label) minAvailable := intstr.FromInt(pdbData.minAvailable) - pdb := &policyv1beta1.PodDisruptionBudget{ + pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: pdbName, Namespace: "kube-system", }, - Spec: policyv1beta1.PodDisruptionBudgetSpec{ + Spec: policyv1.PodDisruptionBudgetSpec{ Selector: &metav1.LabelSelector{MatchLabels: labelMap}, MinAvailable: &minAvailable, }, } - _, err := f.ClientSet.PolicyV1beta1().PodDisruptionBudgets("kube-system").Create(context.TODO(), pdb, metav1.CreateOptions{}) + _, err := f.ClientSet.PolicyV1().PodDisruptionBudgets("kube-system").Create(context.TODO(), pdb, metav1.CreateOptions{}) newPdbs = append(newPdbs, pdbName) if err != nil { diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index 681025ff4da..7b5e9ebb492 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -28,7 +28,7 @@ import ( "github.com/onsi/ginkgo" v1 "k8s.io/api/core/v1" discoveryv1 "k8s.io/api/discovery/v1" - policyv1beta1 "k8s.io/api/policy/v1beta1" + policyv1 "k8s.io/api/policy/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -679,9 +679,9 @@ func (j *TestJig) AddRCAntiAffinity(rc *v1.ReplicationController) { } // CreatePDB returns a PodDisruptionBudget for the given ReplicationController, or returns an error if a PodDisruptionBudget isn't ready -func (j *TestJig) CreatePDB(rc *v1.ReplicationController) (*policyv1beta1.PodDisruptionBudget, error) { +func (j *TestJig) CreatePDB(rc *v1.ReplicationController) (*policyv1.PodDisruptionBudget, error) { pdb := j.newPDBTemplate(rc) - newPdb, err := j.Client.PolicyV1beta1().PodDisruptionBudgets(j.Namespace).Create(context.TODO(), pdb, metav1.CreateOptions{}) + newPdb, err := j.Client.PolicyV1().PodDisruptionBudgets(j.Namespace).Create(context.TODO(), pdb, metav1.CreateOptions{}) if err != nil { return nil, fmt.Errorf("failed to create PDB %q %v", pdb.Name, err) } @@ -692,19 +692,19 @@ func (j *TestJig) CreatePDB(rc *v1.ReplicationController) (*policyv1beta1.PodDis return newPdb, nil } -// newPDBTemplate returns the default policyv1beta1.PodDisruptionBudget object for +// newPDBTemplate returns the default policyv1.PodDisruptionBudget object for // this j, but does not actually create the PDB. The default PDB specifies a // MinAvailable of N-1 and matches the pods created by the RC. -func (j *TestJig) newPDBTemplate(rc *v1.ReplicationController) *policyv1beta1.PodDisruptionBudget { +func (j *TestJig) newPDBTemplate(rc *v1.ReplicationController) *policyv1.PodDisruptionBudget { minAvailable := intstr.FromInt(int(*rc.Spec.Replicas) - 1) - pdb := &policyv1beta1.PodDisruptionBudget{ + pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Namespace: j.Namespace, Name: j.Name, Labels: j.Labels, }, - Spec: policyv1beta1.PodDisruptionBudgetSpec{ + Spec: policyv1.PodDisruptionBudgetSpec{ MinAvailable: &minAvailable, Selector: &metav1.LabelSelector{MatchLabels: j.Labels}, }, @@ -762,7 +762,7 @@ func (j *TestJig) Scale(replicas int) error { func (j *TestJig) waitForPdbReady() error { timeout := 2 * time.Minute for start := time.Now(); time.Since(start) < timeout; time.Sleep(2 * time.Second) { - pdb, err := j.Client.PolicyV1beta1().PodDisruptionBudgets(j.Namespace).Get(context.TODO(), j.Name, metav1.GetOptions{}) + pdb, err := j.Client.PolicyV1().PodDisruptionBudgets(j.Namespace).Get(context.TODO(), j.Name, metav1.GetOptions{}) if err != nil { return err }