Skip to content

Commit c7866c8

Browse files
author
Christophe VILA
committed
Fix #73
1 parent 12a3f8c commit c7866c8

File tree

7 files changed

+128
-154
lines changed

7 files changed

+128
-154
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Release Notes
22

3+
## Version 4.0.9 - 07/12/2021
4+
* Fixed [`#73`](https://github.com/ThalesGroup/helm-spray/issues/73) (cvila84)
5+
36
## Version 4.0.8 - 06/24/2021
47
* Exposed helm install/update --create-namespace flag on spray. Since 4.0.6, --create-namespace is automatically passed to helm install/update commands but because it is trying to create namespace even if it already exists, it can generate errors when user rights on cluster do not include namespace creation
58

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module github.com/gemalto/helm-spray/v4
22

33
go 1.15
44

5-
require github.com/spf13/cobra v1.1.3
6-
7-
require helm.sh/helm/v3 v3.6.1
5+
require (
6+
github.com/spf13/cobra v1.1.3
7+
helm.sh/helm/v3 v3.6.1
8+
k8s.io/api v0.21.0
9+
k8s.io/client-go v0.21.0
10+
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb
552552
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
553553
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
554554
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
555+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
555556
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
556557
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
557558
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=

pkg/helm/helm.go

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3028
type 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+
3538
type 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

Comments
 (0)