Skip to content

Commit 1c692ab

Browse files
committed
Fix duplicateNode check
- Check for both create and update/patch validation - Check for duplicates in the spec itself Signed-off-by: rabi <ramishra@redhat.com>
1 parent 0f3d544 commit 1c692ab

2 files changed

Lines changed: 38 additions & 25 deletions

File tree

‎apis/dataplane/v1beta1/openstackdataplanenodeset_types.go‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,33 @@ func getStrPtr(in string) *string {
268268

269269
// duplicateNodeCheck checks the NodeSetList for pre-existing nodes. If the user is trying to redefine an
270270
// existing node, we will return an error and block resource creation.
271-
func (r *OpenStackDataPlaneNodeSetSpec) duplicateNodeCheck(nodeSetList *OpenStackDataPlaneNodeSetList) (errors field.ErrorList) {
271+
func (r *OpenStackDataPlaneNodeSetSpec) duplicateNodeCheck(nodeSetList *OpenStackDataPlaneNodeSetList, nodesetName string) (errors field.ErrorList) {
272272
existingNodeNames := make([]string, 0)
273-
for _, existingNode := range nodeSetList.Items {
274-
for _, node := range existingNode.Spec.Nodes {
273+
for _, nodeSet := range nodeSetList.Items {
274+
if nodeSet.ObjectMeta.Name == nodesetName {
275+
continue
276+
}
277+
for _, node := range nodeSet.Spec.Nodes {
275278
existingNodeNames = append(existingNodeNames, node.HostName)
276279
if node.Ansible.AnsibleHost != "" {
277280
existingNodeNames = append(existingNodeNames, node.Ansible.AnsibleHost)
278281
}
279282
}
283+
280284
}
281285

282-
for _, newNodeName := range r.Nodes {
283-
if slices.Contains(existingNodeNames, newNodeName.HostName) || slices.Contains(existingNodeNames, newNodeName.Ansible.AnsibleHost) {
286+
for _, newNode := range r.Nodes {
287+
if slices.Contains(existingNodeNames, newNode.HostName) || slices.Contains(existingNodeNames, newNode.Ansible.AnsibleHost) {
284288
errors = append(errors, field.Invalid(
285289
field.NewPath("spec").Child("nodes"),
286-
newNodeName,
287-
fmt.Sprintf("node %s already exists in another cluster", newNodeName.HostName)))
290+
newNode,
291+
fmt.Sprintf("node %s already exists in another cluster", newNode.HostName)))
292+
} else {
293+
existingNodeNames = append(existingNodeNames, newNode.HostName)
294+
if newNode.Ansible.AnsibleHost != "" {
295+
existingNodeNames = append(existingNodeNames, newNode.Ansible.AnsibleHost)
296+
}
288297
}
289298
}
290-
291299
return
292300
}

‎apis/dataplane/v1beta1/openstackdataplanenodeset_webhook.go‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,21 @@ var _ webhook.Validator = &OpenStackDataPlaneNodeSet{}
108108
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
109109
func (r *OpenStackDataPlaneNodeSet) ValidateCreate() (admission.Warnings, error) {
110110
openstackdataplanenodesetlog.Info("validate create", "name", r.Name)
111-
112111
var errors field.ErrorList
113-
114-
nodeSetList := &OpenStackDataPlaneNodeSetList{}
115-
opts := &client.ListOptions{
116-
Namespace: r.ObjectMeta.Namespace,
117-
}
118-
119-
err := webhookClient.List(context.TODO(), nodeSetList, opts)
112+
errors, err := r.validateNodes()
120113
if err != nil {
121114
return nil, err
122115
}
116+
123117
// Check if OpenStackDataPlaneNodeSet name matches RFC1123 for use in labels
124118
validate := validator.New()
125-
if err = validate.Var(r.Name, "hostname_rfc1123"); err != nil {
119+
if err := validate.Var(r.Name, "hostname_rfc1123"); err != nil {
126120
openstackdataplanenodesetlog.Error(err, "Error validating OpenStackDataPlaneNodeSet name, name must follow RFC1123")
127121
errors = append(errors, field.Invalid(
128122
field.NewPath("Name"),
129123
r.Name,
130124
fmt.Sprintf("Error validating OpenStackDataPlaneNodeSet name %s, name must follow RFC1123", r.Name)))
131125
}
132-
133-
errors = append(errors, r.Spec.ValidateCreate(nodeSetList)...)
134-
135126
if len(errors) > 0 {
136127
openstackdataplanenodesetlog.Info("validation failed", "name", r.Name)
137128

@@ -143,17 +134,27 @@ func (r *OpenStackDataPlaneNodeSet) ValidateCreate() (admission.Warnings, error)
143134
return nil, nil
144135
}
145136

146-
func (r *OpenStackDataPlaneNodeSetSpec) ValidateCreate(nodeSetList *OpenStackDataPlaneNodeSetList) field.ErrorList {
137+
func (r *OpenStackDataPlaneNodeSet) validateNodes() (field.ErrorList, error) {
147138
var errors field.ErrorList
139+
nodeSetList := &OpenStackDataPlaneNodeSetList{}
140+
opts := &client.ListOptions{
141+
Namespace: r.ObjectMeta.Namespace,
142+
}
143+
144+
err := webhookClient.List(context.TODO(), nodeSetList, opts)
145+
if err != nil {
146+
return nil, err
147+
}
148+
148149
// Currently, this check is only valid for PreProvisioned nodes. Since we can't possibly
149150
// have duplicates in Baremetal Deployments, we can exit early here for Baremetal NodeSets.
150151
// If this is the first NodeSet being created, then there can be no duplicates
151152
// we can exit early here.
152-
if r.PreProvisioned && len(nodeSetList.Items) != 0 {
153-
errors = append(errors, r.duplicateNodeCheck(nodeSetList)...)
153+
if r.Spec.PreProvisioned && len(nodeSetList.Items) != 0 {
154+
errors = append(errors, r.Spec.duplicateNodeCheck(nodeSetList, r.ObjectMeta.Name)...)
154155
}
155156

156-
return errors
157+
return errors, nil
157158

158159
}
159160

@@ -165,8 +166,12 @@ func (r *OpenStackDataPlaneNodeSet) ValidateUpdate(old runtime.Object) (admissio
165166
return nil, apierrors.NewInternalError(
166167
fmt.Errorf("expected a OpenStackDataPlaneNodeSet object, but got %T", oldNodeSet))
167168
}
169+
errors, err := r.validateNodes()
170+
if err != nil {
171+
return nil, err
172+
}
168173

169-
errors := r.Spec.ValidateUpdate(&oldNodeSet.Spec)
174+
errors = append(errors, r.Spec.ValidateUpdate(&oldNodeSet.Spec)...)
170175

171176
if errors != nil {
172177
openstackdataplanenodesetlog.Info("validation failed", "name", r.Name)

0 commit comments

Comments
 (0)