Skip to content

Commit 8730822

Browse files
Punt on recreating existing primary
Signed-off-by: Kevin Snyder <kevin.snyder@gusto.com>
1 parent 290f7b7 commit 8730822

6 files changed

Lines changed: 6 additions & 99 deletions

pkg/canary/daemonset_controller.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package canary
1919
import (
2020
"context"
2121
"fmt"
22-
"maps"
2322

2423
"go.uber.org/zap"
2524
appsv1 "k8s.io/api/apps/v1"
@@ -121,29 +120,17 @@ func (c *DaemonSetController) Promote(cd *flaggerv1.Canary) error {
121120
return fmt.Errorf("damonset %s.%s get query error: %v", targetName, cd.Namespace, err)
122121
}
123122

124-
matchLabels, label, labelValue, err := c.getSelectorLabel(canary)
123+
_, label, labelValue, err := c.getSelectorLabel(canary)
125124
primaryLabelValue := fmt.Sprintf("%s-primary", labelValue)
126125
if err != nil {
127126
return fmt.Errorf("getSelectorLabel failed: %w", err)
128127
}
129128

130-
matchLabels[label] = primaryLabelValue
131-
132129
primary, err := c.kubeClient.AppsV1().DaemonSets(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
133130
if err != nil {
134131
return fmt.Errorf("daemonset %s.%s get query error: %w", primaryName, cd.Namespace, err)
135132
}
136133

137-
if !maps.Equal(primary.Spec.Selector.MatchLabels, matchLabels) {
138-
c.recreatePrimaryDaemonSet(cd, c.includeLabelPrefix)
139-
}
140-
141-
// Get primary daemonset again, in case it has changed
142-
primary, err = c.kubeClient.AppsV1().DaemonSets(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
143-
if err != nil {
144-
return fmt.Errorf("daemonset %s.%s get query error: %w", primaryName, cd.Namespace, err)
145-
}
146-
147134
// promote secrets and config maps
148135
configRefs, err := c.configTracker.GetTargetConfigs(cd)
149136
if err != nil {
@@ -323,23 +310,6 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, inclu
323310
return nil
324311
}
325312

326-
func (c *DaemonSetController) recreatePrimaryDaemonSet(cd *flaggerv1.Canary, includeLabelPrefix []string) error {
327-
targetName := cd.Spec.TargetRef.Name
328-
primaryName := fmt.Sprintf("%s-primary", targetName)
329-
330-
// TODO: Check if primary daemonset is scaled to zero and not receiving any traffic
331-
332-
if err := c.kubeClient.AppsV1().DaemonSets(cd.Namespace).Delete(context.TODO(), primaryName, metav1.DeleteOptions{}); err != nil {
333-
return fmt.Errorf("daemonset %s.%s delete error: %w", primaryName, cd.Namespace, err)
334-
}
335-
336-
if err := c.createPrimaryDaemonSet(cd, includeLabelPrefix); err != nil {
337-
return fmt.Errorf("createPrimaryDaemonSet failed: %w", err)
338-
}
339-
340-
return nil
341-
}
342-
343313
// getSelectorLabel returns the selector match label
344314
func (c *DaemonSetController) getSelectorLabel(daemonSet *appsv1.DaemonSet) (map[string]string, string, string, error) {
345315
label, labelValue := "", ""

pkg/canary/daemonset_controller_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ func TestDaemonSetController_Promote(t *testing.T) {
7979
require.NoError(t, err)
8080

8181
dae2 := newDaemonSetControllerTestPodInfoV2()
82-
err = mocks.kubeClient.AppsV1().DaemonSets("default").Delete(context.TODO(), dae2.ObjectMeta.Name, metav1.DeleteOptions{})
83-
require.NoError(t, err)
84-
_, err = mocks.kubeClient.AppsV1().DaemonSets("default").Create(context.TODO(), dae2, metav1.CreateOptions{})
82+
_, err = mocks.kubeClient.AppsV1().DaemonSets("default").Update(context.TODO(), dae2, metav1.UpdateOptions{})
8583
require.NoError(t, err)
8684

8785
config2 := newDaemonSetControllerTestConfigMapV2()
@@ -103,12 +101,6 @@ func TestDaemonSetController_Promote(t *testing.T) {
103101
assert.Equal(t, daeSourceLabels["app.kubernetes.io/test-label-1"], daePrimaryLabels["app.kubernetes.io/test-label-1"])
104102
assert.Equal(t, "podinfo-primary", daePrimaryLabels["name"])
105103

106-
daePrimaryMatchLabels := daePrimary.Spec.Selector.MatchLabels
107-
daeSourceMatchLabels := dae2.Spec.Selector.MatchLabels
108-
assert.Equal(t, len(daeSourceMatchLabels), len(daePrimaryMatchLabels))
109-
assert.Equal(t, daeSourceMatchLabels["app.kubernetes.io/test-label-1"], daePrimaryMatchLabels["app.kubernetes.io/test-label-1"])
110-
assert.Equal(t, "podinfo-primary", daePrimaryMatchLabels["name"])
111-
112104
daePrimaryAnnotations := daePrimary.ObjectMeta.Annotations
113105
daeSourceAnnotations := dae2.ObjectMeta.Annotations
114106
assert.Equal(t, daeSourceAnnotations["app.kubernetes.io/test-annotation-1"], daePrimaryAnnotations["app.kubernetes.io/test-annotation-1"])

pkg/canary/daemonset_fixture_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,28 +637,17 @@ func newDaemonSetControllerTestPodInfoV2() *appsv1.DaemonSet {
637637
ObjectMeta: metav1.ObjectMeta{
638638
Namespace: "default",
639639
Name: "podinfo",
640-
Annotations: map[string]string{
641-
"test-annotation-1": "test-annotation-value-1",
642-
},
643-
Labels: map[string]string{
644-
"test-label-1": "test-label-value-1",
645-
},
646640
},
647641
Spec: appsv1.DaemonSetSpec{
648642
Selector: &metav1.LabelSelector{
649643
MatchLabels: map[string]string{
650-
"name": "podinfo",
651-
"test-label-1": "test-label-value-1",
644+
"name": "podinfo",
652645
},
653646
},
654647
Template: corev1.PodTemplateSpec{
655648
ObjectMeta: metav1.ObjectMeta{
656649
Labels: map[string]string{
657-
"name": "podinfo",
658-
"test-label-1": "test-label-value-1",
659-
},
660-
Annotations: map[string]string{
661-
"test-annotation-1": "test-annotation-value-1",
650+
"name": "podinfo",
662651
},
663652
},
664653
Spec: corev1.PodSpec{

pkg/canary/deployment_controller.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package canary
1919
import (
2020
"context"
2121
"fmt"
22-
"maps"
2322

2423
"go.uber.org/zap"
2524
appsv1 "k8s.io/api/apps/v1"
@@ -83,37 +82,17 @@ func (c *DeploymentController) Promote(cd *flaggerv1.Canary) error {
8382
return fmt.Errorf("deployment %s.%s get query error: %w", targetName, cd.Namespace, err)
8483
}
8584

86-
matchLabels, label, labelValue, err := c.getSelectorLabel(canary)
85+
_, label, labelValue, err := c.getSelectorLabel(canary)
8786
primaryLabelValue := fmt.Sprintf("%s-primary", labelValue)
8887
if err != nil {
8988
return fmt.Errorf("getSelectorLabel failed: %w", err)
9089
}
9190

92-
matchLabels[label] = primaryLabelValue
93-
9491
primary, err := c.kubeClient.AppsV1().Deployments(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
9592
if err != nil {
9693
return fmt.Errorf("deployment %s.%s get query error: %w", primaryName, cd.Namespace, err)
9794
}
9895

99-
if !maps.Equal(primary.Spec.Selector.MatchLabels, matchLabels) {
100-
c.logger.With("canary", fmt.Sprintf(cd.Name, cd.Namespace)).
101-
Infof(
102-
"Primary deployment %s.%s matchLabels does not match canary deployment %s.%s, recreating...",
103-
primaryName,
104-
cd.Namespace,
105-
targetName,
106-
cd.Namespace,
107-
)
108-
c.recreatePrimaryDeployment(cd, canary, primaryName, c.includeLabelPrefix)
109-
}
110-
111-
// Get primary deployment again, in case it has changes
112-
primary, err = c.kubeClient.AppsV1().Deployments(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
113-
if err != nil {
114-
return fmt.Errorf("deployment %s.%s get query error: %w", primaryName, cd.Namespace, err)
115-
}
116-
11796
// promote secrets and config maps
11897
configRefs, err := c.configTracker.GetTargetConfigs(cd)
11998
if err != nil {
@@ -346,20 +325,6 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, can
346325
return nil
347326
}
348327

349-
func (c *DeploymentController) recreatePrimaryDeployment(cd *flaggerv1.Canary, canary *appsv1.Deployment, name string, includeLabelPrefix []string) error {
350-
// TODO: Make sure primary deployment is not receiving any traffic
351-
352-
if err := c.kubeClient.AppsV1().Deployments(cd.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}); err != nil {
353-
return fmt.Errorf("deployment %s.%s delete error: %w", name, cd.Namespace, err)
354-
}
355-
356-
if err := c.createPrimaryDeployment(cd, canary, name, includeLabelPrefix); err != nil {
357-
return fmt.Errorf("createPrimaryDeployment failed: %w", err)
358-
}
359-
360-
return nil
361-
}
362-
363328
// getSelectorLabel returns the selector match label
364329
func (c *DeploymentController) getSelectorLabel(deployment *appsv1.Deployment) (map[string]string, string, string, error) {
365330
label, labelValue := "", ""

pkg/canary/deployment_controller_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ func TestDeploymentController_Promote(t *testing.T) {
7575
mocks.initializeCanary(t)
7676

7777
dep2 := newDeploymentControllerTestV2()
78-
err := mocks.kubeClient.AppsV1().Deployments("default").Delete(context.TODO(), dep2.ObjectMeta.Name, metav1.DeleteOptions{})
79-
require.NoError(t, err)
80-
_, err = mocks.kubeClient.AppsV1().Deployments("default").Create(context.TODO(), dep2, metav1.CreateOptions{})
78+
_, err := mocks.kubeClient.AppsV1().Deployments("default").Update(context.TODO(), dep2, metav1.UpdateOptions{})
8179
require.NoError(t, err)
8280

8381
config2 := newDeploymentControllerTestConfigMapV2()
@@ -103,12 +101,6 @@ func TestDeploymentController_Promote(t *testing.T) {
103101
depSourceAnnotations := dep2.ObjectMeta.Annotations
104102
assert.Equal(t, depSourceAnnotations["app.kubernetes.io/test-annotation-1"], depPrimaryAnnotations["app.kubernetes.io/test-annotation-1"])
105103

106-
depPrimaryMatchLabels := depPrimary.Spec.Selector.MatchLabels
107-
depSourceMatchLabels := dep2.Spec.Selector.MatchLabels
108-
assert.Equal(t, len(depSourceMatchLabels), len(depPrimaryMatchLabels))
109-
assert.Equal(t, depSourceMatchLabels["app.kubernetes.io/test-label-1"], depPrimaryMatchLabels["app.kubernetes.io/test-label-1"])
110-
assert.Equal(t, "podinfo-primary", depPrimaryMatchLabels["name"])
111-
112104
configPrimary, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get(context.TODO(), "podinfo-config-env-primary", metav1.GetOptions{})
113105
require.NoError(t, err)
114106
assert.Equal(t, config2.Data["color"], configPrimary.Data["color"])

pkg/canary/deployment_fixture_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ func newDeploymentControllerTestV2() *appsv1.Deployment {
776776
Selector: &metav1.LabelSelector{
777777
MatchLabels: map[string]string{
778778
"name": "podinfo",
779-
"test-label-1": "test-label-value-1",
780779
},
781780
},
782781
Template: corev1.PodTemplateSpec{

0 commit comments

Comments
 (0)