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

support editing before creating resource #33250

Merged
merged 1 commit into from
Oct 29, 2016

Conversation

mengqiy
Copy link
Member

@mengqiy mengqiy commented Sep 22, 2016

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

Support editing before creating resource from files, urls and stdin, e.g. `kubectl create -f config.yaml --edit`
It won't create anything when edit make no change.

This change is Reviewable

@k8s-github-robot k8s-github-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note-label-needed labels Sep 22, 2016
@mengqiy
Copy link
Member Author

mengqiy commented Sep 23, 2016

Improve edit experience a bit according #26050(comment)

a) always go back to the editor
b) always retain what I hand-edited, even if that has to be in comments

@pwittrock pwittrock assigned janetkuo and unassigned pwittrock Sep 23, 2016
@janetkuo janetkuo added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-label-needed labels Sep 23, 2016
@janetkuo
Copy link
Member

I tried the example command, kubectl create -f docker-registry.yaml --edit --output-version=v1 -o json. While yaml editor formatted perfectly, with -o yaml I got this one-line, unformatted output:

{"apiVersion":"v1","kind":"Pod","metadata":{"name":"test-kubectl","namespace":"default"},"spec":{"containers":[{"command":["sh","-c","sleep 5; wget -O - ${KUBERNETES_RO_SERVICE_HOST}:       ${KUBERNETES_RO_SERVICE_PORT}/api/v1/pods/; sleep 10000"],"env":[{"name":"KUBERNETES_RO_SERVICE_HOST","value":"127.0.0.1"},{"name":"KUBERNETES_RO_SERVICE_PORT","value":"8001"}],"image":     "gcr.io/google_containers/busybox","name":"busybox","ports":[{"containerPort":8080}],"volumeMounts":[{"mountPath":"/usr/local","name":"test-kubectl"}]}],"volumes":[{"hostPath":{"path":      "/usr/local/google/home/chiachenk/go/src/k8s.io/kubernetes/_output/local/bin/linux/amd64"},"name":"test-kubectl"}]}}

@janetkuo
Copy link
Member

We should refactor the code in edit and try to reuse them, instead of duplicating them

@mengqiy
Copy link
Member Author

mengqiy commented Sep 28, 2016

While yaml editor formatted perfectly, with -o yaml I got this one-line, unformatted output

I create another PR #33686 to format the JSONPrintor. That PR could fix the issue you mentioned.

@janetkuo
Copy link
Member

cc @kubernetes/kubectl

@soltysh
Copy link
Contributor

soltysh commented Sep 30, 2016

We should refactor the code in edit and try to reuse them, instead of duplicating them

👍 for that idea, to make it more clear.

@k8s-ci-robot
Copy link
Contributor

Jenkins Kubemark GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history.

The magic incantation to run this job again is @k8s-bot kubemark e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@k8s-ci-robot
Copy link
Contributor

Jenkins GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history.

The magic incantation to run this job again is @k8s-bot gce e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@k8s-ci-robot
Copy link
Contributor

Jenkins GCI GCE e2e failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history.

The magic incantation to run this job again is @k8s-bot gci gce e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@k8s-ci-robot
Copy link
Contributor

Jenkins unit/integration failed for commit b763b716895f7fe1a0d3644ee732de937c66281f. Full PR test history.

The magic incantation to run this job again is @k8s-bot unit test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 30, 2016
@mengqiy
Copy link
Member Author

mengqiy commented Sep 30, 2016

Refactored and rebased. PTAL
If you want to try it on your local machine, make sure you has rebased, since this PR is based on #33686 that has been merged recently.

@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 30, 2016
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer runEdit

Copy link
Member Author

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)
Copy link
Contributor

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?

Copy link
Member Author

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 {
Copy link
Contributor

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this?

@janetkuo
Copy link
Member

janetkuo commented Oct 3, 2016

There are a number of changes in this PR that I'm not sure if it'll change edit behavior and cause regression. Are those changes necessary for create --edit, or are they just for improving edit? If they're not required for making create --edit work, separate them into their own commit / PR.

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:

  1. Get printer (json, yaml, etc) (should be the same for all commands)
  2. Get their own mapper / resource mapper
  3. Get their own resource builder / result
  4. Edit the resource in Visit (should be the same or similar in all commands)
  5. PrintSuccess for each command (edit, create, etc)

Also keep in mind that this edit function may be extended to other commands (such as run --edit) in the future.

@adohe-zz
Copy link

adohe-zz commented Oct 6, 2016

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?

I think so, maybe we can open a new PR to do this.

@mengqiy
Copy link
Member Author

mengqiy commented Oct 6, 2016

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

@janetkuo
Copy link
Member

Would you add some tests in test-cmd.sh too?

@mengqiy mengqiy mentioned this pull request Oct 14, 2016
@mengqiy
Copy link
Member Author

mengqiy commented Oct 14, 2016

@janetkuo I created another PR #34862 to fix the panic issue. Sorry about introducing this issue.

k8s-github-robot pushed a commit that referenced this pull request Oct 15, 2016
Automatic merge from submit-queue

fix error handling

Add missing error handling mentioned in [#33250 Comment](#33250 (comment))
@janetkuo
@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 15, 2016
@k8s-ci-robot
Copy link
Contributor

Jenkins GKE smoke e2e failed for commit 36c77a6e144267fda530f8502238555070e2e505. Full PR test history.

The magic incantation to run this job again is @k8s-bot cvm gke e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 19, 2016
@k8s-ci-robot
Copy link
Contributor

Jenkins verification failed for commit 36c77a6e144267fda530f8502238555070e2e505. Full PR test history.

The magic incantation to run this job again is @k8s-bot verify test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@mengqiy
Copy link
Member Author

mengqiy commented Oct 20, 2016

Updated. PTAL
The behavior has been changed. It won't create anything when edit make no change.

@mengqiy
Copy link
Member Author

mengqiy commented Oct 22, 2016

@janetkuo ping

const (
NormalEditMode = "normal_mode"
EditBeforeCreateMode = "edit_before_create_mode"
)
Copy link
Member

@janetkuo janetkuo Oct 22, 2016

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
Copy link
Member

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")
Copy link
Member

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?

Copy link
Member

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

Copy link
Member

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

Copy link
Member Author

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.

@janetkuo
Copy link
Member

Overall looks good. Had a few comments

Copy link
Contributor

@soltysh soltysh left a 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
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@mengqiy
Copy link
Member Author

mengqiy commented Oct 24, 2016

Squashed.

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@janetkuo janetkuo added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 24, 2016
@k8s-ci-robot
Copy link
Contributor

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. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@mengqiy
Copy link
Member Author

mengqiy commented Oct 28, 2016

@k8s-bot gci gke e2e test this

@k8s-github-robot
Copy link

@k8s-bot test this [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants