feat(helm): add 'helm get hooks'.

This adds 'helm get hooks' and updates 'helm get' to return
hook information.
pull/939/head
Matt Butcher 8 years ago
parent cd230ab10e
commit 1cd9f5d541

@ -18,8 +18,8 @@ package main
import (
"errors"
"fmt"
"io"
"text/template"
"time"
"github.com/spf13/cobra"
@ -73,9 +73,27 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
}
cmd.AddCommand(newGetValuesCmd(nil, out))
cmd.AddCommand(newGetManifestCmd(nil, out))
cmd.AddCommand(newGetHooksCmd(nil, out))
return cmd
}
var getTemplate = `VERSION: {{.Release.Version}}
RELEASED: {{.ReleaseDate}}
CHART: {{.Release.Chart.Metadata.Name}}-{{.Release.Chart.Metadata.Version}}
USER-SUPPLIED VALUES:
{{.Release.Config.Raw}}
COMPUTED VALUES:
{{.ComputedValues}}
HOOKS:
{{- range .Release.Hooks }}
---
# {{.Name}}
{{.Manifest}}
{{- end }}
MANIFEST:
{{.Release.Manifest}}
`
// getCmd is the command that implements 'helm get'
func (g *getCmd) run() error {
res, err := g.client.ReleaseContent(g.release)
@ -92,14 +110,25 @@ func (g *getCmd) run() error {
return err
}
fmt.Fprintf(g.out, "VERSION: %v\n", res.Release.Version)
fmt.Fprintf(g.out, "RELEASED: %s\n", timeconv.Format(res.Release.Info.LastDeployed, time.ANSIC))
fmt.Fprintf(g.out, "CHART: %s-%s\n", res.Release.Chart.Metadata.Name, res.Release.Chart.Metadata.Version)
fmt.Fprintln(g.out, "USER-SUPPLIED VALUES:")
fmt.Fprintln(g.out, res.Release.Config.Raw)
fmt.Fprintln(g.out, "COMPUTED VALUES:")
fmt.Fprintln(g.out, cfgStr)
fmt.Fprintln(g.out, "MANIFEST:")
fmt.Fprintln(g.out, res.Release.Manifest)
return nil
data := map[string]interface{}{
"Release": res.Release,
"ComputedValues": cfgStr,
"ReleaseDate": timeconv.Format(res.Release.Info.LastDeployed, time.ANSIC),
}
return tpl(getTemplate, data, g.out)
}
func tpl(t string, vals map[string]interface{}, out io.Writer) error {
tt, err := template.New("_").Parse(t)
if err != nil {
return err
}
return tt.Execute(out, vals)
}
func ensureHelmClient(h helm.Interface) helm.Interface {
if h != nil {
return h
}
return helm.NewClient(helm.HelmHost(helm.Config.ServAddr))
}

@ -0,0 +1,72 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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"
)
const getHooksHelp = `
This command downloads hooks for a given release.
Hooks are formatted in YAML and separated by the YAML '---\n' separator.
`
type getHooksCmd struct {
release string
out io.Writer
client helm.Interface
}
func newGetHooksCmd(client helm.Interface, out io.Writer) *cobra.Command {
ghc := &getHooksCmd{
out: out,
client: client,
}
cmd := &cobra.Command{
Use: "hooks [flags] RELEASE_NAME",
Short: "download all hooks for a named release",
Long: getHooksHelp,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errReleaseRequired
}
ghc.release = args[0]
ghc.client = ensureHelmClient(ghc.client)
return ghc.run()
},
}
return cmd
}
func (g *getHooksCmd) run() error {
res, err := g.client.ReleaseContent(g.release)
if err != nil {
fmt.Fprintln(g.out, g.release)
return prettyError(err)
}
for _, hook := range res.Release.Hooks {
fmt.Fprintf(g.out, "---\n# %s\n%s", hook.Name, hook.Manifest)
}
return nil
}

@ -0,0 +1,43 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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"
)
func TestGetHooks(t *testing.T) {
tests := []releaseCase{
{
name: "get hooks with release",
args: []string{"aeneas"},
expected: mockHookTemplate,
resp: releaseMock("aeneas"),
},
{
name: "get hooks without args",
args: []string{},
err: true,
},
}
runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
return newGetHooksCmd(c, out)
})
}

@ -0,0 +1,43 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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"
)
func TestGetManifest(t *testing.T) {
tests := []releaseCase{
{
name: "get manifest with release",
args: []string{"juno"},
expected: mockManifest,
resp: releaseMock("juno"),
},
{
name: "get manifest without args",
args: []string{},
err: true,
},
}
runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
return newGetManifestCmd(c, out)
})
}

