Define constant for eviction failure cause

This commit is contained in:
Jordan Liggitt 2021-04-14 09:12:21 -04:00
parent f07fc213b0
commit 10b07085f8
3 changed files with 11 additions and 6 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)