Skip to content

Commit

Permalink
Fix #73
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe VILA committed Jul 12, 2021
1 parent 12a3f8c commit c7866c8
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 154 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes

## Version 4.0.9 - 07/12/2021
* Fixed [`#73`](https://github.com/ThalesGroup/helm-spray/issues/73) (cvila84)

## Version 4.0.8 - 06/24/2021
* 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

Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/gemalto/helm-spray/v4

go 1.15

require github.com/spf13/cobra v1.1.3

require helm.sh/helm/v3 v3.6.1
require (
github.com/spf13/cobra v1.1.3
helm.sh/helm/v3 v3.6.1
k8s.io/api v0.21.0
k8s.io/client-go v0.21.0
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
Expand Down
91 changes: 38 additions & 53 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
)

// Types returned by some of the functions
type Status struct {
Namespace string
Status string
}

type UpgradedRelease struct {
Info map[string]interface{} `json:"info"`
Manifest string `json:"manifest"`
}

type Release struct {
Name string `json:"name"`
Revision string `json:"revision"`
Expand All @@ -42,64 +45,46 @@ type Release struct {
Namespace string `json:"namespace"`
}

// Parse the "helm status"-like output to extract relevant information
// WARNING: this code has been developed and tested with version 'v3.2.4' of Helm
// it may need to be adapted to other versions of Helm.
func parseStatusOutput(outs []byte, helmstatus *Status) {
var outStr = string(outs)

// Extract the namespace
var namespace = regexp.MustCompile(`(?m)^NAMESPACE: (.*)$`)
result := namespace.FindStringSubmatch(outStr)
if len(result) > 1 {
helmstatus.Namespace = result[1]
}

// Extract the status
var status = regexp.MustCompile(`(?m)^STATUS: (.*)$`)
result = status.FindStringSubmatch(outStr)
if len(result) > 1 {
helmstatus.Status = result[1]
}
}

// Helm functions calls
// --------------------

// List ...
func List(namespace string) (map[string]Release, error) {
helmlist := make(map[string]Release, 0)
func List(level int, namespace string, debug bool) (map[string]Release, error) {
// Prepare parameters...
var myargs = []string{"list", "--namespace", namespace, "-o", "json"}

// Get the list of Releases of the chunk
cmd := exec.Command("helm", "list", "--namespace", namespace, "-o", "json")
// Run the list command
if debug {
log.Info(level, "running helm command : %v", myargs)
}
cmd := exec.Command("helm", myargs...)
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
err := cmd.Run()
output := cmdOutput.Bytes()
if debug {
log.Info(level, "helm command returned:\n%s", string(output))
}
if err != nil {
return nil, err
}

// Transform the received json into structs
output := cmdOutput.Bytes()
var releases []Release
err := json.Unmarshal(output, &releases)
err = json.Unmarshal(output, &releases)
if err != nil {
return nil, err
}

// Add the Releases into a map
// Return the Releases into a map
releasesMap := make(map[string]Release, 0)
for _, r := range releases {
helmlist[r.Name] = r
releasesMap[r.Name] = r
}

return helmlist, nil
return releasesMap, nil
}

// UpgradeWithValues ...
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) {
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) {
// Prepare parameters...
var myargs = []string{"upgrade", "--install", releaseName, chartPath, "--namespace", namespace, "--timeout", strconv.Itoa(timeout) + "s"}

var myargs = []string{"upgrade", "--install", releaseName, chartPath, "--namespace", namespace, "--timeout", strconv.Itoa(timeout) + "s", "-o", "json"}
for _, v := range valuesSet {
myargs = append(myargs, "--set")
myargs = append(myargs, v)
Expand Down Expand Up @@ -131,31 +116,31 @@ func UpgradeWithValues(namespace string, createNamespace bool, releaseName strin
if createNamespace {
myargs = append(myargs, "--create-namespace")
}
if debug {
myargs = append(myargs, "--debug")
log.Info(1, "running helm command for \"%s\": %v\n", releaseName, myargs)
}

// Run the upgrade command
if debug {
log.Info(level, "running helm command for \"%s\": %v", releaseName, myargs)
}
cmd := exec.Command("helm", myargs...)

cmdOutput := &bytes.Buffer{}
cmd.Stderr = os.Stderr
cmd.Stdout = cmdOutput
err := cmd.Run()
output := cmdOutput.Bytes()

if debug {
log.Info(1, "helm command for \"%s\" returned: \n%s\n", releaseName, string(output))
log.Info(level, "helm command for \"%s\" returned:\n%s", releaseName, string(output))
}
if err != nil {
return Status{}, err
return UpgradedRelease{}, err
}

var upgradedRelease UpgradedRelease
err = json.Unmarshal(output, &upgradedRelease)
if err != nil {
return UpgradedRelease{}, err
}

// Parse the ending helm status.
status := Status{}
parseStatusOutput(output, &status)
return status, nil
return upgradedRelease, nil
}

// Fetch ...
Expand Down
Loading

0 comments on commit c7866c8

Please sign in to comment.