From 11a5cf9e192b57eb594ce276ef7723693ff28014 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 8 Feb 2017 16:19:10 +0100 Subject: feat main: Add replace support & respect context setting * Adds support for calling `kubectl replace` (necessary for resource types that do not support `apply`). * Sets `kubectl` context to whatever is defined in the cluster configuration file --- main.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 22 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index c39972ff60..1a775bf7c5 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ func main() { app.Commands = []cli.Command{ templateCommand(), applyCommand(), + replaceCommand(), } app.Run(os.Args) @@ -36,7 +37,9 @@ func templateCommand() cli.Command { Usage: "Interpolate and print templates", Flags: commonFlags(), Action: func(c *cli.Context) error { - resources, err := templateResources(c) + limit := c.StringSlice("limit") + ctx, err := loadContext(c) + resources, err := templater.LoadAndPrepareTemplates(&limit, ctx) if err != nil { return err @@ -63,42 +66,71 @@ func applyCommand() cli.Command { Destination: &dryRun, }), Action: func(c *cli.Context) error { - resources, err := templateResources(c) + limit := c.StringSlice("limit") + ctx, err := loadContext(c) + resources, err := templater.LoadAndPrepareTemplates(&limit, ctx) if err != nil { return err } - var kubectl *exec.Cmd + var args []string if dryRun { - kubectl = exec.Command("kubectl", "apply", "-f", "-", "--dry-run") + args = []string{"apply", "-f", "-", "--dry-run"} } else { - kubectl = exec.Command("kubectl", "apply", "-f", "-") + args = []string{"apply", "-f", "-"} } - stdin, err := kubectl.StdinPipe() + return runKubectlWithResources(ctx, &args, &resources) + }, + } +} + +func replaceCommand() cli.Command { + return cli.Command{ + Name: "replace", + Usage: "Interpolate templates and run 'kubectl replace'", + Flags: commonFlags(), + Action: func(c *cli.Context) error { + limit := c.StringSlice("limit") + ctx, err := loadContext(c) + resources, err := templater.LoadAndPrepareTemplates(&limit, ctx) + if err != nil { - return meep.New(&KubeCtlError{}, meep.Cause(err)) + return err } - kubectl.Stdout = os.Stdout - kubectl.Stderr = os.Stderr + args := []string{"replace", "--save-config=true", "-f", "-"} + return runKubectlWithResources(ctx, &args, &resources) + }, + } +} - if err = kubectl.Start(); err != nil { - return meep.New(&KubeCtlError{}, meep.Cause(err)) - } +func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resources *[]string) error { + args := append(*kubectlArgs, fmt.Sprintf("--context=%s", c.Name)) - for _, r := range resources { - fmt.Fprintln(stdin, r) - } + kubectl := exec.Command("kubectl", args...) - stdin.Close() + stdin, err := kubectl.StdinPipe() + if err != nil { + return meep.New(&KubeCtlError{}, meep.Cause(err)) + } - kubectl.Wait() + kubectl.Stdout = os.Stdout + kubectl.Stderr = os.Stderr - return nil - }, + if err = kubectl.Start(); err != nil { + return meep.New(&KubeCtlError{}, meep.Cause(err)) + } + + for _, r := range *resources { + fmt.Fprintln(stdin, r) } + stdin.Close() + + kubectl.Wait() + + return nil } func commonFlags() []cli.Flag { @@ -114,8 +146,7 @@ func commonFlags() []cli.Flag { } } -func templateResources(c *cli.Context) ([]string, error) { - limit := c.StringSlice("limit") +func loadContext(c *cli.Context) (*context.Context, error) { f := c.String("file") if f == "" { @@ -133,5 +164,5 @@ func templateResources(c *cli.Context) ([]string, error) { return nil, err } - return templater.LoadAndPrepareTemplates(&limit, ctx) + return ctx, nil } -- cgit 1.4.1