apply_edit_last_applied.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apply
  14. import (
  15. "github.com/spf13/cobra"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  18. "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
  19. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  20. )
  21. var (
  22. applyEditLastAppliedLong = templates.LongDesc(`
  23. Edit the latest last-applied-configuration annotations of resources from the default editor.
  24. The edit-last-applied command allows you to directly edit any API resource you can retrieve via the
  25. command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR
  26. environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.
  27. You can edit multiple objects, although changes are applied one at a time. The command
  28. accepts filenames as well as command line arguments, although the files you point to must
  29. be previously saved versions of resources.
  30. The default format is YAML. To edit in JSON, specify "-o json".
  31. The flag --windows-line-endings can be used to force Windows line endings,
  32. otherwise the default for your operating system will be used.
  33. In the event an error occurs while updating, a temporary file will be created on disk
  34. that contains your unapplied changes. The most common error when updating a resource
  35. is another editor changing the resource on the server. When this occurs, you will have
  36. to apply your changes to the newer version of the resource, or update your temporary
  37. saved copy to include the latest resource version.`)
  38. applyEditLastAppliedExample = templates.Examples(`
  39. # Edit the last-applied-configuration annotations by type/name in YAML.
  40. kubectl apply edit-last-applied deployment/nginx
  41. # Edit the last-applied-configuration annotations by file in JSON.
  42. kubectl apply edit-last-applied -f deploy.yaml -o json`)
  43. )
  44. // NewCmdApplyEditLastApplied created the cobra CLI command for the `apply edit-last-applied` command.
  45. func NewCmdApplyEditLastApplied(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  46. o := editor.NewEditOptions(editor.ApplyEditMode, ioStreams)
  47. cmd := &cobra.Command{
  48. Use: "edit-last-applied (RESOURCE/NAME | -f FILENAME)",
  49. DisableFlagsInUseLine: true,
  50. Short: "Edit latest last-applied-configuration annotations of a resource/object",
  51. Long: applyEditLastAppliedLong,
  52. Example: applyEditLastAppliedExample,
  53. Run: func(cmd *cobra.Command, args []string) {
  54. if err := o.Complete(f, args, cmd); err != nil {
  55. cmdutil.CheckErr(err)
  56. }
  57. if err := o.Run(); err != nil {
  58. cmdutil.CheckErr(err)
  59. }
  60. },
  61. }
  62. // bind flag structs
  63. o.RecordFlags.AddFlags(cmd)
  64. o.PrintFlags.AddFlags(cmd)
  65. usage := "to use to edit the resource"
  66. cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
  67. cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
  68. "Defaults to the line ending native to your platform.")
  69. cmdutil.AddIncludeUninitializedFlag(cmd)
  70. return cmd
  71. }