|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + gruntime "runtime" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + "k8s.io/kubernetes/pkg/kubectl" |
| 26 | + "k8s.io/kubernetes/pkg/kubectl/cmd/templates" |
| 27 | + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" |
| 28 | + "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor" |
| 29 | + "k8s.io/kubernetes/pkg/printers" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + applyEditLastAppliedLong = templates.LongDesc(` |
| 34 | + Edit the latest last-applied-configuration annotations of resources from the default editor. |
| 35 | +
|
| 36 | + The edit-last-applied command allows you to directly edit any API resource you can retrieve via the |
| 37 | + command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR |
| 38 | + environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows. |
| 39 | + You can edit multiple objects, although changes are applied one at a time. The command |
| 40 | + accepts filenames as well as command line arguments, although the files you point to must |
| 41 | + be previously saved versions of resources. |
| 42 | +
|
| 43 | + The default format is YAML. To edit in JSON, specify "-o json". |
| 44 | +
|
| 45 | + The flag --windows-line-endings can be used to force Windows line endings, |
| 46 | + otherwise the default for your operating system will be used. |
| 47 | +
|
| 48 | + In the event an error occurs while updating, a temporary file will be created on disk |
| 49 | + that contains your unapplied changes. The most common error when updating a resource |
| 50 | + is another editor changing the resource on the server. When this occurs, you will have |
| 51 | + to apply your changes to the newer version of the resource, or update your temporary |
| 52 | + saved copy to include the latest resource version.`) |
| 53 | + |
| 54 | + applyEditLastAppliedExample = templates.Examples(` |
| 55 | + # Edit the last-applied-configuration annotations by type/name in YAML. |
| 56 | + kubectl apply edit-last-applied deployment/nginx |
| 57 | +
|
| 58 | + # Edit the last-applied-configuration annotations by file in JSON. |
| 59 | + kubectl apply edit-last-applied -f deploy.yaml -o json`) |
| 60 | +) |
| 61 | + |
| 62 | +func NewCmdApplyEditLastApplied(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { |
| 63 | + options := &editor.EditOptions{ |
| 64 | + EditMode: editor.ApplyEditMode, |
| 65 | + } |
| 66 | + |
| 67 | + // retrieve a list of handled resources from printer as valid args |
| 68 | + validArgs, argAliases := []string{}, []string{} |
| 69 | + p, err := f.Printer(nil, printers.PrintOptions{ |
| 70 | + ColumnLabels: []string{}, |
| 71 | + }) |
| 72 | + cmdutil.CheckErr(err) |
| 73 | + if p != nil { |
| 74 | + validArgs = p.HandledResources() |
| 75 | + argAliases = kubectl.ResourceAliases(validArgs) |
| 76 | + } |
| 77 | + |
| 78 | + cmd := &cobra.Command{ |
| 79 | + Use: "edit-last-applied (RESOURCE/NAME | -f FILENAME)", |
| 80 | + Short: "Edit latest last-applied-configuration annotations of a resource/object", |
| 81 | + Long: applyEditLastAppliedLong, |
| 82 | + Example: applyEditLastAppliedExample, |
| 83 | + Run: func(cmd *cobra.Command, args []string) { |
| 84 | + options.ChangeCause = f.Command(cmd, false) |
| 85 | + if err := options.Complete(f, out, errOut, args); err != nil { |
| 86 | + cmdutil.CheckErr(err) |
| 87 | + } |
| 88 | + if err := options.Run(); err != nil { |
| 89 | + cmdutil.CheckErr(err) |
| 90 | + } |
| 91 | + }, |
| 92 | + ValidArgs: validArgs, |
| 93 | + ArgAliases: argAliases, |
| 94 | + } |
| 95 | + |
| 96 | + usage := "to use to edit the resource" |
| 97 | + cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage) |
| 98 | + cmd.Flags().StringVarP(&options.Output, "output", "o", "yaml", "Output format. One of: yaml|json.") |
| 99 | + cmd.Flags().BoolVar(&options.WindowsLineEndings, "windows-line-endings", gruntime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)") |
| 100 | + cmdutil.AddRecordVarFlag(cmd, &options.Record) |
| 101 | + |
| 102 | + return cmd |
| 103 | +} |
0 commit comments