From bb46b21077df82367383feb0f3dd0b61ea0d4a47 Mon Sep 17 00:00:00 2001 From: ss75710541 <75710541@qq.com> Date: Wed, 27 Apr 2022 15:55:45 +0800 Subject: [PATCH] fix: repo.GetAppDetails().Helm.GetParameterValueByName not get helm.values --- shared/argocd/service.go | 81 ++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/shared/argocd/service.go b/shared/argocd/service.go index 7677dcd8..f4ad89f6 100644 --- a/shared/argocd/service.go +++ b/shared/argocd/service.go @@ -3,6 +3,7 @@ package argocd import ( "context" "fmt" + "github.com/ghodss/yaml" "strings" "github.com/argoproj-labs/argocd-notifications/expr/shared" @@ -118,15 +119,6 @@ func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1 if err != nil { return nil, err } - data, err := appSource.Helm.Marshal() - if err != nil { - return nil, err - } - - fmt.Sprintf("----- appSource: %v", appSource) - - appDetail.Helm.Unmarshal(data) - fmt.Sprintf("----- appDetail: %v", appDetail) var has *shared.HelmAppSpec if appDetail.Helm != nil { @@ -141,33 +133,39 @@ func (svc *argoCDService) GetAppDetails(ctx context.Context, appSource *v1alpha1 } } - //if appSource.Helm.Values != "" { - // - // fmt.Printf("-------start output\n") - // output := map[string]string{} - // valuesMap := make(map[interface{}]interface{}) - // appSource.Helm.Unmarshal([]byte(appSource.Helm.Values)) - // - // fmt.Printf("-------flatVals output\n") - // //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\n", output) - // for i := range appDetail.Helm.Parameters { - // if v, ok := output[appDetail.Helm.Parameters[i].Name]; ok { - // parameter := &v1alpha1.HelmParameter{Name: appDetail.Helm.Parameters[i].Name, Value: v} - // appDetail.Helm.Parameters[i] = parameter - // } - // } - // - // fmt.Printf("-------appSource.Helm.Parameters: %v\n", appSource.Helm.Parameters) - // fmt.Printf("-------appDetail.Helm.Parameters: %v\n", appDetail.Helm.Parameters) - //} + if appSource.Helm.Values != "" { + output := map[string]*v1alpha1.HelmParameter{} + for _, param := range appDetail.Helm.Parameters { + output[param.Name] = param + } + params, err := GetHelmParametersByValues(appSource.Helm.Values) + if err != nil { + return nil, err + } + for k, v := range params { + output[k] = &v1alpha1.HelmParameter{ + Name: k, + Value: v, + } + } + appDetail.Helm.Parameters = nil + for k, v := range output { + appDetail.Helm.Parameters = append(appDetail.Helm.Parameters, &v1alpha1.HelmParameter{ + Name: k, + Value: v.Value, + ForceString: v.ForceString, + }) + } + + fmt.Printf("-------flatVals output\n") + //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 + // } + //} + } has = &shared.HelmAppSpec{ Name: appDetail.Helm.Name, ValueFiles: appDetail.Helm.ValueFiles, @@ -189,6 +187,17 @@ func (svc *argoCDService) Close() { svc.dispose() } +func GetHelmParametersByValues(values string) (map[string]string, error) { + output := map[string]string{} + valuesMap := map[string]interface{}{} + if err := yaml.Unmarshal([]byte(values), &valuesMap); err != nil { + return nil, fmt.Errorf("failed to parse values: %s", err) + } + flatVals(valuesMap, output) + + return output, nil +} + func flatVals(input interface{}, output map[string]string, prefixes ...string) { switch i := input.(type) { case map[string]interface{}: