Skip to content

Commit b42af3d

Browse files
Merge pull request #307 from fmount/topologyref
Update LastAppliedTopology to TopologyRef interface
2 parents b9dd679 + 84e461b commit b42af3d

File tree

11 files changed

+96
-110
lines changed

11 files changed

+96
-110
lines changed

api/bases/mariadb.openstack.org_galeras.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,19 @@ spec:
216216
type: object
217217
lastAppliedTopology:
218218
description: LastAppliedTopology - the last applied Topology
219-
type: string
219+
properties:
220+
name:
221+
description: Name - The Topology CR name that the Service references
222+
type: string
223+
namespace:
224+
description: |-
225+
Namespace - The Namespace to fetch the Topology CR referenced
226+
NOTE: Namespace currently points by default to the same namespace where
227+
the Service is deployed. Customizing the namespace is not supported and
228+
webhooks prevent editing this field to a value different from the
229+
current project
230+
type: string
231+
type: object
220232
observedGeneration:
221233
description: |-
222234
ObservedGeneration - the most recent generation observed for this

api/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/go-logr/logr v1.4.2
77
github.com/onsi/ginkgo/v2 v2.20.1
88
github.com/onsi/gomega v1.34.1
9-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d
9+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded
1010
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a
1111
k8s.io/api v0.29.13
1212
k8s.io/apimachinery v0.29.13

api/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
7676
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
7777
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6Beb1gQ96Ptej9AE/BvwCBiRj1E=
7878
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
79-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d h1:JuIcZjfFUH9G68MR9F62xBK4eS5F2P5nKVCoDXFrvQM=
80-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
79+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded h1:09SyMAXgnohPLQGKuvBeR72nxZWXLXI7309RjmYYBUU=
80+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
8181
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a h1:3LuUgB85VxGD6lmVOeZelYEASmytkrzaudU014PN7xw=
8282
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a/go.mod h1:KxnNSUk15llkKTSq/bQEE7pnc0IMk44fxhoBRpimMa8=
8383
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

api/v1beta1/galera_types.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type GaleraStatus struct {
128128
// the opentack-operator in the top-level CR (e.g. the ContainerImage)
129129
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
130130
// LastAppliedTopology - the last applied Topology
131-
LastAppliedTopology string `json:"lastAppliedTopology,omitempty"`
131+
LastAppliedTopology *topologyv1.TopoRef `json:"lastAppliedTopology,omitempty"`
132132
}
133133

134134
// +kubebuilder:object:root=true
@@ -187,3 +187,16 @@ func SetupDefaults() {
187187

188188
SetupGaleraDefaults(galeraDefaults)
189189
}
190+
191+
// GetLastTopologyRef - Returns a TopoRef object that can be passed to the
192+
// Handle topology logic
193+
func (instance Galera) GetLastAppliedTopologyRef() *topologyv1.TopoRef {
194+
lastAppliedTopologyName := ""
195+
if instance.Status.LastAppliedTopology != nil {
196+
lastAppliedTopologyName = instance.Status.LastAppliedTopology.Name
197+
}
198+
return &topologyv1.TopoRef{
199+
Name: lastAppliedTopologyName,
200+
Namespace: instance.Namespace,
201+
}
202+
}

