Skip to content

Commit 5563492

Browse files
shaofan-hsshaofan-hs
shaofan-hs
authored andcommitted
get workqueue priority from namespace label
1 parent 91d29b6 commit 5563492

File tree

3 files changed

+60
-179
lines changed

3 files changed

+60
-179
lines changed

controller/workqueue/defaults.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ var (
3737
DefaultNumOfPriorityLotteries = []int{1, 2, 4, 8, 16}
3838
)
3939

40-
func DefaultGetPriorityFuncBuilder(cli client.Client, objectGetter func() client.Object) GetPriorityFunc {
41-
return GetPriorityFuncBuilder(cli, objectGetter, DefaultWorkQueuePriorityLabel, DefaultWorkQueuePriority)
40+
func DefaultGetPriorityFuncBuilder(cli client.Client) GetPriorityFunc {
41+
return GetPriorityFuncBuilder(cli, DefaultWorkQueuePriorityLabel, DefaultWorkQueuePriority)
4242
}
4343

4444
// GetPriorityFunc is the function to get the priority of an item
45-
// We use the label to get the priority of an item
46-
// If the label is not set in the item, we will get the priority from the namespace label
47-
func GetPriorityFuncBuilder(cli client.Client, objectGetter func() client.Object, workQueuePriorityLabel string, defaultWorkQueuePriority int) GetPriorityFunc {
45+
// We use the namespace label to get the priority of an item
46+
func GetPriorityFuncBuilder(cli client.Client, workQueuePriorityLabel string, defaultWorkQueuePriority int) GetPriorityFunc {
4847
if cli == nil {
4948
panic("cli is required")
5049
}
@@ -54,45 +53,26 @@ func GetPriorityFuncBuilder(cli client.Client, objectGetter func() client.Object
5453

5554
return func(item interface{}) int {
5655
req, ok := item.(reconcile.Request)
57-
if !ok {
58-
return defaultWorkQueuePriority
59-
}
60-
61-
object := objectGetter()
62-
err := cli.Get(context.Background(), req.NamespacedName, object)
63-
if err != nil {
64-
klog.Errorf("Failed to get object: %v, error: %v", req.NamespacedName, err)
56+
if !ok || req.Namespace == "" {
6557
return defaultWorkQueuePriority
6658
}
6759

6860
var priorityLableValue string
69-
labels := object.GetLabels()
70-
if len(labels) != 0 {
71-
priorityLableValue = labels[workQueuePriorityLabel]
72-
}
73-
74-
if priorityLableValue == "" {
75-
if req.Namespace == "" {
61+
namespace := &corev1.Namespace{}
62+
if err := cli.Get(context.Background(), client.ObjectKey{Name: req.Namespace}, namespace); err != nil {
63+
klog.Errorf("Failed to get namespace: %v, error: %v", req.Namespace, err)
64+
return defaultWorkQueuePriority
65+
} else {
66+
labels := namespace.GetLabels()
67+
if len(labels) == 0 {
7668
return defaultWorkQueuePriority
7769
}
78-
79-
namespace := &corev1.Namespace{}
80-
if err := cli.Get(context.Background(), client.ObjectKey{Name: req.Namespace}, namespace); err != nil {
81-
klog.Errorf("Failed to get namespace: %v, error: %v", req.Namespace, err)
70+
priorityLableValue, ok = namespace.Labels[workQueuePriorityLabel]
71+
if !ok {
8272
return defaultWorkQueuePriority
83-
} else {
84-
labels := namespace.GetLabels()
85-
if len(labels) == 0 {
86-
return defaultWorkQueuePriority
87-
}
88-
priorityLableValue = namespace.Labels[workQueuePriorityLabel]
8973
}
9074
}
9175

92-
if priorityLableValue == "" {
93-
return defaultWorkQueuePriority
94-
}
95-
9676
priority, err := strconv.Atoi(priorityLableValue)
9777
if err != nil {
9878
klog.Errorf("Failed to convert label value: %q to int, error: %v", priorityLableValue, err)

controller/workqueue/defaults_test.go

Lines changed: 24 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -32,142 +32,58 @@ import (
3232

3333
var _ = Describe("Test defaults", func() {
3434
const (
35-
testConfigmap = "configmap1"
35+
testObject = "object1"
3636
testNamespace = "default"
3737
)
3838

3939
var (
40-
objectGetter = func() client.Object {
41-
return &corev1.ConfigMap{}
42-
}
4340
req = reconcile.Request{NamespacedName: client.ObjectKey{
44-
Name: testConfigmap,
41+
Name: testObject,
4542
Namespace: testNamespace,
4643
}}
4744
)
4845

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, "")
5249
Expect(err).NotTo(HaveOccurred())
5350

54-
getPriorityFunc := DefaultGetPriorityFuncBuilder(k8sClient, objectGetter)
51+
getPriorityFunc := DefaultGetPriorityFuncBuilder(k8sClient)
5552
priority := getPriorityFunc(req)
5653
Expect(priority).To(Equal(DefaultWorkQueuePriority))
5754
})
5855

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")
7358
Expect(err).NotTo(HaveOccurred())
7459

75-
getPriorityFunc := DefaultGetPriorityFuncBuilder(k8sClient, objectGetter)
60+
getPriorityFunc := DefaultGetPriorityFuncBuilder(k8sClient)
7661
priority := getPriorityFunc(req)
7762
Expect(priority).To(Equal(4))
7863
})
7964
})
8065

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")
8469
Expect(err).NotTo(HaveOccurred())
8570

86-
getPriorityFunc := GetPriorityFuncBuilder(k8sClient, objectGetter, "custom-priority", 1)
71+
getPriorityFunc := GetPriorityFuncBuilder(k8sClient, "custom-priority", 1)
8772
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))
9874
})
9975

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")
10278
Expect(err).NotTo(HaveOccurred())
10379

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)
10881
priority := getPriorityFunc(req)
10982
Expect(priority).To(Equal(4))
11083
})
11184
})
11285
})
11386

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-
17187
func ensureNamespace(cli client.Client, name, priorityLabelKey, priorityLabelValue string) error {
17288
// Ensure the namespace exists
17389
namespace := &corev1.Namespace{}
@@ -190,17 +106,16 @@ func ensureNamespace(cli client.Client, name, priorityLabelKey, priorityLabelVal
190106
}
191107

192108
// 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]
201110
if priorityLabelValue == "" {
111+
if !ok {
112+
return nil
113+
}
202114
namespace.Labels = map[string]string{}
203115
} else {
116+
if ok && priority == priorityLabelValue {
117+
return nil
118+
}
204119
namespace.Labels = map[string]string{
205120
priorityLabelKey: priorityLabelValue,
206121
}

0 commit comments

Comments
 (0)