2015-07-23 22:43:48 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2015-07-23 22:43:48 +00:00
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 (
"bytes"
"fmt"
2018-04-19 00:02:37 +00:00
"io"
2015-07-23 22:43:48 +00:00
2017-01-22 18:54:39 +00:00
jsonpatch "github.com/evanphx/json-patch"
2016-01-25 16:57:34 +00:00
"github.com/golang/glog"
2015-08-05 22:05:17 +00:00
"github.com/spf13/cobra"
2017-01-22 18:54:39 +00:00
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/api/meta"
2017-01-17 03:38:19 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/runtime"
2017-01-16 20:13:59 +00:00
"k8s.io/apimachinery/pkg/types"
2017-01-22 18:54:39 +00:00
"k8s.io/apimachinery/pkg/util/json"
2018-04-19 00:02:37 +00:00
"k8s.io/kubernetes/pkg/printers"
2017-01-22 18:54:39 +00:00
2015-08-21 07:08:29 +00:00
"k8s.io/kubernetes/pkg/kubectl"
2016-10-07 22:24:42 +00:00
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2015-08-05 22:03:47 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2018-04-18 15:20:33 +00:00
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/kubectl/resource"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2015-07-23 22:43:48 +00:00
)
// AnnotateOptions have the data required to perform the annotate operation
type AnnotateOptions struct {
2018-04-19 00:02:37 +00:00
PrintFlags * printers . PrintFlags
PrintObj printers . ResourcePrinterFunc
2016-10-06 07:17:40 +00:00
// Filename options
2016-08-17 18:28:07 +00:00
resource . FilenameOptions
2018-04-18 15:20:33 +00:00
RecordFlags * genericclioptions . RecordFlags
2016-08-17 18:28:07 +00:00
2016-10-06 07:17:40 +00:00
// Common user flags
2018-04-18 15:20:33 +00:00
overwrite bool
local bool
dryrun bool
all bool
resourceVersion string
selector string
outputFormat string
2016-10-06 07:17:40 +00:00
// results of arg parsing
2015-07-23 22:43:48 +00:00
resources [ ] string
newAnnotations map [ string ] string
removeAnnotations [ ] string
2018-04-18 15:20:33 +00:00
Recorder genericclioptions . Recorder
2015-11-13 19:07:21 +00:00
2018-04-19 21:43:28 +00:00
genericclioptions . IOStreams
2015-07-23 22:43:48 +00:00
}
2016-05-20 17:49:56 +00:00
var (
2017-02-16 03:47:00 +00:00
annotateLong = templates . LongDesc ( `
2017-10-24 01:14:31 +00:00
Update the annotations on one or more resources
2015-07-23 22:43:48 +00:00
2017-10-24 01:14:31 +00:00
All Kubernetes objects support the ability to store additional data with the object as
annotations . Annotations are key / value pairs that can be larger than labels and include
arbitrary string values such as structured JSON . Tools and system extensions may use
annotations to store their own data .
2015-07-23 22:43:48 +00:00
2017-10-24 01:14:31 +00:00
Attempting to set an annotation that already exists will fail unless -- overwrite is set .
If -- resource - version is specified and does not match the current resource version on
the server the command will fail . ` )
2016-04-03 10:13:51 +00:00
2017-02-16 03:47:00 +00:00
annotateExample = templates . Examples ( i18n . T ( `
2016-10-07 22:24:42 +00:00
# Update pod ' foo ' with the annotation ' description ' and the value ' my frontend ' .
# If the same annotation is set multiple times , only the last value will be applied
kubectl annotate pods foo description = ' my frontend '
2015-07-23 22:43:48 +00:00
2016-10-07 22:24:42 +00:00
# Update a pod identified by type and name in "pod.json"
kubectl annotate - f pod . json description = ' my frontend '
2015-08-21 07:08:29 +00:00
2016-10-07 22:24:42 +00:00
# Update pod ' foo ' with the annotation ' description ' and the value ' my frontend running nginx ' , overwriting any existing value .
kubectl annotate -- overwrite pods foo description = ' my frontend running nginx '
2015-07-23 22:43:48 +00:00
2016-10-07 22:24:42 +00:00
# Update all pods in the namespace
kubectl annotate pods -- all description = ' my frontend running nginx '
2015-07-23 22:43:48 +00:00
2016-10-07 22:24:42 +00:00
# Update pod ' foo ' only if the resource is unchanged from version 1.
kubectl annotate pods foo description = ' my frontend running nginx ' -- resource - version = 1
2015-07-23 22:43:48 +00:00
2016-10-07 22:24:42 +00:00
# Update pod ' foo ' by removing an annotation named ' description ' if it exists .
# Does not require the -- overwrite flag .
2017-03-15 03:49:10 +00:00
kubectl annotate pods foo description - ` ) )
2015-07-23 22:43:48 +00:00
)
2018-04-19 21:43:28 +00:00
func NewAnnotateOptions ( ioStreams genericclioptions . IOStreams ) * AnnotateOptions {
2018-04-18 15:20:33 +00:00
return & AnnotateOptions {
2018-04-19 00:02:37 +00:00
PrintFlags : printers . NewPrintFlags ( "annotated" ) ,
2018-04-19 12:32:15 +00:00
2018-04-19 00:02:37 +00:00
RecordFlags : genericclioptions . NewRecordFlags ( ) ,
Recorder : genericclioptions . NoopRecorder { } ,
IOStreams : ioStreams ,
2018-04-18 15:20:33 +00:00
}
}
2018-04-25 23:55:26 +00:00
func NewCmdAnnotate ( parent string , f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-04-19 21:43:28 +00:00
o := NewAnnotateOptions ( ioStreams )
2017-12-15 07:20:13 +00:00
validArgs := cmdutil . ValidArgList ( f )
2016-04-03 10:13:51 +00:00
2015-07-23 22:43:48 +00:00
cmd := & cobra . Command {
2017-10-11 06:26:02 +00:00
Use : "annotate [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]" ,
DisableFlagsInUseLine : true ,
2016-12-14 07:18:11 +00:00
Short : i18n . T ( "Update the annotations on a resource" ) ,
2018-04-25 23:55:26 +00:00
Long : annotateLong + "\n\n" + cmdutil . SuggestApiResources ( parent ) ,
2017-02-16 03:47:00 +00:00
Example : annotateExample ,
2015-07-23 22:43:48 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-04-19 12:32:15 +00:00
if err := o . Complete ( f , cmd , args ) ; err != nil {
2017-06-14 21:14:42 +00:00
cmdutil . CheckErr ( cmdutil . UsageErrorf ( cmd , "%v" , err ) )
2015-07-23 22:43:48 +00:00
}
2018-04-19 12:32:15 +00:00
if err := o . Validate ( ) ; err != nil {
2017-06-14 21:14:42 +00:00
cmdutil . CheckErr ( cmdutil . UsageErrorf ( cmd , "%v" , err ) )
2015-07-23 22:43:48 +00:00
}
2018-04-19 12:32:15 +00:00
cmdutil . CheckErr ( o . RunAnnotate ( f , cmd ) )
2015-07-23 22:43:48 +00:00
} ,
2016-04-03 10:13:51 +00:00
ValidArgs : validArgs ,
2017-12-15 07:20:13 +00:00
ArgAliases : kubectl . ResourceAliases ( validArgs ) ,
2015-07-23 22:43:48 +00:00
}
2018-04-18 15:20:33 +00:00
// bind flag structs
2018-04-19 12:32:15 +00:00
o . RecordFlags . AddFlags ( cmd )
2018-04-19 00:02:37 +00:00
o . PrintFlags . AddFlags ( cmd )
2018-04-18 15:20:33 +00:00
2017-08-11 06:21:44 +00:00
cmdutil . AddIncludeUninitializedFlag ( cmd )
2018-04-19 12:32:15 +00:00
cmd . Flags ( ) . BoolVar ( & o . overwrite , "overwrite" , o . overwrite , "If true, allow annotations to be overwritten, otherwise reject annotation updates that overwrite existing annotations." )
cmd . Flags ( ) . BoolVar ( & o . local , "local" , o . local , "If true, annotation will NOT contact api-server but run locally." )
cmd . Flags ( ) . StringVarP ( & o . selector , "selector" , "l" , o . selector , "Selector (label query) to filter on, not including uninitialized ones, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)." )
cmd . Flags ( ) . BoolVar ( & o . all , "all" , o . all , "Select all resources, including uninitialized ones, in the namespace of the specified resource types." )
cmd . Flags ( ) . StringVar ( & o . resourceVersion , "resource-version" , o . resourceVersion , i18n . T ( "If non-empty, the annotation update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource." ) )
2016-08-17 18:28:07 +00:00
usage := "identifying the resource to update the annotation"
2018-04-19 12:32:15 +00:00
cmdutil . AddFilenameOptionFlags ( cmd , & o . FilenameOptions , usage )
2016-10-06 07:17:40 +00:00
cmdutil . AddDryRunFlag ( cmd )
2015-07-23 22:43:48 +00:00
return cmd
}
// Complete adapts from the command line args and factory to the data required.
2018-04-18 15:20:33 +00:00
func ( o * AnnotateOptions ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
var err error
2018-04-19 12:32:15 +00:00
o . RecordFlags . Complete ( f . Command ( cmd , false ) )
2018-04-18 15:20:33 +00:00
o . Recorder , err = o . RecordFlags . ToRecorder ( )
if err != nil {
return err
}
2016-10-06 07:17:40 +00:00
o . outputFormat = cmdutil . GetFlagString ( cmd , "output" )
o . dryrun = cmdutil . GetDryRunFlag ( cmd )
2015-07-23 22:43:48 +00:00
2018-04-19 00:02:37 +00:00
if o . dryrun {
o . PrintFlags . Complete ( "%s (dry run)" )
}
printer , err := o . PrintFlags . ToPrinter ( )
if err != nil {
return err
}
o . PrintObj = func ( obj runtime . Object , out io . Writer ) error {
return printer . PrintObj ( obj , out )
}
2015-07-23 22:43:48 +00:00
// retrieves resource and annotation args from args
// also checks args to verify that all resources are specified before annotations
2016-05-11 00:26:39 +00:00
resources , annotationArgs , err := cmdutil . GetResourcesAndPairs ( args , "annotation" )
if err != nil {
return err
2015-07-23 22:43:48 +00:00
}
2016-05-11 00:26:39 +00:00
o . resources = resources
2016-10-06 07:17:40 +00:00
o . newAnnotations , o . removeAnnotations , err = parseAnnotations ( annotationArgs )
return err
}
// Validate checks to the AnnotateOptions to see if there is sufficient information run the command.
func ( o AnnotateOptions ) Validate ( ) error {
2017-08-25 08:28:21 +00:00
if o . all && len ( o . selector ) > 0 {
return fmt . Errorf ( "cannot set --all and --selector at the same time" )
}
2017-06-14 21:14:42 +00:00
if len ( o . resources ) < 1 && cmdutil . IsFilenameSliceEmpty ( o . Filenames ) {
2015-07-23 22:43:48 +00:00
return fmt . Errorf ( "one or more resources must be specified as <resource> <name> or <resource>/<name>" )
}
2016-10-06 07:17:40 +00:00
if len ( o . newAnnotations ) < 1 && len ( o . removeAnnotations ) < 1 {
2015-07-23 22:43:48 +00:00
return fmt . Errorf ( "at least one annotation update is required" )
}
2016-10-06 07:17:40 +00:00
return validateAnnotations ( o . removeAnnotations , o . newAnnotations )
}
2015-07-23 22:43:48 +00:00
2016-10-06 07:17:40 +00:00
// RunAnnotate does the work
2016-10-13 00:18:39 +00:00
func ( o AnnotateOptions ) RunAnnotate ( f cmdutil . Factory , cmd * cobra . Command ) error {
2016-10-06 07:17:40 +00:00
namespace , enforceNamespace , err := f . DefaultNamespace ( )
if err != nil {
2015-07-23 22:43:48 +00:00
return err
}
2017-08-11 06:21:44 +00:00
includeUninitialized := cmdutil . ShouldIncludeUninitialized ( cmd , false )
2017-11-15 04:03:06 +00:00
b := f . NewBuilder ( ) .
Unstructured ( ) .
LocalParam ( o . local ) .
ContinueOnError ( ) .
2017-11-14 03:43:58 +00:00
NamespaceParam ( namespace ) . DefaultNamespace ( ) .
FilenameParam ( enforceNamespace , & o . FilenameOptions ) .
IncludeUninitialized ( includeUninitialized ) .
Flatten ( )
2017-08-02 20:23:07 +00:00
2017-11-14 03:43:58 +00:00
if ! o . local {
b = b . LabelSelectorParam ( o . selector ) .
2016-10-05 04:39:45 +00:00
ResourceTypeOrNameArgs ( o . all , o . resources ... ) .
Latest ( )
}
2017-11-14 03:43:58 +00:00
r := b . Do ( )
2015-07-23 22:43:48 +00:00
if err := r . Err ( ) ; err != nil {
return err
}
2015-10-14 05:25:18 +00:00
2017-01-09 19:21:24 +00:00
var singleItemImpliedResource bool
r . IntoSingleItemImplied ( & singleItemImpliedResource )
2016-07-20 20:24:29 +00:00
// only apply resource version locking on a single resource.
// we must perform this check after o.builder.Do() as
// []o.resources can not not accurately return the proper number
// of resources when they are not passed in "resource/name" format.
2017-01-09 19:21:24 +00:00
if ! singleItemImpliedResource && len ( o . resourceVersion ) > 0 {
2016-07-20 20:24:29 +00:00
return fmt . Errorf ( "--resource-version may only be used with a single resource" )
}
2015-06-15 02:48:56 +00:00
return r . Visit ( func ( info * resource . Info , err error ) error {
if err != nil {
return err
}
2015-10-14 05:25:18 +00:00
2016-10-05 04:39:45 +00:00
var outputObj runtime . Object
2017-05-16 21:52:51 +00:00
var obj runtime . Object
2018-04-24 22:31:41 +00:00
obj , err = info . ObjectConverter . ConvertToVersion ( info . Object , info . Mapping . GroupVersionKind . GroupVersion ( ) )
2016-01-22 12:39:18 +00:00
if err != nil {
return err
}
2017-05-16 21:52:51 +00:00
2016-10-06 07:17:40 +00:00
if o . dryrun || o . local {
2016-10-05 04:39:45 +00:00
if err := o . updateAnnotations ( obj ) ; err != nil {
return err
}
outputObj = obj
} else {
name , namespace := info . Name , info . Namespace
oldData , err := json . Marshal ( obj )
if err != nil {
return err
}
2018-04-18 15:20:33 +00:00
if err := o . Recorder . Record ( info . Object ) ; err != nil {
glog . V ( 4 ) . Infof ( "error recording current command: %v" , err )
2016-10-05 04:39:45 +00:00
}
if err := o . updateAnnotations ( obj ) ; err != nil {
return err
}
newData , err := json . Marshal ( obj )
if err != nil {
return err
}
2017-01-22 18:54:39 +00:00
patchBytes , err := jsonpatch . CreateMergePatch ( oldData , newData )
2016-10-05 04:39:45 +00:00
createdPatch := err == nil
if err != nil {
glog . V ( 2 ) . Infof ( "couldn't compute patch: %v" , err )
}
2015-10-14 05:25:18 +00:00
2016-10-05 04:39:45 +00:00
mapping := info . ResourceMapping ( )
2017-01-22 18:54:39 +00:00
client , err := f . UnstructuredClientForMapping ( mapping )
2016-10-05 04:39:45 +00:00
if err != nil {
return err
}
helper := resource . NewHelper ( client , mapping )
2015-10-14 05:25:18 +00:00
2016-10-05 04:39:45 +00:00
if createdPatch {
2017-01-22 18:54:39 +00:00
outputObj , err = helper . Patch ( namespace , name , types . MergePatchType , patchBytes )
2016-10-05 04:39:45 +00:00
} else {
outputObj , err = helper . Replace ( namespace , name , false , obj )
}
if err != nil {
return err
}
}
2017-05-16 21:52:51 +00:00
2018-04-19 00:02:37 +00:00
return o . PrintObj ( outputObj , o . Out )
2015-07-23 22:43:48 +00:00
} )
}
// parseAnnotations retrieves new and remove annotations from annotation args
func parseAnnotations ( annotationArgs [ ] string ) ( map [ string ] string , [ ] string , error ) {
2016-05-11 00:26:39 +00:00
return cmdutil . ParsePairs ( annotationArgs , "annotation" , true )
2015-07-23 22:43:48 +00:00
}
// validateAnnotations checks the format of annotation args and checks removed annotations aren't in the new annotations map
func validateAnnotations ( removeAnnotations [ ] string , newAnnotations map [ string ] string ) error {
var modifyRemoveBuf bytes . Buffer
for _ , removeAnnotation := range removeAnnotations {
if _ , found := newAnnotations [ removeAnnotation ] ; found {
if modifyRemoveBuf . Len ( ) > 0 {
modifyRemoveBuf . WriteString ( ", " )
}
modifyRemoveBuf . WriteString ( fmt . Sprintf ( removeAnnotation ) )
}
}
if modifyRemoveBuf . Len ( ) > 0 {
return fmt . Errorf ( "can not both modify and remove the following annotation(s) in the same command: %s" , modifyRemoveBuf . String ( ) )
}
return nil
}
// validateNoAnnotationOverwrites validates that when overwrite is false, to-be-updated annotations don't exist in the object annotation map (yet)
2017-01-11 20:28:46 +00:00
func validateNoAnnotationOverwrites ( accessor metav1 . Object , annotations map [ string ] string ) error {
2015-07-23 22:43:48 +00:00
var buf bytes . Buffer
for key := range annotations {
2016-01-22 18:33:23 +00:00
// change-cause annotation can always be overwritten
if key == kubectl . ChangeCauseAnnotation {
continue
}
2016-03-25 08:57:45 +00:00
if value , found := accessor . GetAnnotations ( ) [ key ] ; found {
2015-07-23 22:43:48 +00:00
if buf . Len ( ) > 0 {
buf . WriteString ( "; " )
}
buf . WriteString ( fmt . Sprintf ( "'%s' already has a value (%s)" , key , value ) )
}
}
if buf . Len ( ) > 0 {
return fmt . Errorf ( "--overwrite is false but found the following declared annotation(s): %s" , buf . String ( ) )
}
return nil
}
// updateAnnotations updates annotations of obj
func ( o AnnotateOptions ) updateAnnotations ( obj runtime . Object ) error {
2016-03-25 08:57:45 +00:00
accessor , err := meta . Accessor ( obj )
2015-07-23 22:43:48 +00:00
if err != nil {
return err
}
if ! o . overwrite {
2016-03-25 08:57:45 +00:00
if err := validateNoAnnotationOverwrites ( accessor , o . newAnnotations ) ; err != nil {
2015-07-23 22:43:48 +00:00
return err
}
}
2016-03-25 08:57:45 +00:00
annotations := accessor . GetAnnotations ( )
if annotations == nil {
annotations = make ( map [ string ] string )
2015-07-23 22:43:48 +00:00
}
for key , value := range o . newAnnotations {
2016-03-25 08:57:45 +00:00
annotations [ key ] = value
2015-07-23 22:43:48 +00:00
}
for _ , annotation := range o . removeAnnotations {
2016-03-25 08:57:45 +00:00
delete ( annotations , annotation )
2015-07-23 22:43:48 +00:00
}
2016-03-25 08:57:45 +00:00
accessor . SetAnnotations ( annotations )
2015-07-23 22:43:48 +00:00
if len ( o . resourceVersion ) != 0 {
2016-03-25 08:57:45 +00:00
accessor . SetResourceVersion ( o . resourceVersion )
2015-07-23 22:43:48 +00:00
}
return nil
}