Skip to content

Commit 445199b

Browse files
authored
Merge pull request #1183 from gianlucam76/referenced-resource-template
feat: support full cluster-based templating for referenced resource n…
2 parents 0eb99d9 + 81d101f commit 445199b

12 files changed

Lines changed: 240 additions & 101 deletions

controllers/clustersummary_controller.go

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (r *ClusterSummaryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
213213
r.resetFeatureStatus(clusterSummaryScope, configv1beta1.FeatureStatusFailed)
214214
// if cluster is not ready, do nothing and don't queue for reconciliation.
215215
// When cluster becomes ready, all matching clusterSummaries will be requeued for reconciliation
216-
_ = r.updateMaps(clusterSummaryScope, logger)
216+
_ = r.updateMaps(ctx, clusterSummaryScope, logger)
217217

218218
return reconcile.Result{}, nil
219219
}
@@ -337,7 +337,7 @@ func (r *ClusterSummaryReconciler) reconcileNormal(
337337
return reconcile.Result{}, nil
338338
}
339339

340-
err := r.updateMaps(clusterSummaryScope, logger)
340+
err := r.updateMaps(ctx, clusterSummaryScope, logger)
341341
if err != nil {
342342
return reconcile.Result{}, err
343343
}
@@ -711,13 +711,15 @@ func (r *ClusterSummaryReconciler) cleanMaps(clusterSummaryScope *scope.ClusterS
711711
}
712712
}
713713

