Skip to content

Commit

Permalink
add percentage PDB validation and rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sircthulhu committed Apr 2, 2024
1 parent a3107c9 commit 248b31f
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 109 deletions.
94 changes: 64 additions & 30 deletions api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"math"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -130,51 +133,82 @@ func (r *EtcdCluster) validatePdb() (admission.Warnings, field.ErrorList) {
pdb := r.Spec.PodDisruptionBudget
var warnings admission.Warnings
var allErrors field.ErrorList

if pdb.Spec.MinAvailable != nil && pdb.Spec.MaxUnavailable != nil {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
pdb.Spec.MinAvailable.IntValue(),
"minAvailable is mutually exclusive with maxUnavailable"),
)
} else {
minQuorumSize := r.CalculateQuorumSize()
if pdb.Spec.MinAvailable != nil {
if pdb.Spec.MinAvailable.IntValue() < 0 {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
pdb.Spec.MinAvailable.IntValue(),
"value cannot be less than zero"),
)
}
if pdb.Spec.MinAvailable.IntValue() > int(*r.Spec.Replicas) {
return nil, allErrors
}

minQuorumSize := r.CalculateQuorumSize()
if pdb.Spec.MinAvailable != nil {
minAvailable := pdb.Spec.MinAvailable.IntValue()
if pdb.Spec.MinAvailable.IntVal == 0 && pdb.Spec.MinAvailable.IntValue() == 0 {
var percentage int
_, err := fmt.Sscanf(pdb.Spec.MinAvailable.StrVal, "%d%%", &percentage)
if err != nil {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
pdb.Spec.MinAvailable.IntValue(),
"value cannot be larger than number of replicas"),
pdb.Spec.MinAvailable.StrVal,
"invalid percentage value"),
)
}
if pdb.Spec.MinAvailable.IntValue() < minQuorumSize {
warnings = append(warnings, "current number of spec.podDisruptionBudget.minAvailable can lead to loss of quorum")
} else {
minAvailable = int(math.Ceil(float64(*r.Spec.Replicas) * (float64(percentage) / 100)))
}
}
if pdb.Spec.MaxUnavailable != nil {
if pdb.Spec.MaxUnavailable.IntValue() < 0 {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
pdb.Spec.MaxUnavailable.IntValue(),
"value cannot be less than zero"),
)
}
if pdb.Spec.MaxUnavailable.IntValue() > int(*r.Spec.Replicas) {

if minAvailable < 0 {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
pdb.Spec.MinAvailable.IntValue(),
"value cannot be less than zero"),
)
}
if minAvailable > int(*r.Spec.Replicas) {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
pdb.Spec.MinAvailable.IntValue(),
"value cannot be larger than number of replicas"),
)
}
if minAvailable < minQuorumSize {
warnings = append(warnings, "current number of spec.podDisruptionBudget.minAvailable can lead to loss of quorum")
}
}
if pdb.Spec.MaxUnavailable != nil {
maxUnavailable := pdb.Spec.MaxUnavailable.IntValue()
if pdb.Spec.MaxUnavailable.IntVal == 0 && pdb.Spec.MaxUnavailable.IntValue() == 0 {
var percentage int
_, err := fmt.Sscanf(pdb.Spec.MaxUnavailable.StrVal, "%d%%", &percentage)
if err != nil {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
pdb.Spec.MaxUnavailable.IntValue(),
"value cannot be larger than number of replicas"),
pdb.Spec.MaxUnavailable.StrVal,
"invalid percentage value"),
)
} else {
maxUnavailable = int(math.Ceil(float64(*r.Spec.Replicas) * (float64(percentage) / 100)))
}
if int(*r.Spec.Replicas)-pdb.Spec.MaxUnavailable.IntValue() < minQuorumSize {
warnings = append(warnings, "current number of spec.podDisruptionBudget.maxUnavailable can lead to loss of quorum")
}
}
if maxUnavailable < 0 {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
pdb.Spec.MaxUnavailable.IntValue(),
"value cannot be less than zero"),
)
}
if maxUnavailable > int(*r.Spec.Replicas) {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
pdb.Spec.MaxUnavailable.IntValue(),
"value cannot be larger than number of replicas"),
)
}
if int(*r.Spec.Replicas)-maxUnavailable < minQuorumSize {
warnings = append(warnings, "current number of spec.podDisruptionBudget.maxUnavailable can lead to loss of quorum")
}
}

