From e9c1df68289674da83aa83546c47bff01b6d7fac Mon Sep 17 00:00:00 2001 From: zhzhuang-zju Date: Tue, 27 Aug 2024 19:35:13 +0800 Subject: [PATCH] add new command edit Signed-off-by: zhzhuang-zju --- pkg/karmadactl/edit/edit.go | 54 +++++++++++ pkg/karmadactl/karmadactl.go | 2 + vendor/k8s.io/kubectl/pkg/cmd/edit/edit.go | 108 +++++++++++++++++++++ vendor/modules.txt | 1 + 4 files changed, 165 insertions(+) create mode 100644 pkg/karmadactl/edit/edit.go create mode 100644 vendor/k8s.io/kubectl/pkg/cmd/edit/edit.go diff --git a/pkg/karmadactl/edit/edit.go b/pkg/karmadactl/edit/edit.go new file mode 100644 index 000000000..1c7388b4d --- /dev/null +++ b/pkg/karmadactl/edit/edit.go @@ -0,0 +1,54 @@ +/* +Copyright 2024 The Karmada 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 edit + +import ( + "fmt" + + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericiooptions" + kubectledit "k8s.io/kubectl/pkg/cmd/edit" + "k8s.io/kubectl/pkg/util/i18n" + "k8s.io/kubectl/pkg/util/templates" + + "github.com/karmada-io/karmada/pkg/karmadactl/util" +) + +var ( + editExample = templates.Examples(i18n.T(` + # Edit the service named 'registry' + %[1]s edit svc/registry + + # Use an alternative editor + KUBE_EDITOR="nano" %[1]s edit svc/registry + + # Edit the job 'myjob' in JSON using the v1 API format + %[1]s edit job.v1.batch/myjob -o json + + # Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation + %[1]s edit deployment/mydeployment -o yaml --save-config + + # Edit the 'status' subresource for the 'mydeployment' deployment + %[1]s edit deployment mydeployment --subresource='status'`)) +) + +// NewCmdEdit returns new initialized instance of edit sub command +func NewCmdEdit(f util.Factory, parentCommnd string, ioStreams genericiooptions.IOStreams) *cobra.Command { + cmd := kubectledit.NewCmdEdit(f, ioStreams) + cmd.Example = fmt.Sprintf(editExample, parentCommnd) + return cmd +} diff --git a/pkg/karmadactl/karmadactl.go b/pkg/karmadactl/karmadactl.go index 2f7060e61..55f17960d 100644 --- a/pkg/karmadactl/karmadactl.go +++ b/pkg/karmadactl/karmadactl.go @@ -38,6 +38,7 @@ import ( "github.com/karmada-io/karmada/pkg/karmadactl/deinit" karmadactldelete "github.com/karmada-io/karmada/pkg/karmadactl/delete" "github.com/karmada-io/karmada/pkg/karmadactl/describe" + "github.com/karmada-io/karmada/pkg/karmadactl/edit" "github.com/karmada-io/karmada/pkg/karmadactl/exec" "github.com/karmada-io/karmada/pkg/karmadactl/explain" "github.com/karmada-io/karmada/pkg/karmadactl/get" @@ -95,6 +96,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command { get.NewCmdGet(f, parentCommand, ioStreams), create.NewCmdCreate(f, parentCommand, ioStreams), karmadactldelete.NewCmdDelete(f, parentCommand, ioStreams), + edit.NewCmdEdit(f, parentCommand, ioStreams), }, }, { diff --git a/vendor/k8s.io/kubectl/pkg/cmd/edit/edit.go b/vendor/k8s.io/kubectl/pkg/cmd/edit/edit.go new file mode 100644 index 000000000..2e9228324 --- /dev/null +++ b/vendor/k8s.io/kubectl/pkg/cmd/edit/edit.go @@ -0,0 +1,108 @@ +/* +Copyright 2015 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 edit + +import ( + "github.com/spf13/cobra" + + "k8s.io/cli-runtime/pkg/genericiooptions" + + cmdutil "k8s.io/kubectl/pkg/cmd/util" + "k8s.io/kubectl/pkg/cmd/util/editor" + "k8s.io/kubectl/pkg/util/completion" + "k8s.io/kubectl/pkg/util/i18n" + "k8s.io/kubectl/pkg/util/templates" +) + +var ( + editLong = templates.LongDesc(i18n.T(` + Edit a resource from the default editor. + + The edit 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. + When attempting to open the editor, it will first attempt to use the shell + that has been defined in the 'SHELL' environment variable. If this is not defined, + the default shell will be used, which is '/bin/bash' for Linux or 'cmd' for Windows. + + You can edit multiple objects, although changes are applied one at a time. The command + accepts file names as well as command-line arguments, although the files you point to must + be previously saved versions of resources. + + Editing is done with the API version used to fetch the resource. + To edit using a specific API version, fully-qualify the resource, version, and group. + + 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.`)) + + editExample = templates.Examples(i18n.T(` + # Edit the service named 'registry' + kubectl edit svc/registry + + # Use an alternative editor + KUBE_EDITOR="nano" kubectl edit svc/registry + + # Edit the job 'myjob' in JSON using the v1 API format + kubectl edit job.v1.batch/myjob -o json + + # Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation + kubectl edit deployment/mydeployment -o yaml --save-config + + # Edit the 'status' subresource for the 'mydeployment' deployment + kubectl edit deployment mydeployment --subresource='status'`)) +) + +// NewCmdEdit creates the `edit` command +func NewCmdEdit(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobra.Command { + o := editor.NewEditOptions(editor.NormalEditMode, ioStreams) + cmd := &cobra.Command{ + Use: "edit (RESOURCE/NAME | -f FILENAME)", + DisableFlagsInUseLine: true, + Short: i18n.T("Edit a resource on the server"), + Long: editLong, + Example: editExample, + ValidArgsFunction: completion.ResourceTypeAndNameCompletionFunc(f), + Run: func(cmd *cobra.Command, args []string) { + cmdutil.CheckErr(o.Complete(f, args, cmd)) + cmdutil.CheckErr(o.Validate()) + cmdutil.CheckErr(o.Run()) + }, + } + + // bind flag structs + o.RecordFlags.AddFlags(cmd) + o.PrintFlags.AddFlags(cmd) + + usage := "to use to edit the resource" + cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage) + cmdutil.AddValidateFlags(cmd) + cmd.Flags().BoolVarP(&o.OutputPatch, "output-patch", "", o.OutputPatch, "Output the patch if the resource is edited.") + cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings, + "Defaults to the line ending native to your platform.") + cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-edit") + cmdutil.AddApplyAnnotationVarFlags(cmd, &o.ApplyAnnotation) + cmdutil.AddSubresourceFlags(cmd, &o.Subresource, "If specified, edit will operate on the subresource of the requested object.", editor.SupportedSubresources...) + return cmd +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 8f0af3529..5bf6b1bb1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1686,6 +1686,7 @@ k8s.io/kubectl/pkg/cmd/attach k8s.io/kubectl/pkg/cmd/create k8s.io/kubectl/pkg/cmd/delete k8s.io/kubectl/pkg/cmd/describe +k8s.io/kubectl/pkg/cmd/edit k8s.io/kubectl/pkg/cmd/exec k8s.io/kubectl/pkg/cmd/explain k8s.io/kubectl/pkg/cmd/get