From 162b962fad4e9d4f99b865aa4d22e7c3581756e8 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 11 Jun 2017 21:34:22 +0200 Subject: [PATCH] refactor main: Call kubectl individually per resource set Instead of passing the rendered output of all resource sets to kubectl simultaneously, build upon the previous commit and pass resource sets individually to new instances of kubectl. This resolves #51 --- main.go | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 033f2b5ca..57dd62068 100644 --- a/main.go +++ b/main.go @@ -130,13 +130,13 @@ func createCommand() { } } -func loadContextAndResources(file *string) (*context.Context, *[]string) { +func loadContextAndResources(file *string) (*context.Context, *[]templater.RenderedResourceSet) { ctx, err := context.LoadContextFromFile(*file) if err != nil { app.Fatalf("Error loading context: %v\n", err) } - resources, err := templater.LoadAndPrepareTemplates(includes, excludes, ctx) + resources, err := templater.LoadAndApplyTemplates(includes, excludes, ctx) if err != nil { app.Fatalf("Error templating resource sets: %v\n", err) } @@ -144,29 +144,36 @@ func loadContextAndResources(file *string) (*context.Context, *[]string) { return ctx, &resources } -func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resources *[]string) error { +func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resourceSets *[]templater.RenderedResourceSet) error { args := append(*kubectlArgs, fmt.Sprintf("--context=%s", c.Name)) - kubectl := exec.Command("kubectl", args...) + for _, resourceSet := range *resourceSets { + kubectl := exec.Command("kubectl", args...) - stdin, err := kubectl.StdinPipe() - if err != nil { - return meep.New(&KubeCtlError{}, meep.Cause(err)) + stdin, err := kubectl.StdinPipe() + if err != nil { + return meep.New(&KubeCtlError{}, meep.Cause(err)) + } + + kubectl.Stdout = os.Stdout + kubectl.Stderr = os.Stderr + + if err = kubectl.Start(); err != nil { + return meep.New(&KubeCtlError{}, meep.Cause(err)) + } + + for _, r := range resourceSet.Resources { + fmt.Printf("Passing file %s/%s to kubectl", resourceSet.Name, r.Filename) + fmt.Fprintln(stdin, r.Rendered) + } + stdin.Close() + + if err = kubectl.Wait(); err != nil { + return err + } } - kubectl.Stdout = os.Stdout - kubectl.Stderr = os.Stderr - - if err = kubectl.Start(); err != nil { - return meep.New(&KubeCtlError{}, meep.Cause(err)) - } - - for _, r := range *resources { - fmt.Fprintln(stdin, r) - } - stdin.Close() - - return kubectl.Wait() + return nil } func failWithKubectlError(err error) {