@ -17,47 +17,28 @@ limitations under the License.
package main
import (
"bytes"
"regexp"
"io"
"testing"
"k8s.io/helm/pkg/proto/hapi/release"
"github.com/spf13/cobra"
)
func TestGetCmd(t *testing.T) {
tests := []struct {
name string
args []string
resp *release.Release
expected string
err bool
}{
tests := []releaseCase{
{
name: "with a release",
name: "get with a release",
resp: releaseMock("thomas-guide"),
args: []string{"thomas-guide"},
expected: "VERSION: 1\nRELEASED: (.*)\nCHART: foo-0.1.0-beta.1\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nMANIFEST:",
expected: "VERSION: 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" + mockHookTemplate + "\nMANIFEST:",
},
{
name: "requires release name arg",
name: "get requires release name arg",
err: true,
},
}
var buf bytes.Buffer
for _, tt := range tests {
c := &fakeReleaseClient{
rels: []*release.Release{tt.resp},
}
cmd := newGetCmd(c, &buf)
err := cmd.RunE(cmd, tt.args)
if (err != nil) != tt.err {
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
}
re := regexp.MustCompile(tt.expected)
if !re.Match(buf.Bytes()) {
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, buf.String())
}
buf.Reset()
cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
return newGetCmd(c, out)
}
runReleaseCases(t, tests, cmd)
}

@ -51,9 +51,7 @@ func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
return errReleaseRequired
}
get.release = args[0]
if get.client == nil {
get.client = helm.NewClient(helm.HelmHost(helm.Config.ServAddr))
}
get.client = ensureHelmClient(get.client)
return get.run()
},
}

@ -17,46 +17,27 @@ limitations under the License.
package main
import (
"bytes"
"io"
"testing"
"k8s.io/helm/pkg/proto/hapi/release"
"github.com/spf13/cobra"
)
func TestGetValuesCmd(t *testing.T) {
tests := []struct {
name string
args []string
resp *release.Release
expected string
err bool
}{
tests := []releaseCase{
{
name: "with a release",
name: "get values with a release",
resp: releaseMock("thomas-guide"),
args: []string{"thomas-guide"},
expected: "name: \"value\"",
},
{
name: "requires release name arg",
name: "get values requires release name arg",
err: true,
},
}
var buf bytes.Buffer
for _, tt := range tests {
c := &fakeReleaseClient{
rels: []*release.Release{tt.resp},
}
cmd := newGetValuesCmd(c, &buf)
err := cmd.RunE(cmd, tt.args)
if (err != nil) != tt.err {
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
}
actual := string(bytes.TrimSpace(buf.Bytes()))
if actual != tt.expected {
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, actual)
}
buf.Reset()
cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
return newGetValuesCmd(c, out)
}
runReleaseCases(t, tests, cmd)
}

@ -17,7 +17,13 @@ limitations under the License.
package main
import (
"bytes"
"io"
"regexp"
"testing"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/proto/hapi/chart"
@ -25,6 +31,19 @@ import (
rls "k8s.io/helm/pkg/proto/hapi/services"
)
var mockHookTemplate = `apiVersion: v1
kind: Job
metadata:
annotations:
"helm.sh/hooks": pre-install
`
var mockManifest = `apiVersion: v1
kind: Secret
metadata:
name: fixture
`
func releaseMock(name string) *release.Release {
date := timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
return &release.Release{
@ -40,11 +59,22 @@ func releaseMock(name string) *release.Release {
Version: "0.1.0-beta.1",
},
Templates: []*chart.Template{
{Name: "foo.tpl", Data: []byte("Hello")},
{Name: "foo.tpl", Data: []byte(mockManifest)},
},
},
Config: &chart.Config{Raw: `name: "value"`},
Version: 1,
Hooks: []*release.Hook{
{
Name: "pre-install-hook",
Kind: "Job",
Path: "pre-install-hook.yaml",
Manifest: mockHookTemplate,
LastRun: &date,
Events: []release.Hook_Event{release.Hook_PRE_INSTALL},
},
},
Manifest: mockManifest,
}
}
@ -85,3 +115,36 @@ func (c *fakeReleaseClient) ReleaseContent(rlsName string, opts ...helm.ContentO
}
return resp, c.err
}
// releaseCmd is a command that works with a fakeReleaseClient
type releaseCmd func(c *fakeReleaseClient, out io.Writer) *cobra.Command
// runReleaseCases runs a set of release cases through the given releaseCmd.
func runReleaseCases(t *testing.T, tests []releaseCase, rcmd releaseCmd) {
var buf bytes.Buffer
for _, tt := range tests {
c := &fakeReleaseClient{
rels: []*release.Release{tt.resp},
}
cmd := rcmd(c, &buf)
err := cmd.RunE(cmd, tt.args)
if (err != nil) != tt.err {
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
}
re := regexp.MustCompile(tt.expected)
if !re.Match(buf.Bytes()) {
t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, tt.expected, buf.String())
}
buf.Reset()
}
}
// releaseCase describes a test case that works with releases.
type releaseCase struct {
name string
args []string
// expected is the string to be matched. This supports regular expressions.
expected string
err bool
resp *release.Release
}

Loading…
Cancel
Save