From 10b07085f8cc5a5a6dd6d6e6a48324b89fcf8770 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Wed, 14 Apr 2021 09:12:21 -0400 Subject: [PATCH] Define constant for eviction failure cause --- pkg/registry/core/pod/storage/eviction.go | 4 ++-- staging/src/k8s.io/api/policy/v1/types.go | 3 +++ test/e2e/apps/disruption.go | 10 ++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/registry/core/pod/storage/eviction.go b/pkg/registry/core/pod/storage/eviction.go index ade8ffc3c96..d1ed12fb59b 100644 --- a/pkg/registry/core/pod/storage/eviction.go +++ b/pkg/registry/core/pod/storage/eviction.go @@ -323,7 +323,7 @@ func createTooManyRequestsError(name string) error { // even without that, we can give a suggestion (even if small) that // prevents well-behaved clients from hammering us. err := errors.NewTooManyRequests("Cannot evict pod as it would violate the pod's disruption budget.", 10) - err.ErrStatus.Details.Causes = append(err.ErrStatus.Details.Causes, metav1.StatusCause{Type: "DisruptionBudget", Message: fmt.Sprintf("The disruption budget %s is still being processed by the server.", name)}) + err.ErrStatus.Details.Causes = append(err.ErrStatus.Details.Causes, metav1.StatusCause{Type: policyv1.DisruptionBudgetCause, Message: fmt.Sprintf("The disruption budget %s is still being processed by the server.", name)}) return err } @@ -341,7 +341,7 @@ func (r *EvictionREST) checkAndDecrement(namespace string, podName string, pdb p } if pdb.Status.DisruptionsAllowed == 0 { err := errors.NewTooManyRequests("Cannot evict pod as it would violate the pod's disruption budget.", 0) - err.ErrStatus.Details.Causes = append(err.ErrStatus.Details.Causes, metav1.StatusCause{Type: "DisruptionBudget", Message: fmt.Sprintf("The disruption budget %s needs %d healthy pods and has %d currently", pdb.Name, pdb.Status.DesiredHealthy, pdb.Status.CurrentHealthy)}) + err.ErrStatus.Details.Causes = append(err.ErrStatus.Details.Causes, metav1.StatusCause{Type: policyv1.DisruptionBudgetCause, Message: fmt.Sprintf("The disruption budget %s needs %d healthy pods and has %d currently", pdb.Name, pdb.Status.DesiredHealthy, pdb.Status.CurrentHealthy)}) return err } diff --git a/staging/src/k8s.io/api/policy/v1/types.go b/staging/src/k8s.io/api/policy/v1/types.go index f621e784f76..4a03696f000 100644 --- a/staging/src/k8s.io/api/policy/v1/types.go +++ b/staging/src/k8s.io/api/policy/v1/types.go @@ -21,6 +21,9 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) +// DisruptionBudgetCause is the status cause returned for eviction failures caused by PodDisruptionBudget violations. +const DisruptionBudgetCause metav1.CauseType = "DisruptionBudget" + // PodDisruptionBudgetSpec is a description of a PodDisruptionBudget. type PodDisruptionBudgetSpec struct { // An eviction is allowed if at least "minAvailable" pods selected by diff --git a/test/e2e/apps/disruption.go b/test/e2e/apps/disruption.go index 9d10a002ad6..5b91d29d6c0 100644 --- a/test/e2e/apps/disruption.go +++ b/test/e2e/apps/disruption.go @@ -23,7 +23,6 @@ import ( jsonpatch "github.com/evanphx/json-patch" "github.com/onsi/ginkgo" - "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" @@ -293,7 +292,8 @@ var _ = SIGDescribe("DisruptionController", func() { if c.shouldDeny { err = cs.CoreV1().Pods(ns).EvictV1(context.TODO(), e) - gomega.Expect(err).Should(gomega.MatchError("Cannot evict pod as it would violate the pod's disruption budget.")) + framework.ExpectError(err, "pod eviction should fail") + framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause") } else { // Only wait for running pods in the "allow" case // because one of shouldDeny cases relies on the @@ -331,7 +331,8 @@ var _ = SIGDescribe("DisruptionController", func() { }, } err = cs.CoreV1().Pods(ns).EvictV1(context.TODO(), e) - gomega.Expect(err).Should(gomega.MatchError("Cannot evict pod as it would violate the pod's disruption budget.")) + framework.ExpectError(err, "pod eviction should fail") + framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause") ginkgo.By("Updating the pdb to allow a pod to be evicted") updatePDBOrDie(cs, ns, defaultName, func(pdb *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget { @@ -368,7 +369,8 @@ var _ = SIGDescribe("DisruptionController", func() { }, } err = cs.CoreV1().Pods(ns).EvictV1(context.TODO(), e) - gomega.Expect(err).Should(gomega.MatchError("Cannot evict pod as it would violate the pod's disruption budget.")) + framework.ExpectError(err, "pod eviction should fail") + framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause") ginkgo.By("Deleting the pdb to allow a pod to be evicted") deletePDBOrDie(cs, ns, defaultName)