@@ -32,142 +32,58 @@ import (
32
32
33
33
var _ = Describe ("Test defaults" , func () {
34
34
const (
35
- testConfigmap = "configmap1 "
35
+ testObject = "object1 "
36
36
testNamespace = "default"
37
37
)
38
38
39
39
var (
40
- objectGetter = func () client.Object {
41
- return & corev1.ConfigMap {}
42
- }
43
40
req = reconcile.Request {NamespacedName : client.ObjectKey {
44
- Name : testConfigmap ,
41
+ Name : testObject ,
45
42
Namespace : testNamespace ,
46
43
}}
47
44
)
48
45
49
- Context ("Use default workqueue priority" , func () {
50
- It ("Should get default workqueue priority if the item has no labels " , func () {
51
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , DefaultWorkQueuePriorityLabel , "" )
46
+ Context ("Use default workqueue priority label " , func () {
47
+ It ("Should get default priority if default workqueue priority label nil " , func () {
48
+ err := ensureNamespace (k8sClient , testNamespace , DefaultWorkQueuePriorityLabel , "" )
52
49
Expect (err ).NotTo (HaveOccurred ())
53
50
54
- getPriorityFunc := DefaultGetPriorityFuncBuilder (k8sClient , objectGetter )
51
+ getPriorityFunc := DefaultGetPriorityFuncBuilder (k8sClient )
55
52
priority := getPriorityFunc (req )
56
53
Expect (priority ).To (Equal (DefaultWorkQueuePriority ))
57
54
})
58
55
59
- It ("Should get workqueue priority from default item priority label" , func () {
60
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , DefaultWorkQueuePriorityLabel , "3" )
61
- Expect (err ).NotTo (HaveOccurred ())
62
-
63
- getPriorityFunc := DefaultGetPriorityFuncBuilder (k8sClient , objectGetter )
64
- priority := getPriorityFunc (req )
65
- Expect (priority ).To (Equal (3 ))
66
- })
67
-
68
- It ("Should get workqueue priority from default namesapce priority label" , func () {
69
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , DefaultWorkQueuePriorityLabel , "" )
70
- Expect (err ).NotTo (HaveOccurred ())
71
-
72
- err = ensureNamespace (k8sClient , testNamespace , DefaultWorkQueuePriorityLabel , "4" )
56
+ It ("Should get workqueue priority from default workqueue priority label" , func () {
57
+ err := ensureNamespace (k8sClient , testNamespace , DefaultWorkQueuePriorityLabel , "4" )
73
58
Expect (err ).NotTo (HaveOccurred ())
74
59
75
- getPriorityFunc := DefaultGetPriorityFuncBuilder (k8sClient , objectGetter )
60
+ getPriorityFunc := DefaultGetPriorityFuncBuilder (k8sClient )
76
61
priority := getPriorityFunc (req )
77
62
Expect (priority ).To (Equal (4 ))
78
63
})
79
64
})
80
65
81
- Context ("Use custom workqueue priority" , func () {
82
- It ("Should get default workqueue priority if the item has no labels " , func () {
83
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , "custom-priority" , "" )
66
+ Context ("Use custom workqueue priority label " , func () {
67
+ It ("Should get default priority if custom workqueue priority label nil " , func () {
68
+ err := ensureNamespace (k8sClient , testNamespace , "custom-priority" , "4 " )
84
69
Expect (err ).NotTo (HaveOccurred ())
85
70
86
- getPriorityFunc := GetPriorityFuncBuilder (k8sClient , objectGetter , "custom-priority" , 1 )
71
+ getPriorityFunc := GetPriorityFuncBuilder (k8sClient , "custom-priority" , 1 )
87
72
priority := getPriorityFunc (req )
88
- Expect (priority ).To (Equal (1 ))
89
- })
90
-
91
- It ("Should get workqueue priority from custom item priority label" , func () {
92
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , "custom-priority" , "3" )
93
- Expect (err ).NotTo (HaveOccurred ())
94
-
95
- getPriorityFunc := GetPriorityFuncBuilder (k8sClient , objectGetter , "custom-priority" , 1 )
96
- priority := getPriorityFunc (req )
97
- Expect (priority ).To (Equal (3 ))
73
+ Expect (priority ).To (Equal (4 ))
98
74
})
99
75
100
- It ("Should get workqueue priority from custom namesapce priority label" , func () {
101
- err := ensureConfigmap (k8sClient , testNamespace , testConfigmap , "custom-priority" , "" )
76
+ It ("Should get workqueue priority from custom workqueue priority label" , func () {
77
+ err := ensureNamespace (k8sClient , testNamespace , "custom-priority" , "4 " )
102
78
Expect (err ).NotTo (HaveOccurred ())
103
79
104
- err = ensureNamespace (k8sClient , testNamespace , "custom-priority" , "4" )
105
- Expect (err ).NotTo (HaveOccurred ())
106
-
107
- getPriorityFunc := GetPriorityFuncBuilder (k8sClient , objectGetter , "custom-priority" , 1 )
80
+ getPriorityFunc := GetPriorityFuncBuilder (k8sClient , "custom-priority" , 1 )
108
81
priority := getPriorityFunc (req )
109
82
Expect (priority ).To (Equal (4 ))
110
83
})
111
84
})
112
85
})
113
86
114
- func ensureConfigmap (cli client.Client , namespace , name , priorityLabelKey , priorityLabelValue string ) error {
115
- // Ensure the configmap exists
116
- configmap := & corev1.ConfigMap {}
117
- err := cli .Get (context .Background (), client.ObjectKey {Name : name , Namespace : namespace }, configmap )
118
- if err != nil {
119
- if errors .IsNotFound (err ) {
120
- labels := map [string ]string {}
121
- if priorityLabelValue != "" {
122
- labels [priorityLabelKey ] = priorityLabelValue
123
- }
124
-
125
- err := cli .Create (context .Background (), & corev1.ConfigMap {
126
- ObjectMeta : metav1.ObjectMeta {
127
- Name : name ,
128
- Namespace : namespace ,
129
- Labels : labels ,
130
- },
131
- })
132
- return err
133
- }
134
- return err
135
- }
136
-
137
- // If the label is already set, we don't need to update it
138
- labelValue , ok := configmap .Labels [priorityLabelKey ]
139
- if ! ok && priorityLabelValue == "" {
140
- return nil
141
- } else if ok && labelValue == priorityLabelValue {
142
- return nil
143
- }
144
-
145
- // If the label is not set, we need to set it
146
- if priorityLabelValue == "" {
147
- configmap .Labels = map [string ]string {}
148
- } else {
149
- configmap .Labels = map [string ]string {
150
- priorityLabelKey : priorityLabelValue ,
151
- }
152
- }
153
-
154
- // Update the configmap
155
- err = cli .Update (context .Background (), configmap )
156
- if err != nil {
157
- return err
158
- }
159
- Eventually (func () bool {
160
- configmap1 := & corev1.ConfigMap {}
161
- err := cli .Get (context .Background (), client.ObjectKey {Name : name , Namespace : namespace }, configmap1 )
162
- if err != nil {
163
- return false
164
- }
165
- return configmap1 .ResourceVersion >= configmap .ResourceVersion
166
- }, time .Second * 3 , time .Millisecond * 100 ).Should (BeTrue ())
167
-
168
- return nil
169
- }
170
-
171
87
func ensureNamespace (cli client.Client , name , priorityLabelKey , priorityLabelValue string ) error {
172
88
// Ensure the namespace exists
173
89
namespace := & corev1.Namespace {}
@@ -190,17 +106,16 @@ func ensureNamespace(cli client.Client, name, priorityLabelKey, priorityLabelVal
190
106
}
191
107
192
108
// If the label is already set, we don't need to update it
193
- labelValue , ok := namespace .Labels [priorityLabelKey ]
194
- if ! ok && priorityLabelValue == "" {
195
- return nil
196
- } else if ok && labelValue == priorityLabelValue {
197
- return nil
198
- }
199
-
200
- // If the label is not set, we need to set it
109
+ priority , ok := namespace .Labels [priorityLabelKey ]
201
110
if priorityLabelValue == "" {
111
+ if ! ok {
112
+ return nil
113
+ }
202
114
namespace .Labels = map [string ]string {}
203
115
} else {
116
+ if ok && priority == priorityLabelValue {
117
+ return nil
118
+ }
204
119
namespace .Labels = map [string ]string {
205
120
priorityLabelKey : priorityLabelValue ,
206
121
}
0 commit comments