Skip to content

Commit 8b9c9ea

Browse files
authored
Merge pull request kubernetes#92576 from zhouya0/support_kubectl_annotate_list
Support kubectl annotate --list option
2 parents 1d2ccd3 + ca1d598 commit 8b9c9ea

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/annotate/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
deps = [
1010
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
1111
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
12+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme:go_default_library",
1213
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
1314
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
1415
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",

staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"k8s.io/apimachinery/pkg/api/meta"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
3031
"k8s.io/apimachinery/pkg/runtime"
3132
"k8s.io/apimachinery/pkg/types"
3233
"k8s.io/apimachinery/pkg/util/json"
@@ -52,6 +53,7 @@ type AnnotateOptions struct {
5253

5354
// Common user flags
5455
overwrite bool
56+
list bool
5557
local bool
5658
dryRunStrategy cmdutil.DryRunStrategy
5759
dryRunVerifier *resource.DryRunVerifier
@@ -143,6 +145,7 @@ func NewCmdAnnotate(parent string, f cmdutil.Factory, ioStreams genericclioption
143145
o.PrintFlags.AddFlags(cmd)
144146

145147
cmd.Flags().BoolVar(&o.overwrite, "overwrite", o.overwrite, "If true, allow annotations to be overwritten, otherwise reject annotation updates that overwrite existing annotations.")
148+
cmd.Flags().BoolVar(&o.list, "list", o.list, "If true, display the annotations for a given resource.")
146149
cmd.Flags().BoolVar(&o.local, "local", o.local, "If true, annotation will NOT contact api-server but run locally.")
147150
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).")
148151
cmd.Flags().StringVar(&o.fieldSelector, "field-selector", o.fieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
@@ -190,6 +193,10 @@ func (o *AnnotateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
190193
return printer.PrintObj(obj, out)
191194
}
192195

196+
if o.list && len(o.outputFormat) > 0 {
197+
return fmt.Errorf("--list and --output may not be specified together")
198+
}
199+
193200
o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
194201
if err != nil {
195202
return err
@@ -235,7 +242,7 @@ func (o AnnotateOptions) Validate() error {
235242
return fmt.Errorf("one or more files must be specified as -f rsrc.yaml or --filename=rsrc.json")
236243
}
237244
}
238-
if len(o.newAnnotations) < 1 && len(o.removeAnnotations) < 1 {
245+
if len(o.newAnnotations) < 1 && len(o.removeAnnotations) < 1 && !o.list {
239246
return fmt.Errorf("at least one annotation update is required")
240247
}
241248
return validateAnnotations(o.removeAnnotations, o.newAnnotations)
@@ -282,7 +289,7 @@ func (o AnnotateOptions) RunAnnotate() error {
282289
var outputObj runtime.Object
283290
obj := info.Object
284291

285-
if o.dryRunStrategy == cmdutil.DryRunClient || o.local {
292+
if o.dryRunStrategy == cmdutil.DryRunClient || o.local || o.list {
286293
if err := o.updateAnnotations(obj); err != nil {
287294
return err
288295
}
@@ -344,6 +351,28 @@ func (o AnnotateOptions) RunAnnotate() error {
344351
}
345352
}
346353

354+
if o.list {
355+
accessor, err := meta.Accessor(outputObj)
356+
if err != nil {
357+
return err
358+
}
359+
360+
indent := ""
361+
if !singleItemImpliedResource {
362+
indent = " "
363+
gvks, _, err := unstructuredscheme.NewUnstructuredObjectTyper().ObjectKinds(info.Object)
364+
if err != nil {
365+
return err
366+
}
367+
fmt.Fprintf(o.ErrOut, "Listing annotations for %s.%s/%s:\n", gvks[0].Kind, gvks[0].Group, info.Name)
368+
}
369+
for k, v := range accessor.GetAnnotations() {
370+
fmt.Fprintf(o.Out, "%s%s=%s\n", indent, k, v)
371+
}
372+
373+
return nil
374+
}
375+
347376
return o.PrintObj(outputObj, o.Out)
348377
})
349378
}

0 commit comments

Comments
 (0)