Skip to content

Commit 9b33591

Browse files
committed
Update LastAppliedTopology to TopologyRef interface
This patch updates the SpecCore webhook interface to accept the Namespace as parameter. This change ensures that we properly validate the referenced topology from openstack-operator, rejecting requests with a different namespace. It also aligns "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 75e8a0a commit 9b33591

27 files changed

+287
-196
lines changed

api/bases/ovn.openstack.org_ovncontrollers.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,19 @@ spec:
247247
type: object
248248
lastAppliedTopology:
249249
description: LastAppliedTopology - the last applied Topology
250-
type: string
250+
properties:
251+
name:
252+
description: Name - The Topology CR name that the Service references
253+
type: string
254+
namespace:
255+
description: |-
256+
Namespace - The Namespace to fetch the Topology CR referenced
257+
NOTE: Namespace currently points by default to the same namespace where
258+
the Service is deployed. Customizing the namespace is not supported and
259+
webhooks prevent editing this field to a value different from the
260+
current project
261+
type: string
262+
type: object
251263
networkAttachments:
252264
additionalProperties:
253265
items:

api/bases/ovn.openstack.org_ovndbclusters.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,19 @@ spec:
405405
type: string
406406
lastAppliedTopology:
407407
description: LastAppliedTopology - the last applied Topology
408-
type: string
408+
properties:
409+
name:
410+
description: Name - The Topology CR name that the Service references
411+
type: string
412+
namespace:
413+
description: |-
414+
Namespace - The Namespace to fetch the Topology CR referenced
415+
NOTE: Namespace currently points by default to the same namespace where
416+
the Service is deployed. Customizing the namespace is not supported and
417+
webhooks prevent editing this field to a value different from the
418+
current project
419+
type: string
420+
type: object
409421
networkAttachments:
410422
additionalProperties:
411423
items:

api/bases/ovn.openstack.org_ovnnorthds.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,19 @@ spec:
210210
type: array
211211
lastAppliedTopology:
212212
description: LastAppliedTopology - the last applied Topology
213-
type: string
213+
properties:
214+
name:
215+
description: Name - The Topology CR name that the Service references
216+
type: string
217+
namespace:
218+
description: |-
219+
Namespace - The Namespace to fetch the Topology CR referenced
220+
NOTE: Namespace currently points by default to the same namespace where
221+
the Service is deployed. Customizing the namespace is not supported and
222+
webhooks prevent editing this field to a value different from the
223+
current project
224+
type: string
225+
type: object
214226
observedGeneration:
215227
description: ObservedGeneration - the most recent generation observed
216228
for this service. If the observed generation is less than the spec

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/google/uuid v1.6.0
88
github.com/onsi/gomega v1.34.1
9-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250211161037-7dfd80c0fa16
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
@@ -78,8 +78,8 @@ github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
7878
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
7979
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6Beb1gQ96Ptej9AE/BvwCBiRj1E=
8080
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
81-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250211161037-7dfd80c0fa16 h1:dPHy7nEP7EbMip1jPqDhqsOyP4bWHq+UN31NrHNMN1E=
82-
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250211161037-7dfd80c0fa16/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
81+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded h1:09SyMAXgnohPLQGKuvBeR72nxZWXLXI7309RjmYYBUU=
82+
github.com/openstack-k8s-operators/infra-operator/apis v0.5.1-0.20250218115938-ae95bdfefded/go.mod h1:kkjcOSZ7jkHbVzxJd0nDQzjB+vqafuAMgSf7AnEXydw=
8383
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a h1:3LuUgB85VxGD6lmVOeZelYEASmytkrzaudU014PN7xw=
8484
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20250205143454-43504d7ad19a/go.mod h1:KxnNSUk15llkKTSq/bQEE7pnc0IMk44fxhoBRpimMa8=
8585
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

api/v1beta1/ovncontroller_types.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type OVNControllerStatus struct {
117117
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
118118

119119
// LastAppliedTopology - the last applied Topology
120-
LastAppliedTopology string `json:"lastAppliedTopology,omitempty"`
120+
LastAppliedTopology *topologyv1.TopoRef `json:"lastAppliedTopology,omitempty"`
121121
}
122122

