Skip to content

Commit 8395c2e

Browse files
committed
Extract ApplicationTypeHelm case into helper function
Signed-off-by: Graham Beckley <[email protected]>
1 parent e5c9439 commit 8395c2e

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

pkg/argocd/update.go

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -460,95 +460,97 @@ func marshalKustomizeOverride(app *v1alpha1.Application, originalData []byte) ([
460460
return marshalWithIndent(overrides, defaultIndent)
461461
}
462462

463-
// marshalParamsOverride marshals the parameter overrides of a given application
464-
// into YAML bytes
465-
func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]byte, error) {
466-
var override []byte
467-
var err error
468-
463+
func marshalHelmOverride(app *v1alpha1.Application, originalData []byte) (override []byte, err error) {
469464
appSource := getApplicationSource(app)
465+
if appSource.Helm == nil {
466+
return []byte{}, nil
467+
}
470468

471-
switch GetApplicationType(app) {
472-
case ApplicationTypeKustomize:
473-
override, err = marshalKustomizeOverride(app, originalData)
474-
case ApplicationTypeHelm:
475-
if appSource.Helm == nil {
476-
return []byte{}, nil
477-
}
469+
if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
470+
images := GetImagesAndAliasesFromApplication(app)
478471

479-
if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
480-
images := GetImagesAndAliasesFromApplication(app)
472+
helmNewValues := yaml.Node{}
473+
if unmarshalErr := yaml.Unmarshal(originalData, &helmNewValues); unmarshalErr != nil {
474+
return nil, unmarshalErr
475+
}
481476

482-
helmNewValues := yaml.Node{}
483-
err = yaml.Unmarshal(originalData, &helmNewValues)
484-
if err != nil {
485-
return nil, err
477+
for _, c := range images {
478+
if c.ImageAlias == "" {
479+
continue
486480
}
487481

488-
for _, c := range images {
489-
if c.ImageAlias == "" {
490-
continue
491-
}
492-
493-
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c)
482+
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c)
494483

495-
if helmAnnotationParamName == "" {
496-
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
497-
}
498-
// for image-spec annotation, helmAnnotationParamName holds image-spec annotation value,
499-
// and helmAnnotationParamVersion is empty
500-
if helmAnnotationParamVersion == "" {
501-
if c.GetParameterHelmImageSpec(app.Annotations, common.ImageUpdaterAnnotationPrefix) == "" {
502-
// not a full image-spec, so image-tag is required
503-
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
504-
}
505-
} else {
506-
// image-tag annotation is present, so continue to process image-tag
507-
helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
508-
if helmParamVersion == nil {
509-
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
510-
}
511-
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
512-
if err != nil {
513-
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
514-
}
484+
if helmAnnotationParamName == "" {
485+
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
486+
}
487+
// for image-spec annotation, helmAnnotationParamName holds image-spec annotation value,
488+
// and helmAnnotationParamVersion is empty
489+
if helmAnnotationParamVersion == "" {
490+
if c.GetParameterHelmImageSpec(app.Annotations, common.ImageUpdaterAnnotationPrefix) == "" {
491+
// not a full image-spec, so image-tag is required
492+
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
515493
}
516-
517-
helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
518-
if helmParamName == nil {
519-
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamName)
494+
} else {
495+
// image-tag annotation is present, so continue to process image-tag
496+
helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
497+
if helmParamVersion == nil {
498+
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
520499
}
521-
522-
err = setHelmValue(&helmNewValues, helmAnnotationParamName, helmParamName.Value)
500+
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
523501
if err != nil {
524-
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
502+
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
525503
}
526504
}
527505

528-
override, err = marshalWithIndent(&helmNewValues, defaultIndent)
529-
} else {
530-
var params helmOverride
531-
newParams := helmOverride{
532-
Helm: helmParameters{
533-
Parameters: appSource.Helm.Parameters,
534-
},
506+
helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
507+
if helmParamName == nil {
508+
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamName)
535509
}
536510

537-
outputParams := appSource.Helm.ValuesYAML()
538-
log.WithContext().AddField("application", app).Debugf("values: '%s'", outputParams)
539-
540-
if len(originalData) == 0 {
541-
override, err = marshalWithIndent(newParams, defaultIndent)
542-
break
543-
}
544-
err = yaml.Unmarshal(originalData, &params)
511+
err = setHelmValue(&helmNewValues, helmAnnotationParamName, helmParamName.Value)
545512
if err != nil {
546-
override, err = marshalWithIndent(newParams, defaultIndent)
547-
break
513+
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
548514
}
549-
mergeHelmOverride(&params, &newParams)
550-
override, err = marshalWithIndent(params, defaultIndent)
551515
}
516+
return marshalWithIndent(&helmNewValues, defaultIndent)
517+
}
518+
519+
var params helmOverride
520+
newParams := helmOverride{
521+
Helm: helmParameters{
522+
Parameters: appSource.Helm.Parameters,
523+
},
524+
}
525+
526+
outputParams := appSource.Helm.ValuesYAML()
527+
log.WithContext().AddField("application", app).Debugf("values: '%s'", outputParams)
528+
529+
if len(originalData) == 0 {
530+
override, err = marshalWithIndent(newParams, defaultIndent)
531+
return override, err
532+
}
533+
err = yaml.Unmarshal(originalData, &params)
534+
if err != nil {
535+
// TODO: if err is not nill, why do we try to do marshalWithIndent and not return nil?
536+
override, err = marshalWithIndent(newParams, defaultIndent)
537+
return override, err
538+
}
539+
mergeHelmOverride(&params, &newParams)
540+
return marshalWithIndent(params, defaultIndent)
541+
}
542+
543+
// marshalParamsOverride marshals the parameter overrides of a given application
544+
// into YAML bytes
545+
func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]byte, error) {
546+
var override []byte
547+
var err error
548+
549+
switch GetApplicationType(app) {
550+
case ApplicationTypeKustomize:
551+
override, err = marshalKustomizeOverride(app, originalData)
552+
case ApplicationTypeHelm:
553+
override, err = marshalHelmOverride(app, originalData)
552554
default:
553555
err = fmt.Errorf("unsupported application type")
554556
}

0 commit comments

Comments
 (0)