forked from open-cluster-management-io/ocm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplacement_test.go
362 lines (323 loc) · 11.5 KB
/
placement_test.go
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package e2e
import (
"context"
"fmt"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/rand"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/test/integration/util"
)
const (
e2eTestLabel = "created-by"
e2eTestLabelValue = "placement-e2e-test"
maxNumOfClusterDecisions = 100
)
// Test cases with lable "sanity-check" could be ran as sanity check on an existing environment with
// placement controller installed and well configured . Resource leftovers should be cleaned up on
// the hub cluster.
var _ = ginkgo.Describe("Placement", ginkgo.Label("placement", "sanity-check"), func() {
var namespace string
var placementName string
var clusterSet1Name string
var clusterSetGlobal string
var suffix string
var err error
ginkgo.BeforeEach(func() {
suffix = rand.String(5)
namespace = fmt.Sprintf("ns-%s", suffix)
placementName = fmt.Sprintf("placement-%s", suffix)
clusterSet1Name = fmt.Sprintf("clusterset-%s", suffix)
clusterSetGlobal = "global"
// create testing namespace
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: map[string]string{
e2eTestLabel: e2eTestLabelValue,
},
},
}
_, err := hub.KubeClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
ginkgo.AfterEach(func() {
var errs []error
ginkgo.By("Delete managedclustersets")
err := hub.ClusterClient.ClusterV1beta2().ManagedClusterSets().DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: e2eTestLabel + "=" + e2eTestLabelValue,
})
if err != nil {
errs = append(errs, err)
}
ginkgo.By("Delete managedclusters")
err = hub.ClusterClient.ClusterV1().ManagedClusters().DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: e2eTestLabel + "=" + e2eTestLabelValue,
})
if err != nil {
errs = append(errs, err)
}
ginkgo.By("Delete namespace")
err = hub.KubeClient.CoreV1().Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
if err != nil {
errs = append(errs, err)
}
gomega.Expect(utilerrors.NewAggregate(errs)).ToNot(gomega.HaveOccurred())
})
assertPlacementDecisionCreated := func(placement *clusterapiv1beta1.Placement) {
ginkgo.By("Check if placementdecision is created")
gomega.Eventually(func() bool {
pdl, err := hub.ClusterClient.ClusterV1beta1().PlacementDecisions(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: clusterapiv1beta1.PlacementLabel + "=" + placement.Name,
})
if err != nil {
return false
}
if len(pdl.Items) == 0 {
return false
}
for _, pd := range pdl.Items {
objectMeta := pd.ObjectMeta
if controlled := metav1.IsControlledBy(&objectMeta, placement); !controlled {
return false
}
}
return true
}).Should(gomega.BeTrue())
}
assertNumberOfDecisions := func(placementName string, desiredNOD int) {
ginkgo.By("Check the number of decisions in placementdecisions")
desiredNOPD := desiredNOD/maxNumOfClusterDecisions + 1
gomega.Eventually(func() bool {
pdl, err := hub.ClusterClient.ClusterV1beta1().PlacementDecisions(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: clusterapiv1beta1.PlacementLabel + "=" + placementName,
})
if err != nil {
return false
}
if len(pdl.Items) != desiredNOPD {
return false
}
actualNOD := 0
for _, pd := range pdl.Items {
actualNOD += len(pd.Status.Decisions)
}
return actualNOD == desiredNOD
}).Should(gomega.BeTrue())
}
assertPlacementStatus := func(placementName string, numOfSelectedClusters int, satisfied bool) {
ginkgo.By("Check the status of placement")
gomega.Eventually(func() bool {
placement, err := hub.ClusterClient.ClusterV1beta1().Placements(namespace).Get(context.Background(), placementName, metav1.GetOptions{})
if err != nil {
return false
}
status := metav1.ConditionFalse
if satisfied {
status = metav1.ConditionTrue
}
if !util.HasCondition(
placement.Status.Conditions,
clusterapiv1beta1.PlacementConditionSatisfied,
"",
status,
) {
return false
}
if !util.HasCondition(
placement.Status.Conditions,
clusterapiv1beta1.PlacementConditionMisconfigured,
"Succeedconfigured",
metav1.ConditionFalse,
) {
return false
}
return placement.Status.NumberOfSelectedClusters == int32(numOfSelectedClusters)
}).Should(gomega.BeTrue())
}
assertCreatingClusterSet := func(clusterSetName string, matchLabel map[string]string) {
ginkgo.By("Create clusterset")
clusterset := &clusterapiv1beta2.ManagedClusterSet{
ObjectMeta: metav1.ObjectMeta{
Name: clusterSetName,
Labels: map[string]string{
e2eTestLabel: e2eTestLabelValue,
},
},
}
if matchLabel != nil {
clusterset.Spec.ClusterSelector = clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.LabelSelector,
LabelSelector: &metav1.LabelSelector{
MatchLabels: matchLabel,
},
}
}
_, err = hub.ClusterClient.ClusterV1beta2().ManagedClusterSets().Create(context.Background(), clusterset, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
assertCreatingClusterSetBinding := func(clusterSetName string) {
ginkgo.By("Create clustersetbinding")
csb := &clusterapiv1beta2.ManagedClusterSetBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: clusterSetName,
Labels: map[string]string{
e2eTestLabel: e2eTestLabelValue,
},
},
Spec: clusterapiv1beta2.ManagedClusterSetBindingSpec{
ClusterSet: clusterSetName,
},
}
_, err = hub.ClusterClient.ClusterV1beta2().ManagedClusterSetBindings(namespace).Create(context.Background(), csb, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
assertBindingClusterSet := func(clusterSetName string, matchLabel map[string]string) {
ginkgo.By("Create clusterset/clustersetbinding")
assertCreatingClusterSet(clusterSetName, matchLabel)
assertCreatingClusterSetBinding(clusterSetName)
}
assertCreatingClusters := func(clusterSetName string, num int) {
ginkgo.By(fmt.Sprintf("Create %d clusters", num))
for i := 0; i < num; i++ {
labels := map[string]string{
e2eTestLabel: e2eTestLabelValue,
}
if len(clusterSetName) > 0 {
labels[clusterapiv1beta2.ClusterSetLabel] = clusterSetName
}
cluster := &clusterapiv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "cluster-",
Labels: labels,
},
}
_, err = hub.ClusterClient.ClusterV1().ManagedClusters().Create(context.Background(), cluster, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
}
assertCreatingPlacement := func(name string, noc *int32, nod int) {
ginkgo.By("Create placement")
placement := &clusterapiv1beta1.Placement{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: map[string]string{
e2eTestLabel: e2eTestLabelValue,
},
},
Spec: clusterapiv1beta1.PlacementSpec{
NumberOfClusters: noc,
Tolerations: []clusterapiv1beta1.Toleration{
{
Key: clusterapiv1.ManagedClusterTaintUnreachable,
},
},
},
}
placement, err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Create(context.Background(), placement, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
assertPlacementDecisionCreated(placement)
assertNumberOfDecisions(placementName, nod)
if noc != nil {
assertPlacementStatus(placementName, nod, nod == int(*noc))
}
}
ginkgo.It("Should schedule successfully", func() {
assertBindingClusterSet(clusterSet1Name, nil)
assertCreatingClusters(clusterSet1Name, 5)
assertCreatingPlacement(placementName, noc(10), 5)
ginkgo.By("Reduce NOC of the placement")
gomega.Eventually(func() error {
placement, err := hub.ClusterClient.ClusterV1beta1().Placements(namespace).Get(context.Background(), placementName, metav1.GetOptions{})
if err != nil {
return err
}
noc := int32(6)
placement.Spec.NumberOfClusters = &noc
_, err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Update(context.Background(), placement, metav1.UpdateOptions{})
return err
}).ShouldNot(gomega.HaveOccurred())
assertNumberOfDecisions(placementName, 5)
assertPlacementStatus(placementName, 5, false)
// create global clusterset if necessary
assertCreatingClusterSetBinding(clusterSetGlobal)
// create 2 more clusters belong to global clusterset
assertCreatingClusters("", 2)
assertNumberOfDecisions(placementName, 6)
assertPlacementStatus(placementName, 6, true)
ginkgo.By("Delete placement")
err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Delete(context.TODO(), placementName, metav1.DeleteOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
ginkgo.By("Check if placementdecisions are deleted as well")
gomega.Eventually(func() bool {
placementDecisions, err := hub.ClusterClient.ClusterV1beta1().PlacementDecisions(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", clusterapiv1beta1.PlacementLabel, placementName),
})
if err != nil {
return false
}
return len(placementDecisions.Items) == 0
}).Should(gomega.BeTrue())
})
ginkgo.It("Should delete placementdecision successfully", func() {
assertBindingClusterSet(clusterSet1Name, nil)
assertCreatingClusters(clusterSet1Name, 1)
assertCreatingPlacement(placementName, nil, 1)
ginkgo.By("Add cluster predicate")
gomega.Eventually(func() error {
placement, err := hub.ClusterClient.ClusterV1beta1().Placements(namespace).Get(context.Background(), placementName, metav1.GetOptions{})
if err != nil {
return err
}
placement.Spec.Predicates = []clusterapiv1beta1.ClusterPredicate{
{
RequiredClusterSelector: clusterapiv1beta1.ClusterSelector{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"a": "b",
},
},
},
},
}
_, err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Update(context.Background(), placement, metav1.UpdateOptions{})
return err
}).ShouldNot(gomega.HaveOccurred())
ginkgo.By("Create empty placement decision")
assertNumberOfDecisions(placementName, 0)
assertPlacementStatus(placementName, 0, false)
ginkgo.By("Delete placement")
err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Delete(context.TODO(), placementName, metav1.DeleteOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
//update this case when version upgrade
ginkgo.It("Should support v1beta1 placement", func() {
ginkgo.By("Create v1beta1 placement with v1beta1 client")
placement := &clusterapiv1beta1.Placement{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "test",
},
Spec: clusterapiv1beta1.PlacementSpec{
Tolerations: []clusterapiv1beta1.Toleration{
{
Key: clusterapiv1.ManagedClusterTaintUnreachable,
},
},
},
}
_, err = hub.ClusterClient.ClusterV1beta1().Placements(namespace).Create(context.Background(), placement, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
})
func noc(n int) *int32 {
noc := int32(n)
return &noc
}