From e9942a56f4b4dcdc263ef41b7298062d1fc6fea4 Mon Sep 17 00:00:00 2001 From: itaispiegel Date: Fri, 30 Jun 2023 16:06:31 +0300 Subject: [PATCH 1/8] Add --auto-reload-all flag --- README.md | 4 ++++ .../kubernetes/chart/reloader/templates/deployment.yaml | 5 ++++- deployments/kubernetes/chart/reloader/values.yaml | 1 + internal/pkg/cmd/reloader.go | 1 + internal/pkg/handler/upgrade.go | 5 +++-- internal/pkg/options/flags.go | 2 ++ 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f191f1f6..2688f1d6 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,10 @@ not. We can also specify a specific configmap or secret which would trigger rolling upgrade only upon change in our specified configmap or secret, this way, it will not trigger rolling upgrade upon changes in all configmaps or secrets used in a `deploymentconfig`, `deployment`, `daemonset`, `statefulset` or `rollout`. To do this either set the auto annotation to `"false"` (`reloader.stakater.com/auto: "false"`) or remove it altogether, and use annotations for [Configmap](.#Configmap) or [Secret](.#Secret). +It's also possible to enable auto reloading for all resources, by setting the `--auto-reload-all` flag. +In this case, all resources that do not have the auto annotation set to `"false"`, will be reloaded automatically when their ConfigMaps or Secrets are updated. +Notice that setting the auto annotation to an undefined value counts as false as-well. + ### Configmap To perform rolling upgrade when change happens only on specific configmaps use below annotation. diff --git a/deployments/kubernetes/chart/reloader/templates/deployment.yaml b/deployments/kubernetes/chart/reloader/templates/deployment.yaml index e9d91e14..3860f1d5 100644 --- a/deployments/kubernetes/chart/reloader/templates/deployment.yaml +++ b/deployments/kubernetes/chart/reloader/templates/deployment.yaml @@ -165,7 +165,7 @@ spec: - mountPath: /tmp/ name: tmp-volume {{- end }} - {{- if or (.Values.reloader.logFormat) (.Values.reloader.ignoreSecrets) (.Values.reloader.ignoreNamespaces) (.Values.reloader.namespaceSelector) (.Values.reloader.resourceLabelSelector) (.Values.reloader.ignoreConfigMaps) (.Values.reloader.custom_annotations) (eq .Values.reloader.isArgoRollouts true) (eq .Values.reloader.reloadOnCreate true) (ne .Values.reloader.reloadStrategy "default") (.Values.reloader.enableHA)}} + {{- if or (.Values.reloader.logFormat) (.Values.reloader.ignoreSecrets) (.Values.reloader.ignoreNamespaces) (.Values.reloader.namespaceSelector) (.Values.reloader.resourceLabelSelector) (.Values.reloader.ignoreConfigMaps) (.Values.reloader.custom_annotations) (eq .Values.reloader.isArgoRollouts true) (eq .Values.reloader.reloadOnCreate true) (ne .Values.reloader.reloadStrategy "default") (.Values.reloader.enableHA) (.Values.reloader.autoReloadAll)}} args: {{- if .Values.reloader.logFormat }} - "--log-format={{ .Values.reloader.logFormat }}" @@ -222,6 +222,9 @@ spec: {{- if or (gt (int .Values.reloader.deployment.replicas) 1) (.Values.reloader.enableHA) }} - "--enable-ha=true" {{- end}} + {{- if eq .Values.reloader.autoReloadAll true }} + - "--auto-reload-all=true" + {{- end -}} {{- end }} {{- if .Values.reloader.deployment.resources }} resources: diff --git a/deployments/kubernetes/chart/reloader/values.yaml b/deployments/kubernetes/chart/reloader/values.yaml index 90d8b2e4..c6257f1e 100644 --- a/deployments/kubernetes/chart/reloader/values.yaml +++ b/deployments/kubernetes/chart/reloader/values.yaml @@ -12,6 +12,7 @@ nameOverride: "" fullnameOverride: "" reloader: + autoReloadAll: false isArgoRollouts: false isOpenshift: false ignoreSecrets: false diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index 874e776b..4e1e844e 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -32,6 +32,7 @@ func NewReloaderCommand() *cobra.Command { } // options + cmd.PersistentFlags().BoolVar(&options.AutoReloadAll, "auto-reload-all", false, "Auto reload all resources") cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateOnChangeAnnotation, "configmap-annotation", "configmap.reloader.stakater.com/reload", "annotation to detect changes in configmaps, specified by name") cmd.PersistentFlags().StringVar(&options.SecretUpdateOnChangeAnnotation, "secret-annotation", "secret.reloader.stakater.com/reload", "annotation to detect changes in secrets, specified by name") cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets") diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index af06a9f5..98377c17 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -153,8 +153,9 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation] } result := constants.NotUpdated - reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) - if err == nil && reloaderEnabled { + reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue) + reloaderEnabled = reloaderEnabled && foundAuto || !foundAuto && options.AutoReloadAll + if reloaderEnabled { result = invokeReloadStrategy(upgradeFuncs, i, config, true) } diff --git a/internal/pkg/options/flags.go b/internal/pkg/options/flags.go index 54c75dad..0faa6d22 100644 --- a/internal/pkg/options/flags.go +++ b/internal/pkg/options/flags.go @@ -3,6 +3,8 @@ package options import "github.com/stakater/Reloader/internal/pkg/constants" var ( + // Auto reload all resources when their corresponding configmaps/secrets are updated + AutoReloadAll = false // ConfigmapUpdateOnChangeAnnotation is an annotation to detect changes in // configmaps specified by name ConfigmapUpdateOnChangeAnnotation = "configmap.reloader.stakater.com/reload" From 18d8b7e3537fee9a0b01057a27ce3f5a43765f16 Mon Sep 17 00:00:00 2001 From: itaispiegel Date: Fri, 30 Jun 2023 20:02:37 +0300 Subject: [PATCH 2/8] Fix implementation to handle pod annotations --- internal/pkg/handler/upgrade.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 98377c17..d78f1c22 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -153,9 +153,8 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation] } result := constants.NotUpdated - reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue) - reloaderEnabled = reloaderEnabled && foundAuto || !foundAuto && options.AutoReloadAll - if reloaderEnabled { + reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) + if (err != nil && options.AutoReloadAll) || (err == nil && (reloaderEnabled || options.AutoReloadAll)) { result = invokeReloadStrategy(upgradeFuncs, i, config, true) } From ff1946b4062d6ac19341bd949142ab6a1d424e1d Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sat, 29 Jul 2023 11:01:40 +0300 Subject: [PATCH 3/8] Fix the reload condition --- internal/pkg/handler/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index d78f1c22..19d30b97 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -154,7 +154,7 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc } result := constants.NotUpdated reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) - if (err != nil && options.AutoReloadAll) || (err == nil && (reloaderEnabled || options.AutoReloadAll)) { + if err == nil && (reloaderEnabled || options.AutoReloadAll) { result = invokeReloadStrategy(upgradeFuncs, i, config, true) } From 4295b34cb1d02c96eda0c64a338ec3b6be773e1b Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sun, 6 Aug 2023 13:09:37 +0300 Subject: [PATCH 4/8] Fix: Handle empty reloaderEnabledValue --- internal/pkg/handler/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 19d30b97..7df70674 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -154,7 +154,7 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc } result := constants.NotUpdated reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) - if err == nil && (reloaderEnabled || options.AutoReloadAll) { + if (reloaderEnabledValue == "" || err == nil) && (reloaderEnabled || options.AutoReloadAll) { result = invokeReloadStrategy(upgradeFuncs, i, config, true) } From b5fde3876dd0fa1c41fa3deac27c5e9c66cecf74 Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sun, 6 Aug 2023 13:11:26 +0300 Subject: [PATCH 5/8] Add tests for the new feature --- internal/pkg/handler/upgrade_test.go | 92 ++++++++++++++++++++++++++++ internal/pkg/testutil/kube.go | 39 +++++++++++- 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index c0f5a539..5005f447 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -43,6 +43,7 @@ var ( arsConfigmapWithPodAnnotations = "testconfigmapPodAnnotations-handler-" + testutil.RandSeq(5) arsConfigmapWithBothAnnotations = "testconfigmapBothAnnotations-handler-" + testutil.RandSeq(5) arsConfigmapAnnotated = "testconfigmapAnnotated-handler-" + testutil.RandSeq(5) + arsConfigMapWithNonAnnotatedDeployment = "testconfigmapNonAnnotatedDeployment-handler-" + testutil.RandSeq(5) ersNamespace = "test-handler-" + testutil.RandSeq(5) ersConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) @@ -173,6 +174,11 @@ func setupArs() { logrus.Errorf("Error in configmap creation: %v", err) } + _, err = testutil.CreateConfigMap(clients.KubernetesClient, arsNamespace, arsConfigMapWithNonAnnotatedDeployment, "www.google.com") + if err != nil { + logrus.Errorf("Error in configmap creation: %v", err) + } + // Creating Deployment with configmap _, err = testutil.CreateDeployment(clients.KubernetesClient, arsConfigmapName, arsNamespace, true) if err != nil { @@ -268,6 +274,12 @@ func setupArs() { logrus.Errorf("Error in Deployment with secret configmap as envFrom source creation: %v", err) } + // Creating Deployment with configmap and without annotations + _, err = testutil.CreateDeploymentWithoutAnnotations(clients.KubernetesClient, arsConfigMapWithNonAnnotatedDeployment, arsNamespace) + if err != nil { + logrus.Errorf("Error in Deployment with configmap and without annotation creation: %v", err) + } + // Creating DaemonSet with configmap _, err = testutil.CreateDaemonSet(clients.KubernetesClient, arsConfigmapName, arsNamespace, true) if err != nil { @@ -1205,6 +1217,86 @@ func TestRollingUpgradeForDeploymentWithConfigmapUsingArs(t *testing.T) { } } +func TestRollingUpgradeForDeploymentWithConfigmapWithoutReloadAnnotationAndWithoutAutoReloadAllNoTriggersUsingArs(t *testing.T) { + options.ReloadStrategy = constants.AnnotationsReloadStrategy + + shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigMapWithNonAnnotatedDeployment, "www.stakater.com") + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigMapWithNonAnnotatedDeployment, shaData, options.ConfigmapUpdateOnChangeAnnotation) + deploymentFuncs := GetDeploymentRollingUpgradeFuncs() + collectors := getCollectors() + + err := PerformRollingUpgrade(clients, config, deploymentFuncs, collectors, nil) + time.Sleep(5 * time.Second) + if err != nil { + t.Errorf("Rolling upgrade failed for Deployment with Configmap") + } + + logrus.Infof("Verifying deployment update") + updated := testutil.VerifyResourceAnnotationUpdate(clients, config, deploymentFuncs) + if updated { + t.Errorf("Deployment was updated") + } + + if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) > 0 { + t.Errorf("Counter was increased") + } +} + +func TestRollingUpgradeForDeploymentWithConfigmapWithoutReloadAnnotationButWithAutoReloadAllUsingArs(t *testing.T) { + options.ReloadStrategy = constants.AnnotationsReloadStrategy + options.AutoReloadAll = true + defer func() { options.AutoReloadAll = false }() + + shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigMapWithNonAnnotatedDeployment, "www.stakater.com") + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigMapWithNonAnnotatedDeployment, shaData, "") + deploymentFuncs := GetDeploymentRollingUpgradeFuncs() + collectors := getCollectors() + + err := PerformRollingUpgrade(clients, config, deploymentFuncs, collectors, nil) + time.Sleep(5 * time.Second) + if err != nil { + t.Errorf("Rolling upgrade failed for Deployment with Configmap") + } + + logrus.Infof("Verifying deployment update") + updated := testutil.VerifyResourceAnnotationUpdate(clients, config, deploymentFuncs) + if !updated { + t.Errorf("Deployment was not updated") + } + + if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) != 1 { + t.Errorf("Counter was not increased") + } +} + +func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationAndAutoReloadAllSetToTrueNoTriggersUsingArs(t *testing.T) { + options.ReloadStrategy = constants.AnnotationsReloadStrategy + options.AutoReloadAll = true + defer func() { options.AutoReloadAll = false }() + + shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigmapAnnotated, "www.stakater.com") + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigmapAnnotated, shaData, "") + config.ResourceAnnotations = map[string]string{"reloader.stakater.com/match": "false"} + deploymentFuncs := GetDeploymentRollingUpgradeFuncs() + collectors := getCollectors() + + err := PerformRollingUpgrade(clients, config, deploymentFuncs, collectors, nil) + if err != nil { + t.Errorf("Rolling upgrade failed for Deployment with Configmap") + } + + logrus.Infof("Verifying deployment update") + updated := testutil.VerifyResourceAnnotationUpdate(clients, config, deploymentFuncs) + time.Sleep(5 * time.Second) + if updated { + t.Errorf("Deployment was updated unexpectedly") + } + + if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) > 0 { + t.Errorf("Counter was increased unexpectedly") + } +} + func TestRollingUpgradeForDeploymentWithConfigmapInProjectedVolumeUsingArs(t *testing.T) { options.ReloadStrategy = constants.AnnotationsReloadStrategy diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index 9d45c2b3..18c7bf5e 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -78,6 +78,15 @@ func getObjectMeta(namespace string, name string, autoReload bool) metav1.Object } } +func getObjectMetaWithoutAnnotations(namespace string, name string) metav1.ObjectMeta { + return metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + Labels: map[string]string{"firstLabel": "temp"}, + Annotations: map[string]string{}, + } +} + func getAnnotations(name string, autoReload bool) map[string]string { if autoReload { return map[string]string{ @@ -346,6 +355,23 @@ func GetDeployment(namespace string, deploymentName string) *appsv1.Deployment { } } +func getDeploymentWithoutAnnotations(namespace string, deploymentName string) *appsv1.Deployment { + replicaset := int32(1) + return &appsv1.Deployment{ + ObjectMeta: getObjectMetaWithoutAnnotations(namespace, deploymentName), + Spec: appsv1.DeploymentSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{"secondLabel": "temp"}, + }, + Replicas: &replicaset, + Strategy: appsv1.DeploymentStrategy{ + Type: appsv1.RollingUpdateDeploymentStrategyType, + }, + Template: getPodTemplateSpecWithVolumes(deploymentName), + }, + } +} + // GetDeploymentConfig provides deployment for testing func GetDeploymentConfig(namespace string, deploymentConfigName string) *openshiftv1.DeploymentConfig { replicaset := int32(1) @@ -666,6 +692,16 @@ func CreateDeployment(client kubernetes.Interface, deploymentName string, namesp return deployment, err } +// CreateDeployment creates a deployment in given namespace and returns the Deployment +func CreateDeploymentWithoutAnnotations(client kubernetes.Interface, deploymentName string, namespace string) (*appsv1.Deployment, error) { + logrus.Infof("Creating Deployment without annotations") + deploymentClient := client.AppsV1().Deployments(namespace) + deploymentObj := getDeploymentWithoutAnnotations(namespace, deploymentName) + deployment, err := deploymentClient.Create(context.TODO(), deploymentObj, metav1.CreateOptions{}) + time.Sleep(3 * time.Second) + return deployment, err +} + // CreateDeploymentConfig creates a deploymentConfig in given namespace and returns the DeploymentConfig func CreateDeploymentConfig(client appsclient.Interface, deploymentName string, namespace string, volumeMount bool) (*openshiftv1.DeploymentConfig, error) { logrus.Infof("Creating DeploymentConfig") @@ -892,6 +928,7 @@ func VerifyResourceEnvVarUpdate(clients kube.Clients, config util.Config, envVar func VerifyResourceAnnotationUpdate(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, i := range items { + logrus.Printf("Items iteration") podAnnotations := upgradeFuncs.PodAnnotationsFunc(i) accessor, err := meta.Accessor(i) if err != nil { @@ -904,7 +941,7 @@ func VerifyResourceAnnotationUpdate(clients kube.Clients, config util.Config, up reloaderEnabledValue := annotations[options.ReloaderAutoAnnotation] reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) matches := false - if err == nil && reloaderEnabled { + if (reloaderEnabledValue == "" || err == nil) && (reloaderEnabled || options.AutoReloadAll) { matches = true } else if annotationValue != "" { values := strings.Split(annotationValue, ",") From 3e6ccd0a45821112a0cff3bc31587eb9b2e7e44a Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sun, 6 Aug 2023 15:03:34 +0300 Subject: [PATCH 6/8] Fix upgrade condition --- internal/pkg/handler/upgrade.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 7df70674..bc2a04a4 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -153,8 +153,8 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation] } result := constants.NotUpdated - reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) - if (reloaderEnabledValue == "" || err == nil) && (reloaderEnabled || options.AutoReloadAll) { + reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue) + if reloaderEnabled || reloaderEnabledValue == "" && options.AutoReloadAll { result = invokeReloadStrategy(upgradeFuncs, i, config, true) } From b9e24b308ed5670a22627697d7ac7d3449b5b2ca Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sun, 6 Aug 2023 15:03:44 +0300 Subject: [PATCH 7/8] Fix tests according to the updated condition --- internal/pkg/handler/upgrade_test.go | 63 ++++++++++++++++++---------- internal/pkg/testutil/kube.go | 41 +----------------- 2 files changed, 42 insertions(+), 62 deletions(-) diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index 5005f447..a369fbdc 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -25,25 +25,26 @@ import ( var ( clients = kube.Clients{KubernetesClient: testclient.NewSimpleClientset()} - arsNamespace = "test-handler-" + testutil.RandSeq(5) - arsConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) - arsSecretName = "testsecret-handler-" + testutil.RandSeq(5) - arsProjectedConfigMapName = "testprojectedconfigmap-handler-" + testutil.RandSeq(5) - arsProjectedSecretName = "testprojectedsecret-handler-" + testutil.RandSeq(5) - arsConfigmapWithInitContainer = "testconfigmapInitContainerhandler-" + testutil.RandSeq(5) - arsSecretWithInitContainer = "testsecretWithInitContainer-handler-" + testutil.RandSeq(5) - arsProjectedConfigMapWithInitContainer = "testProjectedConfigMapWithInitContainer-handler" + testutil.RandSeq(5) - arsProjectedSecretWithInitContainer = "testProjectedSecretWithInitContainer-handler" + testutil.RandSeq(5) - arsConfigmapWithInitEnv = "configmapWithInitEnv-" + testutil.RandSeq(5) - arsSecretWithInitEnv = "secretWithInitEnv-handler-" + testutil.RandSeq(5) - arsConfigmapWithEnvName = "testconfigmapWithEnv-handler-" + testutil.RandSeq(5) - arsConfigmapWithEnvFromName = "testconfigmapWithEnvFrom-handler-" + testutil.RandSeq(5) - arsSecretWithEnvName = "testsecretWithEnv-handler-" + testutil.RandSeq(5) - arsSecretWithEnvFromName = "testsecretWithEnvFrom-handler-" + testutil.RandSeq(5) - arsConfigmapWithPodAnnotations = "testconfigmapPodAnnotations-handler-" + testutil.RandSeq(5) - arsConfigmapWithBothAnnotations = "testconfigmapBothAnnotations-handler-" + testutil.RandSeq(5) - arsConfigmapAnnotated = "testconfigmapAnnotated-handler-" + testutil.RandSeq(5) - arsConfigMapWithNonAnnotatedDeployment = "testconfigmapNonAnnotatedDeployment-handler-" + testutil.RandSeq(5) + arsNamespace = "test-handler-" + testutil.RandSeq(5) + arsConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) + arsSecretName = "testsecret-handler-" + testutil.RandSeq(5) + arsProjectedConfigMapName = "testprojectedconfigmap-handler-" + testutil.RandSeq(5) + arsProjectedSecretName = "testprojectedsecret-handler-" + testutil.RandSeq(5) + arsConfigmapWithInitContainer = "testconfigmapInitContainerhandler-" + testutil.RandSeq(5) + arsSecretWithInitContainer = "testsecretWithInitContainer-handler-" + testutil.RandSeq(5) + arsProjectedConfigMapWithInitContainer = "testProjectedConfigMapWithInitContainer-handler" + testutil.RandSeq(5) + arsProjectedSecretWithInitContainer = "testProjectedSecretWithInitContainer-handler" + testutil.RandSeq(5) + arsConfigmapWithInitEnv = "configmapWithInitEnv-" + testutil.RandSeq(5) + arsSecretWithInitEnv = "secretWithInitEnv-handler-" + testutil.RandSeq(5) + arsConfigmapWithEnvName = "testconfigmapWithEnv-handler-" + testutil.RandSeq(5) + arsConfigmapWithEnvFromName = "testconfigmapWithEnvFrom-handler-" + testutil.RandSeq(5) + arsSecretWithEnvName = "testsecretWithEnv-handler-" + testutil.RandSeq(5) + arsSecretWithEnvFromName = "testsecretWithEnvFrom-handler-" + testutil.RandSeq(5) + arsConfigmapWithPodAnnotations = "testconfigmapPodAnnotations-handler-" + testutil.RandSeq(5) + arsConfigmapWithBothAnnotations = "testconfigmapBothAnnotations-handler-" + testutil.RandSeq(5) + arsConfigmapAnnotated = "testconfigmapAnnotated-handler-" + testutil.RandSeq(5) + arsConfigMapWithNonAnnotatedDeployment = "testconfigmapNonAnnotatedDeployment-handler-" + testutil.RandSeq(5) + arsConfigMapWithDeploymentReloadSetToFalse = "testconfigMapWithDeploymentReloadSetToFalse-handler-" + testutil.RandSeq(5) ersNamespace = "test-handler-" + testutil.RandSeq(5) ersConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) @@ -179,6 +180,11 @@ func setupArs() { logrus.Errorf("Error in configmap creation: %v", err) } + _, err = testutil.CreateConfigMap(clients.KubernetesClient, arsNamespace, arsConfigMapWithDeploymentReloadSetToFalse, "www.google.com") + if err != nil { + logrus.Errorf("Error in configmap creation: %v", err) + } + // Creating Deployment with configmap _, err = testutil.CreateDeployment(clients.KubernetesClient, arsConfigmapName, arsNamespace, true) if err != nil { @@ -274,8 +280,19 @@ func setupArs() { logrus.Errorf("Error in Deployment with secret configmap as envFrom source creation: %v", err) } + // Creating Deployment with envFrom source as secret + _, err = testutil.CreateDeploymentWithEnvVarSourceAndAnnotations( + clients.KubernetesClient, + arsConfigMapWithDeploymentReloadSetToFalse, + arsNamespace, + map[string]string{options.ReloaderAutoAnnotation: "false"}, + ) + if err != nil { + logrus.Errorf("Error in Deployment with secret configmap as envFrom source creation: %v", err) + } + // Creating Deployment with configmap and without annotations - _, err = testutil.CreateDeploymentWithoutAnnotations(clients.KubernetesClient, arsConfigMapWithNonAnnotatedDeployment, arsNamespace) + _, err = testutil.CreateDeploymentWithEnvVarSourceAndAnnotations(clients.KubernetesClient, arsConfigMapWithNonAnnotatedDeployment, arsNamespace, map[string]string{}) if err != nil { logrus.Errorf("Error in Deployment with configmap and without annotation creation: %v", err) } @@ -360,6 +377,7 @@ func setupArs() { // Creating Deployment with both annotations _, err = testutil.CreateDeploymentWithPodAnnotations(clients.KubernetesClient, arsConfigmapWithBothAnnotations, arsNamespace, true) + if err != nil { logrus.Errorf("Error in Deployment with both annotations: %v", err) } @@ -1274,9 +1292,8 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationAndAutoReloa options.AutoReloadAll = true defer func() { options.AutoReloadAll = false }() - shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigmapAnnotated, "www.stakater.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigmapAnnotated, shaData, "") - config.ResourceAnnotations = map[string]string{"reloader.stakater.com/match": "false"} + shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigMapWithDeploymentReloadSetToFalse, "www.stakater.com") + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigMapWithDeploymentReloadSetToFalse, shaData, options.ReloaderAutoAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() collectors := getCollectors() diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index 18c7bf5e..765a04a2 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -78,15 +78,6 @@ func getObjectMeta(namespace string, name string, autoReload bool) metav1.Object } } -func getObjectMetaWithoutAnnotations(namespace string, name string) metav1.ObjectMeta { - return metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - Labels: map[string]string{"firstLabel": "temp"}, - Annotations: map[string]string{}, - } -} - func getAnnotations(name string, autoReload bool) map[string]string { if autoReload { return map[string]string{ @@ -355,23 +346,6 @@ func GetDeployment(namespace string, deploymentName string) *appsv1.Deployment { } } -func getDeploymentWithoutAnnotations(namespace string, deploymentName string) *appsv1.Deployment { - replicaset := int32(1) - return &appsv1.Deployment{ - ObjectMeta: getObjectMetaWithoutAnnotations(namespace, deploymentName), - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{"secondLabel": "temp"}, - }, - Replicas: &replicaset, - Strategy: appsv1.DeploymentStrategy{ - Type: appsv1.RollingUpdateDeploymentStrategyType, - }, - Template: getPodTemplateSpecWithVolumes(deploymentName), - }, - } -} - // GetDeploymentConfig provides deployment for testing func GetDeploymentConfig(namespace string, deploymentConfigName string) *openshiftv1.DeploymentConfig { replicaset := int32(1) @@ -692,16 +666,6 @@ func CreateDeployment(client kubernetes.Interface, deploymentName string, namesp return deployment, err } -// CreateDeployment creates a deployment in given namespace and returns the Deployment -func CreateDeploymentWithoutAnnotations(client kubernetes.Interface, deploymentName string, namespace string) (*appsv1.Deployment, error) { - logrus.Infof("Creating Deployment without annotations") - deploymentClient := client.AppsV1().Deployments(namespace) - deploymentObj := getDeploymentWithoutAnnotations(namespace, deploymentName) - deployment, err := deploymentClient.Create(context.TODO(), deploymentObj, metav1.CreateOptions{}) - time.Sleep(3 * time.Second) - return deployment, err -} - // CreateDeploymentConfig creates a deploymentConfig in given namespace and returns the DeploymentConfig func CreateDeploymentConfig(client appsclient.Interface, deploymentName string, namespace string, volumeMount bool) (*openshiftv1.DeploymentConfig, error) { logrus.Infof("Creating DeploymentConfig") @@ -928,7 +892,6 @@ func VerifyResourceEnvVarUpdate(clients kube.Clients, config util.Config, envVar func VerifyResourceAnnotationUpdate(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, i := range items { - logrus.Printf("Items iteration") podAnnotations := upgradeFuncs.PodAnnotationsFunc(i) accessor, err := meta.Accessor(i) if err != nil { @@ -939,9 +902,9 @@ func VerifyResourceAnnotationUpdate(clients kube.Clients, config util.Config, up annotationValue := annotations[config.Annotation] searchAnnotationValue := annotations[options.AutoSearchAnnotation] reloaderEnabledValue := annotations[options.ReloaderAutoAnnotation] - reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) + reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue) matches := false - if (reloaderEnabledValue == "" || err == nil) && (reloaderEnabled || options.AutoReloadAll) { + if reloaderEnabled || reloaderEnabledValue == "" && options.AutoReloadAll { matches = true } else if annotationValue != "" { values := strings.Split(annotationValue, ",") From cabe0d8ba42429e86173ac5ea651a32b1f33c8b5 Mon Sep 17 00:00:00 2001 From: Itai Spiegel Date: Sun, 6 Aug 2023 17:51:02 +0300 Subject: [PATCH 8/8] Fix tests --- internal/pkg/handler/upgrade_test.go | 82 +++++++--------------------- 1 file changed, 19 insertions(+), 63 deletions(-) diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index a369fbdc..88f11584 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -25,26 +25,25 @@ import ( var ( clients = kube.Clients{KubernetesClient: testclient.NewSimpleClientset()} - arsNamespace = "test-handler-" + testutil.RandSeq(5) - arsConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) - arsSecretName = "testsecret-handler-" + testutil.RandSeq(5) - arsProjectedConfigMapName = "testprojectedconfigmap-handler-" + testutil.RandSeq(5) - arsProjectedSecretName = "testprojectedsecret-handler-" + testutil.RandSeq(5) - arsConfigmapWithInitContainer = "testconfigmapInitContainerhandler-" + testutil.RandSeq(5) - arsSecretWithInitContainer = "testsecretWithInitContainer-handler-" + testutil.RandSeq(5) - arsProjectedConfigMapWithInitContainer = "testProjectedConfigMapWithInitContainer-handler" + testutil.RandSeq(5) - arsProjectedSecretWithInitContainer = "testProjectedSecretWithInitContainer-handler" + testutil.RandSeq(5) - arsConfigmapWithInitEnv = "configmapWithInitEnv-" + testutil.RandSeq(5) - arsSecretWithInitEnv = "secretWithInitEnv-handler-" + testutil.RandSeq(5) - arsConfigmapWithEnvName = "testconfigmapWithEnv-handler-" + testutil.RandSeq(5) - arsConfigmapWithEnvFromName = "testconfigmapWithEnvFrom-handler-" + testutil.RandSeq(5) - arsSecretWithEnvName = "testsecretWithEnv-handler-" + testutil.RandSeq(5) - arsSecretWithEnvFromName = "testsecretWithEnvFrom-handler-" + testutil.RandSeq(5) - arsConfigmapWithPodAnnotations = "testconfigmapPodAnnotations-handler-" + testutil.RandSeq(5) - arsConfigmapWithBothAnnotations = "testconfigmapBothAnnotations-handler-" + testutil.RandSeq(5) - arsConfigmapAnnotated = "testconfigmapAnnotated-handler-" + testutil.RandSeq(5) - arsConfigMapWithNonAnnotatedDeployment = "testconfigmapNonAnnotatedDeployment-handler-" + testutil.RandSeq(5) - arsConfigMapWithDeploymentReloadSetToFalse = "testconfigMapWithDeploymentReloadSetToFalse-handler-" + testutil.RandSeq(5) + arsNamespace = "test-handler-" + testutil.RandSeq(5) + arsConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) + arsSecretName = "testsecret-handler-" + testutil.RandSeq(5) + arsProjectedConfigMapName = "testprojectedconfigmap-handler-" + testutil.RandSeq(5) + arsProjectedSecretName = "testprojectedsecret-handler-" + testutil.RandSeq(5) + arsConfigmapWithInitContainer = "testconfigmapInitContainerhandler-" + testutil.RandSeq(5) + arsSecretWithInitContainer = "testsecretWithInitContainer-handler-" + testutil.RandSeq(5) + arsProjectedConfigMapWithInitContainer = "testProjectedConfigMapWithInitContainer-handler" + testutil.RandSeq(5) + arsProjectedSecretWithInitContainer = "testProjectedSecretWithInitContainer-handler" + testutil.RandSeq(5) + arsConfigmapWithInitEnv = "configmapWithInitEnv-" + testutil.RandSeq(5) + arsSecretWithInitEnv = "secretWithInitEnv-handler-" + testutil.RandSeq(5) + arsConfigmapWithEnvName = "testconfigmapWithEnv-handler-" + testutil.RandSeq(5) + arsConfigmapWithEnvFromName = "testconfigmapWithEnvFrom-handler-" + testutil.RandSeq(5) + arsSecretWithEnvName = "testsecretWithEnv-handler-" + testutil.RandSeq(5) + arsSecretWithEnvFromName = "testsecretWithEnvFrom-handler-" + testutil.RandSeq(5) + arsConfigmapWithPodAnnotations = "testconfigmapPodAnnotations-handler-" + testutil.RandSeq(5) + arsConfigmapWithBothAnnotations = "testconfigmapBothAnnotations-handler-" + testutil.RandSeq(5) + arsConfigmapAnnotated = "testconfigmapAnnotated-handler-" + testutil.RandSeq(5) + arsConfigMapWithNonAnnotatedDeployment = "testconfigmapNonAnnotatedDeployment-handler-" + testutil.RandSeq(5) ersNamespace = "test-handler-" + testutil.RandSeq(5) ersConfigmapName = "testconfigmap-handler-" + testutil.RandSeq(5) @@ -180,11 +179,6 @@ func setupArs() { logrus.Errorf("Error in configmap creation: %v", err) } - _, err = testutil.CreateConfigMap(clients.KubernetesClient, arsNamespace, arsConfigMapWithDeploymentReloadSetToFalse, "www.google.com") - if err != nil { - logrus.Errorf("Error in configmap creation: %v", err) - } - // Creating Deployment with configmap _, err = testutil.CreateDeployment(clients.KubernetesClient, arsConfigmapName, arsNamespace, true) if err != nil { @@ -280,17 +274,6 @@ func setupArs() { logrus.Errorf("Error in Deployment with secret configmap as envFrom source creation: %v", err) } - // Creating Deployment with envFrom source as secret - _, err = testutil.CreateDeploymentWithEnvVarSourceAndAnnotations( - clients.KubernetesClient, - arsConfigMapWithDeploymentReloadSetToFalse, - arsNamespace, - map[string]string{options.ReloaderAutoAnnotation: "false"}, - ) - if err != nil { - logrus.Errorf("Error in Deployment with secret configmap as envFrom source creation: %v", err) - } - // Creating Deployment with configmap and without annotations _, err = testutil.CreateDeploymentWithEnvVarSourceAndAnnotations(clients.KubernetesClient, arsConfigMapWithNonAnnotatedDeployment, arsNamespace, map[string]string{}) if err != nil { @@ -1287,33 +1270,6 @@ func TestRollingUpgradeForDeploymentWithConfigmapWithoutReloadAnnotationButWithA } } -func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationAndAutoReloadAllSetToTrueNoTriggersUsingArs(t *testing.T) { - options.ReloadStrategy = constants.AnnotationsReloadStrategy - options.AutoReloadAll = true - defer func() { options.AutoReloadAll = false }() - - shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, arsNamespace, arsConfigMapWithDeploymentReloadSetToFalse, "www.stakater.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, arsConfigMapWithDeploymentReloadSetToFalse, shaData, options.ReloaderAutoAnnotation) - deploymentFuncs := GetDeploymentRollingUpgradeFuncs() - collectors := getCollectors() - - err := PerformRollingUpgrade(clients, config, deploymentFuncs, collectors, nil) - if err != nil { - t.Errorf("Rolling upgrade failed for Deployment with Configmap") - } - - logrus.Infof("Verifying deployment update") - updated := testutil.VerifyResourceAnnotationUpdate(clients, config, deploymentFuncs) - time.Sleep(5 * time.Second) - if updated { - t.Errorf("Deployment was updated unexpectedly") - } - - if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) > 0 { - t.Errorf("Counter was increased unexpectedly") - } -} - func TestRollingUpgradeForDeploymentWithConfigmapInProjectedVolumeUsingArs(t *testing.T) { options.ReloadStrategy = constants.AnnotationsReloadStrategy