Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutating maxUnavailable to nil if PDB is disabled. #338

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions api/v1/aerospikecluster_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ func (c *AerospikeCluster) Default(operation v1.Operation) admission.Response {
c.Spec,
)

if err := c.setDefaults(asLog); err != nil {
var (
warn admission.Warnings
err error
)

if warn, err = c.setDefaults(asLog); err != nil {
asLog.Error(err, "Mutate AerospikeCluster create failed")
return webhook.Denied(err.Error())
return webhook.Denied(err.Error()).WithWarnings(warn...)
}

asLog.Info("Setting defaults for aerospikeCluster completed")
Expand All @@ -67,12 +72,19 @@ func (c *AerospikeCluster) Default(operation v1.Operation) admission.Response {
return webhook.Patched(
"Patched aerospike spec with defaults",
patches...,
)
).WithWarnings(warn...)
}

func (c *AerospikeCluster) setDefaults(asLog logr.Logger) error {
// Set maxUnavailable default to 1
if !GetBool(c.Spec.DisablePDB) && c.Spec.MaxUnavailable == nil {
func (c *AerospikeCluster) setDefaults(asLog logr.Logger) (admission.Warnings, error) {
var warn admission.Warnings
// If PDB is disabled, set maxUnavailable to nil
if GetBool(c.Spec.DisablePDB) {
c.Spec.MaxUnavailable = nil
abhishekdwivedi3060 marked this conversation as resolved.
Show resolved Hide resolved

warn = append(warn, fmt.Sprintf("Spec field 'spec.maxUnavailable' will be omitted from Custom Resource (CR) "+
"because 'spec.disablePDB' is true."))
} else if c.Spec.MaxUnavailable == nil {
// Set default maxUnavailable if not set
maxUnavailable := intstr.FromInt32(1)
c.Spec.MaxUnavailable = &maxUnavailable
}
Expand All @@ -87,27 +99,27 @@ func (c *AerospikeCluster) setDefaults(asLog logr.Logger) error {
// Need to set before setting defaults in aerospikeConfig.
// aerospikeConfig.namespace checks for racks
if err := c.setDefaultRackConf(asLog); err != nil {
return err
return warn, err
}

if c.Spec.AerospikeConfig == nil {
return fmt.Errorf("spec.aerospikeConfig cannot be nil")
return warn, fmt.Errorf("spec.aerospikeConfig cannot be nil")
}

// Set common aerospikeConfig defaults
// Update configMap
if err := c.setDefaultAerospikeConfigs(asLog, *c.Spec.AerospikeConfig, nil); err != nil {
return err
return warn, err
}

// Update racks configuration using global values where required.
if err := c.updateRacks(asLog); err != nil {
return err
return warn, err
}

// Set defaults for pod spec
if err := c.Spec.PodSpec.SetDefaults(); err != nil {
return err
return warn, err
}

// Validation policy
Expand Down Expand Up @@ -135,7 +147,7 @@ func (c *AerospikeCluster) setDefaults(asLog logr.Logger) error {
c.Labels[AerospikeAPIVersionLabel] = AerospikeAPIVersion
}

return nil
return warn, nil
}

// SetDefaults applies defaults to the pod spec.
Expand Down
16 changes: 9 additions & 7 deletions api/v1/aerospikecluster_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2294,17 +2294,19 @@ func validateIntOrStringField(value *intstr.IntOrString, fieldPath string) error
}

func (c *AerospikeCluster) validateMaxUnavailable() error {
// safe check for corner cases when mutation webhook somehow didn't work
if !GetBool(c.Spec.DisablePDB) && c.Spec.MaxUnavailable == nil {
return fmt.Errorf("maxUnavailable cannot be nil if PDB is not disabled. Mutation webhook didn't work")
}

if GetBool(c.Spec.DisablePDB) {
// safe checks for corner cases when mutation webhook somehow didn't work
if !GetBool(c.Spec.DisablePDB) {
// Ensure maxUnavailable is set when PDB is enabled
if c.Spec.MaxUnavailable == nil {
return fmt.Errorf("maxUnavailable cannot be nil if PDB is not disabled. Mutation webhook might not have worked")
}
} else {
// Ensure maxUnavailable is unset when PDB is disabled
if c.Spec.MaxUnavailable != nil {
return fmt.Errorf("maxUnavailable must be nil if PDB is disabled")
}

// PDB is disabled, so no need to validate further
// PDB is disabled, no further validation required
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/backup-service/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (r *SingleBackupServiceReconciler) getServiceAccount() string {
return r.aeroBackupService.Spec.PodSpec.ServiceAccountName
}

return common.AerospikeBackupService
return asdbv1beta1.AerospikeBackupServiceKey
}

func (r *SingleBackupServiceReconciler) updateDeploymentFromPodSpec(deploy *app.Deployment) {
Expand Down
4 changes: 2 additions & 2 deletions test/backup_service/backup_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ var _ = Describe(
err = DeployBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

validateSA(common.AerospikeBackupService)
validateSA(asdbv1beta1.AerospikeBackupServiceKey)

By("Update Service Account")
backupService, err = getBackupServiceObj(k8sClient, name, namespace)
Expand All @@ -459,7 +459,7 @@ var _ = Describe(
err = updateBackupService(k8sClient, backupService)
Expect(err).ToNot(HaveOccurred())

validateSA(common.AerospikeBackupService)
validateSA(asdbv1beta1.AerospikeBackupServiceKey)
})

})
Expand Down
16 changes: 4 additions & 12 deletions test/cluster/poddisruptionbudget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ var _ = Describe(
Expect(err).ToNot(HaveOccurred())

aeroCluster.Spec.DisablePDB = ptr.To(true)
aeroCluster.Spec.MaxUnavailable = nil
err = updateCluster(k8sClient, ctx, aeroCluster)
Expect(err).ToNot(HaveOccurred())

aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName)
Expect(err).ToNot(HaveOccurred())
Expect(aeroCluster.Spec.MaxUnavailable).To(BeNil())

_, err = getPDB(ctx, aeroCluster)
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
pkgLog.Info("PDB deleted as expected")

By("Update disablePDB to false")
aeroCluster, err = getCluster(k8sClient, ctx, clusterNamespacedName)
Expect(err).ToNot(HaveOccurred())

aeroCluster.Spec.DisablePDB = ptr.To(false)
err = updateCluster(k8sClient, ctx, aeroCluster)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -161,14 +161,6 @@ var _ = Describe(
err := deployCluster(k8sClient, ctx, aeroCluster)
Expect(err).To(HaveOccurred())
})

It("Should fail if maxUnavailable is given but disablePDB is true", func() {
aeroCluster.Spec.DisablePDB = ptr.To(true)
value := intstr.FromInt32(1)
aeroCluster.Spec.MaxUnavailable = &value
err := deployCluster(k8sClient, ctx, aeroCluster)
Expect(err).To(HaveOccurred())
})
})
})

Expand Down
Loading