Commit cc24e3f5 authored by Ville Aikas's avatar Ville Aikas Committed by GitHub

Merge pull request #1090 from vaikas-google/nodes.txt

First cut of handling NOTES.txt file
parents 27480470 7c4cad5c
...@@ -38,6 +38,10 @@ message Status { ...@@ -38,6 +38,10 @@ message Status {
Code code = 1; Code code = 1;
google.protobuf.Any details = 2; google.protobuf.Any details = 2;
// Cluster resources as kubectl would print them. // Cluster resources as kubectl would print them.
string resources = 3; string resources = 3;
// Contains the rendered templates/NOTES.txt if available
string notes = 4;
} }
...@@ -142,6 +142,9 @@ message GetReleaseStatusResponse { ...@@ -142,6 +142,9 @@ message GetReleaseStatusResponse {
// Info contains information about the release. // Info contains information about the release.
hapi.release.Info info = 2; hapi.release.Info info = 2;
// Namesapce the release was released into
string namespace = 3;
} }
// GetReleaseContentRequest is a request to get the contents of a release. // GetReleaseContentRequest is a request to get the contents of a release.
......
...@@ -67,10 +67,15 @@ func (s *statusCmd) run() error { ...@@ -67,10 +67,15 @@ func (s *statusCmd) run() error {
} }
fmt.Fprintf(s.out, "Last Deployed: %s\n", timeconv.String(res.Info.LastDeployed)) fmt.Fprintf(s.out, "Last Deployed: %s\n", timeconv.String(res.Info.LastDeployed))
fmt.Fprintf(s.out, "Namespace: %s\n", res.Namespace)
fmt.Fprintf(s.out, "Status: %s\n", res.Info.Status.Code) fmt.Fprintf(s.out, "Status: %s\n", res.Info.Status.Code)
fmt.Fprintf(s.out, "Resources:\n%s\n", res.Info.Status.Resources)
if res.Info.Status.Details != nil { if res.Info.Status.Details != nil {
fmt.Fprintf(s.out, "Details: %s\n", res.Info.Status.Details) fmt.Fprintf(s.out, "Details: %s\n", res.Info.Status.Details)
} }
fmt.Fprintf(s.out, "\n")
fmt.Fprintf(s.out, "Resources:\n%s\n", res.Info.Status.Resources)
if len(res.Info.Status.Notes) > 0 {
fmt.Fprintf(s.out, "Notes:\n%s\n", res.Info.Status.Notes)
}
return nil return nil
} }
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"log" "log"
"regexp" "regexp"
"sort" "sort"
"strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/technosophos/moniker" "github.com/technosophos/moniker"
...@@ -47,6 +48,12 @@ var srv *releaseServer ...@@ -47,6 +48,12 @@ var srv *releaseServer
// characters in length. See https://github.com/kubernetes/helm/issues/1071 // characters in length. See https://github.com/kubernetes/helm/issues/1071
const releaseNameMaxLen = 14 const releaseNameMaxLen = 14
// NOTESFILE_SUFFIX that we want to treat special. It goes through the templating engine
// but it's not a yaml file (resource) hence can't have hooks, etc. And the user actually
// wants to see this file after rendering in the status command. However, it must be a suffix
// since there can be filepath in front of it.
const notesFileSuffix = "NOTES.txt"
func init() { func init() {
srv = &releaseServer{ srv = &releaseServer{
env: env, env: env,
...@@ -179,6 +186,9 @@ func (s *releaseServer) GetReleaseStatus(c ctx.Context, req *services.GetRelease ...@@ -179,6 +186,9 @@ func (s *releaseServer) GetReleaseStatus(c ctx.Context, req *services.GetRelease
if rel.Info == nil { if rel.Info == nil {
return nil, errors.New("release info is missing") return nil, errors.New("release info is missing")
} }
if rel.Chart == nil {
return nil, errors.New("release chart is missing")
}
// Ok, we got the status of the release as we had jotted down, now we need to match the // Ok, we got the status of the release as we had jotted down, now we need to match the
// manifest we stashed away with reality from the cluster. // manifest we stashed away with reality from the cluster.
...@@ -190,7 +200,7 @@ func (s *releaseServer) GetReleaseStatus(c ctx.Context, req *services.GetRelease ...@@ -190,7 +200,7 @@ func (s *releaseServer) GetReleaseStatus(c ctx.Context, req *services.GetRelease
} }
rel.Info.Status.Resources = resp rel.Info.Status.Resources = resp
return &services.GetReleaseStatusResponse{Info: rel.Info}, nil return &services.GetReleaseStatusResponse{Info: rel.Info, Namespace: rel.Namespace}, nil
} }
func (s *releaseServer) GetReleaseContent(c ctx.Context, req *services.GetReleaseContentRequest) (*services.GetReleaseContentResponse, error) { func (s *releaseServer) GetReleaseContent(c ctx.Context, req *services.GetReleaseContentRequest) (*services.GetReleaseContentResponse, error) {
...@@ -281,7 +291,7 @@ func (s *releaseServer) prepareUpdate(req *services.UpdateReleaseRequest) (*rele ...@@ -281,7 +291,7 @@ func (s *releaseServer) prepareUpdate(req *services.UpdateReleaseRequest) (*rele
return nil, nil, err return nil, nil, err
} }
hooks, manifestDoc, err := s.renderResources(req.Chart, valuesToRender) hooks, manifestDoc, notesTxt, err := s.renderResources(req.Chart, valuesToRender)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -302,6 +312,9 @@ func (s *releaseServer) prepareUpdate(req *services.UpdateReleaseRequest) (*rele ...@@ -302,6 +312,9 @@ func (s *releaseServer) prepareUpdate(req *services.UpdateReleaseRequest) (*rele
Hooks: hooks, Hooks: hooks,
} }
if len(notesTxt) > 0 {
updatedRelease.Info.Status.Notes = notesTxt
}
return currentRelease, updatedRelease, nil return currentRelease, updatedRelease, nil
} }
...@@ -389,7 +402,7 @@ func (s *releaseServer) prepareRelease(req *services.InstallReleaseRequest) (*re ...@@ -389,7 +402,7 @@ func (s *releaseServer) prepareRelease(req *services.InstallReleaseRequest) (*re
return nil, err return nil, err
} }
hooks, manifestDoc, err := s.renderResources(req.Chart, valuesToRender) hooks, manifestDoc, notesTxt, err := s.renderResources(req.Chart, valuesToRender)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -409,6 +422,9 @@ func (s *releaseServer) prepareRelease(req *services.InstallReleaseRequest) (*re ...@@ -409,6 +422,9 @@ func (s *releaseServer) prepareRelease(req *services.InstallReleaseRequest) (*re
Hooks: hooks, Hooks: hooks,
Version: 1, Version: 1,
} }
if len(notesTxt) > 0 {
rel.Info.Status.Notes = notesTxt
}
return rel, nil return rel, nil
} }
...@@ -437,11 +453,24 @@ func (s *releaseServer) getVersionSet() (versionSet, error) { ...@@ -437,11 +453,24 @@ func (s *releaseServer) getVersionSet() (versionSet, error) {
return newVersionSet(versions...), nil return newVersionSet(versions...), nil
} }
func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values) ([]*release.Hook, *bytes.Buffer, error) { func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values) ([]*release.Hook, *bytes.Buffer, string, error) {
renderer := s.engine(ch) renderer := s.engine(ch)
files, err := renderer.Render(ch, values) files, err := renderer.Render(ch, values)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, "", err
}
// NOTES.txt gets rendered like all the other files, but because it's not a hook nor a resource,
// pull it out of here into a separate file so that we can actually use the output of the rendered
// text file. We have to spin through this map because the file contains path information, so we
// look for terminating NOTES.txt. We also remove it from the files so that we don't have to skip
// it in the sortHooks.
notes := ""
for k, v := range files {
if strings.HasSuffix(k, notesFileSuffix) {
notes = v
delete(files, k)
}
} }
// Sort hooks, manifests, and partials. Only hooks and manifests are returned, // Sort hooks, manifests, and partials. Only hooks and manifests are returned,
...@@ -449,13 +478,13 @@ func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values ...@@ -449,13 +478,13 @@ func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values
// removed here. // removed here.
vs, err := s.getVersionSet() vs, err := s.getVersionSet()
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Could not get apiVersions from Kubernetes: %s", err) return nil, nil, "", fmt.Errorf("Could not get apiVersions from Kubernetes: %s", err)
} }
hooks, manifests, err := sortManifests(files, vs) hooks, manifests, err := sortManifests(files, vs)
if err != nil { if err != nil {
// By catching parse errors here, we can prevent bogus releases from going // By catching parse errors here, we can prevent bogus releases from going
// to Kubernetes. // to Kubernetes.
return nil, nil, err return nil, nil, "", err
} }
// Aggregate all valid manifests into one big doc. // Aggregate all valid manifests into one big doc.
...@@ -465,7 +494,7 @@ func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values ...@@ -465,7 +494,7 @@ func (s *releaseServer) renderResources(ch *chart.Chart, values chartutil.Values
b.WriteString(file) b.WriteString(file)
} }
return hooks, b, nil return hooks, b, notes, nil
} }
// validateYAML checks to see if YAML is well-formed. // validateYAML checks to see if YAML is well-formed.
......
...@@ -35,6 +35,8 @@ import ( ...@@ -35,6 +35,8 @@ import (
"k8s.io/helm/pkg/storage/driver" "k8s.io/helm/pkg/storage/driver"
) )
const notesText = "my notes here"
var manifestWithHook = `apiVersion: v1 var manifestWithHook = `apiVersion: v1
kind: ConfigMap kind: ConfigMap
metadata: metadata:
...@@ -230,6 +232,137 @@ func TestInstallRelease(t *testing.T) { ...@@ -230,6 +232,137 @@ func TestInstallRelease(t *testing.T) {
} }
} }
func TestInstallReleaseWithNotes(t *testing.T) {
c := context.Background()
rs := rsFixture()
// TODO: Refactor this into a mock.
req := &services.InstallReleaseRequest{
Namespace: "spaced",
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "hello", Data: []byte("hello: world")},
{Name: "hooks", Data: []byte(manifestWithHook)},
{Name: "NOTES.txt", Data: []byte(notesText)},
},
},
}
res, err := rs.InstallRelease(c, req)
if err != nil {
t.Errorf("Failed install: %s", err)
}
if res.Release.Name == "" {
t.Errorf("Expected release name.")
}
if res.Release.Namespace != "spaced" {
t.Errorf("Expected release namespace 'spaced', got '%s'.", res.Release.Namespace)
}
rel, err := rs.env.Releases.Get(res.Release.Name)
if err != nil {
t.Errorf("Expected release for %s (%v).", res.Release.Name, rs.env.Releases)
}
t.Logf("rel: %v", rel)
if len(rel.Hooks) != 1 {
t.Fatalf("Expected 1 hook, got %d", len(rel.Hooks))
}
if rel.Hooks[0].Manifest != manifestWithHook {
t.Errorf("Unexpected manifest: %v", rel.Hooks[0].Manifest)
}
if rel.Info.Status.Notes != notesText {
t.Fatalf("Expected '%s', got '%s'", notesText, rel.Info.Status.Notes)
}
if rel.Hooks[0].Events[0] != release.Hook_POST_INSTALL {
t.Errorf("Expected event 0 is post install")
}
if rel.Hooks[0].Events[1] != release.Hook_PRE_DELETE {
t.Errorf("Expected event 0 is pre-delete")
}
if len(res.Release.Manifest) == 0 {
t.Errorf("No manifest returned: %v", res.Release)
}
if len(rel.Manifest) == 0 {
t.Errorf("Expected manifest in %v", res)
}
if !strings.Contains(rel.Manifest, "---\n# Source: hello/hello\nhello: world") {
t.Errorf("unexpected output: %s", rel.Manifest)
}
}
func TestInstallReleaseWithNotesRendered(t *testing.T) {
c := context.Background()
rs := rsFixture()
// TODO: Refactor this into a mock.
req := &services.InstallReleaseRequest{
Namespace: "spaced",
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "hello", Data: []byte("hello: world")},
{Name: "hooks", Data: []byte(manifestWithHook)},
{Name: "NOTES.txt", Data: []byte(notesText + " {{.Release.Name}}")},
},
},
}
res, err := rs.InstallRelease(c, req)
if err != nil {
t.Errorf("Failed install: %s", err)
}
if res.Release.Name == "" {
t.Errorf("Expected release name.")
}
if res.Release.Namespace != "spaced" {
t.Errorf("Expected release namespace 'spaced', got '%s'.", res.Release.Namespace)
}
rel, err := rs.env.Releases.Get(res.Release.Name)
if err != nil {
t.Errorf("Expected release for %s (%v).", res.Release.Name, rs.env.Releases)
}
t.Logf("rel: %v", rel)
if len(rel.Hooks) != 1 {
t.Fatalf("Expected 1 hook, got %d", len(rel.Hooks))
}
if rel.Hooks[0].Manifest != manifestWithHook {
t.Errorf("Unexpected manifest: %v", rel.Hooks[0].Manifest)
}
expectedNotes := fmt.Sprintf("%s %s", notesText, res.Release.Name)
if rel.Info.Status.Notes != expectedNotes {
t.Fatalf("Expected '%s', got '%s'", expectedNotes, rel.Info.Status.Notes)
}
if rel.Hooks[0].Events[0] != release.Hook_POST_INSTALL {
t.Errorf("Expected event 0 is post install")
}
if rel.Hooks[0].Events[1] != release.Hook_PRE_DELETE {
t.Errorf("Expected event 0 is pre-delete")
}
if len(res.Release.Manifest) == 0 {
t.Errorf("No manifest returned: %v", res.Release)
}
if len(rel.Manifest) == 0 {
t.Errorf("Expected manifest in %v", res)
}
if !strings.Contains(rel.Manifest, "---\n# Source: hello/hello\nhello: world") {
t.Errorf("unexpected output: %s", rel.Manifest)
}
}
func TestInstallReleaseDryRun(t *testing.T) { func TestInstallReleaseDryRun(t *testing.T) {
c := context.Background() c := context.Background()
rs := rsFixture() rs := rsFixture()
......
...@@ -54,7 +54,7 @@ func New() *Engine { ...@@ -54,7 +54,7 @@ func New() *Engine {
} }
} }
// Render takes a chart, optional values, and value overrids, and attempts to render the Go templates. // Render takes a chart, optional values, and value overrides, and attempts to render the Go templates.
// //
// Render can be called repeatedly on the same engine. // Render can be called repeatedly on the same engine.
// //
......
...@@ -55,6 +55,8 @@ type Status struct { ...@@ -55,6 +55,8 @@ type Status struct {
Details *google_protobuf1.Any `protobuf:"bytes,2,opt,name=details" json:"details,omitempty"` Details *google_protobuf1.Any `protobuf:"bytes,2,opt,name=details" json:"details,omitempty"`
// Cluster resources as kubectl would print them. // Cluster resources as kubectl would print them.
Resources string `protobuf:"bytes,3,opt,name=resources" json:"resources,omitempty"` Resources string `protobuf:"bytes,3,opt,name=resources" json:"resources,omitempty"`
// Contains the rendered templates/NOTES.txt if available
Notes string `protobuf:"bytes,4,opt,name=notes" json:"notes,omitempty"`
} }
func (m *Status) Reset() { *m = Status{} } func (m *Status) Reset() { *m = Status{} }
...@@ -75,21 +77,22 @@ func init() { ...@@ -75,21 +77,22 @@ func init() {
} }
var fileDescriptor3 = []byte{ var fileDescriptor3 = []byte{
// 247 bytes of a gzipped FileDescriptorProto // 259 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0x48, 0x2c, 0xc8, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x4c, 0x8f, 0xc1, 0x4e, 0xf2, 0x40,
0xd4, 0x2f, 0x4a, 0xcd, 0x49, 0x4d, 0x2c, 0x4e, 0xd5, 0x2f, 0x2e, 0x49, 0x2c, 0x29, 0x2d, 0xd6, 0x14, 0x85, 0xff, 0x42, 0xff, 0xd6, 0x5e, 0x08, 0x21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x40, 0x36, 0x8b, 0x13, 0x67, 0x3b, 0x2d, 0x4a, 0x58, 0xca, 0x5e, 0x4d, 0xe8, 0xe1, 0x08, 0xa0, 0x58, 0x2d, 0x59, 0x5e, 0xb0,
0x14, 0x3b, 0xd4, 0xd9, 0x49, 0x6c, 0x60, 0xb7, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xd1, 0xc4, 0xe4, 0x3e, 0x02, 0x78, 0x8f, 0xf3, 0xa7, 0xd4, 0x7c, 0xbb, 0xf7, 0xc1, 0x87, 0x7f, 0x7a,
0xc3, 0xbf, 0x50, 0x2b, 0x01, 0x00, 0x00, 0xcc, 0xda, 0xb3, 0x37, 0xbc, 0xfb, 0x09, 0x00, 0x00, 0xff, 0xff, 0xae, 0x07, 0x47, 0x1f, 0x41,
0x01, 0x00, 0x00,
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment