From e3072b01fe7d003052d1f8a243f7ca304dd2340c Mon Sep 17 00:00:00 2001 From: ss75710541 <75710541@qq.com> Date: Tue, 26 Apr 2022 19:20:49 +0800 Subject: [PATCH] fix: repo.GetAppDetails().Helm.GetParameterValueByName not get helm.values --- Dockerfile | 1 + shared/argocd/service.go | 50 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index c3fc826a..1437a1c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,4 @@ +ARG BUILDPLATFORM=linux/amd64 FROM --platform=$BUILDPLATFORM golang:1.16.2 as builder RUN apt-get update && apt-get install ca-certificates diff --git a/shared/argocd/service.go b/shared/argocd/service.go index 349fe975..74bff056 100644 --- a/shared/argocd/service.go +++ b/shared/argocd/service.go @@ -3,6 +3,8 @@ package argocd import ( "context" "fmt" + "k8s.io/apimachinery/pkg/util/yaml" + "strings" "github.com/argoproj-labs/argocd-notifications/expr/shared" "github.com/argoproj/argo-cd/v2/common" @@ -117,10 +119,8 @@ func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1 if err != nil { return nil, err } - var has *shared.HelmAppSpec if appDetail.Helm != nil { - if appSource.Helm.Parameters != nil { for _, overrideParam := range appSource.Helm.Parameters { for _, defaultParam := range appDetail.Helm.Parameters { @@ -132,11 +132,37 @@ func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1 } } + if appSource.Helm.Values != "" { + output := map[string]string{} + valuesMap := make(map[string]interface{}) + if err := yaml.Unmarshal([]byte(appSource.Helm.Values), valuesMap); err != nil { + return nil, fmt.Errorf("failed to parse : %s, appSource.Helm.Values: %v", err, appSource.Helm.Values) + } + flatVals(valuesMap, output) + + fmt.Printf("-------flatVals output: %v", output) + for i := range appDetail.Helm.Parameters { + if v, ok := output[has.Parameters[i].Name]; ok { + has.Parameters[i].Value = v + delete(output, has.Parameters[i].Name) + break + } + } + + fmt.Printf("-------after delete output: %v", output) + + for k, v := range output { + appDetail.Helm.Parameters = append(appDetail.Helm.Parameters, &v1alpha1.HelmParameter{Name: k, Value: v}) + } + + fmt.Printf("-------appDetail.Helm.Parameters: %v", appDetail.Helm.Parameters) + } + has = &shared.HelmAppSpec{ Name: appDetail.Helm.Name, - ValueFiles: appDetail.Helm.ValueFiles, + ValueFiles: appSource.Helm.ValueFiles, Parameters: appDetail.Helm.Parameters, - Values: appDetail.Helm.Values, + Values: appSource.Helm.Values, FileParameters: appDetail.Helm.FileParameters, } } @@ -152,3 +178,19 @@ func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1 func (svc *argoCDService) Close() { svc.dispose() } + +func flatVals(input interface{}, output map[string]string, prefixes ...string) { + switch i := input.(type) { + case map[string]interface{}: + for k, v := range i { + flatVals(v, output, append(prefixes, k)...) + } + case []interface{}: + p := append([]string(nil), prefixes...) + for j, v := range i { + flatVals(v, output, append(p[0:len(p)-1], fmt.Sprintf("%s[%v]", prefixes[len(p)-1], j))...) + } + default: + output[strings.Join(prefixes, ".")] = fmt.Sprintf("%v", i) + } +}