Skip to content

Commit

Permalink
Allow intermediate replicas to ease operation
Browse files Browse the repository at this point in the history
Currently the mariadb operator has webhooks to enforce the galera
replica count to be either 1 or 3, which are the two supported
deployments on the OpenstackControlPlane.

However there is a need for more flexibility for day two operations,
where it can be useful to allow scaling down replicas temporarily
to e.g. 2 (for replacement procedure) or even 0 (to temporarily disable
database management).

Replace the kubebuilder annotations with minimum and maximum, and
let the galera webhook warn about setting replicas to 2 (as it is not
advisable to keep that setting for quorum concerns).
  • Loading branch information
dciabrin authored and gibizer committed Nov 21, 2024
1 parent bed16e1 commit 1d933fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
6 changes: 2 additions & 4 deletions api/bases/mariadb.openstack.org_galeras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ spec:
replicas:
default: 1
description: Size of the galera cluster deployment
enum:
- 1
- 3
format: int32
minimum: 1
maximum: 3
minimum: 0
type: integer
secret:
description: Name of the secret to look for password keys
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/galera_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ type GaleraSpecCore struct {
// Storage size allocated for the mariadb databases
// +kubebuilder:validation:Required
StorageRequest string `json:"storageRequest"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=3
// +kubebuilder:default=1
// +kubebuilder:validation:Enum=1;3
// Size of the galera cluster deployment
Replicas *int32 `json:"replicas"`
// +kubebuilder:validation:Optional
Expand Down
22 changes: 21 additions & 1 deletion api/v1beta1/galera_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,28 @@ func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path) (admission.Warn
warn, _ := common_webhook.ValidateStorageRequest(basePath, spec.StorageRequest, storageRequestProdMin, false)
allWarn = append(allWarn, warn...)

warn = spec.ValidateGaleraReplicas(basePath)
allWarn = append(allWarn, warn...)

return allWarn, allErrs
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Galera) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
allWarn := []string{}
galeralog.Info("validate update", "name", r.Name)

oldGalera, ok := old.(*Galera)
if !ok || oldGalera == nil {
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
}

basePath := field.NewPath("spec")
warn := r.Spec.ValidateGaleraReplicas(basePath)
allWarn = append(allWarn, warn...)

// TODO(user): fill in your validation logic upon object update.
return nil, nil
return allWarn, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand All @@ -149,3 +157,15 @@ func SetupGaleraDefaults(defaults GaleraDefaults) {
galeraDefaults = defaults
galeralog.Info("Galera defaults initialized", "defaults", defaults)
}

// SetupGaleraDefaults - Check whether replica count is valid for quorum
func (spec *GaleraSpecCore) ValidateGaleraReplicas(basePath *field.Path) admission.Warnings {
replicas := int(*spec.Replicas)
if replicas > 0 && (replicas%2 == 0) {
res := fmt.Sprintf("%s: %d is not appropriate for quorum! Use an odd value!",
basePath.Child("replicas").String(), replicas)
return []string{res}
} else {
return nil
}
}
6 changes: 2 additions & 4 deletions config/crd/bases/mariadb.openstack.org_galeras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ spec:
replicas:
default: 1
description: Size of the galera cluster deployment
enum:
- 1
- 3
format: int32
minimum: 1
maximum: 3
minimum: 0
type: integer
secret:
description: Name of the secret to look for password keys
Expand Down
2 changes: 1 addition & 1 deletion controllers/galera_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
}

// Check if we have enough info to bootstrap the cluster now
if !found {
if !found && int(*instance.Spec.Replicas) > 0 {
node, found = findBestCandidate(instance)
}
if found {
Expand Down

0 comments on commit 1d933fc

Please sign in to comment.