From acb759c07911491f6860619ce3037cec03d275a7 Mon Sep 17 00:00:00 2001 From: Jeff Knurek Date: Thu, 21 Mar 2019 16:19:02 +0100 Subject: [PATCH 1/4] feat(helm) add 'get version' to get the chart version Signed-off-by: Jeff Knurek --- cmd/helm/get.go | 1 + cmd/helm/get_version.go | 75 ++++++++++++++++++++++++++++++++++++ cmd/helm/get_version_test.go | 47 ++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 cmd/helm/get_version.go create mode 100644 cmd/helm/get_version_test.go diff --git a/cmd/helm/get.go b/cmd/helm/get.go index 20a4c042f..f9ed6fb91 100644 --- a/cmd/helm/get.go +++ b/cmd/helm/get.go @@ -78,6 +78,7 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command { cmd.AddCommand(newGetManifestCmd(nil, out)) cmd.AddCommand(newGetHooksCmd(nil, out)) cmd.AddCommand(newGetNotesCmd(nil, out)) + cmd.AddCommand(newGetVersionCmd(nil, out)) // set defaults from environment settings.InitTLS(f) diff --git a/cmd/helm/get_version.go b/cmd/helm/get_version.go new file mode 100644 index 000000000..c76c6245d --- /dev/null +++ b/cmd/helm/get_version.go @@ -0,0 +1,75 @@ +/* +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 ( + "fmt" + "io" + + "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" +) + +var getVersionHelp = "This command fetches the chart version for a given release." + +type getVersionCmd struct { + release string + out io.Writer + client helm.Interface + revision int32 +} + +func newGetVersionCmd(client helm.Interface, out io.Writer) *cobra.Command { + get := &getVersionCmd{ + out: out, + client: client, + } + cmd := &cobra.Command{ + Use: "version [flags] RELEASE_NAME", + Short: "download the chart version for a named release", + Long: getVersionHelp, + PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() }, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errReleaseRequired + } + get.release = args[0] + get.client = ensureHelmClient(get.client) + return get.run() + }, + } + + f := cmd.Flags() + settings.AddFlagsTLS(f) + f.Int32Var(&get.revision, "revision", 0, "get the named release with revision") + + // set defaults from environment + settings.InitTLS(f) + + return cmd +} + +// getVersion implements 'helm get version' +func (g *getVersionCmd) run() error { + res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.revision)) + if err != nil { + return prettyError(err) + } + fmt.Fprintln(g.out, res.Release.Chart.Metadata.Version) + return nil +} diff --git a/cmd/helm/get_version_test.go b/cmd/helm/get_version_test.go new file mode 100644 index 000000000..81c9cdfd9 --- /dev/null +++ b/cmd/helm/get_version_test.go @@ -0,0 +1,47 @@ +/* +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" + "testing" + + "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" + "k8s.io/helm/pkg/proto/hapi/release" +) + +func TestGetVersion(t *testing.T) { + tests := []releaseCase{ + { + name: "get chart version of release", + args: []string{"kodiak"}, + expected: "0.1.0-beta.1", + resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"}), + rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"})}, + }, + { + name: "get version without args", + args: []string{}, + err: true, + }, + } + runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command { + return newGetVersionCmd(c, out) + }) +} From 6c760297c04aa7eac6cb47829ac6fc7b01c4b3f9 Mon Sep 17 00:00:00 2001 From: Jeff Knurek Date: Thu, 21 Mar 2019 16:40:38 +0100 Subject: [PATCH 2/4] DOCS: ci failed and taught me how the docs get autogenerated Signed-off-by: Jeff Knurek --- docs/helm/helm_get.md | 3 ++- docs/helm/helm_get_version.md | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 docs/helm/helm_get_version.md diff --git a/docs/helm/helm_get.md b/docs/helm/helm_get.md index 3b99c93d5..e113db53f 100644 --- a/docs/helm/helm_get.md +++ b/docs/helm/helm_get.md @@ -53,5 +53,6 @@ helm get [flags] RELEASE_NAME * [helm get manifest](helm_get_manifest.md) - download the manifest for a named release * [helm get notes](helm_get_notes.md) - displays the notes of the named release * [helm get values](helm_get_values.md) - download the values file for a named release +* [helm get version](helm_get_version.md) - download the chart version for a named release -###### Auto generated by spf13/cobra on 1-Sep-2018 +###### Auto generated by spf13/cobra on 21-Mar-2019 diff --git a/docs/helm/helm_get_version.md b/docs/helm/helm_get_version.md new file mode 100644 index 000000000..36695e464 --- /dev/null +++ b/docs/helm/helm_get_version.md @@ -0,0 +1,42 @@ +## helm get version + +download the chart version for a named release + +### Synopsis + +This command fetches the chart version for a given release. + +``` +helm get version [flags] RELEASE_NAME +``` + +### Options + +``` + -h, --help help for version + --revision int32 get the named release with revision + --tls enable TLS for request + --tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem") + --tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem") + --tls-hostname string the server name used to verify the hostname on the returned certificates from the server + --tls-key string path to TLS key file (default "$HELM_HOME/key.pem") + --tls-verify enable TLS for request and verify remote +``` + +### Options inherited from parent commands + +``` + --debug enable verbose output + --home string location of your Helm config. Overrides $HELM_HOME (default "~/.helm") + --host string address of Tiller. Overrides $HELM_HOST + --kube-context string name of the kubeconfig context to use + --kubeconfig string absolute path to the kubeconfig file to use + --tiller-connection-timeout int the duration (in seconds) Helm will wait to establish a connection to tiller (default 300) + --tiller-namespace string namespace of Tiller (default "kube-system") +``` + +### SEE ALSO + +* [helm get](helm_get.md) - download a named release + +###### Auto generated by spf13/cobra on 21-Mar-2019 From 0a3ebb816b76ae2425ec2a8ceb242b0660db9c53 Mon Sep 17 00:00:00 2001 From: Jeff Knurek Date: Mon, 25 Mar 2019 14:21:55 +0100 Subject: [PATCH 3/4] feat(helm) add '--template' option to 'helm get' for custom formatting Signed-off-by: Jeff Knurek --- cmd/helm/get.go | 15 +++++--- cmd/helm/get_test.go | 7 ++++ cmd/helm/get_version.go | 75 ------------------------------------ cmd/helm/get_version_test.go | 47 ---------------------- cmd/helm/printer.go | 2 +- docs/helm/helm_get.md | 4 +- 6 files changed, 20 insertions(+), 130 deletions(-) delete mode 100644 cmd/helm/get_version.go delete mode 100644 cmd/helm/get_version_test.go diff --git a/cmd/helm/get.go b/cmd/helm/get.go index f9ed6fb91..6829122b7 100644 --- a/cmd/helm/get.go +++ b/cmd/helm/get.go @@ -41,10 +41,11 @@ chart, the supplied values, and the generated manifest file. var errReleaseRequired = errors.New("release name is required") type getCmd struct { - release string - out io.Writer - client helm.Interface - version int32 + release string + out io.Writer + client helm.Interface + version int32 + template string } func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command { @@ -73,12 +74,12 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command { f := cmd.Flags() settings.AddFlagsTLS(f) f.Int32Var(&get.version, "revision", 0, "get the named release with revision") + f.StringVar(&get.template, "template", "", "go template for formatting the output, eg: {{.Release.Name}}") cmd.AddCommand(newGetValuesCmd(nil, out)) cmd.AddCommand(newGetManifestCmd(nil, out)) cmd.AddCommand(newGetHooksCmd(nil, out)) cmd.AddCommand(newGetNotesCmd(nil, out)) - cmd.AddCommand(newGetVersionCmd(nil, out)) // set defaults from environment settings.InitTLS(f) @@ -92,5 +93,9 @@ func (g *getCmd) run() error { if err != nil { return prettyError(err) } + + if g.template != "" { + return tpl(g.template, res, g.out) + } return printRelease(g.out, res.Release) } diff --git a/cmd/helm/get_test.go b/cmd/helm/get_test.go index cb230a8a5..d83c85e1e 100644 --- a/cmd/helm/get_test.go +++ b/cmd/helm/get_test.go @@ -35,6 +35,13 @@ func TestGetCmd(t *testing.T) { expected: "REVISION: 1\nRELEASED: (.*)\nCHART: foo-0.1.0-beta.1\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nHOOKS:\n---\n# pre-install-hook\n" + helm.MockHookTemplate + "\nMANIFEST:", rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"})}, }, + { + name: "get with a formatted release", + resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "elevated-turkey"}), + args: []string{"elevated-turkey", "--template", "{{.Release.Chart.Metadata.Version}}"}, + expected: "0.1.0-beta.1", + rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "elevated-turkey"})}, + }, { name: "get requires release name arg", err: true, diff --git a/cmd/helm/get_version.go b/cmd/helm/get_version.go deleted file mode 100644 index c76c6245d..000000000 --- a/cmd/helm/get_version.go +++ /dev/null @@ -1,75 +0,0 @@ -/* -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 ( - "fmt" - "io" - - "github.com/spf13/cobra" - - "k8s.io/helm/pkg/helm" -) - -var getVersionHelp = "This command fetches the chart version for a given release." - -type getVersionCmd struct { - release string - out io.Writer - client helm.Interface - revision int32 -} - -func newGetVersionCmd(client helm.Interface, out io.Writer) *cobra.Command { - get := &getVersionCmd{ - out: out, - client: client, - } - cmd := &cobra.Command{ - Use: "version [flags] RELEASE_NAME", - Short: "download the chart version for a named release", - Long: getVersionHelp, - PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() }, - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return errReleaseRequired - } - get.release = args[0] - get.client = ensureHelmClient(get.client) - return get.run() - }, - } - - f := cmd.Flags() - settings.AddFlagsTLS(f) - f.Int32Var(&get.revision, "revision", 0, "get the named release with revision") - - // set defaults from environment - settings.InitTLS(f) - - return cmd -} - -// getVersion implements 'helm get version' -func (g *getVersionCmd) run() error { - res, err := g.client.ReleaseContent(g.release, helm.ContentReleaseVersion(g.revision)) - if err != nil { - return prettyError(err) - } - fmt.Fprintln(g.out, res.Release.Chart.Metadata.Version) - return nil -} diff --git a/cmd/helm/get_version_test.go b/cmd/helm/get_version_test.go deleted file mode 100644 index 81c9cdfd9..000000000 --- a/cmd/helm/get_version_test.go +++ /dev/null @@ -1,47 +0,0 @@ -/* -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" - "testing" - - "github.com/spf13/cobra" - - "k8s.io/helm/pkg/helm" - "k8s.io/helm/pkg/proto/hapi/release" -) - -func TestGetVersion(t *testing.T) { - tests := []releaseCase{ - { - name: "get chart version of release", - args: []string{"kodiak"}, - expected: "0.1.0-beta.1", - resp: helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"}), - rels: []*release.Release{helm.ReleaseMock(&helm.MockReleaseOptions{Name: "kodiak"})}, - }, - { - name: "get version without args", - args: []string{}, - err: true, - }, - } - runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command { - return newGetVersionCmd(c, out) - }) -} diff --git a/cmd/helm/printer.go b/cmd/helm/printer.go index e98b71c64..2f42bdab0 100644 --- a/cmd/helm/printer.go +++ b/cmd/helm/printer.go @@ -66,7 +66,7 @@ func printRelease(out io.Writer, rel *release.Release) error { return tpl(printReleaseTemplate, data, out) } -func tpl(t string, vals map[string]interface{}, out io.Writer) error { +func tpl(t string, vals interface{}, out io.Writer) error { tt, err := template.New("_").Parse(t) if err != nil { return err diff --git a/docs/helm/helm_get.md b/docs/helm/helm_get.md index e113db53f..3ce0ff191 100644 --- a/docs/helm/helm_get.md +++ b/docs/helm/helm_get.md @@ -26,6 +26,7 @@ helm get [flags] RELEASE_NAME ``` -h, --help help for get --revision int32 get the named release with revision + --template string go template for formatting the output, eg: {{.Release.Name}} --tls enable TLS for request --tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem") --tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem") @@ -53,6 +54,5 @@ helm get [flags] RELEASE_NAME * [helm get manifest](helm_get_manifest.md) - download the manifest for a named release * [helm get notes](helm_get_notes.md) - displays the notes of the named release * [helm get values](helm_get_values.md) - download the values file for a named release -* [helm get version](helm_get_version.md) - download the chart version for a named release -###### Auto generated by spf13/cobra on 21-Mar-2019 +###### Auto generated by spf13/cobra on 25-Mar-2019 From d45095263e47221d0f28c9dc49da18073fcc5b5f Mon Sep 17 00:00:00 2001 From: Jeff Knurek Date: Fri, 3 May 2019 09:31:53 +0200 Subject: [PATCH 4/4] remove obsolete doc file Signed-off-by: Jeff Knurek --- docs/helm/helm_get_version.md | 42 ----------------------------------- 1 file changed, 42 deletions(-) delete mode 100644 docs/helm/helm_get_version.md diff --git a/docs/helm/helm_get_version.md b/docs/helm/helm_get_version.md deleted file mode 100644 index 36695e464..000000000 --- a/docs/helm/helm_get_version.md +++ /dev/null @@ -1,42 +0,0 @@ -## helm get version - -download the chart version for a named release - -### Synopsis - -This command fetches the chart version for a given release. - -``` -helm get version [flags] RELEASE_NAME -``` - -### Options - -``` - -h, --help help for version - --revision int32 get the named release with revision - --tls enable TLS for request - --tls-ca-cert string path to TLS CA certificate file (default "$HELM_HOME/ca.pem") - --tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem") - --tls-hostname string the server name used to verify the hostname on the returned certificates from the server - --tls-key string path to TLS key file (default "$HELM_HOME/key.pem") - --tls-verify enable TLS for request and verify remote -``` - -### Options inherited from parent commands - -``` - --debug enable verbose output - --home string location of your Helm config. Overrides $HELM_HOME (default "~/.helm") - --host string address of Tiller. Overrides $HELM_HOST - --kube-context string name of the kubeconfig context to use - --kubeconfig string absolute path to the kubeconfig file to use - --tiller-connection-timeout int the duration (in seconds) Helm will wait to establish a connection to tiller (default 300) - --tiller-namespace string namespace of Tiller (default "kube-system") -``` - -### SEE ALSO - -* [helm get](helm_get.md) - download a named release - -###### Auto generated by spf13/cobra on 21-Mar-2019