about summary refs log tree commit diff
path: root/main.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-02-08T15·19+0100
committerVincent Ambo <tazjin@gmail.com>2017-02-08T15·19+0100
commit11a5cf9e192b57eb594ce276ef7723693ff28014 (patch)
treef71415e86f1fe45d45064c3d1698b4ec8e02dc98 /main.go
parent250d01c0446a7bc69b466c576731177cf97331d9 (diff)
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
Diffstat (limited to 'main.go')
-rw-r--r--main.go75
1 files changed, 53 insertions, 22 deletions
diff --git a/main.go b/main.go
index c39972ff6010..1a775bf7c5fc 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
 }