forked from openstack-k8s-operators/openstack-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenstackdataplanenodeset_webhook_test.go
More file actions
238 lines (215 loc) · 8.68 KB
/
Copy pathopenstackdataplanenodeset_webhook_test.go
File metadata and controls
238 lines (215 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package functional
import (
"fmt"
"os"
. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports
. "github.com/onsi/gomega" //revive:disable:dot-imports
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
v1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/dataplane/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
var _ = Describe("DataplaneNodeSet Webhook", func() {
var dataplaneNodeSetName types.NamespacedName
var dataplaneDeploymentName types.NamespacedName
BeforeEach(func() {
dataplaneNodeSetName = types.NamespacedName{
Name: "edpm-compute-nodeset",
Namespace: namespace,
}
dataplaneDeploymentName = types.NamespacedName{
Name: "edpm-deployment",
Namespace: namespace,
}
err := os.Setenv("OPERATOR_SERVICES", "../../../config/services")
Expect(err).NotTo(HaveOccurred())
})
When("User tries to change forbidden items in the baremetalSetTemplate", func() {
BeforeEach(func() {
nodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
nodeSetSpec["preProvisioned"] = false
nodeSetSpec["nodes"] = map[string]interface{}{
"compute-0": map[string]interface{}{
"hostName": "compute-0"},
}
nodeSetSpec["baremetalSetTemplate"] = map[string]interface{}{
"bmhLabelSelector": map[string]string{
"app": "test-openstack",
},
}
DeferCleanup(th.DeleteInstance, CreateDataplaneNodeSet(dataplaneNodeSetName, nodeSetSpec))
})
It("Should block changes to the BmhLabelSelector object in baremetalSetTemplate spec", func() {
Eventually(func(_ Gomega) string {
instance := GetDataplaneNodeSet(dataplaneNodeSetName)
if instance.Spec.BaremetalSetTemplate != nil {
instance.Spec.BaremetalSetTemplate.BmhLabelSelector = map[string]string{
"app": "openstack1",
}
}
err := th.K8sClient.Update(th.Ctx, instance)
return fmt.Sprintf("%s", err)
}).Should(ContainSubstring("Forbidden: cannot change"))
})
})
When("A user changes an allowed field in the baremetalSetTemplate", func() {
BeforeEach(func() {
nodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
nodeSetSpec["preProvisioned"] = false
nodeSetSpec["baremetalSetTemplate"] = map[string]interface{}{
"cloudUserName": "test-user",
"bmhLabelSelector": map[string]string{
"app": "test-openstack",
},
"baremetalHosts": map[string]interface{}{
"compute-0": map[string]interface{}{
"ctlPlaneIP": "192.168.1.12/24",
},
},
}
DeferCleanup(th.DeleteInstance, CreateDataplaneNodeSet(dataplaneNodeSetName, nodeSetSpec))
})
It("Should allow changes to the CloudUserName", func() {
Eventually(func(_ Gomega) error {
instance := GetDataplaneNodeSet(dataplaneNodeSetName)
if instance.Spec.BaremetalSetTemplate != nil {
instance.Spec.BaremetalSetTemplate.CloudUserName = "new-user"
instance.Spec.BaremetalSetTemplate.BmhLabelSelector = map[string]string{
"app": "test-openstack",
}
}
return th.K8sClient.Update(th.Ctx, instance)
}).Should(Succeed())
})
})
When("domainName in baremetalSetTemplate", func() {
BeforeEach(func() {
nodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
nodeSetSpec["preProvisioned"] = false
nodeSetSpec["nodes"] = map[string]interface{}{
"compute-0": map[string]interface{}{
"hostName": "compute-0"},
}
nodeSetSpec["baremetalSetTemplate"] = map[string]interface{}{
"domainName": "example.com",
"bmhLabelSelector": map[string]string{
"app": "test-openstack",
},
"baremetalHosts": map[string]interface{}{
"compute-0": map[string]interface{}{
"ctlPlaneIP": "192.168.1.12/24",
},
},
}
DeferCleanup(th.DeleteInstance, CreateDataplaneNodeSet(dataplaneNodeSetName, nodeSetSpec))
})
It("hostName should be fqdn", func() {
instance := GetDataplaneNodeSet(dataplaneNodeSetName)
Expect(instance.Spec.Nodes["compute-0"].HostName).Should(Equal(
"compute-0.example.com"))
})
})
When("A user tries to redeclare an existing node in a new NodeSet", func() {
BeforeEach(func() {
nodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
nodeSetSpec["preProvisioned"] = true
nodeSetSpec["nodes"] = map[string]interface{}{
"compute-0": map[string]interface{}{
"hostName": "compute-0"},
}
DeferCleanup(th.DeleteInstance, CreateDataplaneNodeSet(dataplaneNodeSetName, nodeSetSpec))
})
It("Should block duplicate node declaration", func() {
Eventually(func(_ Gomega) string {
newNodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
newNodeSetSpec["preProvisioned"] = true
newNodeSetSpec["nodes"] = map[string]interface{}{
"compute-0": map[string]interface{}{
"hostName": "compute-0"},
}
newInstance := DefaultDataplaneNodeSetTemplate(types.NamespacedName{Name: "test-duplicate-node", Namespace: namespace}, newNodeSetSpec)
unstructuredObj := &unstructured.Unstructured{Object: newInstance}
_, err := controllerutil.CreateOrPatch(
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
return fmt.Sprintf("%s", err)
}).Should(ContainSubstring("already exists in another cluster"))
})
It("Should block NodeSets if they contain a duplicate ansibleHost", func() {
Eventually(func(_ Gomega) string {
newNodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
newNodeSetSpec["preProvisioned"] = true
newNodeSetSpec["nodes"] = map[string]interface{}{
"compute-3": map[string]interface{}{
"hostName": "compute-3",
"ansible": map[string]interface{}{
"ansibleHost": "compute-3",
},
},
"compute-2": map[string]interface{}{
"hostName": "compute-2"},
"compute-8": map[string]interface{}{
"hostName": "compute-8"},
"compute-0": map[string]interface{}{
"ansible": map[string]interface{}{
"ansibleHost": "compute-0",
},
},
}
newInstance := DefaultDataplaneNodeSetTemplate(types.NamespacedName{Name: "test-nodeset-with-duplicate-node", Namespace: namespace}, newNodeSetSpec)
unstructuredObj := &unstructured.Unstructured{Object: newInstance}
_, err := controllerutil.CreateOrPatch(
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
return fmt.Sprintf("%s", err)
}).Should(ContainSubstring("already exists in another cluster"))
})
})
When("A NodeSet is updated with a OpenStackDataPlaneDeployment", func() {
BeforeEach(func() {
nodeSetSpec := DefaultDataPlaneNoNodeSetSpec(false)
nodeSetSpec["preProvisioned"] = true
nodeSetSpec["nodes"] = map[string]interface{}{
"compute-0": map[string]interface{}{
"hostName": "compute-0"},
}
DeferCleanup(th.DeleteInstance, CreateDataplaneNodeSet(dataplaneNodeSetName, nodeSetSpec))
DeferCleanup(th.DeleteInstance, CreateDataplaneDeployment(dataplaneDeploymentName, DefaultDataPlaneDeploymentSpec()))
})
It("Should allow for NodeSet updates if Deployment is Completed", func() {
Eventually(func(g Gomega) error {
instance := GetDataplaneNodeSet(dataplaneNodeSetName)
instance.Spec.NodeTemplate.Ansible = v1beta1.AnsibleOpts{
AnsibleUser: "random-user",
}
deploymentReadyConditions := condition.Conditions{}
deploymentReadyConditions.MarkTrue(
v1beta1.NodeSetDeploymentReadyCondition,
condition.ReadyMessage)
instance.Status.DeploymentStatuses = make(map[string]condition.Conditions)
instance.Status.DeploymentStatuses[dataplaneDeploymentName.Name] = deploymentReadyConditions
g.Expect(th.K8sClient.Status().Update(th.Ctx, instance)).To(Succeed())
return th.K8sClient.Update(th.Ctx, instance)
}).Should(Succeed())
})
It("Should block NodeSet updates if Deployment is NOT completed", func() {
Eventually(func(g Gomega) string {
instance := GetDataplaneNodeSet(dataplaneNodeSetName)
deploymentReadyConditions := condition.Conditions{}
deploymentReadyConditions.MarkFalse(
v1beta1.NodeSetDeploymentReadyCondition,
"mock-error",
condition.SeverityWarning,
condition.ReadyMessage)
instance.Status.DeploymentStatuses = make(map[string]condition.Conditions)
instance.Status.DeploymentStatuses[dataplaneDeploymentName.Name] = deploymentReadyConditions
g.Expect(th.K8sClient.Status().Update(th.Ctx, instance)).To(Succeed())
instance.Spec.NodeTemplate.Ansible = v1beta1.AnsibleOpts{
AnsibleUser: "random-user",
}
err := th.K8sClient.Update(th.Ctx, instance)
return fmt.Sprintf("%s", err)
}).Should(ContainSubstring(fmt.Sprintf("could not patch openstackdataplanenodeset while openstackdataplanedeployment %s (blocked on %s condition) is running",
dataplaneDeploymentName.Name, string(v1beta1.NodeSetDeploymentReadyCondition))))
})
})
})