Skip to content

Commit 7544a13

Browse files
AlinsRanCopilot
andcommitted
fix: ignore dangling label args
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0d69dd0 commit 7544a13

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

internal/controller/label/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func GenLabel(obj client.Object, args ...string) Label {
4444
label[LabelControllerName] = config.ControllerConfig.ControllerName
4545
label[LabelManagedBy] = "apisix-ingress-controller"
4646
label[LabelResourceKey] = fmt.Sprintf("%s/%s/%s", label[LabelKind], label[LabelNamespace], label[LabelName])
47-
for i := 0; i < len(args); i += 2 {
47+
for i := 0; i+1 < len(args); i += 2 {
4848
label[args[i]] = args[i+1]
4949
}
5050
return label

internal/controller/label/label_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ func TestGenLabelWithObjectLabels(t *testing.T) {
5656
require.Equal(t, "apisix-ingress-controller", labels[LabelManagedBy])
5757
require.Equal(t, "ApisixConsumer/default/demo", labels[LabelResourceKey])
5858
}
59+
60+
func TestGenLabel_IgnoresDanglingKeyArg(t *testing.T) {
61+
consumer := &apiv2.ApisixConsumer{
62+
TypeMeta: metav1.TypeMeta{
63+
Kind: "ApisixConsumer",
64+
APIVersion: apiv2.GroupVersion.String(),
65+
},
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "demo",
68+
Namespace: "default",
69+
},
70+
}
71+
72+
labels := GenLabel(consumer, "team", "payments", "dangling")
73+
74+
require.Equal(t, "payments", labels["team"])
75+
require.NotContains(t, labels, "dangling")
76+
}

0 commit comments

Comments
 (0)