@@ -2,6 +2,7 @@ package openstack
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "strings"
78
@@ -10,8 +11,10 @@ import (
1011 "github.com/openstack-k8s-operators/lib-common/modules/common/clusterdns"
1112 "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
1213 "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
14+ "github.com/openstack-k8s-operators/lib-common/modules/common/object"
1315 mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
1416
17+ "sigs.k8s.io/controller-runtime/pkg/client"
1518 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1619
1720 corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
@@ -28,14 +31,61 @@ const (
2831 galeraReady galeraStatus = iota
2932)
3033
34+ func deleteUndefinedGaleras (
35+ ctx context.Context ,
36+ instance * corev1beta1.OpenStackControlPlane ,
37+ helper * helper.Helper ,
38+ ) (ctrl.Result , error ) {
39+
40+ log := GetLogger (ctx )
41+ // Fetch the list of Galera objects
42+ galeraList := & mariadbv1.GaleraList {}
43+ listOpts := []client.ListOption {
44+ client .InNamespace (instance .GetNamespace ()),
45+ }
46+ err := helper .GetClient ().List (ctx , galeraList , listOpts ... )
47+ if err != nil {
48+ return ctrl.Result {}, fmt .Errorf ("could not get galeras %w" , err )
49+ }
50+
51+ var delErrs []error
52+ for _ , galeraObj := range galeraList .Items {
53+ // if it is not defined in the OpenStackControlPlane then delete it from k8s.
54+ if _ , exists := (* instance .Spec .Galera .Templates )[galeraObj .Name ]; ! exists {
55+ if object .CheckOwnerRefExist (instance .GetUID (), galeraObj .OwnerReferences ) {
56+ log .Info ("Deleting Galera" , "" , galeraObj .Name )
57+
58+ certName := fmt .Sprintf ("galera-%s-svc" , galeraObj .Name )
59+ err = DeleteCertificate (ctx , helper , instance .Namespace , certName )
60+ if err != nil {
61+ delErrs = append (delErrs , fmt .Errorf ("galera cert deletion for '%s' failed, because: %w" , certName , err ))
62+ continue
63+ }
64+
65+ if _ , err := EnsureDeleted (ctx , helper , & galeraObj ); err != nil {
66+ delErrs = append (delErrs , fmt .Errorf ("galera deletion for '%s' failed, because: %w" , galeraObj .Name , err ))
67+ }
68+
69+ }
70+ }
71+ }
72+
73+ if len (delErrs ) > 0 {
74+ delErrs := errors .Join (delErrs ... )
75+ return ctrl.Result {}, delErrs
76+ }
77+
78+ return ctrl.Result {}, nil
79+ }
80+
3181// ReconcileGaleras -
3282func ReconcileGaleras (
3383 ctx context.Context ,
3484 instance * corev1beta1.OpenStackControlPlane ,
3585 version * corev1beta1.OpenStackVersion ,
3686 helper * helper.Helper ,
3787) (ctrl.Result , error ) {
38- Log := GetLogger (ctx )
88+ log := GetLogger (ctx )
3989 if ! instance .Spec .Galera .Enabled {
4090 return ctrl.Result {}, nil
4191 }
@@ -127,20 +177,31 @@ func ReconcileGaleras(
127177 return ctrl.Result {}, fmt .Errorf (errors )
128178
129179 } else if len (inprogress ) > 0 {
130- Log .Info ("Galera in progress" )
180+ log .Info ("Galera in progress" )
131181 instance .Status .Conditions .Set (condition .FalseCondition (
132182 corev1beta1 .OpenStackControlPlaneMariaDBReadyCondition ,
133183 condition .RequestedReason ,
134184 condition .SeverityInfo ,
135185 corev1beta1 .OpenStackControlPlaneMariaDBReadyRunningMessage ))
136186 } else {
137- Log .Info ("Galera ready condition is true" )
187+ log .Info ("Galera ready condition is true" )
138188 instance .Status .Conditions .MarkTrue (
139189 corev1beta1 .OpenStackControlPlaneMariaDBReadyCondition ,
140190 corev1beta1 .OpenStackControlPlaneMariaDBReadyMessage ,
141191 )
142192 }
143193
194+ _ , errs := deleteUndefinedGaleras (ctx , instance , helper )
195+ if errs != nil {
196+ instance .Status .Conditions .Set (condition .FalseCondition (
197+ corev1beta1 .OpenStackControlPlaneMariaDBReadyCondition ,
198+ condition .ErrorReason ,
199+ condition .SeverityWarning ,
200+ corev1beta1 .OpenStackControlPlaneMariaDBReadyErrorMessage ,
201+ errs ))
202+ return ctrl.Result {}, errs
203+ }
204+
144205 return ctrl.Result {}, nil
145206}
146207
@@ -159,7 +220,7 @@ func reconcileGalera(
159220 Namespace : instance .Namespace ,
160221 },
161222 }
162- Log := GetLogger (ctx )
223+ log := GetLogger (ctx )
163224
164225 if ! instance .Spec .Galera .Enabled {
165226 if _ , err := EnsureDeleted (ctx , helper , galera ); err != nil {
@@ -182,7 +243,7 @@ func reconcileGalera(
182243 spec .TopologyRef = instance .Spec .TopologyRef
183244 }
184245
185- Log .Info ("Reconciling Galera" , "Galera.Namespace" , instance .Namespace , "Galera.Name" , name )
246+ log .Info ("Reconciling Galera" , "Galera.Namespace" , instance .Namespace , "Galera.Name" , name )
186247 op , err := controllerutil .CreateOrPatch (ctx , helper .GetClient (), galera , func () error {
187248 spec .DeepCopyInto (& galera .Spec .GaleraSpecCore )
188249 galera .Spec .ContainerImage = * version .Status .ContainerImages .MariadbImage
@@ -198,7 +259,7 @@ func reconcileGalera(
198259 return galeraFailed , err
199260 }
200261 if op != controllerutil .OperationResultNone {
201- Log .Info (fmt .Sprintf ("Galera %s - %s" , galera .Name , op ))
262+ log .Info (fmt .Sprintf ("Galera %s - %s" , galera .Name , op ))
202263 }
203264
204265 if galera .Status .ObservedGeneration == galera .Generation && galera .IsReady () {
@@ -211,10 +272,10 @@ func reconcileGalera(
211272
212273// GaleraImageMatch - return true if the Galera images match on the ControlPlane and Version, or if Galera is not enabled
213274func GaleraImageMatch (ctx context.Context , controlPlane * corev1beta1.OpenStackControlPlane , version * corev1beta1.OpenStackVersion ) bool {
214- Log := GetLogger (ctx )
275+ log := GetLogger (ctx )
215276 if controlPlane .Spec .Galera .Enabled {
216277 if ! stringPointersEqual (controlPlane .Status .ContainerImages .MariadbImage , version .Status .ContainerImages .MariadbImage ) {
217- Log .Info ("Galera images do not match" , "controlPlane.Status.ContainerImages.MariadbImage" , controlPlane .Status .ContainerImages .MariadbImage , "version.Status.ContainerImages.MariadbImage" , version .Status .ContainerImages .MariadbImage )
278+ log .Info ("Galera images do not match" , "controlPlane.Status.ContainerImages.MariadbImage" , controlPlane .Status .ContainerImages .MariadbImage , "version.Status.ContainerImages.MariadbImage" , version .Status .ContainerImages .MariadbImage )
218279 return false
219280 }
220281 }
0 commit comments