Skip to content

Commit

Permalink
feat(get deployed): Add subcommand "get deployed"
Browse files Browse the repository at this point in the history
The command to print the list of resources under a named release:
    helm get deployed <RELEASE_NAME>

Optional flags:
-o / --output <format> - Prints the output in the specified format.
                         Allowed values: table, json, yaml (default table)

Signed-off-by: Bhargav Ravuri <[email protected]>
  • Loading branch information
Bhargav-InfraCloud committed Jun 5, 2024
1 parent c5698e5 commit 14edcaa
Show file tree
Hide file tree
Showing 24 changed files with 495 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cmd/helm/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) {
})

testcmd := fmt.Sprintf("__complete %s ''", cmdName)
_, out, err := executeActionCommandC(storage, testcmd)
_, out, err := executeActionCommandC(storage, testcmd, "")
if err != nil {
t.Errorf("unexpected error, %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/helm/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCreateCmd(t *testing.T) {
defer testChdir(t, dir)()

// Run a create
if _, _, err := executeActionCommand("create " + cname); err != nil {
if _, _, err := executeActionCommand("create "+cname, ""); err != nil {
t.Fatalf("Failed to run create: %s", err)
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func TestCreateStarterCmd(t *testing.T) {
}

// Run a create
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname)); err != nil {
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname), ""); err != nil {
t.Errorf("Failed to run create: %s", err)
return
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestCreateStarterAbsoluteCmd(t *testing.T) {
starterChartPath := filepath.Join(starterchart, "starterchart")

// Run a create
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil {
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname), ""); err != nil {
t.Errorf("Failed to run create: %s", err)
return
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/helm/dependency_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestDependencyBuildCmd(t *testing.T) {
repoFile := filepath.Join(rootDir, "repositories.yaml")

cmd := fmt.Sprintf("dependency build '%s' --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, "")

// In the first pass, we basically want the same results as an update.
if err != nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestDependencyBuildCmd(t *testing.T) {
t.Fatal(err)
}

_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, "")
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestDependencyBuildCmd(t *testing.T) {
}

skipRefreshCmd := fmt.Sprintf("dependency build '%s' --skip-refresh --repository-config %s --repository-cache %s", filepath.Join(rootDir, chartname), repoFile, rootDir)
_, out, err = executeActionCommand(skipRefreshCmd)
_, out, err = executeActionCommand(skipRefreshCmd, "")

// In this pass, we check --skip-refresh option becomes effective.
if err != nil {
Expand All @@ -139,7 +139,7 @@ func TestDependencyBuildCmd(t *testing.T) {
dir("repositories.yaml"),
dir(),
dir())
_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, "")
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand All @@ -154,7 +154,7 @@ func TestDependencyBuildCmdWithHelmV2Hash(t *testing.T) {
chartName := "testdata/testcharts/issue-7233"

cmd := fmt.Sprintf("dependency build '%s'", chartName)
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, "")

// Want to make sure the build can verify Helm v2 hash
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions cmd/helm/dependency_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestDependencyUpdateCmd(t *testing.T) {
}

_, out, err := executeActionCommand(
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()),
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()), "",
)
if err != nil {
t.Logf("Output: %s", out)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestDependencyUpdateCmd(t *testing.T) {
t.Fatal(err)
}

_, out, err = executeActionCommand(fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, out, err = executeActionCommand(fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()), "")
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand All @@ -136,7 +136,7 @@ func TestDependencyUpdateCmd(t *testing.T) {
dir("repositories.yaml"),
dir(),
dir())
_, out, err = executeActionCommand(cmd)
_, out, err = executeActionCommand(cmd, "")
if err != nil {
t.Logf("Output: %s", out)
t.Fatal(err)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
}
createTestingChart(t, dir(), chartname, srv.URL())

_, output, err := executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, output, err := executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()), "")
if err != nil {
t.Logf("Output: %s", output)
t.Fatal(err)
Expand All @@ -178,7 +178,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
// Chart repo is down
srv.Stop()

_, output, err = executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()))
_, output, err = executeActionCommand(fmt.Sprintf("dependency update %s --repository-config %s --repository-cache %s", dir(chartname), dir("repositories.yaml"), dir()), "")
if err == nil {
t.Logf("Output: %s", output)
t.Fatal("Expected error, got nil")
Expand Down Expand Up @@ -232,6 +232,7 @@ func TestDependencyUpdateCmd_WithRepoThatWasNotAdded(t *testing.T) {
_, out, err := executeActionCommand(
fmt.Sprintf("dependency update '%s' --repository-config %s --repository-cache %s", dir(chartname),
dir("repositories.yaml"), dir()),
"",
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/helm/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newGetCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}

cmd.AddCommand(newGetAllCmd(cfg, out))
cmd.AddCommand(newGetDeployedCmd(cfg, out))
cmd.AddCommand(newGetValuesCmd(cfg, out))
cmd.AddCommand(newGetManifestCmd(cfg, out))
cmd.AddCommand(newGetHooksCmd(cfg, out))
Expand Down
72 changes: 72 additions & 0 deletions cmd/helm/get_deployed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"io"

"github.com/spf13/cobra"

"helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli/output"
)

var getDeployedHelp = `
This command prints list of resources deployed under a release.
`

// newGetDeployedCmd creates a command for listing the resources deployed under a named release
func newGetDeployedCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
// Output format for the command output. This will be set by input flag -o (or --output).
var outfmt output.Format

// Create get-deployed action's client
client := action.NewGetDeployed(cfg)

cmd := &cobra.Command{
Use: "deployed RELEASE_NAME",
Short: "list resources deployed under a named release",
Long: getDeployedHelp,
Args: require.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return compListReleases(toComplete, args, cfg)
},
RunE: func(cmd *cobra.Command, args []string) error {
// Run the client to list resources under the release
resourceList, err := client.Run(args[0])
if err != nil {
return err
}

// Create an output writer with resources listed
writer := action.NewResourceListWriter(resourceList, false)

// Write the resources list with output format provided with input flag
return outfmt.Write(out, writer)
},
}

