Skip to content

Commit d695e75

Browse files
Merge pull request #2108 from abays/fix/nodeset-duplicate-validation-race
fix(dataplane): use live reads for NodeSet duplicate validation
2 parents dbcf86b + ee5a9f3 commit d695e75

3 files changed

Lines changed: 25 additions & 26 deletions

File tree

‎api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (spec *OpenStackDataPlaneNodeSetSpec) Default() {
7575
}
7676

7777
// ValidateCreate validates the OpenStackDataPlaneNodeSet on creation
78-
func (r *OpenStackDataPlaneNodeSet) ValidateCreate(ctx context.Context, c client.Client) (admission.Warnings, error) {
78+
func (r *OpenStackDataPlaneNodeSet) ValidateCreate(ctx context.Context, c client.Reader) (admission.Warnings, error) {
7979
openstackdataplanenodesetlog.Info("validate create", "name", r.Name)
8080
var errors field.ErrorList
8181
errors, err := r.validateNodes(ctx, c)
@@ -114,7 +114,7 @@ func (r *OpenStackDataPlaneNodeSet) ValidateCreate(ctx context.Context, c client
114114
return nil, nil
115115
}
116116

117-
func (r *OpenStackDataPlaneNodeSet) validateNodes(ctx context.Context, c client.Client) (field.ErrorList, error) {
117+
func (r *OpenStackDataPlaneNodeSet) validateNodes(ctx context.Context, c client.Reader) (field.ErrorList, error) {
118118
var errors field.ErrorList
119119
nodeSetList := &OpenStackDataPlaneNodeSetList{}
120120
opts := &client.ListOptions{
@@ -143,7 +143,7 @@ func (r *OpenStackDataPlaneNodeSet) validateNodes(ctx context.Context, c client.
143143
}
144144

145145
// ValidateUpdate validates the OpenStackDataPlaneNodeSet on update
146-
func (r *OpenStackDataPlaneNodeSet) ValidateUpdate(ctx context.Context, old runtime.Object, c client.Client) (admission.Warnings, error) {
146+
func (r *OpenStackDataPlaneNodeSet) ValidateUpdate(ctx context.Context, old runtime.Object, c client.Reader) (admission.Warnings, error) {
147147
openstackdataplanenodesetlog.Info("validate update", "name", r.Name)
148148
oldNodeSet, ok := old.(*OpenStackDataPlaneNodeSet)
149149
if !ok {
@@ -209,7 +209,7 @@ func (spec *OpenStackDataPlaneNodeSetSpec) ValidateUpdate(oldSpec *OpenStackData
209209
}
210210

211211
// ValidateDelete validates the OpenStackDataPlaneNodeSet on deletion
212-
func (r *OpenStackDataPlaneNodeSet) ValidateDelete(ctx context.Context, c client.Client) (admission.Warnings, error) {
212+
func (r *OpenStackDataPlaneNodeSet) ValidateDelete(ctx context.Context, c client.Reader) (admission.Warnings, error) {
213213
openstackdataplanenodesetlog.Info("validate delete", "name", r.Name)
214214
errors := r.Spec.ValidateDelete()
215215

‎internal/webhook/dataplane/v1beta1/openstackdataplanenodeset_webhook.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ import (
3434
// log is for logging in this package.
3535
var openstackdataplanenodesetlog = logf.Log.WithName("openstackdataplanenodeset-resource")
3636

37-
var webhookClient client.Client
37+
var webhookReader client.Reader
3838

3939
// SetupOpenStackDataPlaneNodeSetWebhookWithManager registers the webhook for OpenStackDataPlaneNodeSet in the manager.
4040
func SetupOpenStackDataPlaneNodeSetWebhookWithManager(mgr ctrl.Manager) error {
41-
if webhookClient == nil {
42-
webhookClient = mgr.GetClient()
41+
if webhookReader == nil {
42+
// NodeSet validation must read current objects, not potentially stale cache data.
43+
webhookReader = mgr.GetAPIReader()
4344
}
4445

4546
return ctrl.NewWebhookManagedBy(mgr).For(&dataplanev1beta1.OpenStackDataPlaneNodeSet{}).
@@ -101,7 +102,7 @@ func (v *OpenStackDataPlaneNodeSetCustomValidator) ValidateCreate(ctx context.Co
101102
openstackdataplanenodesetlog.Info("Validation for OpenStackDataPlaneNodeSet upon creation", "name", openstackdataplanenodeset.GetName())
102103

103104
// Call the ValidateCreate method on the OpenStackDataPlaneNodeSet type
104-
return openstackdataplanenodeset.ValidateCreate(ctx, webhookClient)
105+
return openstackdataplanenodeset.ValidateCreate(ctx, webhookReader)
105106
}
106107

107108
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type OpenStackDataPlaneNodeSet.
@@ -113,7 +114,7 @@ func (v *OpenStackDataPlaneNodeSetCustomValidator) ValidateUpdate(ctx context.Co
113114
openstackdataplanenodesetlog.Info("Validation for OpenStackDataPlaneNodeSet upon update", "name", openstackdataplanenodeset.GetName())
114115

115116
// Call the ValidateUpdate method on the OpenStackDataPlaneNodeSet type
116-
return openstackdataplanenodeset.ValidateUpdate(ctx, oldObj, webhookClient)
117+
return openstackdataplanenodeset.ValidateUpdate(ctx, oldObj, webhookReader)
117118
}
118119

119120
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type OpenStackDataPlaneNodeSet.
@@ -125,5 +126,5 @@ func (v *OpenStackDataPlaneNodeSetCustomValidator) ValidateDelete(ctx context.Co
125126
openstackdataplanenodesetlog.Info("Validation for OpenStackDataPlaneNodeSet upon deletion", "name", openstackdataplanenodeset.GetName())
126127

127128
// Call the ValidateDelete method on the OpenStackDataPlaneNodeSet type
128-
return openstackdataplanenodeset.ValidateDelete(ctx, webhookClient)
129+
return openstackdataplanenodeset.ValidateDelete(ctx, webhookReader)
129130
}

‎test/functional/dataplane/openstackdataplanenodeset_webhook_test.go‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,21 @@ var _ = Describe("DataplaneNodeSet Webhook", func() {
140140
})
141141

142142
It("Should block duplicate node declaration", func() {
143-
Eventually(func(_ Gomega) string {
144-
newNodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
145-
newNodeSetSpec["preProvisioned"] = true
146-
newNodeSetSpec["nodes"] = map[string]interface{}{
147-
"compute-0": map[string]interface{}{
148-
"hostName": "compute-0",
149-
"ansible": map[string]interface{}{
150-
"ansibleHost": "192.168.122.100",
151-
},
143+
newNodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
144+
newNodeSetSpec["preProvisioned"] = true
145+
newNodeSetSpec["nodes"] = map[string]interface{}{
146+
"compute-0": map[string]interface{}{
147+
"hostName": "compute-0",
148+
"ansible": map[string]interface{}{
149+
"ansibleHost": "192.168.122.100",
152150
},
153-
}
154-
newInstance := DefaultDataplaneNodeSetTemplate(types.NamespacedName{Name: "test-duplicate-node", Namespace: namespace}, newNodeSetSpec)
155-
unstructuredObj := &unstructured.Unstructured{Object: newInstance}
156-
_, err := controllerutil.CreateOrPatch(
157-
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
158-
return fmt.Sprintf("%s", err)
159-
}).Should(ContainSubstring("already exists in another cluster"))
151+
},
152+
}
153+
newInstance := DefaultDataplaneNodeSetTemplate(types.NamespacedName{Name: "test-duplicate-node", Namespace: namespace}, newNodeSetSpec)
154+
unstructuredObj := &unstructured.Unstructured{Object: newInstance}
155+
err := th.K8sClient.Create(th.Ctx, unstructuredObj)
156+
Expect(err).To(HaveOccurred())
157+
Expect(err.Error()).To(ContainSubstring("already exists in another cluster"))
160158
})
161159

162160
It("Should block NodeSets if they contain a duplicate ansibleHost", func() {

0 commit comments

Comments
 (0)