From 23e59168af547d1d9010a65eec8362d6cde61708 Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Sun, 11 Oct 2020 14:10:16 +0100 Subject: [PATCH 1/7] Exclude controller labels by prefix --- charts/flagger/README.md | 1 + charts/flagger/templates/deployment.yaml | 3 ++ cmd/flagger/main.go | 6 ++- pkg/canary/daemonset_controller.go | 22 ++++++----- pkg/canary/deployment_controller.go | 24 ++++++------ pkg/canary/factory.go | 34 +++++++++-------- pkg/canary/util.go | 19 ++++++++++ pkg/canary/util_test.go | 38 +++++++++++++++++++ .../scheduler_daemonset_fixture_test.go | 2 +- .../scheduler_deployment_fixture_test.go | 2 +- 10 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 pkg/canary/util_test.go diff --git a/charts/flagger/README.md b/charts/flagger/README.md index fa5589ae..b82e27c8 100644 --- a/charts/flagger/README.md +++ b/charts/flagger/README.md @@ -125,6 +125,7 @@ Parameter | Description | Default `serviceAccount.name` | The name of the service account to create or use. If not set and `serviceAccount.create` is `true`, a name is generated using the Flagger fullname | `""` `serviceAccount.annotations` | Annotations for service account | `{}` `ingressAnnotationsPrefix` | Annotations prefix for ingresses | `custom.ingress.kubernetes.io` +`excludedLabelsPrefixes` | List of prefixes of labels that are excluded when creating primary controllers | `"fluxcd,jenkins"` `rbac.create` | If `true`, create and use RBAC resources | `true` `rbac.pspEnabled` | If `true`, create and use a restricted pod security policy | `false` `crd.create` | If `true`, create Flagger's CRDs (should be enabled for Helm v2 only) | `false` diff --git a/charts/flagger/templates/deployment.yaml b/charts/flagger/templates/deployment.yaml index cb6ebd58..d67b4273 100644 --- a/charts/flagger/templates/deployment.yaml +++ b/charts/flagger/templates/deployment.yaml @@ -106,6 +106,9 @@ spec: {{- if .Values.ingressAnnotationsPrefix }} - -ingress-annotations-prefix={{ .Values.ingressAnnotationsPrefix }} {{- end }} + {{- if .Values.excludedLabelsPrefixes }} + - -excluded-labels-prefixes={{ .Values.excludedLabelsPrefixes }} + {{- end }} {{- if .Values.ingressClass }} - -ingress-class={{ .Values.ingressClass }} {{- end }} diff --git a/cmd/flagger/main.go b/cmd/flagger/main.go index 0ce5c76c..217d5b70 100644 --- a/cmd/flagger/main.go +++ b/cmd/flagger/main.go @@ -43,6 +43,7 @@ var ( logLevel string port string msteamsURL string + excludedLabelsPrefixes string slackURL string slackUser string slackChannel string @@ -74,6 +75,7 @@ func init() { flag.StringVar(&slackChannel, "slack-channel", "", "Slack channel.") flag.StringVar(&eventWebhook, "event-webhook", "", "Webhook for publishing flagger events") flag.StringVar(&msteamsURL, "msteams-url", "", "MS Teams incoming webhook URL.") + flag.StringVar(&excludedLabelsPrefixes, "excluded-labels-prefixes", "fluxcd,jenkins", "List of prefixes of labels that are excluded when creating primary controllers.") flag.IntVar(&threadiness, "threadiness", 2, "Worker concurrency.") flag.BoolVar(&zapReplaceGlobals, "zap-replace-globals", false, "Whether to change the logging level of the global zap logger.") flag.StringVar(&zapEncoding, "zap-encoding", "json", "Zap logger encoding.") @@ -184,7 +186,9 @@ func main() { configTracker = &canary.NopTracker{} } - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, labels, logger) + excludedLabelsPrefixesArray := strings.Split(excludedLabelsPrefixes, ",") + + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, labels, excludedLabelsPrefixesArray, logger) c := controller.NewController( kubeClient, diff --git a/pkg/canary/daemonset_controller.go b/pkg/canary/daemonset_controller.go index fdd656c1..d7582845 100644 --- a/pkg/canary/daemonset_controller.go +++ b/pkg/canary/daemonset_controller.go @@ -22,11 +22,12 @@ var ( // DaemonSetController is managing the operations for Kubernetes DaemonSet kind type DaemonSetController struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + excludedLabelsPrefixes []string } func (c *DaemonSetController) ScaleToZero(cd *flaggerv1.Canary) error { @@ -76,7 +77,7 @@ func (c *DaemonSetController) ScaleFromZero(cd *flaggerv1.Canary) error { // Initialize creates the primary DaemonSet, scales down the canary DaemonSet, // and returns the pod selector label and container ports func (c *DaemonSetController) Initialize(cd *flaggerv1.Canary) (err error) { - err = c.createPrimaryDaemonSet(cd) + err = c.createPrimaryDaemonSet(cd, c.excludedLabelsPrefixes) if err != nil { return fmt.Errorf("createPrimaryDaemonSet failed: %w", err) } @@ -200,7 +201,7 @@ func (c *DaemonSetController) GetMetadata(cd *flaggerv1.Canary) (string, string, return label, labelValue, ports, nil } -func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary) error { +func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, excludedLabelsPrefixes []string) error { targetName := cd.Spec.TargetRef.Name primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) @@ -215,6 +216,9 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary) error targetName, cd.Namespace, canaryDae.Spec.UpdateStrategy.Type) } + // Create the labels map but filter unwanted labels + labels := excludeLabelsByPrefix(canaryDae.Labels, excludedLabelsPrefixes) + label, labelValue, err := c.getSelectorLabel(canaryDae) primaryLabelValue := fmt.Sprintf("%s-primary", labelValue) if err != nil { @@ -241,9 +245,7 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary) error ObjectMeta: metav1.ObjectMeta{ Name: primaryName, Namespace: cd.Namespace, - Labels: map[string]string{ - label: primaryLabelValue, - }, + Labels: labels, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/canary/deployment_controller.go b/pkg/canary/deployment_controller.go index 40f3ba91..f0fa37ef 100644 --- a/pkg/canary/deployment_controller.go +++ b/pkg/canary/deployment_controller.go @@ -20,18 +20,19 @@ import ( // DeploymentController is managing the operations for Kubernetes Deployment kind type DeploymentController struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + excludedLabelsPrefixes []string } // Initialize creates the primary deployment, hpa, // scales to zero the canary deployment and returns the pod selector label and container ports func (c *DeploymentController) Initialize(cd *flaggerv1.Canary) (err error) { primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) - if err := c.createPrimaryDeployment(cd); err != nil { + if err := c.createPrimaryDeployment(cd, c.excludedLabelsPrefixes); err != nil { return fmt.Errorf("createPrimaryDeployment failed: %w", err) } @@ -202,15 +203,18 @@ func (c *DeploymentController) GetMetadata(cd *flaggerv1.Canary) (string, string return label, labelValue, ports, nil } -func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary) error { +func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, excludedLabelsPrefixes []string) error { targetName := cd.Spec.TargetRef.Name primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) canaryDep, err := c.kubeClient.AppsV1().Deployments(cd.Namespace).Get(context.TODO(), targetName, metav1.GetOptions{}) if err != nil { - return fmt.Errorf("deplyoment %s.%s get query error: %w", targetName, cd.Namespace, err) + return fmt.Errorf("deployment %s.%s get query error: %w", targetName, cd.Namespace, err) } + // Create the labels map but filter unwanted labels + labels := excludeLabelsByPrefix(canaryDep.Labels, excludedLabelsPrefixes) + label, labelValue, err := c.getSelectorLabel(canaryDep) primaryLabelValue := fmt.Sprintf("%s-primary", labelValue) if err != nil { @@ -242,9 +246,7 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary) err ObjectMeta: metav1.ObjectMeta{ Name: primaryName, Namespace: cd.Namespace, - Labels: map[string]string{ - label: primaryLabelValue, - }, + Labels: labels, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/canary/factory.go b/pkg/canary/factory.go index 95f243b8..99db8041 100644 --- a/pkg/canary/factory.go +++ b/pkg/canary/factory.go @@ -8,34 +8,38 @@ import ( ) type Factory struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + excludedLabelsPrefixes []string } func NewFactory(kubeClient kubernetes.Interface, flaggerClient clientset.Interface, configTracker Tracker, labels []string, + excludedLabelsPrefixes []string, logger *zap.SugaredLogger) *Factory { return &Factory{ - kubeClient: kubeClient, - flaggerClient: flaggerClient, - logger: logger, - configTracker: configTracker, - labels: labels, + kubeClient: kubeClient, + flaggerClient: flaggerClient, + logger: logger, + configTracker: configTracker, + labels: labels, + excludedLabelsPrefixes: excludedLabelsPrefixes, } } func (factory *Factory) Controller(kind string) Controller { deploymentCtrl := &DeploymentController{ - logger: factory.logger, - kubeClient: factory.kubeClient, - flaggerClient: factory.flaggerClient, - labels: factory.labels, - configTracker: factory.configTracker, + logger: factory.logger, + kubeClient: factory.kubeClient, + flaggerClient: factory.flaggerClient, + labels: factory.labels, + configTracker: factory.configTracker, + excludedLabelsPrefixes: factory.excludedLabelsPrefixes, } daemonSetCtrl := &DaemonSetController{ logger: factory.logger, diff --git a/pkg/canary/util.go b/pkg/canary/util.go index 254cd38a..d3554a93 100644 --- a/pkg/canary/util.go +++ b/pkg/canary/util.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "fmt" "io" + "strings" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" @@ -75,6 +76,24 @@ func makeAnnotations(annotations map[string]string) (map[string]string, error) { return res, nil } +func excludeLabelsByPrefix(labels map[string]string, excludedLabelsPrefixes []string) map[string]string { + filteredLabels := make(map[string]string) + for key, value := range labels { + isPrefixExcluded := false + for _, excludeLabelPrefix := range excludedLabelsPrefixes { + if strings.HasPrefix(key, excludeLabelPrefix) { + isPrefixExcluded = true + break + } + } + if !isPrefixExcluded { + filteredLabels[key] = value + } + } + + return filteredLabels +} + func makePrimaryLabels(labels map[string]string, labelValue string, label string) map[string]string { res := make(map[string]string) for k, v := range labels { diff --git a/pkg/canary/util_test.go b/pkg/canary/util_test.go new file mode 100644 index 00000000..f4858f27 --- /dev/null +++ b/pkg/canary/util_test.go @@ -0,0 +1,38 @@ +package canary + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExcludeLabelsByPrefix(t *testing.T) { + labels := map[string]string{ + "foo": "bar", + "jenkins": "foo", + "flux123": "bar", + } + excludedLabelsPrefixes := []string{"jenkins", "flux"} + + filteredLabels := excludeLabelsByPrefix(labels, excludedLabelsPrefixes) + + assert.Equal(t, filteredLabels, map[string]string{ + "foo": "bar", + // jenkins excluded + // and flux123 also excluded + }) +} + +func TestMakePrimaryLabels(t *testing.T) { + labels := map[string]string{ + "lorem": "ipsum", + "foo": "old-bar", + } + + primaryLabels := makePrimaryLabels(labels, "new-bar", "foo") + + assert.Equal(t, primaryLabels, map[string]string{ + "lorem": "ipsum", // values from old map + "foo": "new-bar", // overriden value for a specific label + }) +} diff --git a/pkg/controller/scheduler_daemonset_fixture_test.go b/pkg/controller/scheduler_daemonset_fixture_test.go index 3c85484b..81c7574e 100644 --- a/pkg/controller/scheduler_daemonset_fixture_test.go +++ b/pkg/controller/scheduler_daemonset_fixture_test.go @@ -87,7 +87,7 @@ func newDaemonSetFixture(c *flaggerv1.Canary) daemonSetFixture { KubeClient: kubeClient, FlaggerClient: flaggerClient, } - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, logger) + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{"jenkins"}, logger) ctrl := &Controller{ kubeClient: kubeClient, diff --git a/pkg/controller/scheduler_deployment_fixture_test.go b/pkg/controller/scheduler_deployment_fixture_test.go index e0a95758..73d59d2e 100644 --- a/pkg/controller/scheduler_deployment_fixture_test.go +++ b/pkg/controller/scheduler_deployment_fixture_test.go @@ -115,7 +115,7 @@ func newDeploymentFixture(c *flaggerv1.Canary) fixture { KubeClient: kubeClient, FlaggerClient: flaggerClient, } - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, logger) + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{"jenkins"}, logger) ctrl := &Controller{ kubeClient: kubeClient, From 6ec377181afc953aa22355046d65a2b4271cc840 Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Tue, 13 Oct 2020 21:58:47 +0100 Subject: [PATCH 2/7] Change from exclude labels to include labels --- charts/flagger/README.md | 2 +- cmd/flagger/main.go | 8 +++--- pkg/canary/daemonset_controller.go | 20 +++++++-------- pkg/canary/deployment_controller.go | 20 +++++++-------- pkg/canary/factory.go | 38 ++++++++++++++--------------- pkg/canary/util.go | 12 +++------ pkg/canary/util_test.go | 35 +++++++++++++++++++------- 7 files changed, 74 insertions(+), 61 deletions(-) diff --git a/charts/flagger/README.md b/charts/flagger/README.md index b82e27c8..0c684acc 100644 --- a/charts/flagger/README.md +++ b/charts/flagger/README.md @@ -125,7 +125,7 @@ Parameter | Description | Default `serviceAccount.name` | The name of the service account to create or use. If not set and `serviceAccount.create` is `true`, a name is generated using the Flagger fullname | `""` `serviceAccount.annotations` | Annotations for service account | `{}` `ingressAnnotationsPrefix` | Annotations prefix for ingresses | `custom.ingress.kubernetes.io` -`excludedLabelsPrefixes` | List of prefixes of labels that are excluded when creating primary controllers | `"fluxcd,jenkins"` +`includeLabelPrefix` | List of prefixes of labels that are copied when creating primary deployments or daemonsets. Use * to include all | `""` `rbac.create` | If `true`, create and use RBAC resources | `true` `rbac.pspEnabled` | If `true`, create and use a restricted pod security policy | `false` `crd.create` | If `true`, create Flagger's CRDs (should be enabled for Helm v2 only) | `false` diff --git a/cmd/flagger/main.go b/cmd/flagger/main.go index 217d5b70..5267f08e 100644 --- a/cmd/flagger/main.go +++ b/cmd/flagger/main.go @@ -43,7 +43,7 @@ var ( logLevel string port string msteamsURL string - excludedLabelsPrefixes string + includeLabelPrefix string slackURL string slackUser string slackChannel string @@ -75,7 +75,7 @@ func init() { flag.StringVar(&slackChannel, "slack-channel", "", "Slack channel.") flag.StringVar(&eventWebhook, "event-webhook", "", "Webhook for publishing flagger events") flag.StringVar(&msteamsURL, "msteams-url", "", "MS Teams incoming webhook URL.") - flag.StringVar(&excludedLabelsPrefixes, "excluded-labels-prefixes", "fluxcd,jenkins", "List of prefixes of labels that are excluded when creating primary controllers.") + flag.StringVar(&includeLabelPrefix, "include-label-prefix", "", "List of prefixes of labels that are copied when creating primary deployments or daemonsets. Use * to include all.") flag.IntVar(&threadiness, "threadiness", 2, "Worker concurrency.") flag.BoolVar(&zapReplaceGlobals, "zap-replace-globals", false, "Whether to change the logging level of the global zap logger.") flag.StringVar(&zapEncoding, "zap-encoding", "json", "Zap logger encoding.") @@ -186,9 +186,9 @@ func main() { configTracker = &canary.NopTracker{} } - excludedLabelsPrefixesArray := strings.Split(excludedLabelsPrefixes, ",") + includeLabelPrefixArray := strings.Split(includeLabelPrefix, ",") - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, labels, excludedLabelsPrefixesArray, logger) + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, labels, includeLabelPrefixArray, logger) c := controller.NewController( kubeClient, diff --git a/pkg/canary/daemonset_controller.go b/pkg/canary/daemonset_controller.go index d7582845..faa35cd3 100644 --- a/pkg/canary/daemonset_controller.go +++ b/pkg/canary/daemonset_controller.go @@ -22,12 +22,12 @@ var ( // DaemonSetController is managing the operations for Kubernetes DaemonSet kind type DaemonSetController struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string - excludedLabelsPrefixes []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + includeLabelPrefix []string } func (c *DaemonSetController) ScaleToZero(cd *flaggerv1.Canary) error { @@ -77,7 +77,7 @@ func (c *DaemonSetController) ScaleFromZero(cd *flaggerv1.Canary) error { // Initialize creates the primary DaemonSet, scales down the canary DaemonSet, // and returns the pod selector label and container ports func (c *DaemonSetController) Initialize(cd *flaggerv1.Canary) (err error) { - err = c.createPrimaryDaemonSet(cd, c.excludedLabelsPrefixes) + err = c.createPrimaryDaemonSet(cd, c.includeLabelPrefix) if err != nil { return fmt.Errorf("createPrimaryDaemonSet failed: %w", err) } @@ -201,7 +201,7 @@ func (c *DaemonSetController) GetMetadata(cd *flaggerv1.Canary) (string, string, return label, labelValue, ports, nil } -func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, excludedLabelsPrefixes []string) error { +func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, includeLabelPrefix []string) error { targetName := cd.Spec.TargetRef.Name primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) @@ -217,7 +217,7 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, exclu } // Create the labels map but filter unwanted labels - labels := excludeLabelsByPrefix(canaryDae.Labels, excludedLabelsPrefixes) + labels := includeLabelsByPrefix(canaryDae.Labels, includeLabelPrefix) label, labelValue, err := c.getSelectorLabel(canaryDae) primaryLabelValue := fmt.Sprintf("%s-primary", labelValue) @@ -245,7 +245,7 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, exclu ObjectMeta: metav1.ObjectMeta{ Name: primaryName, Namespace: cd.Namespace, - Labels: labels, + Labels: makePrimaryLabels(labels, primaryLabelValue, label), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/canary/deployment_controller.go b/pkg/canary/deployment_controller.go index f0fa37ef..030ae808 100644 --- a/pkg/canary/deployment_controller.go +++ b/pkg/canary/deployment_controller.go @@ -20,19 +20,19 @@ import ( // DeploymentController is managing the operations for Kubernetes Deployment kind type DeploymentController struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string - excludedLabelsPrefixes []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + includeLabelPrefix []string } // Initialize creates the primary deployment, hpa, // scales to zero the canary deployment and returns the pod selector label and container ports func (c *DeploymentController) Initialize(cd *flaggerv1.Canary) (err error) { primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) - if err := c.createPrimaryDeployment(cd, c.excludedLabelsPrefixes); err != nil { + if err := c.createPrimaryDeployment(cd, c.includeLabelPrefix); err != nil { return fmt.Errorf("createPrimaryDeployment failed: %w", err) } @@ -203,7 +203,7 @@ func (c *DeploymentController) GetMetadata(cd *flaggerv1.Canary) (string, string return label, labelValue, ports, nil } -func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, excludedLabelsPrefixes []string) error { +func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, includeLabelPrefix []string) error { targetName := cd.Spec.TargetRef.Name primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name) @@ -213,7 +213,7 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, exc } // Create the labels map but filter unwanted labels - labels := excludeLabelsByPrefix(canaryDep.Labels, excludedLabelsPrefixes) + labels := includeLabelsByPrefix(canaryDep.Labels, includeLabelPrefix) label, labelValue, err := c.getSelectorLabel(canaryDep) primaryLabelValue := fmt.Sprintf("%s-primary", labelValue) @@ -246,7 +246,7 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, exc ObjectMeta: metav1.ObjectMeta{ Name: primaryName, Namespace: cd.Namespace, - Labels: labels, + Labels: makePrimaryLabels(labels, primaryLabelValue, label), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/canary/factory.go b/pkg/canary/factory.go index 99db8041..9c121b2b 100644 --- a/pkg/canary/factory.go +++ b/pkg/canary/factory.go @@ -8,38 +8,38 @@ import ( ) type Factory struct { - kubeClient kubernetes.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger - configTracker Tracker - labels []string - excludedLabelsPrefixes []string + kubeClient kubernetes.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + configTracker Tracker + labels []string + includeLabelPrefix []string } func NewFactory(kubeClient kubernetes.Interface, flaggerClient clientset.Interface, configTracker Tracker, labels []string, - excludedLabelsPrefixes []string, + includeLabelPrefix []string, logger *zap.SugaredLogger) *Factory { return &Factory{ - kubeClient: kubeClient, - flaggerClient: flaggerClient, - logger: logger, - configTracker: configTracker, - labels: labels, - excludedLabelsPrefixes: excludedLabelsPrefixes, + kubeClient: kubeClient, + flaggerClient: flaggerClient, + logger: logger, + configTracker: configTracker, + labels: labels, + includeLabelPrefix: includeLabelPrefix, } } func (factory *Factory) Controller(kind string) Controller { deploymentCtrl := &DeploymentController{ - logger: factory.logger, - kubeClient: factory.kubeClient, - flaggerClient: factory.flaggerClient, - labels: factory.labels, - configTracker: factory.configTracker, - excludedLabelsPrefixes: factory.excludedLabelsPrefixes, + logger: factory.logger, + kubeClient: factory.kubeClient, + flaggerClient: factory.flaggerClient, + labels: factory.labels, + configTracker: factory.configTracker, + includeLabelPrefix: factory.includeLabelPrefix, } daemonSetCtrl := &DaemonSetController{ logger: factory.logger, diff --git a/pkg/canary/util.go b/pkg/canary/util.go index d3554a93..77b6fdfa 100644 --- a/pkg/canary/util.go +++ b/pkg/canary/util.go @@ -76,19 +76,15 @@ func makeAnnotations(annotations map[string]string) (map[string]string, error) { return res, nil } -func excludeLabelsByPrefix(labels map[string]string, excludedLabelsPrefixes []string) map[string]string { +func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []string) map[string]string { filteredLabels := make(map[string]string) for key, value := range labels { - isPrefixExcluded := false - for _, excludeLabelPrefix := range excludedLabelsPrefixes { - if strings.HasPrefix(key, excludeLabelPrefix) { - isPrefixExcluded = true + for _, includeLabelPrefix := range includeLabelPrefixes { + if key == "*" || strings.HasPrefix(key, includeLabelPrefix) { + filteredLabels[key] = value break } } - if !isPrefixExcluded { - filteredLabels[key] = value - } } return filteredLabels diff --git a/pkg/canary/util_test.go b/pkg/canary/util_test.go index f4858f27..e8e36f01 100644 --- a/pkg/canary/util_test.go +++ b/pkg/canary/util_test.go @@ -6,20 +6,37 @@ import ( "github.com/stretchr/testify/assert" ) -func TestExcludeLabelsByPrefix(t *testing.T) { +func TestIncludeLabelsByPrefix(t *testing.T) { labels := map[string]string{ - "foo": "bar", - "jenkins": "foo", - "flux123": "bar", + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", } - excludedLabelsPrefixes := []string{"jenkins", "flux"} + includeLabelPrefix := []string{"foo", "lor"} - filteredLabels := excludeLabelsByPrefix(labels, excludedLabelsPrefixes) + filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) assert.Equal(t, filteredLabels, map[string]string{ - "foo": "bar", - // jenkins excluded - // and flux123 also excluded + "foo": "foo-value", + "lorem": "ipsum", + // bar excluded + }) +} + +func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) { + labels := map[string]string{ + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", + } + includeLabelPrefix := []string{"*"} + + filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) + + assert.Equal(t, filteredLabels, map[string]string{ + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", }) } From 8b87cf1757dc6fe86511b9e36c66d0883808e9be Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Tue, 13 Oct 2020 21:59:26 +0100 Subject: [PATCH 3/7] MIssing commit --- charts/flagger/templates/deployment.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/flagger/templates/deployment.yaml b/charts/flagger/templates/deployment.yaml index d67b4273..edb88ae6 100644 --- a/charts/flagger/templates/deployment.yaml +++ b/charts/flagger/templates/deployment.yaml @@ -106,8 +106,8 @@ spec: {{- if .Values.ingressAnnotationsPrefix }} - -ingress-annotations-prefix={{ .Values.ingressAnnotationsPrefix }} {{- end }} - {{- if .Values.excludedLabelsPrefixes }} - - -excluded-labels-prefixes={{ .Values.excludedLabelsPrefixes }} + {{- if .Values.includeLabelPrefix }} + - -excluded-labels-prefixes={{ .Values.includeLabelPrefix }} {{- end }} {{- if .Values.ingressClass }} - -ingress-class={{ .Values.ingressClass }} From bef02d8e1f39c466e85fb7ce084469e4d3c1a5bb Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Tue, 13 Oct 2020 22:00:31 +0100 Subject: [PATCH 4/7] Rename proprty from exclude to include --- charts/flagger/templates/deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/flagger/templates/deployment.yaml b/charts/flagger/templates/deployment.yaml index edb88ae6..363c18ad 100644 --- a/charts/flagger/templates/deployment.yaml +++ b/charts/flagger/templates/deployment.yaml @@ -107,7 +107,7 @@ spec: - -ingress-annotations-prefix={{ .Values.ingressAnnotationsPrefix }} {{- end }} {{- if .Values.includeLabelPrefix }} - - -excluded-labels-prefixes={{ .Values.includeLabelPrefix }} + - -include-label-prefix={{ .Values.includeLabelPrefix }} {{- end }} {{- if .Values.ingressClass }} - -ingress-class={{ .Values.ingressClass }} From 5ca5647faba6b0aaeae3a2563dc0c7501164b63f Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Tue, 13 Oct 2020 22:01:49 +0100 Subject: [PATCH 5/7] Remove refs to jenkins --- pkg/controller/scheduler_daemonset_fixture_test.go | 2 +- pkg/controller/scheduler_deployment_fixture_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/controller/scheduler_daemonset_fixture_test.go b/pkg/controller/scheduler_daemonset_fixture_test.go index 81c7574e..b4d2f52a 100644 --- a/pkg/controller/scheduler_daemonset_fixture_test.go +++ b/pkg/controller/scheduler_daemonset_fixture_test.go @@ -87,7 +87,7 @@ func newDaemonSetFixture(c *flaggerv1.Canary) daemonSetFixture { KubeClient: kubeClient, FlaggerClient: flaggerClient, } - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{"jenkins"}, logger) + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{""}, logger) ctrl := &Controller{ kubeClient: kubeClient, diff --git a/pkg/controller/scheduler_deployment_fixture_test.go b/pkg/controller/scheduler_deployment_fixture_test.go index 73d59d2e..3b84e220 100644 --- a/pkg/controller/scheduler_deployment_fixture_test.go +++ b/pkg/controller/scheduler_deployment_fixture_test.go @@ -115,7 +115,7 @@ func newDeploymentFixture(c *flaggerv1.Canary) fixture { KubeClient: kubeClient, FlaggerClient: flaggerClient, } - canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{"jenkins"}, logger) + canaryFactory := canary.NewFactory(kubeClient, flaggerClient, configTracker, []string{"app", "name"}, []string{""}, logger) ctrl := &Controller{ kubeClient: kubeClient, From bd536b689faf3fd38bc602577b44b37adba78fd9 Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Wed, 14 Oct 2020 15:20:15 +0100 Subject: [PATCH 6/7] Fix filtering of labels --- pkg/canary/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/canary/util.go b/pkg/canary/util.go index 77b6fdfa..75d789fe 100644 --- a/pkg/canary/util.go +++ b/pkg/canary/util.go @@ -80,7 +80,7 @@ func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []stri filteredLabels := make(map[string]string) for key, value := range labels { for _, includeLabelPrefix := range includeLabelPrefixes { - if key == "*" || strings.HasPrefix(key, includeLabelPrefix) { + if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) { filteredLabels[key] = value break } From fbece964e0ad47992dfa2905c80569d331358954 Mon Sep 17 00:00:00 2001 From: Daniel Albuquerque Date: Wed, 21 Oct 2020 14:20:09 +0100 Subject: [PATCH 7/7] Copy annotations to deployment and daemonset --- pkg/canary/daemonset_controller.go | 7 ++++--- pkg/canary/deployment_controller.go | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/canary/daemonset_controller.go b/pkg/canary/daemonset_controller.go index faa35cd3..b3c39143 100644 --- a/pkg/canary/daemonset_controller.go +++ b/pkg/canary/daemonset_controller.go @@ -243,9 +243,10 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, inclu // create primary daemonset primaryDae = &appsv1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ - Name: primaryName, - Namespace: cd.Namespace, - Labels: makePrimaryLabels(labels, primaryLabelValue, label), + Name: primaryName, + Namespace: cd.Namespace, + Labels: makePrimaryLabels(labels, primaryLabelValue, label), + Annotations: canaryDae.Annotations, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group, diff --git a/pkg/canary/deployment_controller.go b/pkg/canary/deployment_controller.go index 030ae808..10064acc 100644 --- a/pkg/canary/deployment_controller.go +++ b/pkg/canary/deployment_controller.go @@ -244,9 +244,10 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, inc // create primary deployment primaryDep = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: primaryName, - Namespace: cd.Namespace, - Labels: makePrimaryLabels(labels, primaryLabelValue, label), + Name: primaryName, + Namespace: cd.Namespace, + Labels: makePrimaryLabels(labels, primaryLabelValue, label), + Annotations: canaryDep.Annotations, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(cd, schema.GroupVersionKind{ Group: flaggerv1.SchemeGroupVersion.Group,