// Add flag for specifying the output format
bindOutputFlag(cmd, &outfmt)

return cmd
}
43 changes: 43 additions & 0 deletions cmd/helm/get_deployed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"testing"

"helm.sh/helm/v3/pkg/release"
)

func TestGetDeployed(t *testing.T) {
tests := []cmdTestCase{
{
name: "get deployed with a release",
cmd: "get deployed thomas-guide",
golden: "output/get-deployed.txt",
rels: []*release.Release{
release.Mock(
&release.MockReleaseOptions{
Name: "thomas-guide",
},
),
},
namespace: "default",
},
}

runTestCmd(t, tests)
}
26 changes: 16 additions & 10 deletions cmd/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runTestCmd(t *testing.T, tests []cmdTestCase) {
}
}
t.Logf("running cmd (attempt %d): %s", i+1, tt.cmd)
_, out, err := executeActionCommandC(storage, tt.cmd)
_, out, err := executeActionCommandC(storage, tt.cmd, tt.namespace)
if tt.wantError && err == nil {
t.Errorf("expected error, got success with the following output:\n%s", out)
}
Expand All @@ -78,11 +78,11 @@ func storageFixture() *storage.Storage {
return storage.Init(driver.NewMemory())
}

func executeActionCommandC(store *storage.Storage, cmd string) (*cobra.Command, string, error) {
return executeActionCommandStdinC(store, nil, cmd)
func executeActionCommandC(store *storage.Storage, cmd, ns string) (*cobra.Command, string, error) {
return executeActionCommandStdinC(store, nil, cmd, ns)
}

func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string) (*cobra.Command, string, error) {
func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd, ns string) (*cobra.Command, string, error) {
args, err := shellwords.Parse(cmd)
if err != nil {
return nil, "", err
Expand All @@ -91,10 +91,14 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string)
buf := new(bytes.Buffer)

actionConfig := &action.Configuration{
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard},
Capabilities: chartutil.DefaultCapabilities,
Log: func(format string, v ...interface{}) {},
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{
Out: io.Discard,
Namespace: ns,
},
Capabilities: chartutil.DefaultCapabilities,
Log: func(format string, v ...interface{}) {},
RESTClientGetter: settings.RESTClientGetter(),
}

