Skip to content

Commit 223bcdb

Browse files
Fix not matching first selector label
Signed-off-by: Kevin Snyder <[email protected]>
1 parent f71e19d commit 223bcdb

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

pkg/canary/daemonset_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ func (c *DaemonSetController) getSelectorLabel(daemonSet *appsv1.DaemonSet) (map
316316
for _, l := range c.labels {
317317
if _, ok := daemonSet.Spec.Selector.MatchLabels[l]; ok {
318318
label, labelValue = l, daemonSet.Spec.Selector.MatchLabels[l]
319+
break
319320
}
320321
}
321322

pkg/canary/daemonset_controller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ func TestDaemonSetController_Sync_InconsistentNaming(t *testing.T) {
7272
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", sourceSelectorValue))
7373
}
7474

75+
func TestDaemonSetController_GetMetadata(t *testing.T) {
76+
dc := daemonsetConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
77+
mocks := newDaemonSetFixture(dc)
78+
_, err := mocks.controller.Initialize(mocks.canary)
79+
require.NoError(t, err)
80+
81+
matchLabels, label, labelValue, _, err := mocks.controller.GetMetadata(mocks.canary)
82+
require.NoError(t, err)
83+
84+
assert.Equal(t, map[string]string{"name": "podinfo", "test-label-1": "test-label-value-1"}, matchLabels)
85+
assert.Equal(t, "name", label)
86+
assert.Equal(t, "podinfo", labelValue)
87+
88+
mocks.controller.labels = []string{"app", "name", "test-label-1"}
89+
90+
matchLabels, label, labelValue, _, err = mocks.controller.GetMetadata(mocks.canary)
91+
require.NoError(t, err)
92+
93+
assert.Equal(t, map[string]string{"name": "podinfo", "test-label-1": "test-label-value-1"}, matchLabels)
94+
assert.Equal(t, "name", label)
95+
assert.Equal(t, "podinfo", labelValue)
96+
}
97+
7598
func TestDaemonSetController_Initialize(t *testing.T) {
7699
dc := daemonsetConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
77100
mocks := newDaemonSetFixture(dc)

pkg/canary/deployment_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func (c *DeploymentController) getSelectorLabel(deployment *appsv1.Deployment) (
332332
for _, l := range c.labels {
333333
if _, ok := deployment.Spec.Selector.MatchLabels[l]; ok {
334334
label, labelValue = l, deployment.Spec.Selector.MatchLabels[l]
335+
break
335336
}
336337
}
337338

pkg/canary/deployment_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ func TestDeploymentController_Sync_InconsistentNaming(t *testing.T) {
6969
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", dc.labelValue))
7070
}
7171

72+
func TestDeploymentController_GetMetadata(t *testing.T) {
73+
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
74+
mocks := newDeploymentFixture(dc)
75+
mocks.initializeCanary(t)
76+
77+
matchLabels, label, labelValue, _, err := mocks.controller.GetMetadata(mocks.canary)
78+
require.NoError(t, err)
79+
80+
assert.Equal(t, map[string]string{"name": "podinfo", "test-label-1": "test-label-value-1"}, matchLabels)
81+
assert.Equal(t, "name", label)
82+
assert.Equal(t, "podinfo", labelValue)
83+
84+
mocks.controller.labels = []string{"app", "name", "test-label-1"}
85+
86+
matchLabels, label, labelValue, _, err = mocks.controller.GetMetadata(mocks.canary)
87+
require.NoError(t, err)
88+
89+
assert.Equal(t, map[string]string{"name": "podinfo", "test-label-1": "test-label-value-1"}, matchLabels)
90+
assert.Equal(t, "name", label)
91+
assert.Equal(t, "podinfo", labelValue)
92+
}
93+
7294
func TestDeploymentController_Initialize(t *testing.T) {
7395
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
7496
mocks := newDeploymentFixture(dc)

0 commit comments

Comments
 (0)