Skip to content

Commit 84e461b

Browse files
committed
Align LastAppliedTopology to TopologyRef interface
While TopologyRef is able to reference Name and namespace, we currently only save TopologyRef.Name to .Status.LastAppliedTopology. This patch aligns both interfaces to use the same type in the same form. In addition, some logic is moved to infra-operator because is common to all operators, and this patch also adopts this new form. Signed-off-by: Francesco Pantano <[email protected]>
1 parent 5f717fe commit 84e461b

File tree

10 files changed

+58
-90
lines changed

10 files changed

+58
-90
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/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=

pkg/mariadb/statefulset.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,7 @@ func StatefulSet(g *mariadbv1.Galera, configHash string, topology *topologyv1.To
7474
sts.Spec.Template.Spec.NodeSelector = *g.Spec.NodeSelector
7575
}
7676
if topology != nil {
77-
// Get the Topology .Spec
78-
ts := topology.Spec
79-
// Process TopologySpreadConstraints if defined in the referenced Topology
80-
if ts.TopologySpreadConstraints != nil {
81-
sts.Spec.Template.Spec.TopologySpreadConstraints = *topology.Spec.TopologySpreadConstraints
82-
}
83-
// Process Affinity if defined in the referenced Topology
84-
if ts.Affinity != nil {
85-
sts.Spec.Template.Spec.Affinity = ts.Affinity
86-
}
77+
topology.ApplyTo(&sts.Spec.Template)
8778
} else {
8879
// If possible two pods of the same service should not
8980
// run on the same worker node. If this is not possible

0 commit comments

Comments
 (0)