root, err := newRootCmd(actionConfig, buf, args)
Expand Down Expand Up @@ -135,10 +139,12 @@ type cmdTestCase struct {
// Number of repeats (in case a feature was previously flaky and the test checks
// it's now stably producing identical results). 0 means test is run exactly once.
repeat int
// namespace is the default namespace set in kube client
namespace string
}

func executeActionCommand(cmd string) (*cobra.Command, string, error) {
return executeActionCommandC(storageFixture(), cmd)
func executeActionCommand(cmd, ns string) (*cobra.Command, string, error) {
return executeActionCommandC(storageFixture(), cmd, ns)
}

func resetEnv() func() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestPackage(t *testing.T) {
cmd = append(cmd, fmt.Sprintf("--%s=%s", k, v))
}
}
_, _, err = executeActionCommand(strings.Join(cmd, " "))
_, _, err = executeActionCommand(strings.Join(cmd, " "), "")
if err != nil {
if tt.err && re.MatchString(err.Error()) {
return
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestSetAppVersion(t *testing.T) {
chartToPackage := "testdata/testcharts/alpine"
dir := t.TempDir()
cmd := fmt.Sprintf("package %s --destination=%s --app-version=%s", chartToPackage, dir, expectedAppVersion)
_, output, err := executeActionCommand(cmd)
_, output, err := executeActionCommand(cmd, "")
if err != nil {
t.Logf("Output: %s", output)
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestPullCmd(t *testing.T) {
t.Fatal(err)
}
}
_, out, err := executeActionCommand(cmd)
_, out, err := executeActionCommand(cmd, "")
if err != nil {
if tt.wantError {
if tt.wantErrorMsg != "" && tt.wantErrorMsg == err.Error() {
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestPullWithCredentialsCmd(t *testing.T) {
t.Fatal(err)
}
}
_, _, err := executeActionCommand(cmd)
_, _, err := executeActionCommand(cmd, "")
if err != nil {
if tt.wantError {
if tt.wantErrorMsg != "" && tt.wantErrorMsg == err.Error() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/repo_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestRepoAddWithPasswordFromStdin(t *testing.T) {
const username = "username"
cmd := fmt.Sprintf("repo add %s %s --repository-config %s --repository-cache %s --username %s --password-stdin", testName, srv.URL(), repoFile, tmpdir, username)
var result string
_, result, err = executeActionCommandStdinC(store, in, cmd)
_, result, err = executeActionCommandStdinC(store, in, cmd, "")
if err != nil {
t.Errorf("unexpected error, got '%v'", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestRollbackWithLabels(t *testing.T) {
t.Fatal(err)
}
}
_, _, err := executeActionCommandC(storage, fmt.Sprintf("rollback %s 1", releaseName))
_, _, err := executeActionCommandC(storage, fmt.Sprintf("rollback %s 1", releaseName), "")
if err != nil {
t.Errorf("unexpected error, got '%v'", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestRootCmd(t *testing.T) {
os.Setenv(k, v)
}

if _, _, err := executeActionCommand(tt.args); err != nil {
if _, _, err := executeActionCommand(tt.args, ""); err != nil {
t.Fatalf("unexpected error: %s", err)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestRootCmd(t *testing.T) {
}

func TestUnknownSubCmd(t *testing.T) {
_, _, err := executeActionCommand("foobar")
_, _, err := executeActionCommand("foobar", "")

if err == nil || err.Error() != `unknown command "foobar" for "helm"` {
t.Errorf("Expect unknown command error, got %q", err)
Expand Down
Loading

0 comments on commit 14edcaa

Please sign in to comment.