From 482fd2408c9755b2b78f1f9e2fb73ac1fe70e6cb Mon Sep 17 00:00:00 2001 From: mgianluc Date: Fri, 6 Dec 2024 08:20:32 +0100 Subject: [PATCH] StopMatchingBehavior: accept only two correct values Enforce validations for StopMatchingBehavior. This PR also changes behavior when in DryRun mode. Value hashes are not considered in DryRun mode. That is because in this mode if version is same, value diffs will be evaluated and reported. --- api/v1alpha1/spec.go | 1 + api/v1beta1/spec.go | 1 + ...fig.projectsveltos.io_clusterprofiles.yaml | 6 +++++ ...ig.projectsveltos.io_clustersummaries.yaml | 6 +++++ .../config.projectsveltos.io_profiles.yaml | 6 +++++ controllers/handlers_helm.go | 23 +++++++++++-------- manifest/manifest.yaml | 18 +++++++++++++++ 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/api/v1alpha1/spec.go b/api/v1alpha1/spec.go index 82854117..cc442eff 100644 --- a/api/v1alpha1/spec.go +++ b/api/v1alpha1/spec.go @@ -436,6 +436,7 @@ type KustomizationRef struct { // a ClusterProfile. By default, withdrawpolicies, deployed Helm charts and Kubernetes // resources will be removed from Cluster. LeavePolicy instead leaves Helm charts // and Kubernetes policies in the Cluster. +// +kubebuilder:validation:Enum:=WithdrawPolicies;LeavePolicies type StopMatchingBehavior string // Define the StopMatchingBehavior constants. diff --git a/api/v1beta1/spec.go b/api/v1beta1/spec.go index 1313561b..af2408e6 100644 --- a/api/v1beta1/spec.go +++ b/api/v1beta1/spec.go @@ -505,6 +505,7 @@ type KustomizationRef struct { // a ClusterProfile. By default, withdrawpolicies, deployed Helm charts and Kubernetes // resources will be removed from Cluster. LeavePolicy instead leaves Helm charts // and Kubernetes policies in the Cluster. +// +kubebuilder:validation:Enum:=WithdrawPolicies;LeavePolicies type StopMatchingBehavior string // Define the StopMatchingBehavior constants. diff --git a/config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml b/config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml index 5cb816cb..24fc5d84 100644 --- a/config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml +++ b/config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml @@ -583,6 +583,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -1781,6 +1784,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous diff --git a/config/crd/bases/config.projectsveltos.io_clustersummaries.yaml b/config/crd/bases/config.projectsveltos.io_clustersummaries.yaml index f8156dc8..559c4d52 100644 --- a/config/crd/bases/config.projectsveltos.io_clustersummaries.yaml +++ b/config/crd/bases/config.projectsveltos.io_clustersummaries.yaml @@ -601,6 +601,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -1809,6 +1812,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous diff --git a/config/crd/bases/config.projectsveltos.io_profiles.yaml b/config/crd/bases/config.projectsveltos.io_profiles.yaml index 434d0834..43cc5eb7 100644 --- a/config/crd/bases/config.projectsveltos.io_profiles.yaml +++ b/config/crd/bases/config.projectsveltos.io_profiles.yaml @@ -583,6 +583,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -1781,6 +1784,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index 0d520f8c..64cb9352 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -1361,17 +1361,20 @@ func shouldUpgrade(ctx context.Context, currentRelease *releaseInfo, requestedCh } if clusterSummary.Spec.ClusterProfileSpec.SyncMode != configv1beta1.SyncModeContinuousWithDriftDetection { - oldValueHash := getValueHashFromHelmChartSummary(requestedChart, clusterSummary) + if clusterSummary.Spec.ClusterProfileSpec.SyncMode != configv1beta1.SyncModeDryRun { + // In DryRun mode, if values are different, report will be generated + oldValueHash := getValueHashFromHelmChartSummary(requestedChart, clusterSummary) - // If Values configuration has changed, trigger an upgrade - c := getManagementClusterClient() - currentValueHash, err := getHelmChartValuesHash(ctx, c, requestedChart, clusterSummary, logger) - if err != nil { - logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get current values hash: %v", err)) - currentValueHash = []byte("") // force upgrade - } - if !reflect.DeepEqual(oldValueHash, currentValueHash) { - return true + // If Values configuration has changed, trigger an upgrade + c := getManagementClusterClient() + currentValueHash, err := getHelmChartValuesHash(ctx, c, requestedChart, clusterSummary, logger) + if err != nil { + logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get current values hash: %v", err)) + currentValueHash = []byte("") // force upgrade + } + if !reflect.DeepEqual(oldValueHash, currentValueHash) { + return true + } } // With drift detection mode, there is reconciliation due to configuration drift even diff --git a/manifest/manifest.yaml b/manifest/manifest.yaml index 9dc6fd25..0ea5c2a8 100644 --- a/manifest/manifest.yaml +++ b/manifest/manifest.yaml @@ -1364,6 +1364,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -2562,6 +2565,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -4184,6 +4190,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -5392,6 +5401,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -6327,6 +6339,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous @@ -7525,6 +7540,9 @@ spec: the ClusterProfile. By default all deployed Helm charts and Kubernetes resources will be withdrawn from Cluster. Setting StopMatchingBehavior to LeavePolicies will instead leave ClusterProfile deployed policies in the Cluster. + enum: + - WithdrawPolicies + - LeavePolicies type: string syncMode: default: Continuous