diff --git a/controllers/clustersummary_controller.go b/controllers/clustersummary_controller.go index 664f0234..58b12c34 100644 --- a/controllers/clustersummary_controller.go +++ b/controllers/clustersummary_controller.go @@ -213,7 +213,7 @@ func (r *ClusterSummaryReconciler) Reconcile(ctx context.Context, req ctrl.Reque r.resetFeatureStatus(clusterSummaryScope, configv1beta1.FeatureStatusFailed) // if cluster is not ready, do nothing and don't queue for reconciliation. // When cluster becomes ready, all matching clusterSummaries will be requeued for reconciliation - _ = r.updateMaps(clusterSummaryScope, logger) + _ = r.updateMaps(ctx, clusterSummaryScope, logger) return reconcile.Result{}, nil } @@ -337,7 +337,7 @@ func (r *ClusterSummaryReconciler) reconcileNormal( return reconcile.Result{}, nil } - err := r.updateMaps(clusterSummaryScope, logger) + err := r.updateMaps(ctx, clusterSummaryScope, logger) if err != nil { return reconcile.Result{}, err } @@ -711,13 +711,15 @@ func (r *ClusterSummaryReconciler) cleanMaps(clusterSummaryScope *scope.ClusterS } } -func (r *ClusterSummaryReconciler) updateMaps(clusterSummaryScope *scope.ClusterSummaryScope, logger logr.Logger) error { +func (r *ClusterSummaryReconciler) updateMaps(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope, + logger logr.Logger) error { + if clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeOneTime { logger.V(logs.LogDebug).Info("sync mode is one time. No need to reconcile on policies change.") return nil } logger.V(logs.LogDebug).Info("update policy map") - currentReferences, err := r.getCurrentReferences(clusterSummaryScope) + currentReferences, err := r.getCurrentReferences(ctx, clusterSummaryScope) if err != nil { logger.V(logs.LogInfo).Info("failed to get current references: %v", err) return err @@ -834,21 +836,21 @@ func (r *ClusterSummaryReconciler) shouldReconcile(clusterSummaryScope *scope.Cl return false } -func (r *ClusterSummaryReconciler) getCurrentReferences(clusterSummaryScope *scope.ClusterSummaryScope, -) (*libsveltosset.Set, error) { +func (r *ClusterSummaryReconciler) getCurrentReferences(ctx context.Context, + clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) { - currentReferences, err := r.getPolicyRefReferences(clusterSummaryScope) + currentReferences, err := r.getPolicyRefReferences(ctx, clusterSummaryScope) if err != nil { return nil, err } - kustomizationRefs, err := r.getKustomizationRefReferences(clusterSummaryScope) + kustomizationRefs, err := r.getKustomizationRefReferences(ctx, clusterSummaryScope) if err != nil { return nil, err } currentReferences.Append(kustomizationRefs) - helmRefs, err := r.getHelmChartsReferences(clusterSummaryScope) + helmRefs, err := r.getHelmChartsReferences(ctx, clusterSummaryScope) if err != nil { return nil, err } @@ -858,17 +860,22 @@ func (r *ClusterSummaryReconciler) getCurrentReferences(clusterSummaryScope *sco } // getPolicyRefReferences get all references considering the PolicyRef section -func (r *ClusterSummaryReconciler) getPolicyRefReferences(clusterSummaryScope *scope.ClusterSummaryScope, -) (*libsveltosset.Set, error) { +func (r *ClusterSummaryReconciler) getPolicyRefReferences(ctx context.Context, + clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) { + cs := clusterSummaryScope.ClusterSummary currentReferences := &libsveltosset.Set{} for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs { referencedNamespace := clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Namespace - namespace := libsveltostemplate.GetReferenceResourceNamespace(clusterSummaryScope.Namespace(), referencedNamespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType) + if err != nil { + return nil, err + } - cs := clusterSummaryScope.ClusterSummary - referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace, cs.Spec.ClusterName, - string(cs.Spec.ClusterType), clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name) + referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, + clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name, cs.Spec.ClusterType) if err != nil { return nil, err } @@ -884,21 +891,24 @@ func (r *ClusterSummaryReconciler) getPolicyRefReferences(clusterSummaryScope *s } // getKustomizationRefReferences get all references considering the KustomizationRef section -func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryScope *scope.ClusterSummaryScope, -) (*libsveltosset.Set, error) { +func (r *ClusterSummaryReconciler) getKustomizationRefReferences(ctx context.Context, + clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) { + cs := clusterSummaryScope.ClusterSummary currentReferences := &libsveltosset.Set{} for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.KustomizationRefs { kr := &clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.KustomizationRefs[i] referencedNamespace := kr.Namespace - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummaryScope.Namespace(), referencedNamespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType) + if err != nil { + return nil, err + } - cs := clusterSummaryScope.ClusterSummary - referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace, - cs.Spec.ClusterName, string(cs.Spec.ClusterType), kr.Name) + referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, kr.Name, cs.Spec.ClusterType) if err != nil { return nil, err } @@ -921,7 +931,7 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryS Name: referencedName, }) - valuesFromReferences, err := getKustomizationValueFrom(clusterSummaryScope, kr) + valuesFromReferences, err := getKustomizationValueFrom(ctx, clusterSummaryScope, kr) if err != nil { return nil, err } @@ -933,19 +943,22 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryS // getKustomizationValueFrom gets referenced ConfigMap/Secret in a KustomizationRef. // KustomizationRef can reference both ConfigMap/Secret each containing key-value pairs that will be used, if defined, // to replace placeholder value in the output generated by Kustomize SDK. -func getKustomizationValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, kr *configv1beta1.KustomizationRef, -) (*libsveltosset.Set, error) { +func getKustomizationValueFrom(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope, + kr *configv1beta1.KustomizationRef) (*libsveltosset.Set, error) { currentValuesFromReferences := &libsveltosset.Set{} + cs := clusterSummaryScope.ClusterSummary for i := range kr.ValuesFrom { referencedNamespace := kr.ValuesFrom[i].Namespace - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummaryScope.Namespace(), referencedNamespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType) + if err != nil { + return nil, err + } - cs := clusterSummaryScope.ClusterSummary - referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace, - cs.Spec.ClusterName, string(cs.Spec.ClusterType), kr.ValuesFrom[i].Name) + referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, kr.ValuesFrom[i].Name, cs.Spec.ClusterType) if err != nil { return nil, err } @@ -962,8 +975,8 @@ func getKustomizationValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, k } // getHelmChartsReferences get all references considering the HelmChart section -func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope *scope.ClusterSummaryScope, -) (*libsveltosset.Set, error) { +func (r *ClusterSummaryReconciler) getHelmChartsReferences(ctx context.Context, + clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) { currentReferences := &libsveltosset.Set{} for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.HelmCharts { @@ -977,7 +990,7 @@ func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope * currentReferences.Insert(sourceRef) } - valuesFromReferences, err := getHelmChartValueFrom(clusterSummaryScope, hc) + valuesFromReferences, err := getHelmChartValueFrom(ctx, clusterSummaryScope, hc) if err != nil { return nil, err } @@ -988,19 +1001,22 @@ func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope * // getHelmChartValueFrom gets referenced ConfigMap/Secret in a HelmChart. // HelmChart can reference both ConfigMap/Secret each containing configuration for the helm release. -func getHelmChartValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, hc *configv1beta1.HelmChart, -) (*libsveltosset.Set, error) { +func getHelmChartValueFrom(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope, + hc *configv1beta1.HelmChart) (*libsveltosset.Set, error) { currentValuesFromReferences := &libsveltosset.Set{} + cs := clusterSummaryScope.ClusterSummary for i := range hc.ValuesFrom { referencedNamespace := hc.ValuesFrom[i].Namespace - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummaryScope.Namespace(), referencedNamespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType) + if err != nil { + return nil, err + } - cs := clusterSummaryScope.ClusterSummary - referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace, - cs.Spec.ClusterName, string(cs.Spec.ClusterType), hc.ValuesFrom[i].Name) + referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(), + cs.Spec.ClusterNamespace, cs.Spec.ClusterName, hc.ValuesFrom[i].Name, cs.Spec.ClusterType) if err != nil { return nil, err } diff --git a/controllers/clustersummary_controller_test.go b/controllers/clustersummary_controller_test.go index 37cb549c..3b44f3be 100644 --- a/controllers/clustersummary_controller_test.go +++ b/controllers/clustersummary_controller_test.go @@ -870,7 +870,7 @@ var _ = Describe("ClustersummaryController", func() { clusterSummaryScope := getClusterSummaryScope(c, textlogger.NewLogger(textlogger.NewConfig()), clusterProfile, clusterSummary) reconciler := getClusterSummaryReconciler(nil, nil) - set, err := controllers.GetCurrentReferences(reconciler, clusterSummaryScope) + set, err := controllers.GetCurrentReferences(reconciler, context.TODO(), clusterSummaryScope) Expect(err).To(BeNil()) Expect(set.Len()).To(Equal(4)) }) @@ -885,7 +885,7 @@ var _ = Describe("ClustersummaryController", func() { clusterSummaryScope := getClusterSummaryScope(c, textlogger.NewLogger(textlogger.NewConfig()), clusterProfile, clusterSummary) reconciler := getClusterSummaryReconciler(nil, nil) - set, err := controllers.GetCurrentReferences(reconciler, clusterSummaryScope) + set, err := controllers.GetCurrentReferences(reconciler, context.TODO(), clusterSummaryScope) Expect(err).To(BeNil()) Expect(set.Len()).To(Equal(1)) items := set.Items() diff --git a/controllers/clustersummary_deployer_test.go b/controllers/clustersummary_deployer_test.go index 1ab5f368..60c9c95f 100644 --- a/controllers/clustersummary_deployer_test.go +++ b/controllers/clustersummary_deployer_test.go @@ -291,6 +291,7 @@ var _ = Describe("ClustersummaryDeployer", func() { configMap, clusterSummary, clusterProfile, + cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(initObjects...).WithObjects(initObjects...).Build() diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index 1b1b3f2b..92e25432 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -350,8 +350,7 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c logger.V(logs.LogInfo).Info("ClusterProfile StopMatchingBehavior set to LeavePolicies") } else { - credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, - clusterSummary.Spec.ClusterNamespace, currentChart) + credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, clusterSummary, currentChart) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err)) return nil, err @@ -828,8 +827,8 @@ func createRegistryClientOptions(ctx context.Context, clusterSummary *configv1be return registryOptions, nil } - credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, getManagementClusterClient(), - clusterSummary.Spec.ClusterNamespace, currentChart) + credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, getManagementClusterClient(), clusterSummary, + currentChart) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err)) return registryOptions, err @@ -841,8 +840,12 @@ func createRegistryClientOptions(ctx context.Context, clusterSummary *configv1be registryOptions.skipTLSVerify = getInsecureSkipTLSVerify(currentChart) if currentChart.RegistryCredentialsConfig.CredentialsSecretRef != nil { - credentialSecretNamespace := libsveltostemplate.GetReferenceResourceNamespace(clusterSummary.Spec.ClusterNamespace, - currentChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace) + credentialSecretNamespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, + currentChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return nil, err + } secret := &corev1.Secret{} err = getManagementClusterClient().Get(ctx, @@ -2077,8 +2080,7 @@ func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context, l.V(logs.LogDebug).Info("collecting resources for helm chart") // Conflicts are already resolved by the time this is invoked. So it is safe to call CanManageChart if chartManager.CanManageChart(clusterSummary, currentChart) { - credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, - clusterSummary.Spec.ClusterNamespace, currentChart) + credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, clusterSummary, currentChart) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err)) return nil, err @@ -2666,15 +2668,15 @@ func getValueHashFromHelmChartSummary(requestedChart *configv1beta1.HelmChart, return nil } -func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterNamespace string, +func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary, requestedChart *configv1beta1.HelmChart) (credentialsPath, caPath string, err error) { - credentialsPath, err = createFileWithCredentials(ctx, c, clusterNamespace, requestedChart) + credentialsPath, err = createFileWithCredentials(ctx, c, clusterSummary, requestedChart) if err != nil { return "", "", err } - caPath, err = createFileWithCA(ctx, c, clusterNamespace, requestedChart) + caPath, err = createFileWithCA(ctx, c, clusterSummary, requestedChart) if err != nil { return "", "", err } @@ -2684,7 +2686,7 @@ func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterNames // createFileWithCredentials fetches the credentials from a Secret and writes it to a temporary file. // Returns the path to the temporary file. -func createFileWithCredentials(ctx context.Context, c client.Client, clusterNamespace string, +func createFileWithCredentials(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary, requestedChart *configv1beta1.HelmChart) (string, error) { if requestedChart.RegistryCredentialsConfig == nil || @@ -2693,11 +2695,15 @@ func createFileWithCredentials(ctx context.Context, c client.Client, clusterName return "", nil } credSecretRef := requestedChart.RegistryCredentialsConfig.CredentialsSecretRef - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterNamespace, requestedChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, + requestedChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return "", err + } secret := &corev1.Secret{} - err := c.Get(ctx, + err = c.Get(ctx, types.NamespacedName{ Namespace: namespace, Name: credSecretRef.Name, @@ -2734,7 +2740,7 @@ func createFileWithCredentials(ctx context.Context, c client.Client, clusterName // createFileWithCA fetches the CA certificate from a Secret and writes it to a temporary file. // Returns the path to the temporary file. -func createFileWithCA(ctx context.Context, c client.Client, clusterNamespace string, +func createFileWithCA(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary, requestedChart *configv1beta1.HelmChart) (string, error) { if requestedChart.RegistryCredentialsConfig == nil { @@ -2745,11 +2751,15 @@ func createFileWithCA(ctx context.Context, c client.Client, clusterNamespace str return "", nil } - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterNamespace, requestedChart.RegistryCredentialsConfig.CASecretRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, + requestedChart.RegistryCredentialsConfig.CASecretRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return "", err + } secret := &corev1.Secret{} - err := c.Get(ctx, + err = c.Get(ctx, types.NamespacedName{ Namespace: namespace, Name: requestedChart.RegistryCredentialsConfig.CASecretRef.Name, diff --git a/controllers/handlers_helm_test.go b/controllers/handlers_helm_test.go index a1c6ffa1..77c8f8b8 100644 --- a/controllers/handlers_helm_test.go +++ b/controllers/handlers_helm_test.go @@ -775,7 +775,7 @@ var _ = Describe("Hash methods", func() { Expect(reflect.DeepEqual(hash, expectHash)).To(BeTrue()) }) - It(`getHelmReferenceResourceHash returns the hash considering all referenced + It(`getHelmReferenceResourceHash returns the hash considering all referenced ConfigMap/Secret in the ValueFrom section`, func() { namespace := randomString() @@ -838,10 +838,18 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + initObjects := []client.Object{ configMap, secret, clusterSummary, + cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build() @@ -890,13 +898,20 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + h := sha256.New() expectedHash := render.AsCode(requestedChart.Values) expectedHash += render.AsCode(configMap.Data[key]) h.Write([]byte(expectedHash)) initObjects := []client.Object{ - configMap, + configMap, cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build() @@ -946,8 +961,35 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: randomString(), + Name: randomString(), + }, + } + + clusterSummary := &configv1beta1.ClusterSummary{ + ObjectMeta: metav1.ObjectMeta{ + Name: randomString(), + Namespace: cluster.Namespace, + OwnerReferences: []metav1.OwnerReference{ + { + Kind: configv1beta1.ClusterProfileKind, + Name: randomString(), + APIVersion: "config.projectsveltos.io/v1beta1", + UID: types.UID(randomString()), + }, + }, + }, + Spec: configv1beta1.ClusterSummarySpec{ + ClusterNamespace: cluster.Namespace, + ClusterName: cluster.Name, + ClusterType: libsveltosv1beta1.ClusterTypeCapi, + }, + } + initObjects := []client.Object{ - secretCredentials, secretCA, + secretCredentials, secretCA, cluster, } requestedChart := configv1beta1.HelmChart{ @@ -967,7 +1009,7 @@ var _ = Describe("Hash methods", func() { c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build() credentialsPath, caPath, err := controllers.GetCredentialsAndCAFiles(context.TODO(), c, - randomString(), &requestedChart) + clusterSummary, &requestedChart) Expect(err).To(BeNil()) Expect(credentialsPath).ToNot(BeEmpty()) verifyFileContent(credentialsPath, credentialsBytes) diff --git a/controllers/handlers_kustomize.go b/controllers/handlers_kustomize.go index 1ffd66ef..7246fdee 100644 --- a/controllers/handlers_kustomize.go +++ b/controllers/handlers_kustomize.go @@ -338,11 +338,14 @@ func getHashFromKustomizationRef(ctx context.Context, c client.Client, clusterSu kustomizationRef *configv1beta1.KustomizationRef, logger logr.Logger) ([]byte, error) { var result string - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, kustomizationRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return nil, err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), kustomizationRef.Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType) if err != nil { return nil, err } @@ -513,11 +516,14 @@ func prepareFileSystem(ctx context.Context, c client.Client, return prepareFileSystemWithSecret(ctx, c, kustomizationRef, clusterSummary, logger) } - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, kustomizationRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return "", err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), kustomizationRef.Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType) if err != nil { return "", err } @@ -539,11 +545,14 @@ func prepareFileSystemWithConfigMap(ctx context.Context, c client.Client, kustomizationRef *configv1beta1.KustomizationRef, clusterSummary *configv1beta1.ClusterSummary, logger logr.Logger) (string, error) { - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, kustomizationRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return "", err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), kustomizationRef.Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType) if err != nil { return "", err } @@ -560,11 +569,14 @@ func prepareFileSystemWithSecret(ctx context.Context, c client.Client, kustomizationRef *configv1beta1.KustomizationRef, clusterSummary *configv1beta1.ClusterSummary, logger logr.Logger) (string, error) { - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, kustomizationRef.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType) + if err != nil { + return "", err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), kustomizationRef.Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType) if err != nil { return "", err } diff --git a/controllers/handlers_kustomize_test.go b/controllers/handlers_kustomize_test.go index cd3a5de8..59c6ca0d 100644 --- a/controllers/handlers_kustomize_test.go +++ b/controllers/handlers_kustomize_test.go @@ -301,8 +301,16 @@ var _ = Describe("Hash methods", func() { } } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + initObjects := []client.Object{ clusterSummary, + cluster, } for i := 0; i < repoNum; i++ { initObjects = append(initObjects, &gitRepositories[i]) @@ -345,7 +353,7 @@ var _ = Describe("Hash methods", func() { Expect(reflect.DeepEqual(hash, expectHash)).To(BeTrue()) }) - It(`getKustomizeReferenceResourceHash returns the hash considering all referenced + It(`getKustomizeReferenceResourceHash returns the hash considering all referenced ConfigMap/Secret in the ValueFrom section`, func() { namespace := randomString() configMap := &corev1.ConfigMap{ @@ -405,10 +413,18 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + initObjects := []client.Object{ configMap, secret, clusterSummary, + cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build() @@ -562,9 +578,17 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + initObjects := []client.Object{ configMap, secret, + cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build() diff --git a/controllers/handlers_resources.go b/controllers/handlers_resources.go index 6b4b27be..2b104803 100644 --- a/controllers/handlers_resources.go +++ b/controllers/handlers_resources.go @@ -356,11 +356,17 @@ func resourcesHash(ctx context.Context, c client.Client, clusterSummaryScope *sc referencedObjects := make([]corev1.ObjectReference, len(clusterSummary.Spec.ClusterProfileSpec.PolicyRefs)) for i := range clusterSummary.Spec.ClusterProfileSpec.PolicyRefs { reference := &clusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i] - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummaryScope.Namespace(), reference.Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, reference.Namespace, + clusterSummary.Spec.ClusterType) + if err != nil { + logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v", + reference.Kind, reference.Namespace, reference.Name, err)) + return nil, err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), reference.Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, reference.Name, clusterSummary.Spec.ClusterType) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v", reference.Kind, reference.Namespace, reference.Name, err)) diff --git a/controllers/handlers_resources_test.go b/controllers/handlers_resources_test.go index 459842f5..1a36522d 100644 --- a/controllers/handlers_resources_test.go +++ b/controllers/handlers_resources_test.go @@ -375,10 +375,18 @@ var _ = Describe("Hash methods", func() { }, } + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterSummary.Spec.ClusterName, + Namespace: clusterSummary.Spec.ClusterNamespace, + }, + } + initObjects := []client.Object{ clusterSummary, configMap1, configMap2, + cluster, } c := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(initObjects...).WithObjects(initObjects...).Build() diff --git a/controllers/handlers_utils.go b/controllers/handlers_utils.go index d0f85dd0..ca835117 100644 --- a/controllers/handlers_utils.go +++ b/controllers/handlers_utils.go @@ -1141,11 +1141,18 @@ func collectReferencedObjects(ctx context.Context, controlClusterClient client.C var object client.Object reference := &references[i] - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, references[i].Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(), + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, references[i].Namespace, + clusterSummary.Spec.ClusterType) + if err != nil { + logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v", + reference.Kind, reference.Namespace, reference.Name, err)) + return nil, nil, err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), references[i].Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(), + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, references[i].Name, + clusterSummary.Spec.ClusterType) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v", reference.Kind, reference.Namespace, reference.Name, err)) @@ -1838,11 +1845,18 @@ func getValuesFromResourceHash(ctx context.Context, c client.Client, clusterSumm var config string for i := range valuesFrom { - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, valuesFrom[i].Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, valuesFrom[i].Namespace, + clusterSummary.Spec.ClusterType) + if err != nil { + logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v", + valuesFrom[i].Kind, valuesFrom[i].Namespace, valuesFrom[i].Name, err)) + return "", err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), valuesFrom[i].Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, valuesFrom[i].Name, + clusterSummary.Spec.ClusterType) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v", valuesFrom[i].Kind, valuesFrom[i].Namespace, valuesFrom[i].Name, err)) @@ -1883,11 +1897,17 @@ func getValuesFrom(ctx context.Context, c client.Client, clusterSummary *configv template = make(map[string]string) nonTemplate = make(map[string]string) for i := range valuesFrom { - namespace := libsveltostemplate.GetReferenceResourceNamespace( - clusterSummary.Namespace, valuesFrom[i].Namespace) + namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, + clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, valuesFrom[i].Namespace, + clusterSummary.Spec.ClusterType) + if err != nil { + logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v", + valuesFrom[i].Kind, valuesFrom[i].Namespace, valuesFrom[i].Name, err)) + return nil, nil, err + } - name, err := libsveltostemplate.GetReferenceResourceName(clusterSummary.Spec.ClusterNamespace, - clusterSummary.Spec.ClusterName, string(clusterSummary.Spec.ClusterType), valuesFrom[i].Name) + name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace, + clusterSummary.Spec.ClusterName, valuesFrom[i].Name, clusterSummary.Spec.ClusterType) if err != nil { logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v", valuesFrom[i].Kind, valuesFrom[i].Namespace, valuesFrom[i].Name, err)) diff --git a/go.mod b/go.mod index 63041d64..d1f295b6 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/onsi/ginkgo/v2 v2.23.4 github.com/onsi/gomega v1.37.0 github.com/pkg/errors v0.9.1 - github.com/projectsveltos/libsveltos v0.53.0 + github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1 github.com/prometheus/client_golang v1.22.0 github.com/spf13/pflag v1.0.6 github.com/yuin/gopher-lua v1.1.1 diff --git a/go.sum b/go.sum index e756b628..44b7fdb1 100644 --- a/go.sum +++ b/go.sum @@ -329,8 +329,8 @@ github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY= github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg= github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U= -github.com/projectsveltos/libsveltos v0.53.0 h1:TiMMujh6kX6evFYk1XrPrwQhiv0pNSxuwRmGeGcGEiM= -github.com/projectsveltos/libsveltos v0.53.0/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0= +github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1 h1:62mLgYOrZiqhMD6vc8JcndMs8obZpq+tfvNePsi774Y= +github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0= github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7 h1:KdDtBEJPgavOHlut1gq2i6bFm5dgoNHNsOUC8oe2hK4= github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7/go.mod h1:AIzg+JWbfrFWazyM5Ka2fX69r9aFr3+o2Mvn9SfKDYU= github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20250301182851-e4fbb9fd7ff7 h1:kZzOx+XTEfCRjxw1yACuGhFSyS7ybP/NNJFAZYNARCk=