github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/generators/value_interpolation.go (about)

     1  package generators
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  func appendTemplatedValues(values map[string]string, params map[string]interface{}, useGoTemplate bool, goTemplateOptions []string) error {
     8  	// We create a local map to ensure that we do not fall victim to a billion-laughs attack. We iterate through the
     9  	// cluster values map and only replace values in said map if it has already been allowlisted in the params map.
    10  	// Once we iterate through all the cluster values we can then safely merge the `tmp` map into the main params map.
    11  	tmp := map[string]interface{}{}
    12  
    13  	for key, value := range values {
    14  		result, err := replaceTemplatedString(value, params, useGoTemplate, goTemplateOptions)
    15  
    16  		if err != nil {
    17  			return fmt.Errorf("failed to replace templated string: %w", err)
    18  		}
    19  
    20  		if useGoTemplate {
    21  			if tmp["values"] == nil {
    22  				tmp["values"] = map[string]string{}
    23  			}
    24  			tmp["values"].(map[string]string)[key] = result
    25  		} else {
    26  			tmp[fmt.Sprintf("values.%s", key)] = result
    27  		}
    28  	}
    29  
    30  	for key, value := range tmp {
    31  		params[key] = value
    32  	}
    33  
    34  	return nil
    35  }
    36  
    37  func replaceTemplatedString(value string, params map[string]interface{}, useGoTemplate bool, goTemplateOptions []string) (string, error) {
    38  	replacedTmplStr, err := render.Replace(value, params, useGoTemplate, goTemplateOptions)
    39  	if err != nil {
    40  		return "", fmt.Errorf("failed to replace templated string with rendered values: %w", err)
    41  	}
    42  	return replacedTmplStr, nil
    43  }