Skip to content

Commit

Permalink
Merge pull request #108 from UKHomeOffice/add_replace
Browse files Browse the repository at this point in the history
Add optional replace flag
  • Loading branch information
lewismarshall authored and KashifSaadat committed Aug 1, 2018
1 parent 06fb2c9 commit 4be920a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ $ kd --context=mykube --namespace=testing --file nginx-deployment.yaml
You can fail an ongoing deployment if there's been a new deployment by adding `--fail-superseded` flag.

### Replace

kd will use the `apply` verb to create / update resources which is [appropriate
in most cases](https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/#in-place-updates-of-resources).

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.

#### 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`.

#### 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`.

### Run command

You can run kubectl with the support of the same flags and environment variables
Expand Down Expand Up @@ -261,6 +290,7 @@ GLOBAL OPTIONS:
--config value Env file location [$CONFIG_FILE, $PLUGIN_CONFIG_FILE]
--create-only only create resources (do not update, skip if exists). [$CREATE_ONLY, $PLUGIN_CREATE_ONLY]
--create-only-resource value only create specified resources e.g. 'kind/name' (do not update, skip if exists). [$CREATE_ONLY_RESOURCES, $PLUGIN_CREATE_ONLY_RESOURCES]
--replace use replace instead of apply for updating objects [$KUBE_REPLACE, $PLUGIN_KUBE_REPLACE]
--context CONTEXT, -c CONTEXT kube config CONTEXT [$KUBE_CONTEXT, $PLUGIN_CONTEXT]
--namespace NAMESPACE, -n NAMESPACE kubernetes NAMESPACE [$KUBE_NAMESPACE, $PLUGIN_KUBE_NAMESPACE]
--fail-superseded fail deployment if it has been superseded by another deployment. WARNING: there are some bugs in kubernetes. [$FAIL_SUPERSEDED, $PLUGIN_FAIL_SUPERSEDED]
Expand Down
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
// FlagCaFile is the sytax to specify a CA file when FlagCa specifies a URL or
// when FlagCaData is set
FlagCaFile = "certificate-authority-file"
// FlagReplace allows the resources to be re-created rather than patched
FlagReplace = "replace"
)

var (
Expand Down Expand Up @@ -132,6 +134,11 @@ func main() {
EnvVar: "CREATE_ONLY_RESOURCES,PLUGIN_CREATE_ONLY_RESOURCES",
Value: nil,
},
cli.BoolFlag{
Name: FlagReplace,
Usage: "use replace instead of apply for updating objects",
EnvVar: "KUBE_REPLACE,PLUGIN_KUBE_REPLACE",
},
cli.StringFlag{
Name: "context, c",
Usage: "kube config `CONTEXT`",
Expand Down Expand Up @@ -396,12 +403,16 @@ func splitYamlDocs(data string) []string {

func deploy(c *cli.Context, r *ObjectResource) error {

if r.CreateOnly {
exists, err := checkResourceExist(c, r)
exists := false
if r.CreateOnly || c.Bool(FlagReplace) {
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 @@ -413,6 +424,11 @@ func deploy(c *cli.Context, r *ObjectResource) error {

name := r.Name
command := "apply"
if c.Bool(FlagReplace) {
if exists {
command = "replace"
}
}

if r.GenerateName != "" {
name = r.GenerateName
Expand Down

0 comments on commit 4be920a

Please sign in to comment.