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
This commit is contained in:
Vincent Ambo 2017-02-08 16:19:10 +01:00
parent 250d01c044
commit 11a5cf9e19

89
main.go
View file

@ -25,6 +25,7 @@ func main() {
app.Commands = []cli.Command{ app.Commands = []cli.Command{
templateCommand(), templateCommand(),
applyCommand(), applyCommand(),
replaceCommand(),
} }
app.Run(os.Args) app.Run(os.Args)
@ -36,7 +37,9 @@ func templateCommand() cli.Command {
Usage: "Interpolate and print templates", Usage: "Interpolate and print templates",
Flags: commonFlags(), Flags: commonFlags(),
Action: func(c *cli.Context) error { 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 { if err != nil {
return err return err
@ -63,44 +66,73 @@ func applyCommand() cli.Command {
Destination: &dryRun, Destination: &dryRun,
}), }),
Action: func(c *cli.Context) error { 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 { if err != nil {
return err return err
} }
var kubectl *exec.Cmd var args []string
if dryRun { if dryRun {
kubectl = exec.Command("kubectl", "apply", "-f", "-", "--dry-run") args = []string{"apply", "-f", "-", "--dry-run"}
} else { } else {
kubectl = exec.Command("kubectl", "apply", "-f", "-") args = []string{"apply", "-f", "-"}
} }
stdin, err := kubectl.StdinPipe() return runKubectlWithResources(ctx, &args, &resources)
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 resources {
fmt.Fprintln(stdin, r)
}
stdin.Close()
kubectl.Wait()
return nil
}, },
} }
} }
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 err
}
args := []string{"replace", "--save-config=true", "-f", "-"}
return runKubectlWithResources(ctx, &args, &resources)
},
}
}
func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resources *[]string) error {
args := append(*kubectlArgs, fmt.Sprintf("--context=%s", c.Name))
kubectl := exec.Command("kubectl", args...)
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 *resources {
fmt.Fprintln(stdin, r)
}
stdin.Close()
kubectl.Wait()
return nil
}
func commonFlags() []cli.Flag { func commonFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
cli.StringFlag{ cli.StringFlag{
@ -114,8 +146,7 @@ func commonFlags() []cli.Flag {
} }
} }
func templateResources(c *cli.Context) ([]string, error) { func loadContext(c *cli.Context) (*context.Context, error) {
limit := c.StringSlice("limit")
f := c.String("file") f := c.String("file")
if f == "" { if f == "" {
@ -133,5 +164,5 @@ func templateResources(c *cli.Context) ([]string, error) {
return nil, err return nil, err
} }
return templater.LoadAndPrepareTemplates(&limit, ctx) return ctx, nil
} }