api/v1beta1/galera_webhook.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,7 @@ func (r *Galera) ValidateCreate() (admission.Warnings, error) {
9090
[]string{r.Name},
9191
CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
9292

93-
// When a TopologyRef CR is referenced, fail if a different Namespace is
94-
// referenced because is not supported
95-
if r.Spec.TopologyRef != nil {
96-
if err := topologyv1.ValidateTopologyNamespace(r.Spec.TopologyRef.Namespace, *basePath, r.Namespace); err != nil {
97-
allErrs = append(allErrs, err)
98-
}
99-
}
100-
101-
warn, err := r.Spec.ValidateCreate(basePath)
93+
warn, err := r.Spec.ValidateCreate(basePath, r.Namespace)
10294

10395
if err != nil {
10496
allErrs = append(allErrs, err...)
@@ -117,12 +109,12 @@ func (r *Galera) ValidateCreate() (admission.Warnings, error) {
117109

118110
// ValidateCreate - Exported function wrapping non-exported validate functions,
119111
// this function can be called externally to validate an KeystoneAPI spec.
120-
func (spec *GaleraSpec) ValidateCreate(basePath *field.Path) (admission.Warnings, field.ErrorList) {
121-
return spec.GaleraSpecCore.ValidateCreate(basePath)
112+
func (spec *GaleraSpec) ValidateCreate(basePath *field.Path, namespace string) (admission.Warnings, field.ErrorList) {
113+
return spec.GaleraSpecCore.ValidateCreate(basePath, namespace)
122114
}
123115

124116
// ValidateCreate -
125-
func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path) (admission.Warnings, field.ErrorList) {
117+
func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path, namespace string) (admission.Warnings, field.ErrorList) {
126118
var allErrs field.ErrorList
127119
allWarn := []string{}
128120

@@ -132,6 +124,14 @@ func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path) (admission.Warn
132124
warn = spec.ValidateGaleraReplicas(basePath)
133125
allWarn = append(allWarn, warn...)
134126

127+
// When a TopologyRef CR is referenced, fail if a different Namespace is
128+
// referenced because is not supported
129+
if spec.TopologyRef != nil {
130+
if err := topologyv1.ValidateTopologyNamespace(spec.TopologyRef.Namespace, *basePath, namespace); err != nil {
131+
allErrs = append(allErrs, err)
132+
}
133+
}
134+
135135
return allWarn, allErrs
136136
}
137137

@@ -145,16 +145,9 @@ func (r *Galera) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
145145
if !ok || oldGalera == nil {
146146
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
147147
}
148-
149148
basePath := field.NewPath("spec")
149+
allErrs = append(allErrs, r.Spec.ValidateUpdate(oldGalera.Spec, basePath, r.Namespace)...)
150150

151-
// When a TopologyRef CR is referenced, fail if a different Namespace is
152-
// referenced because is not supported
153-
if r.Spec.TopologyRef != nil {
154-
if err := topologyv1.ValidateTopologyNamespace(r.Spec.TopologyRef.Namespace, *basePath, r.Namespace); err != nil {
155-
allErrs = append(allErrs, err)
156-
}
157-
}
158151
warn := r.Spec.ValidateGaleraReplicas(basePath)
159152
allWarn = append(allWarn, warn...)
160153

@@ -163,6 +156,31 @@ func (r *Galera) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
163156
}
164157
return allWarn, nil
165158
}
159+
func (r *GaleraSpec) ValidateUpdate(old GaleraSpec, basePath *field.Path, namespace string) field.ErrorList {
160+
allErrs := field.ErrorList{}
161+
162+
// When a TopologyRef CR is referenced, fail if a different Namespace is
163+
// referenced because is not supported
164+
if r.TopologyRef != nil {
165+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
166+
allErrs = append(allErrs, err)
167+
}
168+
}
169+
return allErrs
170+
}
171+
172+
func (r *GaleraSpecCore) ValidateUpdate(old GaleraSpecCore, basePath *field.Path, namespace string) field.ErrorList {
173+
allErrs := field.ErrorList{}
174+
175+
// When a TopologyRef CR is referenced, fail if a different Namespace is
176+
// referenced because is not supported
177+
if r.TopologyRef != nil {
178+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
179+
allErrs = append(allErrs, err)
180+
}
181+
}
182+
return allErrs
183+
}
166184

