Skip to content

Commit

Permalink
Add Kafka halt related webhook
Browse files Browse the repository at this point in the history
Signed-off-by: obaydullahmhs <[email protected]>
  • Loading branch information
obaydullahmhs committed Aug 8, 2024
1 parent 7b9fbc7 commit ce94167
Showing 4 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apis/kubedb/v1/kafka_helpers.go
Original file line number Diff line number Diff line change
@@ -300,6 +300,14 @@ func (k *Kafka) SetHealthCheckerDefaults() {
}

func (k *Kafka) SetDefaults() {
if k.Spec.Halted {
if k.Spec.DeletionPolicy == DeletionPolicyDoNotTerminate {
klog.Errorf(`Can't halt, since deletion policy is 'DoNotTerminate'`)
return
}
k.Spec.DeletionPolicy = DeletionPolicyHalt
}

if k.Spec.DeletionPolicy == "" {
k.Spec.DeletionPolicy = DeletionPolicyDelete
}
11 changes: 11 additions & 0 deletions apis/kubedb/v1/kafka_webhooks.go
Original file line number Diff line number Diff line change
@@ -166,6 +166,12 @@ func (k *Kafka) ValidateCreateOrUpdate() error {
}
}

if k.Spec.Halted && k.Spec.DeletionPolicy == DeletionPolicyDoNotTerminate {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("halted"),
k.Name,
`can't halt if deletionPolicy is set to "DoNotTerminate"`))
}

err = k.validateVolumes(k)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate").Child("spec").Child("volumes"),
@@ -190,6 +196,11 @@ func (k *Kafka) ValidateCreateOrUpdate() error {
k.Name,
"StorageType should be either durable or ephemeral"))
}
if k.Spec.StorageType == StorageTypeEphemeral && k.Spec.DeletionPolicy == DeletionPolicyHalt {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("deletionPolicy"),
k.Name,
`'spec.deletionPolicy: Halt' can not be used for 'Ephemeral' storage`))
}
}

if len(allErr) == 0 {
8 changes: 8 additions & 0 deletions apis/kubedb/v1alpha2/kafka_helpers.go
Original file line number Diff line number Diff line change
@@ -299,6 +299,14 @@ func (k *Kafka) SetHealthCheckerDefaults() {
}

func (k *Kafka) SetDefaults() {
if k.Spec.Halted {
if k.Spec.DeletionPolicy == TerminationPolicyDoNotTerminate {
klog.Errorf(`Can't halt, since deletion policy is 'DoNotTerminate'`)
return
}
k.Spec.DeletionPolicy = TerminationPolicyHalt
}

if k.Spec.DeletionPolicy == "" {
k.Spec.DeletionPolicy = TerminationPolicyDelete
}
11 changes: 11 additions & 0 deletions apis/kubedb/v1alpha2/kafka_webhook.go
Original file line number Diff line number Diff line change
@@ -170,6 +170,12 @@ func (k *Kafka) ValidateCreateOrUpdate() error {
}
}

if k.Spec.Halted && k.Spec.DeletionPolicy == TerminationPolicyDoNotTerminate {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("halted"),
k.Name,
`can't halt if deletionPolicy is set to "DoNotTerminate"`))
}

err := k.validateVersion(k)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"),
@@ -201,6 +207,11 @@ func (k *Kafka) ValidateCreateOrUpdate() error {
k.Name,
"StorageType should be either durable or ephemeral"))
}
if k.Spec.StorageType == StorageTypeEphemeral && k.Spec.DeletionPolicy == TerminationPolicyHalt {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("deletionPolicy"),
k.Name,
`'spec.deletionPolicy: Halt' can not be used for 'Ephemeral' storage`))
}
}

if len(allErr) == 0 {

0 comments on commit ce94167

Please sign in to comment.