...

Source file src/k8s.io/kubectl/pkg/cmd/apply/apply_edit_last_applied.go

Documentation: k8s.io/kubectl/pkg/cmd/apply

     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 apply
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  
    22  	"k8s.io/cli-runtime/pkg/genericiooptions"
    23  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    24  	"k8s.io/kubectl/pkg/cmd/util/editor"
    25  	"k8s.io/kubectl/pkg/util/completion"
    26  	"k8s.io/kubectl/pkg/util/i18n"
    27  	"k8s.io/kubectl/pkg/util/templates"
    28  )
    29  
    30  var (
    31  	applyEditLastAppliedLong = templates.LongDesc(i18n.T(`
    32  		Edit the latest last-applied-configuration annotations of resources from the default editor.
    33  
    34  		The edit-last-applied command allows you to directly edit any API resource you can retrieve via the
    35  		command-line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR
    36  		environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.
    37  		You can edit multiple objects, although changes are applied one at a time. The command
    38  		accepts file names as well as command-line arguments, although the files you point to must
    39  		be previously saved versions of resources.
    40  
    41  		The default format is YAML. To edit in JSON, specify "-o json".
    42  
    43  		The flag --windows-line-endings can be used to force Windows line endings,
    44  		otherwise the default for your operating system will be used.
    45  
    46  		In the event an error occurs while updating, a temporary file will be created on disk
    47  		that contains your unapplied changes. The most common error when updating a resource
    48  		is another editor changing the resource on the server. When this occurs, you will have
    49  		to apply your changes to the newer version of the resource, or update your temporary
    50  		saved copy to include the latest resource version.`))
    51  
    52  	applyEditLastAppliedExample = templates.Examples(`
    53  		# Edit the last-applied-configuration annotations by type/name in YAML
    54  		kubectl apply edit-last-applied deployment/nginx
    55  
    56  		# Edit the last-applied-configuration annotations by file in JSON
    57  		kubectl apply edit-last-applied -f deploy.yaml -o json`)
    58  )
    59  
    60  // NewCmdApplyEditLastApplied created the cobra CLI command for the `apply edit-last-applied` command.
    61  func NewCmdApplyEditLastApplied(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobra.Command {
    62  	o := editor.NewEditOptions(editor.ApplyEditMode, ioStreams)
    63  
    64  	cmd := &cobra.Command{
    65  		Use:                   "edit-last-applied (RESOURCE/NAME | -f FILENAME)",
    66  		DisableFlagsInUseLine: true,
    67  		Short:                 i18n.T("Edit latest last-applied-configuration annotations of a resource/object"),
    68  		Long:                  applyEditLastAppliedLong,
    69  		Example:               applyEditLastAppliedExample,
    70  		ValidArgsFunction:     completion.ResourceTypeAndNameCompletionFunc(f),
    71  		Run: func(cmd *cobra.Command, args []string) {
    72  			cmdutil.CheckErr(o.Complete(f, args, cmd))
    73  			cmdutil.CheckErr(o.Run())
    74  		},
    75  	}
    76  
    77  	// bind flag structs
    78  	o.RecordFlags.AddFlags(cmd)
    79  	o.PrintFlags.AddFlags(cmd)
    80  
    81  	usage := "to use to edit the resource"
    82  	cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
    83  	cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
    84  		"Defaults to the line ending native to your platform.")
    85  	cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, FieldManagerClientSideApply)
    86  	cmdutil.AddValidateFlags(cmd)
    87  
    88  	return cmd
    89  }
    90  

View as plain text