Expand Down
187 changes: 108 additions & 79 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -92,85 +93,6 @@ var _ = Describe("EtcdCluster Webhook", func() {
Expect(err).To(Succeed())
Expect(w).To(BeEmpty())
})
It("Should admit enabled empty PDB", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{},
},
}
w, err := etcdCluster.ValidateCreate()
Expect(err).To(Succeed())
Expect(w).To(BeEmpty())
})
It("Should reject if negative spec.podDisruptionBudget.minAvailable", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{
Spec: PodDisruptionBudgetSpec{
MinAvailable: ptr.To(intstr.FromInt32(int32(-1))),
},
},
},
}
_, err := etcdCluster.ValidateCreate()
if Expect(err).To(HaveOccurred()) {
statusErr := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Message).To(ContainSubstring("value cannot be less than zero"))
}
})
It("Should reject if negative spec.podDisruptionBudget.maxUnavailable", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{
Spec: PodDisruptionBudgetSpec{
MaxUnavailable: ptr.To(intstr.FromInt32(int32(-1))),
},
},
},
}
_, err := etcdCluster.ValidateCreate()
if Expect(err).To(HaveOccurred()) {
statusErr := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Message).To(ContainSubstring("maxUnavailable: Invalid value: -1: value cannot be less than zero"))
}
})
It("Should reject if min available field larger than replicas", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{
Spec: PodDisruptionBudgetSpec{
MinAvailable: ptr.To(intstr.FromInt32(int32(2))),
},
},
},
}
_, err := etcdCluster.ValidateCreate()
if Expect(err).To(HaveOccurred()) {
statusErr := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Message).To(ContainSubstring("minAvailable: Invalid value: 2: value cannot be larger than number of replicas"))
}
})
It("Should reject if max unavailable field larger than replicas", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{
Spec: PodDisruptionBudgetSpec{
MaxUnavailable: ptr.To(intstr.FromInt32(int32(2))),
},
},
},
}
_, err := etcdCluster.ValidateCreate()
if Expect(err).To(HaveOccurred()) {
statusErr := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Message).To(ContainSubstring("maxUnavailable: Invalid value: 2: value cannot be larger than number of replicas"))
}
})
})

Context("When updating EtcdCluster under Validating Webhook", func() {
Expand Down Expand Up @@ -211,4 +133,111 @@ var _ = Describe("EtcdCluster Webhook", func() {
Expect(err).To(Succeed())
})
})

Context("Validate PDB", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(3)),
PodDisruptionBudget: &EmbeddedPodDisruptionBudget{},
},
}
It("Should admit enabled empty PDB", func() {
localCluster := etcdCluster.DeepCopy()
w, err := localCluster.validatePdb()
Expect(err).To(BeNil())
Expect(w).To(BeEmpty())
})
It("Should reject if negative spec.podDisruptionBudget.minAvailable", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MinAvailable = ptr.To(intstr.FromInt32(int32(-1)))
_, err := localCluster.validatePdb()
if Expect(err).NotTo(BeNil()) {
expectedFieldErr := field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
-1,
"value cannot be less than zero",
)
if Expect(err).To(HaveLen(1)) {
Expect(*(err[0])).To(Equal(*expectedFieldErr))
}
}
})
It("Should reject if negative spec.podDisruptionBudget.maxUnavailable", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromInt32(int32(-1)))
_, err := localCluster.validatePdb()
if Expect(err).NotTo(BeNil()) {
expectedFieldErr := field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
-1,
"value cannot be less than zero",
)
if Expect(err).To(HaveLen(1)) {
Expect(*(err[0])).To(Equal(*expectedFieldErr))
}
}
})
It("Should reject if min available field larger than replicas", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.Replicas = ptr.To(int32(1))
localCluster.Spec.PodDisruptionBudget.Spec.MinAvailable = ptr.To(intstr.FromInt32(int32(2)))
_, err := localCluster.validatePdb()
if Expect(err).NotTo(BeNil()) {
expectedFieldErr := field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "minAvailable"),
2,
"value cannot be larger than number of replicas",
)
if Expect(err).To(HaveLen(1)) {
Expect(*(err[0])).To(Equal(*expectedFieldErr))
}
}
})
It("Should reject if max unavailable field larger than replicas", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.Replicas = ptr.To(int32(1))
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromInt32(int32(2)))
_, err := localCluster.validatePdb()
if Expect(err).NotTo(BeNil()) {
expectedFieldErr := field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
2,
"value cannot be larger than number of replicas",
)
if Expect(err).To(HaveLen(1)) {
Expect(*(err[0])).To(Equal(*expectedFieldErr))
}
}
})
It("should accept correct percentage value for minAvailable", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.Replicas = ptr.To(int32(4))
localCluster.Spec.PodDisruptionBudget.Spec.MinAvailable = ptr.To(intstr.FromString("50%"))
warnings, err := localCluster.validatePdb()
Expect(err).To(BeNil())
Expect(warnings).To(ContainElement("current number of spec.podDisruptionBudget.minAvailable can lead to loss of quorum"))
})
It("should accept correct percentage value for maxUnavailable", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromString("50%"))
warnings, err := localCluster.validatePdb()
Expect(err).To(BeNil())
Expect(warnings).To(ContainElement("current number of spec.podDisruptionBudget.maxUnavailable can lead to loss of quorum"))
})
It("Should reject incorrect value for maxUnavailable", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromString("50$"))
_, err := localCluster.validatePdb()
if Expect(err).NotTo(BeNil()) {
expectedFieldErr := field.Invalid(
field.NewPath("spec", "podDisruptionBudget", "maxUnavailable"),
"50$",
"invalid percentage value",
)
if Expect(err).To(HaveLen(1)) {
Expect(*(err[0])).To(Equal(*expectedFieldErr))
}
}
})
})
})

0 comments on commit 248b31f

Please sign in to comment.