From c8c1f0a59a6a0e554ff3e6c4c5a0d63f45ead2e9 Mon Sep 17 00:00:00 2001 From: Lewis Marshall Date: Thu, 2 Aug 2018 19:11:18 +0100 Subject: [PATCH] when using replace use --force if not specified (#109) --- README.md | 12 +++++------- main.go | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 221f54d..7d97f47 100644 --- a/README.md +++ b/README.md @@ -89,25 +89,23 @@ The flag `--replace` can be used to override this behaviour can be useful in some very specific scenarios but the result is a [disruptive update](https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/#disruptive-updates) which should not be the default. -To have the desired affect when updating objects, you may need to use optional -kubectl arguments e.g.: `-- --force` to force deletion of some objects. **NOTE** -history of an object is lost with `--force`. - -**NOTE** `apply` is used internally to create the resource if it doesn't exist. +To have the desired affect when updating objects, `--force` is used to enable +creation of objects created with replace. **NOTE** history of an object is lost +with `--force`. #### Cronjobs When a cronjob object is created and only updated, any old jobs will continue and some fields are imutable so use of the force option may be required. -E.g. to update a large cron job use `kd --replace -f cronjob.yml -- --force`. +E.g. to update a large cron job use `kd --replace -f cronjob.yml`. #### Large Objects e.g. Configmaps As an apply uses 'patch' internally, there is a limit to the size of objects that can be updated this way. -E.g. to update a large config map use `kd --replace -f myconfigmap.yml -- --force`. +E.g. to update a large config map use `kd --replace -f myconfigmap.yml`. ### Run command diff --git a/main.go b/main.go index 9e95173..b88feea 100644 --- a/main.go +++ b/main.go @@ -404,15 +404,13 @@ func splitYamlDocs(data string) []string { func deploy(c *cli.Context, r *ObjectResource) error { exists := false - if r.CreateOnly || c.Bool(FlagReplace) { + if r.CreateOnly { var err error exists, err = checkResourceExist(c, r) if err != nil { return fmt.Errorf( "problem checking if resource %s/%s exists", r.Kind, r.Name) } - } - if r.CreateOnly { if exists { log.Printf( "skipping deploy for resource (%s/%s) marked as create only.", @@ -425,9 +423,7 @@ func deploy(c *cli.Context, r *ObjectResource) error { name := r.Name command := "apply" if c.Bool(FlagReplace) { - if exists { - command = "replace" - } + command = "replace" } if r.GenerateName != "" { @@ -692,6 +688,20 @@ func newKubeCmdSub(c *cli.Context, args []string, subCommand bool) (*exec.Cmd, e if err != nil { return nil, err } + // If we've been asked to replace and we haven't provided the '-- --force' + // extra args, add it here (a update will fail if the object doesn't exist) + if c.Bool(FlagReplace) { + forceSet := false + for _, flag := range flags { + if strings.Contains(flag, "--force") { + forceSet = true + break + } + } + if !forceSet { + flags = append(flags, "--force") + } + } args = append(args, flags...) return exec.Command(kube, args...), nil