@@ -20,18 +20,21 @@ import (
2020 "io/ioutil"
2121 "os"
2222 "os/exec"
23- "regexp"
2423 "runtime"
2524 "strconv"
2625 "strings"
2726)
2827
29- // Types returned by some of the functions
3028type Status struct {
3129 Namespace string
3230 Status string
3331}
3432
33+ type UpgradedRelease struct {
34+ Info map [string ]interface {} `json:"info"`
35+ Manifest string `json:"manifest"`
36+ }
37+
3538type Release struct {
3639 Name string `json:"name"`
3740 Revision string `json:"revision"`
@@ -42,64 +45,46 @@ type Release struct {
4245 Namespace string `json:"namespace"`
4346}
4447
45- // Parse the "helm status"-like output to extract relevant information
46- // WARNING: this code has been developed and tested with version 'v3.2.4' of Helm
47- // it may need to be adapted to other versions of Helm.
48- func parseStatusOutput (outs []byte , helmstatus * Status ) {
49- var outStr = string (outs )
50-
51- // Extract the namespace
52- var namespace = regexp .MustCompile (`(?m)^NAMESPACE: (.*)$` )
53- result := namespace .FindStringSubmatch (outStr )
54- if len (result ) > 1 {
55- helmstatus .Namespace = result [1 ]
56- }
57-
58- // Extract the status
59- var status = regexp .MustCompile (`(?m)^STATUS: (.*)$` )
60- result = status .FindStringSubmatch (outStr )
61- if len (result ) > 1 {
62- helmstatus .Status = result [1 ]
63- }
64- }
65-
66- // Helm functions calls
67- // --------------------
68-
6948// List ...
70- func List (namespace string ) (map [string ]Release , error ) {
71- helmlist := make (map [string ]Release , 0 )
49+ func List (level int , namespace string , debug bool ) (map [string ]Release , error ) {
50+ // Prepare parameters...
51+ var myargs = []string {"list" , "--namespace" , namespace , "-o" , "json" }
7252
73- // Get the list of Releases of the chunk
74- cmd := exec .Command ("helm" , "list" , "--namespace" , namespace , "-o" , "json" )
53+ // Run the list command
54+ if debug {
55+ log .Info (level , "running helm command : %v" , myargs )
56+ }
57+ cmd := exec .Command ("helm" , myargs ... )
7558 cmdOutput := & bytes.Buffer {}
7659 cmd .Stdout = cmdOutput
7760 cmd .Stderr = os .Stderr
78- if err := cmd .Run (); err != nil {
61+ err := cmd .Run ()
62+ output := cmdOutput .Bytes ()
63+ if debug {
64+ log .Info (level , "helm command returned:\n %s" , string (output ))
65+ }
66+ if err != nil {
7967 return nil , err
8068 }
8169
82- // Transform the received json into structs
83- output := cmdOutput .Bytes ()
8470 var releases []Release
85- err : = json .Unmarshal (output , & releases )
71+ err = json .Unmarshal (output , & releases )
8672 if err != nil {
8773 return nil , err
8874 }
8975
90- // Add the Releases into a map
76+ // Return the Releases into a map
77+ releasesMap := make (map [string ]Release , 0 )
9178 for _ , r := range releases {
92- helmlist [r .Name ] = r
79+ releasesMap [r .Name ] = r
9380 }
94-
95- return helmlist , nil
81+ return releasesMap , nil
9682}
9783
9884// UpgradeWithValues ...
99- func UpgradeWithValues (namespace string , createNamespace bool , releaseName string , chartPath string , resetValues bool , reuseValues bool , valueFiles []string , valuesSet []string , valuesSetString []string , valuesSetFile []string , force bool , timeout int , dryRun bool , debug bool ) (Status , error ) {
85+ func UpgradeWithValues (level int , namespace string , createNamespace bool , releaseName string , chartPath string , resetValues bool , reuseValues bool , valueFiles []string , valuesSet []string , valuesSetString []string , valuesSetFile []string , force bool , timeout int , dryRun bool , debug bool ) (UpgradedRelease , error ) {
10086 // Prepare parameters...
101- var myargs = []string {"upgrade" , "--install" , releaseName , chartPath , "--namespace" , namespace , "--timeout" , strconv .Itoa (timeout ) + "s" }
102-
87+ var myargs = []string {"upgrade" , "--install" , releaseName , chartPath , "--namespace" , namespace , "--timeout" , strconv .Itoa (timeout ) + "s" , "-o" , "json" }
10388 for _ , v := range valuesSet {
10489 myargs = append (myargs , "--set" )
10590 myargs = append (myargs , v )
@@ -131,31 +116,31 @@ func UpgradeWithValues(namespace string, createNamespace bool, releaseName strin
131116 if createNamespace {
132117 myargs = append (myargs , "--create-namespace" )
133118 }
134- if debug {
135- myargs = append (myargs , "--debug" )
136- log .Info (1 , "running helm command for \" %s\" : %v\n " , releaseName , myargs )
137- }
138119
139120 // Run the upgrade command
121+ if debug {
122+ log .Info (level , "running helm command for \" %s\" : %v" , releaseName , myargs )
123+ }
140124 cmd := exec .Command ("helm" , myargs ... )
141-
142125 cmdOutput := & bytes.Buffer {}
143126 cmd .Stderr = os .Stderr
144127 cmd .Stdout = cmdOutput
145128 err := cmd .Run ()
146129 output := cmdOutput .Bytes ()
147-
148130 if debug {
149- log .Info (1 , "helm command for \" %s\" returned: \n %s\n " , releaseName , string (output ))
131+ log .Info (level , "helm command for \" %s\" returned:\n %s" , releaseName , string (output ))
150132 }
151133 if err != nil {
152- return Status {}, err
134+ return UpgradedRelease {}, err
135+ }
136+
137+ var upgradedRelease UpgradedRelease
138+ err = json .Unmarshal (output , & upgradedRelease )
139+ if err != nil {
140+ return UpgradedRelease {}, err
153141 }
154142
155- // Parse the ending helm status.
156- status := Status {}
157- parseStatusOutput (output , & status )
158- return status , nil
143+ return upgradedRelease , nil
159144}
160145
161146// Fetch ...
0 commit comments