// substituteVariables replaces the vars with their values in the specified resource.
// If a resource is labeled or annotated with
// 'kustomize.toolkit.fluxcd.io/substitute: disabled' the substitution is skipped.
substituteVariables replaces the vars with their values in the specified resource.
If a resource is labeled or annotated with
'kustomize.toolkit.fluxcd.io/substitute: disabled' the substitution is skipped.
// substituteVariables replaces the vars with their values in the specified resource . // If a resource is labeled or annotated with // ' kustomize . toolkit . fluxcd . io / substitute : disabled ' the substitution is skipped .
func substituteVariables(
	ctx context.Context,
	kubeClient client.Client,
	kustomization kustomizev1.Kustomization,
	res *resource.Resource) (*resource.Resource, error) {
	resData, err := res.AsYAML()
	if err != nil {
		return nil, err
	}
	key := fmt.Sprintf("%s/substitute", kustomizev1.GroupVersion.Group)
	if res.GetLabels()[key] == kustomizev1.DisabledValue || res.GetAnnotations()[key] == kustomizev1.DisabledValue {
		return nil, nil
	}
	vars := make(map[string]string)
	for _, reference := range kustomization.Spec.PostBuild.SubstituteFrom {
		namespacedName := types.NamespacedName{Namespace: kustomization.Namespace, Name: reference.Name}
		switch reference.Kind {
		case "ConfigMap":
			resource := &corev1.ConfigMap{}
			if err := kubeClient.Get(ctx, namespacedName, resource); err != nil {
				return nil, fmt.Errorf("substitute from 'ConfigMap/%s' error: %w", reference.Name, err)
			}
			for k, v := range resource.Data {
				vars[k] = strings.Replace(v, "\n", "", -1)
			}
		case "Secret":
			resource := &corev1.Secret{}
			if err := kubeClient.Get(ctx, namespacedName, resource); err != nil {
				return nil, fmt.Errorf("substitute from 'Secret/%s' error: %w", reference.Name, err)
			}
			for k, v := range resource.Data {
				vars[k] = strings.Replace(string(v), "\n", "", -1)
			}
		}
	}
	if kustomization.Spec.PostBuild.Substitute != nil {
		for k, v := range kustomization.Spec.PostBuild.Substitute {
			vars[k] = strings.Replace(v, "\n", "", -1)
		}
	}
	if len(vars) > 0 {
		output, err := envsubst.Eval(string(resData), func(s string) string {
			return vars[s]
		})
		if err != nil {
			return nil, fmt.Errorf("variable substitution failed: %w", err)
		}
		jsonData, err := yaml.YAMLToJSON([]byte(output))
		if err != nil {
			return nil, fmt.Errorf("YAMLToJSON: %w", err)
		}
		err = res.UnmarshalJSON(jsonData)
		if err != nil {
			return nil, fmt.Errorf("UnmarshalJSON: %w", err)
		}
	}
	return res, nil
}
func substituteVariables ( ctx context . Context , kubeClient client . Client , kustomization kustomizev1 . Kustomization , res * resource . Resource ) ( * resource . Resource , error ) { resData , err := res . AsYAML ( ) if err != nil { return nil , err } key := fmt . Sprintf ( " " , kustomizev1 . GroupVersion . Group ) if res . GetLabels ( ) [ key ] == kustomizev1 . DisabledValue || res . GetAnnotations ( ) [ key ] == kustomizev1 . DisabledValue { return nil , nil } vars := make ( map [ string ] string ) for _ , reference := range kustomization . Spec . PostBuild . SubstituteFrom { namespacedName := types . NamespacedName { Namespace : kustomization . Namespace , Name : reference . Name } switch reference . Kind { case " " : resource := & corev1 . ConfigMap { } if err := kubeClient . Get ( ctx , namespacedName , resource ) ; err != nil { return nil , fmt . Errorf ( " " , reference . Name , err ) } for k , v := range resource . Data { vars [ k ] = strings . Replace ( v , " \n " , " " , - 1 ) } case " " : resource := & corev1 . Secret { } if err := kubeClient . Get ( ctx , namespacedName , resource ) ; err != nil { return nil , fmt . Errorf ( " " , reference . Name , err ) } for k , v := range resource . Data { vars [ k ] = strings . Replace ( string ( v ) , " \n " , " " , - 1 ) } } } if kustomization . Spec . PostBuild . Substitute != nil { for k , v := range kustomization . Spec . PostBuild . Substitute { vars [ k ] = strings . Replace ( v , " \n " , " " , - 1 ) } } if len ( vars ) > 0 { output , err := envsubst . Eval ( string ( resData ) , func ( s string ) string { return vars [ s ] } ) if err != nil { return nil , fmt . Errorf ( " " , err ) } jsonData , err := yaml . YAMLToJSON ( [ ] byte ( output ) ) if err != nil { return nil , fmt . Errorf ( " " , err ) } err = res . UnmarshalJSON ( jsonData ) if err != nil { return nil , fmt . Errorf ( " " , err ) } } return res , nil }
substituteVariables replaces the vars with their values in the specified resource.
substituteVariables replaces the vars with their values in the specified resource .
// load vars from ConfigMaps and Secrets data keys // load in-line vars (overrides the ones from resources) // run bash variable substitutions