@@ -51,6 +51,7 @@ import (
51
51
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
52
52
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
53
53
"github.com/projectsveltos/libsveltos/lib/deployer"
54
+ "github.com/projectsveltos/libsveltos/lib/funcmap"
54
55
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
55
56
libsveltostemplate "github.com/projectsveltos/libsveltos/lib/template"
56
57
"github.com/projectsveltos/libsveltos/lib/utils"
@@ -415,27 +416,30 @@ func instantiateKustomizeSubstituteValues(ctx context.Context, clusterSummary *c
415
416
416
417
// getKustomizeSubstituteValues returns all key-value pair looking at both Values and ValuesFrom
417
418
func getKustomizeSubstituteValues (ctx context.Context , c client.Client , clusterSummary * configv1beta1.ClusterSummary ,
418
- kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ) (map [string ]string , error ) {
419
+ kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ) (templatedValues , nonTemplatedValues map [string ]string , err error ) {
419
420
420
421
values := make (map [string ]string )
421
422
for k := range kustomizationRef .Values {
422
423
values [k ] = kustomizationRef .Values [k ]
423
424
}
425
+
424
426
// Get key-value pairs from ValuesFrom
425
- valuesFrom , err : = getKustomizeSubstituteValuesFrom (ctx , c , clusterSummary , kustomizationRef , logger )
427
+ templatedValues , nonTemplatedValues , err = getKustomizeSubstituteValuesFrom (ctx , c , clusterSummary , kustomizationRef , logger )
426
428
if err != nil {
427
- return nil , err
429
+ return nil , nil , err
428
430
}
429
- for k := range valuesFrom {
430
- values [k ] = valuesFrom [k ]
431
+
432
+ // Values are always treated as templates. So copy the templated values here
433
+ for k := range templatedValues {
434
+ values [k ] = templatedValues [k ]
431
435
}
432
436
433
- return values , nil
437
+ return values , nonTemplatedValues , nil
434
438
}
435
439
436
440
// getKustomizeSubstituteValuesFrom return key-value pair from referenced ConfigMap/Secret
437
441
func getKustomizeSubstituteValuesFrom (ctx context.Context , c client.Client , clusterSummary * configv1beta1.ClusterSummary ,
438
- kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ) (map [string ]string , error ) {
442
+ kustomizationRef * configv1beta1.KustomizationRef , logger logr.Logger ) (templatedValues , nonTemplatedValues map [string ]string , err error ) {
439
443
440
444
return getValuesFrom (ctx , c , clusterSummary , kustomizationRef .ValuesFrom , true , logger )
441
445
}
@@ -615,18 +619,21 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
615
619
}
616
620
617
621
// Get key-value pairs from ValuesFrom
618
- values , err := getKustomizeSubstituteValues (ctx , c , clusterSummary , kustomizationRef , logger )
622
+ templatedValues , nonTemplatedValues , err := getKustomizeSubstituteValues (ctx , c , clusterSummary , kustomizationRef , logger )
619
623
if err != nil {
620
624
return nil , nil , nil , err
621
625
}
622
626
623
627
// Get substitute values. Those are collected from Data sections of referenced ConfigMap/Secret instances.
624
- // Those values can be expressed as template. This method collects them and instantiate those using resources in
628
+ // Those values can be expressed as template. This method instantiates those using resources in
625
629
// the managemenet cluster
626
- instantiateSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , clusterSummary , mgmtResources , values , logger )
630
+ instantiatedSubstituteValues , err := instantiateKustomizeSubstituteValues (ctx , clusterSummary , mgmtResources , templatedValues , logger )
627
631
if err != nil {
628
632
return nil , nil , nil , err
629
633
}
634
+ for k := range nonTemplatedValues {
635
+ instantiatedSubstituteValues [k ] = nonTemplatedValues [k ]
636
+ }
630
637
631
638
resources := resMap .Resources ()
632
639
for i := range resources {
@@ -638,11 +645,11 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
638
645
}
639
646
640
647
// Assume it is a template only if there are values to substitute
641
- if len (instantiateSubstituteValues ) > 0 {
648
+ if len (instantiatedSubstituteValues ) > 0 {
642
649
// All objects coming from Kustomize output can be expressed as template. Those will be instantiated using
643
650
// substitute values first, and the resource in the management cluster later.
644
651
templateName := fmt .Sprintf ("%s-substitutevalues" , clusterSummary .Name )
645
- yaml , err = instantiateResourceWithSubstituteValues (templateName , yaml , instantiateSubstituteValues , logger )
652
+ yaml , err = instantiateResourceWithSubstituteValues (templateName , yaml , instantiatedSubstituteValues , logger )
646
653
if err != nil {
647
654
msg := fmt .Sprintf ("failed to instantiate resource with substitute values: %v" , err )
648
655
logger .V (logs .LogInfo ).Info (msg )
@@ -882,7 +889,7 @@ func extractTarGz(src, dest string) error {
882
889
func instantiateResourceWithSubstituteValues (templateName string , resource []byte ,
883
890
substituteValues map [string ]string , logger logr.Logger ) ([]byte , error ) {
884
891
885
- tmpl , err := template .New (templateName ).Option ("missingkey=error" ).Funcs (ExtraFuncMap ()).Parse (string (resource ))
892
+ tmpl , err := template .New (templateName ).Option ("missingkey=error" ).Funcs (funcmap . SveltosFuncMap ()).Parse (string (resource ))
886
893
if err != nil {
887
894
return nil , err
888
895
}
0 commit comments