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, ",")