Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubectl replace-or-create would be helpful #14040

Closed
justinsb opened this issue Sep 16, 2015 · 30 comments
Closed

kubectl replace-or-create would be helpful #14040

justinsb opened this issue Sep 16, 2015 · 30 comments
Labels
area/kubectl kind/feature Categorizes issue or PR as related to a new feature. priority/backlog Higher priority than priority/awaiting-more-evidence. sig/cli Categorizes an issue or PR as relevant to SIG CLI.

Comments

@justinsb
Copy link
Member

I often find myself doing this: kubectl replace -f template || kubectl create -f template, because I want to replace the existing RC, but I also want to create it if it was deleted. This works, but it is messy (the error handling isn't quite right; error messages when kubectl replace fails etc).

It would be helpful to have kubectl replace -or-create -f template do the right thing: check if the named object exists, then do a replace if so and a create if not.

(Deliberately bad straw-man argument naming :-) )

@bgrant0607 bgrant0607 added the priority/backlog Higher priority than priority/awaiting-more-evidence. label Sep 17, 2015
@bgrant0607
Copy link
Member

Seems reasonable. In the api, PUT should have create-or-replace semantics #2114.

But kubectl apply #1702 is really a better solution and hopefully we'll have that soon.

@bgrant0607
Copy link
Member

apply is the recommended way to do this.

@discordianfish
Copy link
Contributor

I use replace instead of apply when updating large configmaps to get around the annotation size limitation, so in this case a replace-or-create would be still useful..

@bgrant0607 bgrant0607 reopened this Jan 16, 2019
@k8s-ci-robot k8s-ci-robot added the needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Jan 16, 2019
@bgrant0607 bgrant0607 added the sig/cli Categorizes an issue or PR as relevant to SIG CLI. label Jan 16, 2019
@k8s-ci-robot k8s-ci-robot removed the needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Jan 16, 2019
@bgrant0607
Copy link
Member

A flag on create would be the simplest approach to this.

#30558 (comment)

@bgrant0607
Copy link
Member

cc @seans3 @monopole

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 16, 2019
@discordianfish
Copy link
Contributor

/remove-lifecycle stale
/remove-lifecycle rotten
/lifecycle freeze
/cc @fejta Feedback: I don't like it.

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 16, 2019
@montanaflynn
Copy link

montanaflynn commented Apr 18, 2019

I'm creating or replacing a configmap from a file and use this pattern:

kubectl create configmap db-schema --from-file=schema.sql -o yaml --dry-run | kubectl apply -f -`
configmap/db-schema configured

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 17, 2019
@discordianfish
Copy link
Contributor

/reopen
/remove-lifecycle stale
/remove-lifecycle rotten
/lifecycle freeze
/cc @fejta Feedback: I don't like it.

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 23, 2019
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 21, 2019
@fentas
Copy link

fentas commented Nov 18, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 18, 2019
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 16, 2020
@AndrewSav
Copy link

/remove-lifecycle stale

@brianpursley
Copy link
Member

/kind feature

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Apr 29, 2020
@Asday
Copy link

Asday commented May 10, 2020

This seems useful to me as someone completely new to k8s. I'm reading the docs and trying to futz my way through how I'd use it in a sensible manner, and it looks like I don't want to use apply at all, because that could mean that the cluster state drifts from my version-controlled configuration. This means I'd always do git pull && kubectl replace -R -f . on push, except when something's new, in which case I'd have to add some intermediate step that somehow checks for new stuff and creates it instead.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 8, 2020
@AndrewSav
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 8, 2020
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 6, 2020
@AndrewSav
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 6, 2020
@prein
Copy link

prein commented Nov 23, 2020

@montanaflynn this doesn't help for the issue described by @discordianfish in #14040 (comment)

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 21, 2021
@AndrewSav
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 21, 2021
@alculquicondor
Copy link
Member

@soltysh has this been considered?

@soltysh
Copy link
Contributor

soltysh commented Mar 24, 2021

@alculquicondor with kubectl apply that'd be my current recommendation rather than implementing replace or create.

@seans3
Copy link
Contributor

seans3 commented Mar 31, 2021

The canonical way to create or update is kubectl apply. If the reservation with using kubectl apply is the size of the resource (because it stores the entire applied config in the last-applied-annotation), then the user should use server-side apply: kubectl apply --server-side .... Server-side apply went beta in version 1.16. Closing.

/close

@k8s-ci-robot
Copy link
Contributor

@seans3: Closing this issue.

In response to this:

The canonical way to create or update is kubectl apply. If the reservation with using kubectl apply is the size of the resource (because it stores the entire applied config in the last-applied-annotation), then the user should use server-side apply: kubectl apply --server-side .... Server-side apply went beta in version 1.16. Closing.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@llamasoft
Copy link

Just a heads up that kubectl apply isn't a perfect replacement for kubectl create || kubectl replace, specifically when it comes to certain list types. kubectl apply will attempt to merge some types of lists whereas kubectl replace will always replace them.

This normally isn't a big deal but this subtle difference can cause production issues. For example, if you rename a container or volumeMount between deployments, the merge result may end up with more containers or volumes than you wanted.

@mjamaah
Copy link

mjamaah commented Feb 20, 2024

you can do this: kubectl create -f template 2>/dev/null || kubectl replace -f template, you will create the resource if it does not exist and suppress the error if it is, then replace it if exist.

@bhperry
Copy link

bhperry commented Jun 3, 2024

create || replace is pretty good, although it's worth noting that for yamls with multiple objects in them it would be really nice to have a kubectl command that handles the create/replace on a case-by-case basis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl kind/feature Categorizes issue or PR as related to a new feature. priority/backlog Higher priority than priority/awaiting-more-evidence. sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
Development

No branches or pull requests