From 797d23f7146243b6d170b5c436c97d65679d46fb Mon Sep 17 00:00:00 2001 From: Gianluca Mardente Date: Fri, 4 Dec 2026 09:35:32 +0100 Subject: [PATCH] fix: stop writing extraLabels/extraAnnotations to Helm releases after install addExtraMetadata read an existing Helm release through the managed cluster rest.Config carried in the context, but built the client used for the writes with clientcmd.BuildConfigFromFlags("", kubeconfig). In push mode kubeconfig is empty, so client-go fell back to the in-cluster config and the server-side apply was sent to the management cluster with addon-controller's ServiceAccount. A chart whose manifest contained a kind known to the management cluster could create or overwrite objects there. ExtraLabels and ExtraAnnotations are deprecated in favor of Patches, which Helm applies through its post-renderer using the managed cluster client. Drop addExtraMetadata and the helpers only it used instead of fixing the client. The path was only reachable in push mode: pull mode returns earlier in handleChart. Resources deployed outside Helm still get the extra labels and annotations through deployer.AddMetadata. --- controllers/export_test.go | 4 +- controllers/handlers_helm.go | 138 ----------------------------- controllers/handlers_utils_test.go | 56 ------------ 3 files changed, 1 insertion(+), 197 deletions(-) diff --git a/controllers/export_test.go b/controllers/export_test.go index 5b5a2361..2296539c 100644 --- a/controllers/export_test.go +++ b/controllers/export_test.go @@ -170,9 +170,7 @@ var ( GetClusterScopingAnnotation = getClusterScopingAnnotation AddStaleResourceScopingAnnotations = addStaleResourceScopingAnnotations - AddExtraLabels = addExtraLabels - AddExtraAnnotations = addExtraAnnotations - AdjustNamespace = adjustNamespace + AdjustNamespace = adjustNamespace ResourcesHash = resourcesHash ResourcesConfiguration = resourcesConfiguration diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index cc25699a..7c00d9a4 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -1943,13 +1943,6 @@ func deployHelmChart(ctx context.Context, dCtx *deploymentContext, } } - if currentRelease != nil { - err = addExtraMetadata(ctx, instantiatedChart, dCtx.clusterSummary, kubeconfig, registryOptions, logger) - if err != nil { - return nil, nil, err - } - } - currentRelease, err = getReleaseInfo(ctx, instantiatedChart.ReleaseName, instantiatedChart.ReleaseNamespace, kubeconfig, registryOptions, getEnableClientCacheValue(instantiatedChart.Options)) if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) { @@ -4986,103 +4979,6 @@ func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfi return uninstallClient } -func addExtraMetadata(ctx context.Context, requestedChart *configv1beta1.HelmChart, - clusterSummary *configv1beta1.ClusterSummary, kubeconfig string, - registryOptions *registryClientOptions, logger logr.Logger) error { - - // No-op in DryRun mode - if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun { - return nil - } - - if clusterSummary.Spec.ClusterProfileSpec.ExtraLabels == nil && - clusterSummary.Spec.ClusterProfileSpec.ExtraAnnotations == nil { - - return nil - } - - actionConfig, err := actionConfigInit(ctx, requestedChart.ReleaseNamespace, kubeconfig, registryOptions, - getEnableClientCacheValue(requestedChart.Options)) - if err != nil { - return err - } - - statusObject := action.NewStatus(actionConfig) - rawResults, err := statusObject.Run(requestedChart.ReleaseName) - if err != nil { - return err - } - results, ok := rawResults.(*releasev1.Release) - if !ok { - return fmt.Errorf("unexpected release type %T", rawResults) - } - - resources, err := collectHelmContent(results, install, true, false, logger) - if err != nil { - return err - } - - config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) - if err != nil { - logger.Error(err, "BuildConfigFromFlags") - return err - } - - logger.V(logs.LogDebug).Info(fmt.Sprintf("adding extra labels/annotations to %d resources", len(resources))) - for i := range resources { - r := resources[i] - - // For some helm charts, collectHelmContent returns an empty namespace for namespaced resources - // If resource is a namespaced one and namespace is empty, set namespace to release namespace. - namespace, err := getResourceNamespace(ctx, r, requestedChart.ReleaseNamespace, - clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, - clusterSummary.Spec.ClusterType, logger) - if err != nil { - return err - } - - var dr dynamic.ResourceInterface - dr, err = k8s_utils.GetDynamicResourceInterface(config, r.GroupVersionKind(), namespace) - if err != nil { - return err - } - - addExtraLabels(r, clusterSummary.Spec.ClusterProfileSpec.ExtraLabels) - addExtraAnnotations(r, clusterSummary.Spec.ClusterProfileSpec.ExtraAnnotations) - - isDriftDetection := clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeContinuousWithDriftDetection - isDryRun := clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun - _, err = deployer.UpdateResource(ctx, dr, isDriftDetection, isDryRun, getForceValue(requestedChart.Options), - clusterSummary.Spec.ClusterProfileSpec.DriftExclusions, r, []string{}, logger) - if err != nil { - logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to update resource %s %s/%s: %v", - r.GetKind(), r.GetNamespace(), r.GetName(), err)) - return err - } - } - - return nil -} - -func getResourceNamespace(ctx context.Context, r *unstructured.Unstructured, releaseNamespace string, - clusterNamespace, clusterName string, clusterType libsveltosv1beta1.ClusterType, - logger logr.Logger) (string, error) { - - namespace := r.GetNamespace() - if namespace == "" { - namespacedResource, err := isNamespaced(ctx, r, clusterNamespace, clusterName, clusterType, - false, logger) - if err != nil { - return "", err - } - if namespacedResource { - namespace = releaseNamespace - } - } - - return namespace, nil -} - // desiredValuesAreSubset returns true when every key-value pair in desired is present // with an equal value in full. Nested maps are checked recursively. This lets Sveltos // confirm that the values it would set are already reflected in the deployed release's @@ -5916,40 +5812,6 @@ func canManageChart(ctx context.Context, c client.Client, clusterSummary *config return determineChartOwnership(ctx, c, clusterSummary, instantiatedChart, logger) } -// addExtraLabels adds ExtraLabels to policy. -// If policy already has a label with a key present in `ExtraLabels`, the value from `ExtraLabels` will -// override the existing value. -func addExtraLabels(policy *unstructured.Unstructured, extraLabels map[string]string) { - if extraLabels == nil { - return - } - - if len(extraLabels) == 0 { - return - } - - for k := range extraLabels { - deployer.AddLabel(policy, k, extraLabels[k]) - } -} - -// addExtraAnnotations adds ExtraAnnotations to policy. -// If policy already has an annotation with a key present in `ExtraAnnotations`, the value from `ExtraAnnotations` -// will override the existing value. -func addExtraAnnotations(policy *unstructured.Unstructured, extraAnnotations map[string]string) { - if extraAnnotations == nil { - return - } - - if len(extraAnnotations) == 0 { - return - } - - for k := range extraAnnotations { - deployer.AddAnnotation(policy, k, extraAnnotations[k]) - } -} - // If in pull mode either remove resources or treat it as an upgrade func prepareChartForAgent(ctx context.Context, dCtx *deploymentContext, instantiatedChart *configv1beta1.HelmChart, diff --git a/controllers/handlers_utils_test.go b/controllers/handlers_utils_test.go index 9dec6baa..58788260 100644 --- a/controllers/handlers_utils_test.go +++ b/controllers/handlers_utils_test.go @@ -1716,62 +1716,6 @@ var _ = Describe("HandlersUtils", func() { }, timeout, pollingInterval).Should(BeNil()) }) - It("addExtraLabels adds extra labels on unstructured", func() { - u := &unstructured.Unstructured{} - extraLabels := map[string]string{ - randomString(): randomString(), - randomString(): randomString(), - } - - controllers.AddExtraLabels(u, extraLabels) - labels := u.GetLabels() - Expect(labels).ToNot(BeNil()) - for k := range extraLabels { - Expect(labels[k]).To(Equal(extraLabels[k])) - } - - // Add extra labels again - extraLabels = map[string]string{ - randomString(): randomString(), - randomString(): randomString(), - } - - controllers.AddExtraLabels(u, extraLabels) - labels = u.GetLabels() - Expect(labels).ToNot(BeNil()) - for k := range extraLabels { - Expect(labels[k]).To(Equal(extraLabels[k])) - } - }) - - It("addExtraAnnotations adds extra annotations on unstructured", func() { - u := &unstructured.Unstructured{} - extraAnnotations := map[string]string{ - randomString(): randomString(), - randomString(): randomString(), - } - - controllers.AddExtraAnnotations(u, extraAnnotations) - annotations := u.GetAnnotations() - Expect(annotations).ToNot(BeNil()) - for k := range extraAnnotations { - Expect(annotations[k]).To(Equal(extraAnnotations[k])) - } - - // Add extra annotations again - extraAnnotations = map[string]string{ - randomString(): randomString(), - randomString(): randomString(), - } - - controllers.AddExtraAnnotations(u, extraAnnotations) - annotations = u.GetAnnotations() - Expect(annotations).ToNot(BeNil()) - for k := range extraAnnotations { - Expect(annotations[k]).To(Equal(extraAnnotations[k])) - } - }) - It("adjustNamespace adjusts namespace for both namespaced and cluster wide resources", func() { logger := textlogger.NewLogger(textlogger.NewConfig())