label.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. Copyright 2014 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 label
  14. import (
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. jsonpatch "github.com/evanphx/json-patch"
  19. "github.com/spf13/cobra"
  20. "k8s.io/klog"
  21. "k8s.io/apimachinery/pkg/api/meta"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. "k8s.io/apimachinery/pkg/types"
  26. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  27. "k8s.io/apimachinery/pkg/util/json"
  28. "k8s.io/apimachinery/pkg/util/validation"
  29. "k8s.io/cli-runtime/pkg/genericclioptions"
  30. "k8s.io/cli-runtime/pkg/printers"
  31. "k8s.io/cli-runtime/pkg/resource"
  32. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  33. "k8s.io/kubernetes/pkg/kubectl/scheme"
  34. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  35. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  36. )
  37. // LabelOptions have the data required to perform the label operation
  38. type LabelOptions struct {
  39. // Filename options
  40. resource.FilenameOptions
  41. RecordFlags *genericclioptions.RecordFlags
  42. PrintFlags *genericclioptions.PrintFlags
  43. ToPrinter func(string) (printers.ResourcePrinter, error)
  44. // Common user flags
  45. overwrite bool
  46. list bool
  47. local bool
  48. dryrun bool
  49. all bool
  50. resourceVersion string
  51. selector string
  52. fieldSelector string
  53. outputFormat string
  54. // results of arg parsing
  55. resources []string
  56. newLabels map[string]string
  57. removeLabels []string
  58. Recorder genericclioptions.Recorder
  59. namespace string
  60. enforceNamespace bool
  61. builder *resource.Builder
  62. unstructuredClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
  63. // Common shared fields
  64. genericclioptions.IOStreams
  65. }
  66. var (
  67. labelLong = templates.LongDesc(i18n.T(`
  68. Update the labels on a resource.
  69. * A label key and value must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to %[1]d characters each.
  70. * Optionally, the key can begin with a DNS subdomain prefix and a single '/', like example.com/my-app
  71. * If --overwrite is true, then existing labels can be overwritten, otherwise attempting to overwrite a label will result in an error.
  72. * If --resource-version is specified, then updates will use this resource version, otherwise the existing resource-version will be used.`))
  73. labelExample = templates.Examples(i18n.T(`
  74. # Update pod 'foo' with the label 'unhealthy' and the value 'true'.
  75. kubectl label pods foo unhealthy=true
  76. # Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.
  77. kubectl label --overwrite pods foo status=unhealthy
  78. # Update all pods in the namespace
  79. kubectl label pods --all status=unhealthy
  80. # Update a pod identified by the type and name in "pod.json"
  81. kubectl label -f pod.json status=unhealthy
  82. # Update pod 'foo' only if the resource is unchanged from version 1.
  83. kubectl label pods foo status=unhealthy --resource-version=1
  84. # Update pod 'foo' by removing a label named 'bar' if it exists.
  85. # Does not require the --overwrite flag.
  86. kubectl label pods foo bar-`))
  87. )
  88. func NewLabelOptions(ioStreams genericclioptions.IOStreams) *LabelOptions {
  89. return &LabelOptions{
  90. RecordFlags: genericclioptions.NewRecordFlags(),
  91. Recorder: genericclioptions.NoopRecorder{},
  92. PrintFlags: genericclioptions.NewPrintFlags("labeled").WithTypeSetter(scheme.Scheme),
  93. IOStreams: ioStreams,
  94. }
  95. }
  96. func NewCmdLabel(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  97. o := NewLabelOptions(ioStreams)
  98. cmd := &cobra.Command{
  99. Use: "label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]",
  100. DisableFlagsInUseLine: true,
  101. Short: i18n.T("Update the labels on a resource"),
  102. Long: fmt.Sprintf(labelLong, validation.LabelValueMaxLength),
  103. Example: labelExample,
  104. Run: func(cmd *cobra.Command, args []string) {
  105. cmdutil.CheckErr(o.Complete(f, cmd, args))
  106. cmdutil.CheckErr(o.Validate())
  107. cmdutil.CheckErr(o.RunLabel())
  108. },
  109. }
  110. o.RecordFlags.AddFlags(cmd)
  111. o.PrintFlags.AddFlags(cmd)
  112. cmd.Flags().BoolVar(&o.overwrite, "overwrite", o.overwrite, "If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.")
  113. cmd.Flags().BoolVar(&o.list, "list", o.list, "If true, display the labels for a given resource.")
  114. cmd.Flags().BoolVar(&o.local, "local", o.local, "If true, label will NOT contact api-server but run locally.")
  115. 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).")
  116. 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.")
  117. cmd.Flags().BoolVar(&o.all, "all", o.all, "Select all resources, including uninitialized ones, in the namespace of the specified resource types")
  118. cmd.Flags().StringVar(&o.resourceVersion, "resource-version", o.resourceVersion, i18n.T("If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource."))
  119. usage := "identifying the resource to update the labels"
  120. cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
  121. cmdutil.AddDryRunFlag(cmd)
  122. cmdutil.AddIncludeUninitializedFlag(cmd)
  123. return cmd
  124. }
  125. // Complete adapts from the command line args and factory to the data required.
  126. func (o *LabelOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  127. var err error
  128. o.RecordFlags.Complete(cmd)
  129. o.Recorder, err = o.RecordFlags.ToRecorder()
  130. if err != nil {
  131. return err
  132. }
  133. o.outputFormat = cmdutil.GetFlagString(cmd, "output")
  134. o.dryrun = cmdutil.GetDryRunFlag(cmd)
  135. o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
  136. o.PrintFlags.NamePrintFlags.Operation = operation
  137. if o.dryrun {
  138. o.PrintFlags.Complete("%s (dry run)")
  139. }
  140. return o.PrintFlags.ToPrinter()
  141. }
  142. resources, labelArgs, err := cmdutil.GetResourcesAndPairs(args, "label")
  143. if err != nil {
  144. return err
  145. }
  146. o.resources = resources
  147. o.newLabels, o.removeLabels, err = parseLabels(labelArgs)
  148. if o.list && len(o.outputFormat) > 0 {
  149. return fmt.Errorf("--list and --output may not be specified together")
  150. }
  151. o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
  152. if err != nil {
  153. return err
  154. }
  155. o.builder = f.NewBuilder()
  156. o.unstructuredClientForMapping = f.UnstructuredClientForMapping
  157. return nil
  158. }
  159. // Validate checks to the LabelOptions to see if there is sufficient information run the command.
  160. func (o *LabelOptions) Validate() error {
  161. if o.all && len(o.selector) > 0 {
  162. return fmt.Errorf("cannot set --all and --selector at the same time")
  163. }
  164. if o.all && len(o.fieldSelector) > 0 {
  165. return fmt.Errorf("cannot set --all and --field-selector at the same time")
  166. }
  167. if len(o.resources) < 1 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
  168. return fmt.Errorf("one or more resources must be specified as <resource> <name> or <resource>/<name>")
  169. }
  170. if len(o.newLabels) < 1 && len(o.removeLabels) < 1 && !o.list {
  171. return fmt.Errorf("at least one label update is required")
  172. }
  173. return nil
  174. }
  175. // RunLabel does the work
  176. func (o *LabelOptions) RunLabel() error {
  177. b := o.builder.
  178. Unstructured().
  179. LocalParam(o.local).
  180. ContinueOnError().
  181. NamespaceParam(o.namespace).DefaultNamespace().
  182. FilenameParam(o.enforceNamespace, &o.FilenameOptions).
  183. Flatten()
  184. if !o.local {
  185. b = b.LabelSelectorParam(o.selector).
  186. FieldSelectorParam(o.fieldSelector).
  187. ResourceTypeOrNameArgs(o.all, o.resources...).
  188. Latest()
  189. }
  190. one := false
  191. r := b.Do().IntoSingleItemImplied(&one)
  192. if err := r.Err(); err != nil {
  193. return err
  194. }
  195. // only apply resource version locking on a single resource
  196. if !one && len(o.resourceVersion) > 0 {
  197. return fmt.Errorf("--resource-version may only be used with a single resource")
  198. }
  199. // TODO: support bulk generic output a la Get
  200. return r.Visit(func(info *resource.Info, err error) error {
  201. if err != nil {
  202. return err
  203. }
  204. var outputObj runtime.Object
  205. var dataChangeMsg string
  206. obj := info.Object
  207. oldData, err := json.Marshal(obj)
  208. if err != nil {
  209. return err
  210. }
  211. if o.dryrun || o.local || o.list {
  212. err = labelFunc(obj, o.overwrite, o.resourceVersion, o.newLabels, o.removeLabels)
  213. if err != nil {
  214. return err
  215. }
  216. newObj, err := json.Marshal(obj)
  217. if err != nil {
  218. return err
  219. }
  220. dataChangeMsg = updateDataChangeMsg(oldData, newObj)
  221. outputObj = info.Object
  222. } else {
  223. name, namespace := info.Name, info.Namespace
  224. if err != nil {
  225. return err
  226. }
  227. accessor, err := meta.Accessor(obj)
  228. if err != nil {
  229. return err
  230. }
  231. for _, label := range o.removeLabels {
  232. if _, ok := accessor.GetLabels()[label]; !ok {
  233. fmt.Fprintf(o.Out, "label %q not found.\n", label)
  234. }
  235. }
  236. if err := labelFunc(obj, o.overwrite, o.resourceVersion, o.newLabels, o.removeLabels); err != nil {
  237. return err
  238. }
  239. if err := o.Recorder.Record(obj); err != nil {
  240. klog.V(4).Infof("error recording current command: %v", err)
  241. }
  242. newObj, err := json.Marshal(obj)
  243. if err != nil {
  244. return err
  245. }
  246. dataChangeMsg = updateDataChangeMsg(oldData, newObj)
  247. patchBytes, err := jsonpatch.CreateMergePatch(oldData, newObj)
  248. createdPatch := err == nil
  249. if err != nil {
  250. klog.V(2).Infof("couldn't compute patch: %v", err)
  251. }
  252. mapping := info.ResourceMapping()
  253. client, err := o.unstructuredClientForMapping(mapping)
  254. if err != nil {
  255. return err
  256. }
  257. helper := resource.NewHelper(client, mapping)
  258. if createdPatch {
  259. outputObj, err = helper.Patch(namespace, name, types.MergePatchType, patchBytes, nil)
  260. } else {
  261. outputObj, err = helper.Replace(namespace, name, false, obj)
  262. }
  263. if err != nil {
  264. return err
  265. }
  266. }
  267. if o.list {
  268. accessor, err := meta.Accessor(outputObj)
  269. if err != nil {
  270. return err
  271. }
  272. indent := ""
  273. if !one {
  274. indent = " "
  275. gvks, _, err := unstructuredscheme.NewUnstructuredObjectTyper().ObjectKinds(info.Object)
  276. if err != nil {
  277. return err
  278. }
  279. fmt.Fprintf(o.ErrOut, "Listing labels for %s.%s/%s:\n", gvks[0].Kind, gvks[0].Group, info.Name)
  280. }
  281. for k, v := range accessor.GetLabels() {
  282. fmt.Fprintf(o.Out, "%s%s=%s\n", indent, k, v)
  283. }
  284. return nil
  285. }
  286. printer, err := o.ToPrinter(dataChangeMsg)
  287. if err != nil {
  288. return err
  289. }
  290. return printer.PrintObj(info.Object, o.Out)
  291. })
  292. }
  293. func updateDataChangeMsg(oldObj []byte, newObj []byte) string {
  294. msg := "not labeled"
  295. if !reflect.DeepEqual(oldObj, newObj) {
  296. msg = "labeled"
  297. }
  298. return msg
  299. }
  300. func validateNoOverwrites(accessor metav1.Object, labels map[string]string) error {
  301. allErrs := []error{}
  302. for key := range labels {
  303. if value, found := accessor.GetLabels()[key]; found {
  304. allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value))
  305. }
  306. }
  307. return utilerrors.NewAggregate(allErrs)
  308. }
  309. func parseLabels(spec []string) (map[string]string, []string, error) {
  310. labels := map[string]string{}
  311. var remove []string
  312. for _, labelSpec := range spec {
  313. if strings.Contains(labelSpec, "=") {
  314. parts := strings.Split(labelSpec, "=")
  315. if len(parts) != 2 {
  316. return nil, nil, fmt.Errorf("invalid label spec: %v", labelSpec)
  317. }
  318. if errs := validation.IsValidLabelValue(parts[1]); len(errs) != 0 {
  319. return nil, nil, fmt.Errorf("invalid label value: %q: %s", labelSpec, strings.Join(errs, ";"))
  320. }
  321. labels[parts[0]] = parts[1]
  322. } else if strings.HasSuffix(labelSpec, "-") {
  323. remove = append(remove, labelSpec[:len(labelSpec)-1])
  324. } else {
  325. return nil, nil, fmt.Errorf("unknown label spec: %v", labelSpec)
  326. }
  327. }
  328. for _, removeLabel := range remove {
  329. if _, found := labels[removeLabel]; found {
  330. return nil, nil, fmt.Errorf("can not both modify and remove a label in the same command")
  331. }
  332. }
  333. return labels, remove, nil
  334. }
  335. func labelFunc(obj runtime.Object, overwrite bool, resourceVersion string, labels map[string]string, remove []string) error {
  336. accessor, err := meta.Accessor(obj)
  337. if err != nil {
  338. return err
  339. }
  340. if !overwrite {
  341. if err := validateNoOverwrites(accessor, labels); err != nil {
  342. return err
  343. }
  344. }
  345. objLabels := accessor.GetLabels()
  346. if objLabels == nil {
  347. objLabels = make(map[string]string)
  348. }
  349. for key, value := range labels {
  350. objLabels[key] = value
  351. }
  352. for _, label := range remove {
  353. delete(objLabels, label)
  354. }
  355. accessor.SetLabels(objLabels)
  356. if len(resourceVersion) != 0 {
  357. accessor.SetResourceVersion(resourceVersion)
  358. }
  359. return nil
  360. }