Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix canary to primary label promotion #1405

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pkg/canary/daemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ func (c *DaemonSetController) Promote(cd *flaggerv1.Canary) error {
primaryCopy.ObjectMeta.Annotations[k] = v
}
// update ds labels
primaryCopy.ObjectMeta.Labels = make(map[string]string)
filteredLabels := includeLabelsByPrefix(canary.ObjectMeta.Labels, c.includeLabelPrefix)
for k, v := range filteredLabels {
primaryCopy.ObjectMeta.Labels[k] = v
}
primaryCopy.ObjectMeta.Labels = makePrimaryLabels(filteredLabels, primaryLabelValue, label)

// apply update
_, err = c.kubeClient.AppsV1().DaemonSets(cd.Namespace).Update(context.TODO(), primaryCopy, metav1.UpdateOptions{})
Expand Down
1 change: 1 addition & 0 deletions pkg/canary/daemonset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestDaemonSetController_Promote(t *testing.T) {
daePrimaryLabels := daePrimary.ObjectMeta.Labels
daeSourceLabels := dae2.ObjectMeta.Labels
assert.Equal(t, daeSourceLabels["app.kubernetes.io/test-label-1"], daePrimaryLabels["app.kubernetes.io/test-label-1"])
assert.Equal(t, "podinfo-primary", daePrimaryLabels["name"])

daePrimaryAnnotations := daePrimary.ObjectMeta.Annotations
daeSourceAnnotations := dae2.ObjectMeta.Annotations
Expand Down
5 changes: 1 addition & 4 deletions pkg/canary/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,8 @@ func (c *DeploymentController) Promote(cd *flaggerv1.Canary) error {
primaryCopy.ObjectMeta.Annotations[k] = v
}
// update deploy labels
primaryCopy.ObjectMeta.Labels = make(map[string]string)
filteredLabels := includeLabelsByPrefix(canary.ObjectMeta.Labels, c.includeLabelPrefix)
for k, v := range filteredLabels {
primaryCopy.ObjectMeta.Labels[k] = v
}
primaryCopy.ObjectMeta.Labels = makePrimaryLabels(filteredLabels, primaryLabelValue, label)

// apply update
_, err = c.kubeClient.AppsV1().Deployments(cd.Namespace).Update(context.TODO(), primaryCopy, metav1.UpdateOptions{})
Expand Down
1 change: 1 addition & 0 deletions pkg/canary/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestDeploymentController_Promote(t *testing.T) {
depPrimaryLabels := depPrimary.ObjectMeta.Labels
depSourceLabels := dep2.ObjectMeta.Labels
assert.Equal(t, depSourceLabels["app.kubernetes.io/test-label-1"], depPrimaryLabels["app.kubernetes.io/test-label-1"])
assert.Equal(t, "podinfo-primary", depPrimaryLabels["name"])

depPrimaryAnnotations := depPrimary.ObjectMeta.Annotations
depSourceAnnotations := dep2.ObjectMeta.Annotations
Expand Down
2 changes: 1 addition & 1 deletion pkg/canary/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []stri
continue
}
for _, includeLabelPrefix := range includeLabelPrefixes {
if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) {
if includeLabelPrefix == "*" || (includeLabelPrefix != "" && strings.HasPrefix(key, includeLabelPrefix)) {
filteredLabels[key] = value
break
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/canary/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) {
})
}

func TestIncludeLabelsNoIncludes(t *testing.T) {
labels := map[string]string{
"foo": "foo-value",
"bar": "bar-value",
"lorem": "ipsum",
}
includeLabelPrefix := []string{""}

filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix)

assert.Equal(t, map[string]string{}, filteredLabels)
}

func TestMakePrimaryLabels(t *testing.T) {
labels := map[string]string{
"lorem": "ipsum",
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []stri
continue
}
for _, includeLabelPrefix := range includeLabelPrefixes {
if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) {
if includeLabelPrefix == "*" || (includeLabelPrefix != "" && strings.HasPrefix(key, includeLabelPrefix)) {
filteredLabels[key] = value
break
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/router/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) {
"lorem": "ipsum",
})
}

func TestIncludeLabelsNoIncludes(t *testing.T) {
labels := map[string]string{
"foo": "foo-value",
"bar": "bar-value",
"lorem": "ipsum",
}
includeLabelPrefix := []string{""}

filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix)

assert.Equal(t, map[string]string{}, filteredLabels)
}