-
Notifications
You must be signed in to change notification settings - Fork 38.8k
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
support editing before creating resource #33250
support editing before creating resource #33250
Conversation
Improve edit experience a bit according #26050(comment)
|
I tried the example command,
|
We should refactor the code in edit and try to reuse them, instead of duplicating them |
I create another PR #33686 to format the JSONPrintor. That PR could fix the issue you mentioned. |
cc @kubernetes/kubectl |
👍 for that idea, to make it more clear. |
5a76c2d
to
b763b71
Compare
Jenkins Kubemark GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history. The magic incantation to run this job again is |
Jenkins GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history. The magic incantation to run this job again is |
Jenkins GCI GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history. The magic incantation to run this job again is |
Jenkins unit/integration failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history. The magic incantation to run this job again is |
b763b71
to
28f44cd
Compare
Refactored and rebased. PTAL |
return EditHelper(f, out, errOut, cmd, args, options, true) | ||
} | ||
|
||
func EditHelper(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions, isUsedByEditCmd bool) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer runEdit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
ResourceTypeOrNameArgs(true, args...). | ||
Latest() | ||
} else { | ||
b = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.UnstructuredClientForMapping), runtime.UnstructuredJSONScheme) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both create and edit are meant to work with k8s resources, not sure why using UnstructuredClientForMapping
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because I want to support the use case:
kubectl create -f invalid-k8s-resource.yaml --edit
where 'invalid-k8s-resource.yaml' contains some invalid configuration. User can edit these invalid fields before creating them. I think it will make --edit
more useful.
Otherwise, it will simply return an error, the editor won't even pop out to let user edit it.
os.Remove(file) | ||
fmt.Fprintln(errOut, "Edit cancelled, no changes made.") | ||
return nil | ||
if isUsedByEditCmd { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder all of those if isUsedByEditCmd
, haven't tried but I'd like to have only the necessary required ones. I'll try to check it out on Mon.
@@ -430,9 +468,9 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args | |||
} | |||
return nil | |||
} | |||
|
|||
// loop again and edit the remaining items | |||
infos = results.edit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
infos
will be overwritten immediately at the start of next iteration in the for loop. See: this line
So this line is actually useless.
if err != nil { | ||
return preservedFile(err, results.file, errOut) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change this?
There are a number of changes in this PR that I'm not sure if it'll change Also, the edit function is getting very long (300+ lines) and becomes harder to read / maintain. Is it possible to separate it into multiple smaller functions? (Let me know if it takes too much time.) Something like:
Also keep in mind that this edit function may be extended to other commands (such as |
I think so, maybe we can open a new PR to do this. |
I will create another PR for refactoring, and it will base on #33973 |
Would you add some tests in test-cmd.sh too? |
Automatic merge from submit-queue fix error handling Add missing error handling mentioned in [#33250 Comment](#33250 (comment)) @janetkuo
56006dc
to
36c77a6
Compare
Jenkins GKE smoke e2e failed for commit 36c77a6e144267fda530f8502238555070e2e505. Full PR test history. The magic incantation to run this job again is |
Jenkins verification failed for commit 36c77a6e144267fda530f8502238555070e2e505. Full PR test history. The magic incantation to run this job again is |
36c77a6
to
32d21b7
Compare
Updated. PTAL |
32d21b7
to
d7edbb3
Compare
@janetkuo ping |
const ( | ||
NormalEditMode = "normal_mode" | ||
EditBeforeCreateMode = "edit_before_create_mode" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest something like
type EditMode string
const (
NormalEditMode EditMode = "normal_mode"
EditBeforeCreateMode EditMode = "edit_before_create_mode"
)
@@ -720,6 +720,24 @@ runTests() { | |||
# Pre-condition: no POD exists | |||
create_and_use_new_namespace | |||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' | |||
## kubectl edit can update the image field of a POD. tmp-editor.sh is a fake editor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: comment needs to be changed to kubectl create --edit
func RunEditOnCreate(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, options *resource.FilenameOptions) error { | ||
outputFlag := cmdutil.GetFlagString(cmd, "output") | ||
if outputFlag != "json" && outputFlag != "yaml" { | ||
return cmdutil.UsageError(cmd, "--output must be one of json|yaml, when --edit is true") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If --edit
is specified, we should set --output
to either json
or yaml
, so that one can do kubectl create --edit
without specifying -o
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we change this, need a test for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe choose yaml
, the same as kubectl edit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, if --edit
is specified, we can use yaml
as default when -o
is not specified.
Overall looks good. Had a few comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One final comment and please squash your changes at this point already.
@@ -720,6 +720,24 @@ runTests() { | |||
# Pre-condition: no POD exists | |||
create_and_use_new_namespace | |||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' | |||
## kubectl create --edit can update the image field of a POD. tmp-editor.sh is a fake editor | |||
echo -e "#!/bin/bash\n$SED -i \"s/gcr.io\/google_containers\/serve_hostname/nginx/g\" \$1" > /tmp/tmp-editor.sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use mktemp
instead of creating a file that might conflict with something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
51397fb
to
c641834
Compare
Squashed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Jenkins GCI GKE smoke e2e failed for commit c641834. Full PR test history. The magic incantation to run this job again is |
@k8s-bot gci gke e2e test this |
@k8s-bot test this [submit-queue is verifying that this PR is safe to merge] |
Automatic merge from submit-queue |
Support
kubectl create -f config.yaml --edit
Support editing before creating resource from files, urls and stdin.
The behavior is similar to
kubectl edit
It won't create anything when edit make no change.
partial: #18064
Based on: #33686 and #33973
This change is![Reviewable](https://camo.githubusercontent.com/2d899f4291d07d3cd2fa4aaae1e3b243f164c23fce87d30a589ace0d496a444c/68747470733a2f2f72657669657761626c652e6b756265726e657465732e696f2f7265766965775f627574746f6e2e737667)