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

     1  package generators
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"sigs.k8s.io/yaml"
     9  
    10  	argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    11  )
    12  
    13  var _ Generator = (*ListGenerator)(nil)
    14  
    15  type ListGenerator struct {
    16  }
    17  
    18  func NewListGenerator() Generator {
    19  	g := &ListGenerator{}
    20  	return g
    21  }
    22  
    23  func (g *ListGenerator) GetRequeueAfter(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) time.Duration {
    24  	return NoRequeueAfter
    25  }
    26  
    27  func (g *ListGenerator) GetTemplate(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) *argoprojiov1alpha1.ApplicationSetTemplate {
    28  	return &appSetGenerator.List.Template
    29  }
    30  
    31  func (g *ListGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, appSet *argoprojiov1alpha1.ApplicationSet) ([]map[string]interface{}, error) {
    32  	if appSetGenerator == nil {
    33  		return nil, EmptyAppSetGeneratorError
    34  	}
    35  
    36  	if appSetGenerator.List == nil {
    37  		return nil, EmptyAppSetGeneratorError
    38  	}
    39  
    40  	res := make([]map[string]interface{}, len(appSetGenerator.List.Elements))
    41  
    42  	for i, tmpItem := range appSetGenerator.List.Elements {
    43  		params := map[string]interface{}{}
    44  		var element map[string]interface{}
    45  		err := json.Unmarshal(tmpItem.Raw, &element)
    46  		if err != nil {
    47  			return nil, fmt.Errorf("error unmarshling list element %v", err)
    48  		}
    49  
    50  		if appSet.Spec.GoTemplate {
    51  			res[i] = element
    52  		} else {
    53  			for key, value := range element {
    54  				if key == "values" {
    55  					values, ok := (value).(map[string]interface{})
    56  					if !ok {
    57  						return nil, fmt.Errorf("error parsing values map")
    58  					}
    59  					for k, v := range values {
    60  						value, ok := v.(string)
    61  						if !ok {
    62  							return nil, fmt.Errorf("error parsing value as string %v", err)
    63  						}
    64  						params[fmt.Sprintf("values.%s", k)] = value
    65  					}
    66  				} else {
    67  					v, ok := value.(string)
    68  					if !ok {
    69  						return nil, fmt.Errorf("error parsing value as string %v", err)
    70  					}
    71  					params[key] = v
    72  				}
    73  				res[i] = params
    74  			}
    75  		}
    76  	}
    77  
    78  	// Append elements from ElementsYaml to the response
    79  	if len(appSetGenerator.List.ElementsYaml) > 0 {
    80  
    81  		var yamlElements []map[string]interface{}
    82  		err := yaml.Unmarshal([]byte(appSetGenerator.List.ElementsYaml), &yamlElements)
    83  		if err != nil {
    84  			return nil, fmt.Errorf("error unmarshling decoded ElementsYaml %v", err)
    85  		}
    86  		res = append(res, yamlElements...)
    87  	}
    88  
    89  	return res, nil
    90  }