From e7d00d75373e877cbc0297b4919840bf23fbf6a0 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Wed, 7 Jul 2021 23:29:00 +0800 Subject: [PATCH] fix chart nil issue Signed-off-by: yxxhero --- cmd/helm/list.go | 19 +++++++++++++------ cmd/helm/list_test.go | 9 +++++++++ pkg/chartutil/coalesce.go | 4 ++++ pkg/chartutil/coalesce_test.go | 5 +++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index f8be65b17..e28f61406 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -153,15 +153,22 @@ func newReleaseListWriter(releases []*release.Release, timeFormat string) *relea elements := make([]releaseElement, 0, len(releases)) for _, r := range releases { element := releaseElement{ - Name: r.Name, - Namespace: r.Namespace, - Revision: strconv.Itoa(r.Version), - Status: r.Info.Status.String(), - Chart: fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version), - AppVersion: r.Chart.Metadata.AppVersion, + Name: r.Name, + Namespace: r.Namespace, + Revision: strconv.Itoa(r.Version), + Status: r.Info.Status.String(), } t := "-" + + if r.Chart != nil { + element.Chart = fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version) + element.AppVersion = r.Chart.Metadata.AppVersion + } else { + element.Chart = t + element.AppVersion = t + } + if tspb := r.Info.LastDeployed; !tspb.IsZero() { if timeFormat != "" { t = tspb.Format(timeFormat) diff --git a/cmd/helm/list_test.go b/cmd/helm/list_test.go index b3b29356e..8a049210e 100644 --- a/cmd/helm/list_test.go +++ b/cmd/helm/list_test.go @@ -51,6 +51,15 @@ func TestListCmd(t *testing.T) { }, Chart: chartInfo, }, + { + Name: "nochartinfo", + Version: 1, + Namespace: defaultNamespace, + Info: &release.Info{ + LastDeployed: timestamp1, + Status: release.StatusSuperseded, + }, + }, { Name: "starlord", Version: 2, diff --git a/pkg/chartutil/coalesce.go b/pkg/chartutil/coalesce.go index 7b1ee1d80..75fe18a3a 100644 --- a/pkg/chartutil/coalesce.go +++ b/pkg/chartutil/coalesce.go @@ -17,6 +17,7 @@ limitations under the License. package chartutil import ( + "fmt" "log" "github.com/mitchellh/copystructure" @@ -35,6 +36,9 @@ import ( // - A chart has access to all of the variables for it, as well as all of // the values destined for its dependencies. func CoalesceValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) { + if chrt == nil { + return nil, fmt.Errorf("the chart is nil") + } v, err := copystructure.Copy(vals) if err != nil { return vals, err diff --git a/pkg/chartutil/coalesce_test.go b/pkg/chartutil/coalesce_test.go index fc55e0a25..292b9a035 100644 --- a/pkg/chartutil/coalesce_test.go +++ b/pkg/chartutil/coalesce_test.go @@ -116,6 +116,11 @@ func TestCoalesceValues(t *testing.T) { valsCopy[key] = value } + _, err = CoalesceValues(nil, vals) + if err == nil { + t.Errorf("Excepted errors when chart is nil") + } + v, err := CoalesceValues(c, vals) if err != nil { t.Fatal(err)