123123
//+kubebuilder:object:root=true
@@ -193,3 +193,18 @@ func (instance OVNController) RbacNamespace() string {
193193
func (instance OVNController) RbacResourceName() string {
194194
return "ovncontroller-" + instance.Name
195195
}
196+
197+
// GetSpecTopologyRef - Returns the LastAppliedTopology Set in the Status
198+
func (instance *OVNController) GetSpecTopologyRef() *topologyv1.TopoRef {
199+
return instance.Spec.TopologyRef
200+
}
201+
202+
// GetLastAppliedTopology - Returns the LastAppliedTopology Set in the Status
203+
func (instance *OVNController) GetLastAppliedTopology() *topologyv1.TopoRef {
204+
return instance.Status.LastAppliedTopology
205+
}
206+
207+
// SetLastAppliedTopology - Sets the LastAppliedTopology value in the Status
208+
func (instance *OVNController) SetLastAppliedTopology(topologyRef *topologyv1.TopoRef) {
209+
instance.Status.LastAppliedTopology = topologyRef
210+
}

api/v1beta1/ovncontroller_webhook.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,33 @@ var _ webhook.Validator = &OVNController{}
8585
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
8686
func (r *OVNController) ValidateCreate() (admission.Warnings, error) {
8787
ovncontrollerlog.Info("validate create", "name", r.Name)
88+
errors := field.ErrorList{}
89+
90+
errors = r.Spec.ValidateCreate(field.NewPath("spec"), r.Namespace)
91+
if len(errors) != 0 {
92+
ovncontrollerlog.Info("validation failed", "name", r.Name)
93+
return nil, apierrors.NewInvalid(
94+
schema.GroupKind{Group: "ovn.openstack.org", Kind: "OVNController"},
95+
r.Name, errors)
96+
}
97+
return nil, nil
98+
}
8899

100+
func (r OVNControllerSpec) ValidateCreate(basePath *field.Path, namespace string) field.ErrorList {
101+
return r.OVNControllerSpecCore.ValidateCreate(basePath, namespace)
102+
}
103+
104+
func (r *OVNControllerSpecCore) ValidateCreate(basePath *field.Path, namespace string) field.ErrorList {
89105
errors := field.ErrorList{}
90-
basePath := field.NewPath("spec")
91106

92107
// When a TopologyRef CR is referenced, fail if a different Namespace is
93108
// referenced because is not supported
94-
if r.Spec.TopologyRef != nil {
95-
if err := topologyv1.ValidateTopologyNamespace(r.Spec.TopologyRef.Namespace, *basePath, r.Namespace); err != nil {
109+
if r.TopologyRef != nil {
110+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
96111
errors = append(errors, err)
97112
}
98113
}
99-
if len(errors) != 0 {
100-
return nil, apierrors.NewInvalid(
101-
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
102-
r.Name, errors)
103-
}
104-
105-
return nil, nil
114+
return errors
106115
}
107116

108117
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
@@ -121,12 +130,29 @@ func (r *OVNController) ValidateUpdate(old runtime.Object) (admission.Warnings,
121130
}
122131
if len(errors) != 0 {
123132
return nil, apierrors.NewInvalid(
124-
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
133+
schema.GroupKind{Group: "ovn.openstack.org", Kind: "OVNController"},
125134
r.Name, errors)
126135
}
127136
return nil, nil
128137
}
129138

139+
func (r OVNControllerSpec) ValidateUpdate(old OVNControllerSpec, basePath *field.Path, namespace string) field.ErrorList {
140+
return r.OVNControllerSpecCore.ValidateCreate(basePath, namespace)
141+
}
142+
143+
func (r *OVNControllerSpecCore) ValidateUpdate(old OVNControllerSpec, basePath *field.Path, namespace string) field.ErrorList {
144+
errors := field.ErrorList{}
145+
146+
// When a TopologyRef CR is referenced, fail if a different Namespace is
147+
// referenced because is not supported
148+
if r.TopologyRef != nil {
149+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
150+
errors = append(errors, err)
151+
}
152+
}
153+
return errors
154+
}
155+
130156
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
131157
func (r *OVNController) ValidateDelete() (admission.Warnings, error) {
132158
ovncontrollerlog.Info("validate delete", "name", r.Name)

api/v1beta1/ovndbcluster_types.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type OVNDBClusterStatus struct {
161161
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
162162

163163
// LastAppliedTopology - the last applied Topology
164-
LastAppliedTopology string `json:"lastAppliedTopology,omitempty"`
164+
LastAppliedTopology *topologyv1.TopoRef `json:"lastAppliedTopology,omitempty"`
165165
}
166166

167167
//+kubebuilder:object:root=true
@@ -229,3 +229,18 @@ func (instance OVNDBCluster) GetExternalEndpoint() (string, error) {
229229
}
230230
return instance.Status.DBAddress, nil
231231
}
232+
233+
// GetSpecTopologyRef - Returns the LastAppliedTopology Set in the Status
234+
func (instance *OVNDBCluster) GetSpecTopologyRef() *topologyv1.TopoRef {
235+
return instance.Spec.TopologyRef
236+
}
237+
238+
// GetLastAppliedTopology - Returns the LastAppliedTopology Set in the Status
239+
func (instance *OVNDBCluster) GetLastAppliedTopology() *topologyv1.TopoRef {
240+
return instance.Status.LastAppliedTopology
241+
}
242+
243+
// SetLastAppliedTopology - Sets the LastAppliedTopology value in the Status
244+
func (instance *OVNDBCluster) SetLastAppliedTopology(topologyRef *topologyv1.TopoRef) {
245+
instance.Status.LastAppliedTopology = topologyRef
246+
}

