@@ -355,6 +355,12 @@ func kustomizationHash(ctx context.Context, c client.Client, clusterSummary *con
355355 hash := sha256 .Sum256 (raw )
356356 config += hex .EncodeToString ((hash [:]))
357357
358+ clusterObjects , err := fetchClusterObjects (ctx , getManagementClusterConfig (), getManagementClusterClient (),
359+ clusterSummary .Spec .ClusterNamespace , clusterSummary .Spec .ClusterName , clusterSummary .Spec .ClusterType , logger )
360+ if err != nil {
361+ return nil , err
362+ }
363+
358364 for i := range sortedKustomizationRefs {
359365 kustomizationRef := & sortedKustomizationRefs [i ]
360366
@@ -370,7 +376,7 @@ func kustomizationHash(ctx context.Context, c client.Client, clusterSummary *con
370376 config += fmt .Sprintf ("%d" , kustomizationRef .Tier )
371377
372378 valueFromHash , err := getKustomizeReferenceResourceHash (ctx , c , clusterSummary ,
373- kustomizationRef , logger )
379+ kustomizationRef , clusterObjects , logger )
374380 if err != nil {
375381 logger .V (logs .LogInfo ).Info (
376382 fmt .Sprintf ("failed to get hash from referenced ConfigMap/Secret in ValuesFrom %v" , err ))
@@ -461,27 +467,20 @@ func getHashFromKustomizationRef(ctx context.Context, c client.Client, clusterSu
461467
462468// instantiateKustomizeSubstituteValues gets all substitute values for a KustomizationRef and
463469// instantiate those using resources in the management cluster.
464- func instantiateKustomizeSubstituteValues (ctx context.Context , clusterSummary * configv1beta1.ClusterSummary ,
465- mgmtResources map [string ]* unstructured.Unstructured , values map [string ]string , logger logr.Logger ,
466- ) (map [string ]string , error ) {
470+ func instantiateKustomizeSubstituteValues (ctx context.Context , dCtx * deploymentContext ,
471+ values map [string ]string , logger logr.Logger ) (map [string ]string , error ) {
467472
468473 stringifiedValues , err := stringifyMap (values )
469474 if err != nil {
470475 logger .V (logs .LogInfo ).Info (fmt .Sprintf ("failed to stringify values map: %v" , err ))
471476 return nil , err
472477 }
473478
474- requestorName := clusterSummary .Namespace + clusterSummary .Name + "kustomize"
475-
476- objects , err := fetchClusterObjects (ctx , getManagementClusterConfig (), getManagementClusterClient (),
477- clusterSummary .Spec .ClusterNamespace , clusterSummary .Spec .ClusterName , clusterSummary .Spec .ClusterType , logger )
478- if err != nil {
479- return nil , err
480- }
479+ requestorName := dCtx .clusterSummary .Namespace + dCtx .clusterSummary .Name + "kustomize"
481480
482481 instantiatedValue , err :=
483482 instantiateTemplateValues (ctx , getManagementClusterConfig (), getManagementClusterClient (),
484- clusterSummary , requestorName , stringifiedValues , objects , mgmtResources , logger )
483+ dCtx . clusterSummary , requestorName , stringifiedValues , dCtx . clusterObjects , dCtx . mgmtResources , logger )
485484 if err != nil {
486485 logger .V (logs .LogInfo ).Info (fmt .Sprintf ("failed to instantiate values %v" , err ))
487486 return nil , err
@@ -617,13 +616,18 @@ func getKustomizeValuesFrom(ctx context.Context, c client.Client, clusterSummary
617616}
618617
619618func getKustomizeReferenceResourceHash (ctx context.Context , c client.Client , clusterSummary * configv1beta1.ClusterSummary ,
620- kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ) (string , error ) {
619+ kustomizationRef * configv1beta1.KustomizationRef , clusterObjects * currentClusterObjects , logger logr.Logger ) (string , error ) {
621620
622621 mgmtResources , err := collectTemplateResourceRefs (ctx , clusterSummary )
623622 if err != nil {
624623 logger .V (logs .LogInfo ).Error (err , "failed to collect templateResourceRefs" )
625624 return "" , err
626625 }
626+ innerDCtx := & deploymentContext {
627+ clusterSummary : clusterSummary ,
628+ clusterObjects : clusterObjects ,
629+ mgmtResources : mgmtResources ,
630+ }
627631
628632 // Get key-value pairs from ValuesFrom
629633 templatedValues , nonTemplatedValues , err := getKustomizeSubstituteValues (ctx , c , clusterSummary , kustomizationRef , logger )
@@ -635,7 +639,8 @@ func getKustomizeReferenceResourceHash(ctx context.Context, c client.Client, clu
635639 // Get substitute values. Those are collected from Data sections of referenced ConfigMap/Secret instances.
636640 // Those values can be expressed as template. This method instantiates those using resources in
637641 // the managemenet cluster
638- instantiatedSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , clusterSummary , mgmtResources , templatedValues , logger )
642+ instantiatedSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , innerDCtx ,
643+ templatedValues , logger )
639644 if err != nil {
640645 logger .V (logs .LogInfo ).Error (err , "failed to instantiate kustomize substitute values" )
641646 return "" , err
@@ -669,11 +674,11 @@ func getKustomizationRefs(clusterSummary *configv1beta1.ClusterSummary) []config
669674}
670675
671676func deployKustomizeRef (ctx context.Context , c client.Client , remoteRestConfig * rest.Config ,
672- kustomizationRef * configv1beta1.KustomizationRef , clusterSummary * configv1beta1. ClusterSummary ,
673- logger logr. Logger ) (localReports , remoteReports []libsveltosv1beta1.ResourceReport , err error ) {
677+ kustomizationRef * configv1beta1.KustomizationRef , dCtx * deploymentContext , logger logr. Logger ,
678+ ) (localReports , remoteReports []libsveltosv1beta1.ResourceReport , err error ) {
674679
675680 var tmpDir string
676- tmpDir , err = prepareFileSystem (ctx , c , kustomizationRef , clusterSummary , logger )
681+ tmpDir , err = prepareFileSystem (ctx , c , kustomizationRef , dCtx . clusterSummary , logger )
677682 if err != nil {
678683 return nil , nil , err
679684 }
@@ -684,15 +689,9 @@ func deployKustomizeRef(ctx context.Context, c client.Client, remoteRestConfig *
684689
685690 defer os .RemoveAll (tmpDir )
686691
687- objects , err := fetchClusterObjects (ctx , getManagementClusterConfig (), getManagementClusterClient (),
688- clusterSummary .Spec .ClusterNamespace , clusterSummary .Spec .ClusterName , clusterSummary .Spec .ClusterType , logger )
689- if err != nil {
690- return nil , nil , err
691- }
692-
693692 // Path can be expressed as a template and instantiate using Cluster fields.
694693 instantiatedPath , err := instantiateTemplateValues (ctx , getManagementClusterConfig (), getManagementClusterClient (),
695- clusterSummary , clusterSummary .GetName (), kustomizationRef .Path , objects , nil , logger )
694+ dCtx . clusterSummary , dCtx . clusterSummary .GetName (), kustomizationRef .Path , dCtx . clusterObjects , nil , logger )
696695 if err != nil {
697696 return nil , nil , err
698697 }
@@ -703,7 +702,7 @@ func deployKustomizeRef(ctx context.Context, c client.Client, remoteRestConfig *
703702 var instantiatedComponents []string
704703 for _ , comp := range kustomizationRef .Components {
705704 instantiatedComp , err := instantiateTemplateValues (ctx , getManagementClusterConfig (), getManagementClusterClient (),
706- clusterSummary , clusterSummary .GetName (), comp , objects , nil , logger )
705+ dCtx . clusterSummary , dCtx . clusterSummary .GetName (), comp , dCtx . clusterObjects , nil , logger )
707706 if err != nil {
708707 return nil , nil , err
709708 }
@@ -771,7 +770,8 @@ func deployKustomizeRef(ctx context.Context, c client.Client, remoteRestConfig *
771770 return nil , nil , err
772771 }
773772
774- return deployKustomizeResources (ctx , c , remoteRestConfig , kustomizationRef , resMap , clusterSummary , logger )
773+ return deployKustomizeResources (ctx , c , remoteRestConfig , kustomizationRef , resMap ,
774+ dCtx , logger )
775775}
776776
777777func prepareFileSystem (ctx context.Context , c client.Client ,
@@ -894,13 +894,18 @@ func prepareFileSystemWithData(binaryData map[string][]byte,
894894
895895func getKustomizedResources (ctx context.Context , c client.Client , clusterSummary * configv1beta1.ClusterSummary ,
896896 deploymentType configv1beta1.DeploymentType , resMap resmap.ResMap ,
897- kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ,
897+ kustomizationRef * configv1beta1.KustomizationRef , clusterObjects * currentClusterObjects , logger logr.Logger ,
898898) (objectsToDeployLocally , objectsToDeployRemotely []* unstructured.Unstructured , mgmtResources map [string ]* unstructured.Unstructured , err error ) {
899899
900900 mgmtResources , err = collectTemplateResourceRefs (ctx , clusterSummary )
901901 if err != nil {
902902 return nil , nil , nil , err
903903 }
904+ innerDCtx := & deploymentContext {
905+ clusterSummary : clusterSummary ,
906+ clusterObjects : clusterObjects ,
907+ mgmtResources : mgmtResources ,
908+ }
904909
905910 // Get key-value pairs from ValuesFrom
906911 templatedValues , nonTemplatedValues , err := getKustomizeSubstituteValues (ctx , c , clusterSummary , kustomizationRef , logger )
@@ -911,7 +916,8 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
911916 // Get substitute values. Those are collected from Data sections of referenced ConfigMap/Secret instances.
912917 // Those values can be expressed as template. This method instantiates those using resources in
913918 // the managemenet cluster
914- instantiatedSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , clusterSummary , mgmtResources , templatedValues , logger )
919+ instantiatedSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , innerDCtx ,
920+ templatedValues , logger )
915921 if err != nil {
916922 return nil , nil , nil , err
917923 }
@@ -968,23 +974,23 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
968974
969975func deployKustomizeResources (ctx context.Context , c client.Client , remoteRestConfig * rest.Config ,
970976 kustomizationRef * configv1beta1.KustomizationRef , resMap resmap.ResMap ,
971- clusterSummary * configv1beta1. ClusterSummary , logger logr.Logger ,
977+ dCtx * deploymentContext , logger logr.Logger ,
972978) (localReports , remoteReports []libsveltosv1beta1.ResourceReport , err error ) {
973979
974980 // Assume that if objects are deployed in the management clusters, those are needed before any resource is deployed
975981 // in the managed cluster. So try to deploy those first if any.
976982
977983 localConfig := rest .CopyConfig (getManagementClusterConfig ())
978- adminNamespace , adminName := getClusterSummaryAdmin (clusterSummary )
984+ adminNamespace , adminName := getClusterSummaryAdmin (dCtx . clusterSummary )
979985 if adminName != "" {
980986 localConfig .Impersonate = rest.ImpersonationConfig {
981987 UserName : fmt .Sprintf ("system:serviceaccount:%s:%s" , adminNamespace , adminName ),
982988 }
983989 }
984990
985991 objectsToDeployLocally , objectsToDeployRemotely , _ , err :=
986- getKustomizedResources (ctx , c , clusterSummary , kustomizationRef .DeploymentType , resMap ,
987- kustomizationRef , logger )
992+ getKustomizedResources (ctx , c , dCtx . clusterSummary , kustomizationRef .DeploymentType , resMap ,
993+ kustomizationRef , dCtx . clusterObjects , logger )
988994 if err != nil {
989995 return nil , nil , err
990996 }
@@ -996,7 +1002,7 @@ func deployKustomizeResources(ctx context.Context, c client.Client, remoteRestCo
9961002 }
9971003 localReports , err = deployUnstructured (ctx , true , localConfig , c , objectsToDeployLocally ,
9981004 ref , kustomizationRef .Tier , kustomizationRef .SkipNamespaceCreation , libsveltosv1beta1 .FeatureKustomize ,
999- clusterSummary , []string {}, logger )
1005+ dCtx . clusterSummary , []string {}, logger )
10001006 if err != nil {
10011007 logger .V (logs .LogInfo ).Info (fmt .Sprintf ("failed to deploy to management cluster %v" , err ))
10021008 return localReports , nil , err
@@ -1012,8 +1018,8 @@ func deployKustomizeResources(ctx context.Context, c client.Client, remoteRestCo
10121018 kustomizationRef .SkipNamespaceCreation )
10131019
10141020 return localReports , nil , pullmode .StageResourcesForDeployment (ctx , getManagementClusterClient (),
1015- clusterSummary .Spec .ClusterNamespace , clusterSummary .Spec .ClusterName , configv1beta1 .ClusterSummaryKind ,
1016- clusterSummary .Name , string (libsveltosv1beta1 .FeatureKustomize ), bundleResources , false , logger , setters ... )
1021+ dCtx . clusterSummary .Spec .ClusterNamespace , dCtx . clusterSummary .Spec .ClusterName , configv1beta1 .ClusterSummaryKind ,
1022+ dCtx . clusterSummary .Name , string (libsveltosv1beta1 .FeatureKustomize ), bundleResources , false , logger , setters ... )
10171023 }
10181024
10191025 remoteClient , err := client .New (remoteRestConfig , client.Options {})
@@ -1023,7 +1029,7 @@ func deployKustomizeResources(ctx context.Context, c client.Client, remoteRestCo
10231029
10241030 remoteReports , err = deployUnstructured (ctx , false , remoteRestConfig , remoteClient , objectsToDeployRemotely ,
10251031 ref , kustomizationRef .Tier , kustomizationRef .SkipNamespaceCreation , libsveltosv1beta1 .FeatureKustomize ,
1026- clusterSummary , []string {}, logger )
1032+ dCtx . clusterSummary , []string {}, logger )
10271033 if err != nil {
10281034 logger .V (logs .LogInfo ).Info (fmt .Sprintf ("failed to deploy to remote cluster %v" , err ))
10291035 return localReports , remoteReports , err
@@ -1141,14 +1147,31 @@ func deployEachKustomizeRefs(ctx context.Context, c client.Client, remoteRestCon
11411147 clusterSummary * configv1beta1.ClusterSummary , logger logr.Logger ,
11421148) (localResourceReports , remoteResourceReports []libsveltosv1beta1.ResourceReport , err error ) {
11431149
1150+ clusterObjects , err := fetchClusterObjects (ctx , getManagementClusterConfig (), getManagementClusterClient (),
1151+ clusterSummary .Spec .ClusterNamespace , clusterSummary .Spec .ClusterName , clusterSummary .Spec .ClusterType , logger )
1152+ if err != nil {
1153+ return nil , nil , err
1154+ }
1155+
1156+ mgmtResources , err := collectTemplateResourceRefs (ctx , clusterSummary )
1157+ if err != nil {
1158+ return nil , nil , err
1159+ }
1160+ dCtx := & deploymentContext {
1161+ clusterSummary : clusterSummary ,
1162+ clusterObjects : clusterObjects ,
1163+ mgmtResources : mgmtResources ,
1164+ }
1165+
11441166 capacity := len (clusterSummary .Spec .ClusterProfileSpec .KustomizationRefs )
11451167 localResourceReports = make ([]libsveltosv1beta1.ResourceReport , 0 , capacity )
11461168 remoteResourceReports = make ([]libsveltosv1beta1.ResourceReport , 0 , capacity )
11471169 for i := range clusterSummary .Spec .ClusterProfileSpec .KustomizationRefs {
11481170 kustomizationRef := & clusterSummary .Spec .ClusterProfileSpec .KustomizationRefs [i ]
11491171 var tmpLocal []libsveltosv1beta1.ResourceReport
11501172 var tmpRemote []libsveltosv1beta1.ResourceReport
1151- tmpLocal , tmpRemote , err = deployKustomizeRef (ctx , c , remoteRestConfig , kustomizationRef , clusterSummary , logger )
1173+ tmpLocal , tmpRemote , err = deployKustomizeRef (ctx , c , remoteRestConfig , kustomizationRef ,
1174+ dCtx , logger )
11521175 if err != nil {
11531176 return nil , nil , err
11541177 }
0 commit comments