-
Notifications
You must be signed in to change notification settings - Fork 40k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42256 from shiywang/edit
Automatic merge from submit-queue (batch tested with PRs 42256, 46479, 45436, 46440, 46417) Add `kubectl apply edit-last-applied` subcommand third command of kubernetes/community#287 Fixes #44905 @pwittrock @adohe @ymqytw @kubernetes/sig-cli-feature-requests could you guys have an early review ? cause some of feature I'm not sure about, will add unit tests if you think it's ok.
- Loading branch information
Showing
37 changed files
with
881 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
gruntime "runtime" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/kubernetes/pkg/kubectl" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor" | ||
"k8s.io/kubernetes/pkg/printers" | ||
) | ||
|
||
var ( | ||
applyEditLastAppliedLong = templates.LongDesc(` | ||
Edit the latest last-applied-configuration annotations of resources from the default editor. | ||
The edit-last-applied command allows you to directly edit any API resource you can retrieve via the | ||
command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR | ||
environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows. | ||
You can edit multiple objects, although changes are applied one at a time. The command | ||
accepts filenames as well as command line arguments, although the files you point to must | ||
be previously saved versions of resources. | ||
The default format is YAML. To edit in JSON, specify "-o json". | ||
The flag --windows-line-endings can be used to force Windows line endings, | ||
otherwise the default for your operating system will be used. | ||
In the event an error occurs while updating, a temporary file will be created on disk | ||
that contains your unapplied changes. The most common error when updating a resource | ||
is another editor changing the resource on the server. When this occurs, you will have | ||
to apply your changes to the newer version of the resource, or update your temporary | ||
saved copy to include the latest resource version.`) | ||
|
||
applyEditLastAppliedExample = templates.Examples(` | ||
# Edit the last-applied-configuration annotations by type/name in YAML. | ||
kubectl apply edit-last-applied deployment/nginx | ||
# Edit the last-applied-configuration annotations by file in JSON. | ||
kubectl apply edit-last-applied -f deploy.yaml -o json`) | ||
) | ||
|
||
func NewCmdApplyEditLastApplied(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { | ||
options := &editor.EditOptions{ | ||
EditMode: editor.ApplyEditMode, | ||
} | ||
|
||
// retrieve a list of handled resources from printer as valid args | ||
validArgs, argAliases := []string{}, []string{} | ||
p, err := f.Printer(nil, printers.PrintOptions{ | ||
ColumnLabels: []string{}, | ||
}) | ||
cmdutil.CheckErr(err) | ||
if p != nil { | ||
validArgs = p.HandledResources() | ||
argAliases = kubectl.ResourceAliases(validArgs) | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "edit-last-applied (RESOURCE/NAME | -f FILENAME)", | ||
Short: "Edit latest last-applied-configuration annotations of a resource/object", | ||
Long: applyEditLastAppliedLong, | ||
Example: applyEditLastAppliedExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
options.ChangeCause = f.Command(cmd, false) | ||
if err := options.Complete(f, out, errOut, args); err != nil { | ||
cmdutil.CheckErr(err) | ||
} | ||
if err := options.Run(); err != nil { | ||
cmdutil.CheckErr(err) | ||
} | ||
}, | ||
ValidArgs: validArgs, | ||
ArgAliases: argAliases, | ||
} | ||
|
||
usage := "to use to edit the resource" | ||
cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage) | ||
cmd.Flags().StringVarP(&options.Output, "output", "o", "yaml", "Output format. One of: yaml|json.") | ||
cmd.Flags().BoolVar(&options.WindowsLineEndings, "windows-line-endings", gruntime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)") | ||
cmdutil.AddRecordVarFlag(cmd, &options.Record) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
21 changes: 21 additions & 0 deletions
21
pkg/kubectl/cmd/testdata/edit/testcase-apply-edit-last-applied-list/0.response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"kind": "ConfigMap", | ||
"apiVersion": "v1", | ||
"metadata": { | ||
"name": "cm1", | ||
"namespace": "myproject", | ||
"selfLink": "/api/v1/namespaces/myproject/configmaps/cm1", | ||
"uid": "cc08a131-3d6f-11e7-8ef0-c85b76034b7b", | ||
"resourceVersion": "3518", | ||
"creationTimestamp": "2017-05-20T15:20:03Z", | ||
"annotations": { | ||
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"data\":{\"baz\":\"qux\",\"foo\":\"changed-value\",\"new-data\":\"new-value\",\"new-data2\":\"new-value\"},\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":{},\"name\":\"cm1\",\"namespace\":\"myproject\"}}\n" | ||
} | ||
}, | ||
"data": { | ||
"baz": "qux", | ||
"foo": "changed-value", | ||
"new-data": "new-value", | ||
"new-data2": "new-value" | ||
} | ||
} |
Empty file.
35 changes: 35 additions & 0 deletions
35
pkg/kubectl/cmd/testdata/edit/testcase-apply-edit-last-applied-list/1.response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"kind": "Service", | ||
"apiVersion": "v1", | ||
"metadata": { | ||
"name": "svc1", | ||
"namespace": "myproject", | ||
"selfLink": "/api/v1/namespaces/myproject/services/svc1", | ||
"uid": "d8b96f0b-3d6f-11e7-8ef0-c85b76034b7b", | ||
"resourceVersion": "3525", | ||
"creationTimestamp": "2017-05-20T15:20:24Z", | ||
"labels": { | ||
"app": "svc1", | ||
"new-label": "foo" | ||
}, | ||
"annotations": { | ||
"kubectl.kubernetes.io/last-applied-configuration": "{\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"svc1\",\"new-label\":\"foo\"},\"name\":\"svc1\",\"namespace\":\"myproject\"},\"spec\":{\"ports\":[{\"name\":\"80\",\"port\":81,\"protocol\":\"TCP\",\"targetPort\":81}],\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}\n" | ||
} | ||
}, | ||
"spec": { | ||
"ports": [ | ||
{ | ||
"name": "80", | ||
"protocol": "TCP", | ||
"port": 81, | ||
"targetPort": 81 | ||
} | ||
], | ||
"clusterIP": "172.30.32.183", | ||
"type": "ClusterIP", | ||
"sessionAffinity": "None" | ||
}, | ||
"status": { | ||
"loadBalancer": {} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
pkg/kubectl/cmd/testdata/edit/testcase-apply-edit-last-applied-list/2.edited
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Please edit the 'last-applied-configuration' annotations below. | ||
# Lines beginning with a '#' will be ignored, and an empty file will abort the edit. | ||
# | ||
apiVersion: v1 | ||
items: | ||
- apiVersion: v1 | ||
data: | ||
baz: qux | ||
foo: changed-value | ||
new-data: new-value | ||
new-data2: new-value | ||
new-data3: newivalue | ||
kind: ConfigMap | ||
metadata: | ||
annotations: {} | ||
name: cm1 | ||
namespace: myproject | ||
- kind: Service | ||
metadata: | ||
annotations: {} | ||
labels: | ||
app: svc1 | ||
new-label: foo | ||
new-label2: foo2 | ||
name: svc1 | ||
namespace: myproject | ||
spec: | ||
ports: | ||
- name: "80" | ||
port: 82 | ||
protocol: TCP | ||
targetPort: 81 | ||
sessionAffinity: None | ||
type: ClusterIP | ||
status: | ||
loadBalancer: {} | ||
kind: List | ||
metadata: {} |
36 changes: 36 additions & 0 deletions
36
pkg/kubectl/cmd/testdata/edit/testcase-apply-edit-last-applied-list/2.original
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Please edit the 'last-applied-configuration' annotations below. | ||
# Lines beginning with a '#' will be ignored, and an empty file will abort the edit. | ||
# | ||
apiVersion: v1 | ||
items: | ||
- apiVersion: v1 | ||
data: | ||
baz: qux | ||
foo: changed-value | ||
new-data: new-value | ||
new-data2: new-value | ||
kind: ConfigMap | ||
metadata: | ||
annotations: {} | ||
name: cm1 | ||
namespace: myproject | ||
- kind: Service | ||
metadata: | ||
annotations: {} | ||
labels: | ||
app: svc1 | ||
new-label: foo | ||
name: svc1 | ||
namespace: myproject | ||
spec: | ||
ports: | ||
- name: "80" | ||
port: 81 | ||
protocol: TCP | ||
targetPort: 81 | ||
sessionAffinity: None | ||
type: ClusterIP | ||
status: | ||
loadBalancer: {} | ||
kind: List | ||
metadata: {} |
7 changes: 7 additions & 0 deletions
7
pkg/kubectl/cmd/testdata/edit/testcase-apply-edit-last-applied-list/3.request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"metadata": { | ||
"annotations": { | ||
"kubectl.kubernetes.io~1last-applied-configuration": "{\"apiVersion\":\"v1\",\"data\":{\"baz\":\"qux\",\"foo\":\"changed-value\",\"new-data\":\"new-value\",\"new-data2\":\"new-value\",\"new-data3\":\"newivalue\"},\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":{},\"name\":\"cm1\",\"namespace\":\"myproject\"}}\n" | ||
} | ||
} | ||
} |
Oops, something went wrong.