From da950c5c57dfe93dc04bd94696c2e96f1f2c4b85 Mon Sep 17 00:00:00 2001 From: "Steven E. Harris" Date: Thu, 30 Mar 2017 15:12:57 -0400 Subject: [PATCH] Separate manifests with document boundary markers In order to allow the stream emitted by "helm init --debug" to be fed back into "kubectl create/apply -f", use YAML starting and ending document boundary markers instead of blank lines to separate the individual manifests. --- cmd/helm/init.go | 51 ++++++++++++++++++++++++++++++++++--------- cmd/helm/init_test.go | 16 +++++++++----- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/cmd/helm/init.go b/cmd/helm/init.go index bf7d38f16..bc73a6a6c 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -145,27 +145,58 @@ func (i *initCmd) run() error { i.opts.ImageSpec = i.image if flagDebug { - var mfs string + writeYAMLManifest := func(apiVersion, kind, body string, first, last bool) error { + w := i.out + if !first { + // YAML starting document boundary marker + if _, err := fmt.Fprintln(w, "---"); err != nil { + return err + } + } + if _, err := fmt.Fprintln(w, "apiVersion:", apiVersion); err != nil { + return err + } + if _, err := fmt.Fprintln(w, "kind:", kind); err != nil { + return err + } + if _, err := fmt.Fprint(w, body); err != nil { + return err + } + if !last { + return nil + } + // YAML ending document boundary marker + _, err := fmt.Fprintln(w, "...") + return err + } + + var body string var err error - // write deployment manifest - if mfs, err = installer.DeploymentManifest(&i.opts); err != nil { + // write Deployment manifest + if body, err = installer.DeploymentManifest(&i.opts); err != nil { + return err + } + if err := writeYAMLManifest("extensions/v1beta1", "Deployment", body, true, false); err != nil { return err } - fmt.Fprintln(i.out, fmt.Sprintf("apiVersion: extensions/v1beta1\nkind: Deployment\n%s", mfs)) - // write service manifest - if mfs, err = installer.ServiceManifest(i.namespace); err != nil { + // write Service manifest + if body, err = installer.ServiceManifest(i.namespace); err != nil { + return err + } + if err := writeYAMLManifest("v1", "Service", body, false, !i.opts.EnableTLS); err != nil { return err } - fmt.Fprintln(i.out, fmt.Sprintf("apiVersion: v1\nkind: Service\n%s", mfs)) - // write secret manifest + // write Secret manifest if i.opts.EnableTLS { - if mfs, err = installer.SecretManifest(&i.opts); err != nil { + if body, err = installer.SecretManifest(&i.opts); err != nil { + return err + } + if err := writeYAMLManifest("v1", "Secret", body, false, true); err != nil { return err } - fmt.Fprintln(i.out, fmt.Sprintf("apiVersion: v1\nkind: Secret\n%s", mfs)) } } diff --git a/cmd/helm/init_test.go b/cmd/helm/init_test.go index 00c754cfa..0ef111a72 100644 --- a/cmd/helm/init_test.go +++ b/cmd/helm/init_test.go @@ -156,13 +156,19 @@ func TestInitCmd_dryRun(t *testing.T) { if err := cmd.run(); err != nil { t.Fatal(err) } - if len(fc.Actions()) != 0 { - t.Error("expected no server calls") + if got := len(fc.Actions()); got != 0 { + t.Errorf("expected no server calls, got %d", got) } - var y map[string]interface{} - if err := yaml.Unmarshal(buf.Bytes(), &y); err != nil { - t.Errorf("Expected parseable YAML, got %q\n\t%s", buf.String(), err) + docs := bytes.Split(buf.Bytes(), []byte("\n---")) + if got, want := len(docs), 2; got != want { + t.Fatalf("Expected document count of %d, got %d", want, got) + } + for _, doc := range docs { + var y map[string]interface{} + if err := yaml.Unmarshal(doc, &y); err != nil { + t.Errorf("Expected parseable YAML, got %q\n\t%s", doc, err) + } } }