714-
func (r *ClusterSummaryReconciler) updateMaps(clusterSummaryScope *scope.ClusterSummaryScope, logger logr.Logger) error {
714+
func (r *ClusterSummaryReconciler) updateMaps(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope,
715+
logger logr.Logger) error {
716+
715717
if clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeOneTime {
716718
logger.V(logs.LogDebug).Info("sync mode is one time. No need to reconcile on policies change.")
717719
return nil
718720
}
719721
logger.V(logs.LogDebug).Info("update policy map")
720-
currentReferences, err := r.getCurrentReferences(clusterSummaryScope)
722+
currentReferences, err := r.getCurrentReferences(ctx, clusterSummaryScope)
721723
if err != nil {
722724
logger.V(logs.LogInfo).Info("failed to get current references: %v", err)
723725
return err
@@ -834,21 +836,21 @@ func (r *ClusterSummaryReconciler) shouldReconcile(clusterSummaryScope *scope.Cl
834836
return false
835837
}
836838

837-
func (r *ClusterSummaryReconciler) getCurrentReferences(clusterSummaryScope *scope.ClusterSummaryScope,
838-
) (*libsveltosset.Set, error) {
839+
func (r *ClusterSummaryReconciler) getCurrentReferences(ctx context.Context,
840+
clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) {
839841

840-
currentReferences, err := r.getPolicyRefReferences(clusterSummaryScope)
842+
currentReferences, err := r.getPolicyRefReferences(ctx, clusterSummaryScope)
841843
if err != nil {
842844
return nil, err
843845
}
844846

845-
kustomizationRefs, err := r.getKustomizationRefReferences(clusterSummaryScope)
847+
kustomizationRefs, err := r.getKustomizationRefReferences(ctx, clusterSummaryScope)
846848
if err != nil {
847849
return nil, err
848850
}
849851
currentReferences.Append(kustomizationRefs)
850852

851-
helmRefs, err := r.getHelmChartsReferences(clusterSummaryScope)
853+
helmRefs, err := r.getHelmChartsReferences(ctx, clusterSummaryScope)
852854
if err != nil {
853855
return nil, err
854856
}
@@ -858,17 +860,22 @@ func (r *ClusterSummaryReconciler) getCurrentReferences(clusterSummaryScope *sco
858860
}
859861

860862
// getPolicyRefReferences get all references considering the PolicyRef section
861-
func (r *ClusterSummaryReconciler) getPolicyRefReferences(clusterSummaryScope *scope.ClusterSummaryScope,
862-
) (*libsveltosset.Set, error) {
863+
func (r *ClusterSummaryReconciler) getPolicyRefReferences(ctx context.Context,
864+
clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) {
863865

866+
cs := clusterSummaryScope.ClusterSummary
864867
currentReferences := &libsveltosset.Set{}
865868
for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs {
866869
referencedNamespace := clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Namespace
867-
namespace := libsveltostemplate.GetReferenceResourceNamespace(clusterSummaryScope.Namespace(), referencedNamespace)
870+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(),
871+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType)
872+
if err != nil {
873+
return nil, err
874+
}
868875

869-
cs := clusterSummaryScope.ClusterSummary
870-
referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace, cs.Spec.ClusterName,
871-
string(cs.Spec.ClusterType), clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name)
876+
referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(),
877+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName,
878+
clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name, cs.Spec.ClusterType)
872879
if err != nil {
873880
return nil, err
874881
}
@@ -884,21 +891,24 @@ func (r *ClusterSummaryReconciler) getPolicyRefReferences(clusterSummaryScope *s
884891
}
885892

886893
// getKustomizationRefReferences get all references considering the KustomizationRef section
887-
func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryScope *scope.ClusterSummaryScope,
888-
) (*libsveltosset.Set, error) {
894+
func (r *ClusterSummaryReconciler) getKustomizationRefReferences(ctx context.Context,
895+
clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) {
889896

897+
cs := clusterSummaryScope.ClusterSummary
890898
currentReferences := &libsveltosset.Set{}
891899
for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.KustomizationRefs {
892900
kr := &clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.KustomizationRefs[i]
893901

894902
referencedNamespace := kr.Namespace
895903

896-
namespace := libsveltostemplate.GetReferenceResourceNamespace(
897-
clusterSummaryScope.Namespace(), referencedNamespace)
904+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(),
905+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType)
906+
if err != nil {
907+
return nil, err
908+
}
898909

899-
cs := clusterSummaryScope.ClusterSummary
900-
referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace,
901-
cs.Spec.ClusterName, string(cs.Spec.ClusterType), kr.Name)
910+
referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(),
911+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, kr.Name, cs.Spec.ClusterType)
902912
if err != nil {
903913
return nil, err
904914
}
@@ -921,7 +931,7 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryS
921931
Name: referencedName,
922932
})
923933

924-
valuesFromReferences, err := getKustomizationValueFrom(clusterSummaryScope, kr)
934+
valuesFromReferences, err := getKustomizationValueFrom(ctx, clusterSummaryScope, kr)
925935
if err != nil {
926936
return nil, err
927937
}
@@ -933,19 +943,22 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(clusterSummaryS
933943
// getKustomizationValueFrom gets referenced ConfigMap/Secret in a KustomizationRef.
934944
// KustomizationRef can reference both ConfigMap/Secret each containing key-value pairs that will be used, if defined,
935945
// to replace placeholder value in the output generated by Kustomize SDK.
936-
func getKustomizationValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, kr *configv1beta1.KustomizationRef,
937-
) (*libsveltosset.Set, error) {
946+
func getKustomizationValueFrom(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope,
947+
kr *configv1beta1.KustomizationRef) (*libsveltosset.Set, error) {
938948

939949
currentValuesFromReferences := &libsveltosset.Set{}
940950

951+
cs := clusterSummaryScope.ClusterSummary
941952
for i := range kr.ValuesFrom {
942953
referencedNamespace := kr.ValuesFrom[i].Namespace
943-
namespace := libsveltostemplate.GetReferenceResourceNamespace(
944-
clusterSummaryScope.Namespace(), referencedNamespace)
954+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(),
955+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType)
956+
if err != nil {
957+
return nil, err
958+
}
945959

946-
cs := clusterSummaryScope.ClusterSummary
947-
referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace,
948-
cs.Spec.ClusterName, string(cs.Spec.ClusterType), kr.ValuesFrom[i].Name)
960+
referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(),
961+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, kr.ValuesFrom[i].Name, cs.Spec.ClusterType)
949962
if err != nil {
950963
return nil, err
951964
}
@@ -962,8 +975,8 @@ func getKustomizationValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, k
962975
}
963976

964977
// getHelmChartsReferences get all references considering the HelmChart section
965-
func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope *scope.ClusterSummaryScope,
966-
) (*libsveltosset.Set, error) {
978+
func (r *ClusterSummaryReconciler) getHelmChartsReferences(ctx context.Context,
979+
clusterSummaryScope *scope.ClusterSummaryScope) (*libsveltosset.Set, error) {
967980

968981
currentReferences := &libsveltosset.Set{}
969982
for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.HelmCharts {
@@ -977,7 +990,7 @@ func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope *
977990
currentReferences.Insert(sourceRef)
978991
}
979992

980-
valuesFromReferences, err := getHelmChartValueFrom(clusterSummaryScope, hc)
993+
valuesFromReferences, err := getHelmChartValueFrom(ctx, clusterSummaryScope, hc)
981994
if err != nil {
982995
return nil, err
983996
}
@@ -988,19 +1001,22 @@ func (r *ClusterSummaryReconciler) getHelmChartsReferences(clusterSummaryScope *
9881001

9891002
// getHelmChartValueFrom gets referenced ConfigMap/Secret in a HelmChart.
9901003
// HelmChart can reference both ConfigMap/Secret each containing configuration for the helm release.
991-
func getHelmChartValueFrom(clusterSummaryScope *scope.ClusterSummaryScope, hc *configv1beta1.HelmChart,
992-
) (*libsveltosset.Set, error) {
1004+
func getHelmChartValueFrom(ctx context.Context, clusterSummaryScope *scope.ClusterSummaryScope,
1005+
hc *configv1beta1.HelmChart) (*libsveltosset.Set, error) {
9931006

9941007
currentValuesFromReferences := &libsveltosset.Set{}
9951008

1009+
cs := clusterSummaryScope.ClusterSummary
9961010
for i := range hc.ValuesFrom {
9971011
referencedNamespace := hc.ValuesFrom[i].Namespace
998-
namespace := libsveltostemplate.GetReferenceResourceNamespace(
999-
clusterSummaryScope.Namespace(), referencedNamespace)
1012+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(),
1013+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, referencedNamespace, cs.Spec.ClusterType)
1014+
if err != nil {
1015+
return nil, err
1016+
}
10001017

1001-
cs := clusterSummaryScope.ClusterSummary
1002-
referencedName, err := libsveltostemplate.GetReferenceResourceName(cs.Spec.ClusterNamespace,
1003-
cs.Spec.ClusterName, string(cs.Spec.ClusterType), hc.ValuesFrom[i].Name)
1018+
referencedName, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(),
1019+
cs.Spec.ClusterNamespace, cs.Spec.ClusterName, hc.ValuesFrom[i].Name, cs.Spec.ClusterType)
10041020
if err != nil {
10051021
return nil, err
10061022
}

controllers/clustersummary_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ var _ = Describe("ClustersummaryController", func() {
870870
clusterSummaryScope := getClusterSummaryScope(c,
871871
textlogger.NewLogger(textlogger.NewConfig()), clusterProfile, clusterSummary)
872872
reconciler := getClusterSummaryReconciler(nil, nil)
873-
set, err := controllers.GetCurrentReferences(reconciler, clusterSummaryScope)
873+
set, err := controllers.GetCurrentReferences(reconciler, context.TODO(), clusterSummaryScope)
874874
Expect(err).To(BeNil())
875875
Expect(set.Len()).To(Equal(4))
876876
})
@@ -885,7 +885,7 @@ var _ = Describe("ClustersummaryController", func() {
885885
clusterSummaryScope := getClusterSummaryScope(c,
886886
textlogger.NewLogger(textlogger.NewConfig()), clusterProfile, clusterSummary)
887887
reconciler := getClusterSummaryReconciler(nil, nil)
888-
set, err := controllers.GetCurrentReferences(reconciler, clusterSummaryScope)
888+
set, err := controllers.GetCurrentReferences(reconciler, context.TODO(), clusterSummaryScope)
889889
Expect(err).To(BeNil())
890890
Expect(set.Len()).To(Equal(1))
891891
items := set.Items()

controllers/clustersummary_deployer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ var _ = Describe("ClustersummaryDeployer", func() {
291291
configMap,
292292
clusterSummary,
293293
clusterProfile,
294+
cluster,
294295
}
295296

296297
c := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(initObjects...).WithObjects(initObjects...).Build()

controllers/handlers_helm.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c
350350

351351
logger.V(logs.LogInfo).Info("ClusterProfile StopMatchingBehavior set to LeavePolicies")
352352
} else {
353-
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c,
354-
clusterSummary.Spec.ClusterNamespace, currentChart)
353+
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, clusterSummary, currentChart)
355354
if err != nil {
356355
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err))
357356
return nil, err
@@ -828,8 +827,8 @@ func createRegistryClientOptions(ctx context.Context, clusterSummary *configv1be
828827
return registryOptions, nil
829828
}
830829

831-
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, getManagementClusterClient(),
832-
clusterSummary.Spec.ClusterNamespace, currentChart)
830+
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, getManagementClusterClient(), clusterSummary,
831+
currentChart)
833832
if err != nil {
834833
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err))
835834
return registryOptions, err
@@ -841,8 +840,12 @@ func createRegistryClientOptions(ctx context.Context, clusterSummary *configv1be
841840
registryOptions.skipTLSVerify = getInsecureSkipTLSVerify(currentChart)
842841

843842
if currentChart.RegistryCredentialsConfig.CredentialsSecretRef != nil {
844-
credentialSecretNamespace := libsveltostemplate.GetReferenceResourceNamespace(clusterSummary.Spec.ClusterNamespace,
845-
currentChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace)
843+
credentialSecretNamespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, getManagementClusterClient(),
844+
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName,
845+
currentChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace, clusterSummary.Spec.ClusterType)
846+
if err != nil {
847+
return nil, err
848+
}
846849

847850
secret := &corev1.Secret{}
848851
err = getManagementClusterClient().Get(ctx,
@@ -2077,8 +2080,7 @@ func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context,
20772080
l.V(logs.LogDebug).Info("collecting resources for helm chart")
20782081
// Conflicts are already resolved by the time this is invoked. So it is safe to call CanManageChart
20792082
if chartManager.CanManageChart(clusterSummary, currentChart) {
2080-
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c,
2081-
clusterSummary.Spec.ClusterNamespace, currentChart)
2083+
credentialsPath, caPath, err := getCredentialsAndCAFiles(ctx, c, clusterSummary, currentChart)
20822084
if err != nil {
20832085
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to process credentials %v", err))
20842086
return nil, err
@@ -2666,15 +2668,15 @@ func getValueHashFromHelmChartSummary(requestedChart *configv1beta1.HelmChart,
26662668
return nil
26672669
}
26682670

2669-
func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterNamespace string,
2671+
func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary,
26702672
requestedChart *configv1beta1.HelmChart) (credentialsPath, caPath string, err error) {
26712673

2672-
credentialsPath, err = createFileWithCredentials(ctx, c, clusterNamespace, requestedChart)
2674+
credentialsPath, err = createFileWithCredentials(ctx, c, clusterSummary, requestedChart)
26732675
if err != nil {
26742676
return "", "", err
26752677
}
26762678

2677-
caPath, err = createFileWithCA(ctx, c, clusterNamespace, requestedChart)
2679+
caPath, err = createFileWithCA(ctx, c, clusterSummary, requestedChart)
26782680
if err != nil {
26792681
return "", "", err
26802682
}
@@ -2684,7 +2686,7 @@ func getCredentialsAndCAFiles(ctx context.Context, c client.Client, clusterNames
26842686

26852687
// createFileWithCredentials fetches the credentials from a Secret and writes it to a temporary file.
26862688
// Returns the path to the temporary file.
2687-
func createFileWithCredentials(ctx context.Context, c client.Client, clusterNamespace string,
2689+
func createFileWithCredentials(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary,
26882690
requestedChart *configv1beta1.HelmChart) (string, error) {
26892691

26902692
if requestedChart.RegistryCredentialsConfig == nil ||
@@ -2693,11 +2695,15 @@ func createFileWithCredentials(ctx context.Context, c client.Client, clusterName
26932695
return "", nil
26942696
}
26952697
credSecretRef := requestedChart.RegistryCredentialsConfig.CredentialsSecretRef
2696-
namespace := libsveltostemplate.GetReferenceResourceNamespace(
2697-
clusterNamespace, requestedChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace)
2698+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c,
2699+
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName,
2700+
requestedChart.RegistryCredentialsConfig.CredentialsSecretRef.Namespace, clusterSummary.Spec.ClusterType)
2701+
if err != nil {
2702+
return "", err
2703+
}
26982704

26992705
secret := &corev1.Secret{}
2700-
err := c.Get(ctx,
2706+
err = c.Get(ctx,
27012707
types.NamespacedName{
27022708
Namespace: namespace,
27032709
Name: credSecretRef.Name,
@@ -2734,7 +2740,7 @@ func createFileWithCredentials(ctx context.Context, c client.Client, clusterName
27342740

27352741
// createFileWithCA fetches the CA certificate from a Secret and writes it to a temporary file.
27362742
// Returns the path to the temporary file.
2737-
func createFileWithCA(ctx context.Context, c client.Client, clusterNamespace string,
2743+
func createFileWithCA(ctx context.Context, c client.Client, clusterSummary *configv1beta1.ClusterSummary,
27382744
requestedChart *configv1beta1.HelmChart) (string, error) {
27392745

27402746
if requestedChart.RegistryCredentialsConfig == nil {
@@ -2745,11 +2751,15 @@ func createFileWithCA(ctx context.Context, c client.Client, clusterNamespace str
27452751
return "", nil
27462752
}
27472753

2748-
namespace := libsveltostemplate.GetReferenceResourceNamespace(
2749-
clusterNamespace, requestedChart.RegistryCredentialsConfig.CASecretRef.Namespace)
2754+
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c,
2755+
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName,
2756+
requestedChart.RegistryCredentialsConfig.CASecretRef.Namespace, clusterSummary.Spec.ClusterType)
2757+
if err != nil {
2758+
return "", err
2759+
}
27502760

27512761
secret := &corev1.Secret{}
2752-
err := c.Get(ctx,
2762+
err = c.Get(ctx,
27532763
types.NamespacedName{
27542764
Namespace: namespace,
27552765
Name: requestedChart.RegistryCredentialsConfig.CASecretRef.Name,

0 commit comments

Comments
 (0)