Skip to content

Commit

Permalink
when using replace use --force if not specified (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewismarshall authored and KashifSaadat committed Aug 3, 2018
1 parent 4be920a commit afe813b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit afe813b

Please sign in to comment.