167185
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
168186
func (r *Galera) ValidateDelete() (admission.Warnings, error) {

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/mariadb.openstack.org_galeras.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,19 @@ spec:
216216
type: object
217217
lastAppliedTopology:
218218
description: LastAppliedTopology - the last applied Topology
219-
type: string
219+
properties:
220+
name:
221+
description: Name - The Topology CR name that the Service references
222+
type: string
223+
namespace:
224+
description: |-
225+
Namespace - The Namespace to fetch the Topology CR referenced
226+
NOTE: Namespace currently points by default to the same namespace where
227+
the Service is deployed. Customizing the namespace is not supported and
228+
webhooks prevent editing this field to a value different from the
229+
current project
230+
type: string
231+
type: object
220232
observedGeneration:
221233
description: |-
222234
ObservedGeneration - the most recent generation observed for this

controllers/galera_controller.go

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -708,17 +708,13 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
708708
//
709709
// Handle Topology
710710
//
711-
lastTopologyRef := topologyv1.TopoRef{
712-
Name: instance.Status.LastAppliedTopology,
713-
Namespace: instance.Namespace,
714-
}
715-
topology, err := ensureGaleraTopology(
711+
topology, err := topologyv1.EnsureServiceTopology(
716712
ctx,
717713
helper,
718714
instance.Spec.TopologyRef,
719-
&lastTopologyRef,
715+
instance.GetLastAppliedTopologyRef(),
720716
instance.Name,
721-
mariadb.ServiceName,
717+
labels.GetAppLabelSelector(mariadb.ServiceName),
722718
)
723719
if err != nil {
724720
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -735,12 +731,12 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
735731
// and mark the condition as true
736732
if instance.Spec.TopologyRef != nil {
737733
// update the Status with the last retrieved Topology name
738-
instance.Status.LastAppliedTopology = instance.Spec.TopologyRef.Name
734+
instance.Status.LastAppliedTopology = instance.Spec.TopologyRef
739735
// update the TopologyRef associated condition
740736
instance.Status.Conditions.MarkTrue(condition.TopologyReadyCondition, condition.TopologyReadyMessage)
741737
} else {
742738
// remove LastAppliedTopology from the .Status
743-
instance.Status.LastAppliedTopology = ""
739+
instance.Status.LastAppliedTopology = nil
744740
}
745741

746742
commonstatefulset := commonstatefulset.NewStatefulSet(mariadb.StatefulSet(instance, hashOfHashes, topology), 5)
@@ -1103,10 +1099,7 @@ func (r *GaleraReconciler) reconcileDelete(ctx context.Context, instance *databa
11031099
if ctrlResult, err := topologyv1.EnsureDeletedTopologyRef(
11041100
ctx,
11051101
helper,
1106-
&topologyv1.TopoRef{
1107-
Name: instance.Status.LastAppliedTopology,
1108-
Namespace: instance.Namespace,
1109-
},
1102+
instance.Status.LastAppliedTopology,
11101103
instance.Name,
11111104
); err != nil {
11121105
return ctrlResult, err
@@ -1118,61 +1111,3 @@ func (r *GaleraReconciler) reconcileDelete(ctx context.Context, instance *databa
11181111

11191112
return ctrl.Result{}, nil
11201113
}
1121-
1122-
// ensureGaleraTopology - when a Topology CR is referenced, remove the
1123-
// finalizer from a previous referenced Topology (if any), and retrieve the
1124-
// newly referenced topology object
1125-
func ensureGaleraTopology(
1126-
ctx context.Context,
1127-
helper *helper.Helper,
1128-
tpRef *topologyv1.TopoRef,
1129-
lastAppliedTopology *topologyv1.TopoRef,
1130-
finalizer string,
1131-
selector string,
1132-
) (*topologyv1.Topology, error) {
1133-
1134-
var podTopology *topologyv1.Topology
1135-
var err error
1136-
1137-
// Remove (if present) the finalizer from a previously referenced topology
1138-
//
1139-
// 1. a topology reference is removed (tpRef == nil) from the Manila Component
1140-
// subCR and the finalizer should be deleted from the last applied topology
1141-
// (lastAppliedTopology != "")
1142-
// 2. a topology reference is updated in the Manila Component CR (tpRef != nil)
1143-
// and the finalizer should be removed from the previously
1144-
// referenced topology (tpRef.Name != lastAppliedTopology.Name)
1145-
if (tpRef == nil && lastAppliedTopology.Name != "") ||
1146-
(tpRef != nil && tpRef.Name != lastAppliedTopology.Name) {
1147-
_, err = topologyv1.EnsureDeletedTopologyRef(
1148-
ctx,
1149-
helper,
1150-
lastAppliedTopology,
1151-
finalizer,
1152-
)
1153-
if err != nil {
1154-
return nil, err
1155-
}
1156-
}
1157-
// TopologyRef is passed as input, get the Topology object
1158-
if tpRef != nil {
1159-
// no Namespace is provided, default to instance.Namespace
1160-
if tpRef.Namespace == "" {
1161-
tpRef.Namespace = helper.GetBeforeObject().GetNamespace()
1162-
}
1163-
// Build the defaultLabelSelector based on serviceName (service=mariadb)
1164-
defaultLabelSelector := labels.GetAppLabelSelector(selector)
1165-
// Retrieve the referenced Topology
1166-
podTopology, _, err = topologyv1.EnsureTopologyRef(
1167-
ctx,
1168-
helper,
1169-
tpRef,
1170-
finalizer,
1171-
&defaultLabelSelector,
1172-
)
1173-
if err != nil {
1174-
return nil, err
1175-
}
1176-
}
1177-
return podTopology, nil
1178-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/google/uuid v1.6.0
88
github.com/onsi/ginkgo/v2 v2.20.1
99
github.com/onsi/gomega v1.34.1
10-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d
10+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded
1111
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a
1212
github.com/openstack-k8s-operators/mariadb-operator/api v0.0.0-00010101000000-000000000000
1313
go.uber.org/zap v1.27.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
8585
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
8686
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6Beb1gQ96Ptej9AE/BvwCBiRj1E=
8787
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
88-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d h1:JuIcZjfFUH9G68MR9F62xBK4eS5F2P5nKVCoDXFrvQM=
89-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250210183624-a8bf66784d6d/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
88+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded h1:09SyMAXgnohPLQGKuvBeR72nxZWXLXI7309RjmYYBUU=
89+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
9090
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a h1:3LuUgB85VxGD6lmVOeZelYEASmytkrzaudU014PN7xw=
9191
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a/go.mod h1:KxnNSUk15llkKTSq/bQEE7pnc0IMk44fxhoBRpimMa8=
9292
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

0 commit comments

Comments
 (0)