Merge pull request #395 from felipecrs/helm-show-version

Use helm show chart to identify chart version
This commit is contained in:
yxxhero 2022-10-05 10:09:47 +08:00 committed by GitHub
commit e92c71f9d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 115 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package app
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
@ -18,6 +19,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/variantdev/vals"
"go.uber.org/zap"
"helm.sh/helm/v3/pkg/chart"
"github.com/helmfile/helmfile/pkg/envvar"
"github.com/helmfile/helmfile/pkg/exectest"
@ -2596,6 +2598,10 @@ func (helm *mockHelmExec) IsVersionAtLeast(versionStr string) bool {
return false
}
func (helm *mockHelmExec) ShowChart(chartPath string) (chart.Metadata, error) {
return chart.Metadata{}, errors.New("tests logs rely on this error")
}
func TestTemplate_SingleStateFile(t *testing.T) {
files := map[string]string{
"/path/to/helmfile.yaml": `

View File

@ -1,6 +1,10 @@
package app
import "github.com/helmfile/helmfile/pkg/helmexec"
import (
"helm.sh/helm/v3/pkg/chart"
"github.com/helmfile/helmfile/pkg/helmexec"
)
type noCallHelmExec struct {
}
@ -113,3 +117,8 @@ func (helm *noCallHelmExec) IsVersionAtLeast(versionStr string) bool {
helm.doPanic()
return false
}
func (helm *noCallHelmExec) ShowChart(chartPath string) (chart.Metadata, error) {
helm.doPanic()
return chart.Metadata{}, nil
}

View File

@ -7,6 +7,7 @@ import (
"sync"
"github.com/Masterminds/semver/v3"
"helm.sh/helm/v3/pkg/chart"
"github.com/helmfile/helmfile/pkg/helmexec"
)
@ -227,3 +228,12 @@ func (helm *Helm) sync(m *sync.Mutex, f func()) {
f()
}
func (helm *Helm) ShowChart(chartPath string) (chart.Metadata, error) {
switch chartPath {
case "../../foo-bar":
return chart.Metadata{Version: "3.2.0"}, nil
default:
return chart.Metadata{}, errors.New("fake test error")
}
}

View File

@ -14,6 +14,8 @@ import (
"github.com/Masterminds/semver/v3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/plugin"
@ -599,3 +601,21 @@ func resolveOciChart(ociChart string) (ociChartURL, ociChartTag string) {
ociChartURL = fmt.Sprintf("oci://%s", ociChart[:urlTagIndex])
return ociChartURL, ociChartTag
}
func (helm *execer) ShowChart(chartPath string) (chart.Metadata, error) {
if !helm.IsHelm3() {
// show chart command isn't supported in helm2
return chart.Metadata{}, fmt.Errorf("helm show isn't supported in helm2")
}
var helmArgs = []string{"show", "chart", chartPath}
out, error := helm.exec(helmArgs, map[string]string{}, nil)
if error != nil {
return chart.Metadata{}, error
}
var metadata chart.Metadata
error = yaml.Unmarshal(out, &metadata)
if error != nil {
return chart.Metadata{}, error
}
return metadata, nil
}

View File

@ -1008,3 +1008,32 @@ func Test_resolveOciChart(t *testing.T) {
})
}
}
func Test_ShowChart(t *testing.T) {
helm2Runner := mockRunner{output: []byte("Client: v2.16.1+ge13bc94\n")}
helm := New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
_, err := helm.ShowChart("fake-chart")
if err == nil {
t.Error("helmexec.ShowChart() - helm show isn't supported in helm2")
}
showChartRunner := mockRunner{output: []byte("name: my-chart\nversion: 3.2.0\n")}
helm = &execer{
helmBinary: "helm",
version: *semver.MustParse("3.3.2"),
logger: NewLogger(os.Stdout, "info"),
kubeContext: "dev",
runner: &showChartRunner,
}
metadata, err := helm.ShowChart("my-chart")
if err != nil {
t.Errorf("helmexec.ShowChart() - unexpected error: %v", err)
}
if metadata.Name != "my-chart" {
t.Errorf("helmexec.ShowChart() - expected chart name was %s, received: %s", "my-chart", metadata.Name)
}
if metadata.Version != "3.2.0" {
t.Errorf("helmexec.ShowChart() - expected chart version was %s, received: %s", "3.2.0", metadata.Version)
}
}

View File

@ -1,5 +1,7 @@
package helmexec
import "helm.sh/helm/v3/pkg/chart"
// Version represents the version of helm
type Version struct {
Major int
@ -33,6 +35,7 @@ type Interface interface {
IsHelm3() bool
GetVersion() Version
IsVersionAtLeast(versionStr string) bool
ShowChart(chart string) (chart.Metadata, error)
}
type DependencyUpdater interface {

View File

@ -938,9 +938,12 @@ func (st *HelmState) getDeployedVersion(context helmexec.HelmContext, helm helme
if len(versions) > 0 {
return versions[1], nil
} else {
//fails to find the version
chartMetadata, err := helm.ShowChart(release.Chart)
if err != nil {
return "failed to get version", errors.New("Failed to get the version for: " + chartName)
}
return chartMetadata.Version, nil
}
} else {
return "failed to get version", err
}

View File

@ -1455,6 +1455,16 @@ func TestGetDeployedVersion(t *testing.T) {
foo-bar-release 1 Wed Apr 17 17:39:04 2019 DEPLOYED foo-bar-1.0.0-alpha+001 0.1.0 default`,
installedVersion: "1.0.0-alpha+001",
},
{
name: "chart version from helm show chart",
release: ReleaseSpec{
Name: "foo",
Chart: "../../foo-bar",
},
listResult: `NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
foo 1 Wed Apr 17 17:39:04 2019 DEPLOYED foo-bar 0.1.0 default`,
installedVersion: "3.2.0",
},
}
for i := range tests {
tt := tests[i]
@ -1467,6 +1477,7 @@ func TestGetDeployedVersion(t *testing.T) {
valsRuntime: valsRuntime,
RenderedValues: map[string]interface{}{},
}
helm := &exectest.Helm{
Lists: map[exectest.ListKey]string{},
}