Skip to content

Commit

Permalink
Merge pull request helm#10912 from SoujanyaMangipudi/main
Browse files Browse the repository at this point in the history
feat(helm): Supporting helm3 to show up resource names in helm status
  • Loading branch information
mattfarina authored Dec 23, 2022
2 parents 5a8e91a + 9de543a commit eb7150f
Show file tree
Hide file tree
Showing 15 changed files with 982 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/helm/get_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return tpl(template, data, out)
}

return output.Table.Write(out, &statusPrinter{res, true, false})
return output.Table.Write(out, &statusPrinter{res, true, false, false})
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return errors.Wrap(err, "INSTALLATION FAILED")
}

return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false})
return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false, false})
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/release_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
return runErr
}

if err := outfmt.Write(out, &statusPrinter{rel, settings.Debug, false}); err != nil {
if err := outfmt.Write(out, &statusPrinter{rel, settings.Debug, false, false}); err != nil {
return err
}

Expand Down
37 changes: 35 additions & 2 deletions cmd/helm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"bytes"
"fmt"
"io"
"log"
Expand All @@ -25,6 +26,8 @@ import (

"github.com/spf13/cobra"

"k8s.io/kubectl/pkg/cmd/get"

"helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chartutil"
Expand All @@ -41,7 +44,7 @@ The status consists of:
- state of the release (can be: unknown, deployed, uninstalled, superseded, failed, uninstalling, pending-install, pending-upgrade or pending-rollback)
- revision of the release
- description of the release (can be completion message or error message, need to enable --show-desc)
- list of resources that this release consists of, sorted by kind
- list of resources that this release consists of (need to enable --show-resources)
- details on last test suite run, if applicable
- additional notes provided by the chart
`
Expand Down Expand Up @@ -70,7 +73,7 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
// strip chart metadata from the output
rel.Chart = nil

return outfmt.Write(out, &statusPrinter{rel, false, client.ShowDescription})
return outfmt.Write(out, &statusPrinter{rel, false, client.ShowDescription, client.ShowResources})
},
}

Expand All @@ -92,13 +95,16 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
bindOutputFlag(cmd, &outfmt)
f.BoolVar(&client.ShowDescription, "show-desc", false, "if set, display the description message of the named release")

f.BoolVar(&client.ShowResources, "show-resources", false, "if set, display the resources of the named release")

return cmd
}

type statusPrinter struct {
release *release.Release
debug bool
showDescription bool
showResources bool
}

func (s statusPrinter) WriteJSON(out io.Writer) error {
Expand All @@ -124,6 +130,33 @@ func (s statusPrinter) WriteTable(out io.Writer) error {
fmt.Fprintf(out, "DESCRIPTION: %s\n", s.release.Info.Description)
}

if s.showResources && s.release.Info.Resources != nil && len(s.release.Info.Resources) > 0 {
buf := new(bytes.Buffer)
printFlags := get.NewHumanPrintFlags()
typePrinter, _ := printFlags.ToPrinter("")
printer := &get.TablePrinter{Delegate: typePrinter}

var keys []string
for key := range s.release.Info.Resources {
keys = append(keys, key)
}

for _, t := range keys {
fmt.Fprintf(buf, "==> %s\n", t)

vk := s.release.Info.Resources[t]
for _, resource := range vk {
if err := printer.PrintObj(resource, buf); err != nil {
fmt.Fprintf(buf, "failed to print object type %s: %v\n", t, err)
}
}

buf.WriteString("\n")
}

fmt.Fprintf(out, "RESOURCES:\n%s\n", buf.String())
}

executions := executionsByHookEvent(s.release)
if tests, ok := executions[release.HookTest]; !ok || len(tests) == 0 {
fmt.Fprintln(out, "TEST SUITE: None")
Expand Down
18 changes: 18 additions & 0 deletions cmd/helm/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ func TestStatusCmd(t *testing.T) {
Status: release.StatusDeployed,
Notes: "release notes",
}),
}, {
name: "get status of a deployed release with resources",
cmd: "status --show-resources flummoxed-chickadee",
golden: "output/status-with-resources.txt",
rels: releasesMockWithStatus(
&release.Info{
Status: release.StatusDeployed,
},
),
}, {
name: "get status of a deployed release with resources in json",
cmd: "status --show-resources flummoxed-chickadee -o json",
golden: "output/status-with-resources.json",
rels: releasesMockWithStatus(
&release.Info{
Status: release.StatusDeployed,
},
),
}, {
name: "get status of a deployed release with test suite",
cmd: "status flummoxed-chickadee",
Expand Down
1 change: 1 addition & 0 deletions cmd/helm/testdata/output/status-with-resources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"flummoxed-chickadee","info":{"first_deployed":"","last_deployed":"2016-01-16T00:00:00Z","deleted":"","status":"deployed"},"namespace":"default"}
6 changes: 6 additions & 0 deletions cmd/helm/testdata/output/status-with-resources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NAME: flummoxed-chickadee
LAST DEPLOYED: Sat Jan 16 00:00:00 2016
NAMESPACE: default
STATUS: deployed
REVISION: 0
TEST SUITE: None
4 changes: 2 additions & 2 deletions cmd/helm/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false})
return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false, false})
} else if err != nil {
return err
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
fmt.Fprintf(out, "Release %q has been upgraded. Happy Helming!\n", args[0])
}

return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false})
return outfmt.Write(out, &statusPrinter{rel, settings.Debug, false, false})
},
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module helm.sh/helm/v3

go 1.18
go 1.17

require (
github.com/BurntSushi/toml v1.2.0
Expand Down Expand Up @@ -29,7 +29,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.7.4
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.3.0
golang.org/x/term v0.2.0
Expand Down Expand Up @@ -82,6 +82,7 @@ require (
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
Loading

0 comments on commit eb7150f

Please sign in to comment.