From 54e5dfae784e6de9fb3c3c8288a1fa707425a475 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Fri, 8 Jul 2016 16:13:29 -0700 Subject: [PATCH] Validate flags when generating charts, and prints message for file created --- cli/app/app.go | 24 +++++++++++++++--------- cli/app/k8sutils.go | 39 ++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/cli/app/app.go b/cli/app/app.go index 084be7a6..2a6d2999 100644 --- a/cli/app/app.go +++ b/cli/app/app.go @@ -562,12 +562,16 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) { createD := c.BoolT("deployment") createDS := c.BoolT("daemonset") createRS := c.BoolT("replicaset") + createChart := c.BoolT("chart") singleOutput := len(outFile) != 0 || toStdout // Validate the flags if len(outFile) != 0 && toStdout { logrus.Fatalf("Error: --out and --stdout can't be set at the same time") } + if createChart && toStdout { + logrus.Fatalf("Error: chart cannot be generated when --stdout is specified") + } if singleOutput { count := 0 if createD { @@ -593,8 +597,11 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) { logrus.Fatalf("Failed to parse the compose project from %s: %v", composeFile, err) } - f := createOutFile(outFile) - defer f.Close() + var f *os.File + if !createChart { + f = createOutFile(outFile) + defer f.Close() + } var mServices map[string][]byte = make(map[string][]byte) var mReplicationControllers map[string][]byte = make(map[string][]byte) @@ -602,8 +609,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) { var mDaemonSets map[string][]byte = make(map[string][]byte) var mReplicaSets map[string][]byte = make(map[string][]byte) var serviceLinks []string + var svcnames []string for name, service := range p.Configs { + svcnames = append(svcnames, name) checkUnsupportedKey(*service) @@ -813,13 +822,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) { fmt.Fprintf(os.Stdout, "file %q created\n", outFile) } - /* Need to iterate through one more time to ensure we capture all service/rc */ - for name := range p.Configs { - if c.BoolT("chart") { - err := generateHelm(composeFile, name, generateYaml) - if err != nil { - logrus.Fatalf("Failed to create Chart data: %s\n", err) - } + if createChart { + err := generateHelm(composeFile, svcnames, generateYaml) + if err != nil { + logrus.Fatalf("Failed to create Chart data: %s\n", err) } } } diff --git a/cli/app/k8sutils.go b/cli/app/k8sutils.go index 2a54867d..3d77bcc7 100644 --- a/cli/app/k8sutils.go +++ b/cli/app/k8sutils.go @@ -18,6 +18,7 @@ package app import ( "bytes" + "fmt" "io/ioutil" "os" "strings" @@ -31,7 +32,7 @@ import ( /** * Generate Helm Chart configuration */ -func generateHelm(filename string, svcname string, generateYaml bool) error { +func generateHelm(filename string, svcnames []string, generateYaml bool) error { type ChartDetails struct { Name string } @@ -90,29 +91,33 @@ home: /* Copy all related json/yaml files into the newly created manifests directory */ // TODO: support copying controller files other than rc? // TODO: support copying the file specified by --out? - extension := ".json" - if generateYaml { - extension = ".yaml" - } - infile, err := ioutil.ReadFile(svcname + "-rc" + extension) - if err != nil { - logrus.Infof("Error reading %s: %s\n", svcname+"-rc"+extension, err) - return err - } + for _, svcname := range svcnames { + extension := ".json" + if generateYaml { + extension = ".yaml" + } + infile, err := ioutil.ReadFile(svcname + "-rc" + extension) + if err != nil { + logrus.Infof("Error reading %s: %s\n", svcname+"-rc"+extension, err) + return err + } - err = ioutil.WriteFile(manifestDir+string(os.PathSeparator)+svcname+"-rc"+extension, infile, 0644) - if err != nil { - return err - } + err = ioutil.WriteFile(manifestDir+string(os.PathSeparator)+svcname+"-rc"+extension, infile, 0644) + if err != nil { + return err + } - /* The svc file is optional */ - infile, err = ioutil.ReadFile(svcname + "-svc" + extension) - if err == nil { + /* The svc file is optional */ + infile, err = ioutil.ReadFile(svcname + "-svc" + extension) + if err != nil { + continue + } err = ioutil.WriteFile(manifestDir+string(os.PathSeparator)+svcname+"-svc"+extension, infile, 0644) if err != nil { return err } } + fmt.Fprintf(os.Stdout, "chart created in %q\n", "."+string(os.PathSeparator)+dirName+string(os.PathSeparator)) return nil }