api/v1beta1/ovndbcluster_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *OVNDBCluster) ValidateCreate() (admission.Warnings, error) {
107107
}
108108
if len(errors) != 0 {
109109
return nil, apierrors.NewInvalid(
110-
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
110+
schema.GroupKind{Group: "ovn.openstack.org", Kind: "OVNDBCluster"},
111111
r.Name, errors)
112112
}
113113
return nil, nil
@@ -129,7 +129,7 @@ func (r *OVNDBCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, e
129129
}
130130
if len(errors) != 0 {
131131
return nil, apierrors.NewInvalid(
132-
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
132+
schema.GroupKind{Group: "ovn.openstack.org", Kind: "OVNDBCluster"},
133133
r.Name, errors)
134134
}
135135
return nil, nil

api/v1beta1/ovnnorthd_types.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type OVNNorthdStatus struct {
9797
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
9898

9999
// LastAppliedTopology - the last applied Topology
100-
LastAppliedTopology string `json:"lastAppliedTopology,omitempty"`
100+
LastAppliedTopology *topologyv1.TopoRef `json:"lastAppliedTopology,omitempty"`
101101
}
102102

103103
//+kubebuilder:object:root=true
@@ -148,3 +148,18 @@ func (instance OVNNorthd) RbacNamespace() string {
148148
func (instance OVNNorthd) RbacResourceName() string {
149149
return "ovnnorthd-" + instance.Name
150150
}
151+
152+
// GetSpecTopologyRef - Returns the LastAppliedTopology Set in the Status
153+
func (instance *OVNNorthd) GetSpecTopologyRef() *topologyv1.TopoRef {
154+
return instance.Spec.TopologyRef
155+
}
156+
157+
// GetLastAppliedTopology - Returns the LastAppliedTopology Set in the Status
158+
func (instance *OVNNorthd) GetLastAppliedTopology() *topologyv1.TopoRef {
159+
return instance.Status.LastAppliedTopology
160+
}
161+
162+
// SetLastAppliedTopology - Sets the LastAppliedTopology value in the Status
163+
func (instance *OVNNorthd) SetLastAppliedTopology(topologyRef *topologyv1.TopoRef) {
164+
instance.Status.LastAppliedTopology = topologyRef
165+
}

0 commit comments

Comments
 (0)