label.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 cmd
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io"
  18. "reflect"
  19. "strings"
  20. "github.com/golang/glog"
  21. "github.com/renstrom/dedent"
  22. "github.com/spf13/cobra"
  23. "k8s.io/kubernetes/pkg/api"
  24. "k8s.io/kubernetes/pkg/api/meta"
  25. "k8s.io/kubernetes/pkg/kubectl"
  26. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  27. "k8s.io/kubernetes/pkg/kubectl/resource"
  28. "k8s.io/kubernetes/pkg/runtime"
  29. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  30. "k8s.io/kubernetes/pkg/util/strategicpatch"
  31. "k8s.io/kubernetes/pkg/util/validation"
  32. )
  33. // LabelOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  34. // referencing the cmd.Flags()
  35. type LabelOptions struct {
  36. Filenames []string
  37. Recursive bool
  38. }
  39. var (
  40. label_long = dedent.Dedent(`
  41. Update the labels on a resource.
  42. A label must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to %[1]d characters.
  43. If --overwrite is true, then existing labels can be overwritten, otherwise attempting to overwrite a label will result in an error.
  44. If --resource-version is specified, then updates will use this resource version, otherwise the existing resource-version will be used.`)
  45. label_example = dedent.Dedent(`
  46. # Update pod 'foo' with the label 'unhealthy' and the value 'true'.
  47. kubectl label pods foo unhealthy=true
  48. # Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.
  49. kubectl label --overwrite pods foo status=unhealthy
  50. # Update all pods in the namespace
  51. kubectl label pods --all status=unhealthy
  52. # Update a pod identified by the type and name in "pod.json"
  53. kubectl label -f pod.json status=unhealthy
  54. # Update pod 'foo' only if the resource is unchanged from version 1.
  55. kubectl label pods foo status=unhealthy --resource-version=1
  56. # Update pod 'foo' by removing a label named 'bar' if it exists.
  57. # Does not require the --overwrite flag.
  58. kubectl label pods foo bar-`)
  59. )
  60. func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  61. options := &LabelOptions{}
  62. // retrieve a list of handled resources from printer as valid args
  63. validArgs, argAliases := []string{}, []string{}
  64. p, err := f.Printer(nil, kubectl.PrintOptions{
  65. ColumnLabels: []string{},
  66. })
  67. cmdutil.CheckErr(err)
  68. if p != nil {
  69. validArgs = p.HandledResources()
  70. argAliases = kubectl.ResourceAliases(validArgs)
  71. }
  72. cmd := &cobra.Command{
  73. Use: "label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]",
  74. Short: "Update the labels on a resource",
  75. Long: fmt.Sprintf(label_long, validation.LabelValueMaxLength),
  76. Example: label_example,
  77. Run: func(cmd *cobra.Command, args []string) {
  78. err := RunLabel(f, out, cmd, args, options)
  79. cmdutil.CheckErr(err)
  80. },
  81. ValidArgs: validArgs,
  82. ArgAliases: argAliases,
  83. }
  84. cmdutil.AddPrinterFlags(cmd)
  85. cmd.Flags().Bool("overwrite", false, "If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.")
  86. cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
  87. cmd.Flags().Bool("all", false, "select all resources in the namespace of the specified resource types")
  88. cmd.Flags().String("resource-version", "", "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.")
  89. usage := "Filename, directory, or URL to a file identifying the resource to update the labels"
  90. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  91. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  92. cmdutil.AddDryRunFlag(cmd)
  93. cmdutil.AddRecordFlag(cmd)
  94. cmdutil.AddInclude3rdPartyFlags(cmd)
  95. return cmd
  96. }
  97. func validateNoOverwrites(accessor meta.Object, labels map[string]string) error {
  98. allErrs := []error{}
  99. for key := range labels {
  100. if value, found := accessor.GetLabels()[key]; found {
  101. allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value))
  102. }
  103. }
  104. return utilerrors.NewAggregate(allErrs)
  105. }
  106. func parseLabels(spec []string) (map[string]string, []string, error) {
  107. labels := map[string]string{}
  108. var remove []string
  109. for _, labelSpec := range spec {
  110. if strings.Index(labelSpec, "=") != -1 {
  111. parts := strings.Split(labelSpec, "=")
  112. if len(parts) != 2 || len(parts[1]) == 0 {
  113. return nil, nil, fmt.Errorf("invalid label spec: %v", labelSpec)
  114. }
  115. if errs := validation.IsValidLabelValue(parts[1]); len(errs) != 0 {
  116. return nil, nil, fmt.Errorf("invalid label value: %q: %s", labelSpec, strings.Join(errs, ";"))
  117. }
  118. labels[parts[0]] = parts[1]
  119. } else if strings.HasSuffix(labelSpec, "-") {
  120. remove = append(remove, labelSpec[:len(labelSpec)-1])
  121. } else {
  122. return nil, nil, fmt.Errorf("unknown label spec: %v", labelSpec)
  123. }
  124. }
  125. for _, removeLabel := range remove {
  126. if _, found := labels[removeLabel]; found {
  127. return nil, nil, fmt.Errorf("can not both modify and remove a label in the same command")
  128. }
  129. }
  130. return labels, remove, nil
  131. }
  132. func labelFunc(obj runtime.Object, overwrite bool, resourceVersion string, labels map[string]string, remove []string) error {
  133. accessor, err := meta.Accessor(obj)
  134. if err != nil {
  135. return err
  136. }
  137. if !overwrite {
  138. if err := validateNoOverwrites(accessor, labels); err != nil {
  139. return err
  140. }
  141. }
  142. objLabels := accessor.GetLabels()
  143. if objLabels == nil {
  144. objLabels = make(map[string]string)
  145. }
  146. for key, value := range labels {
  147. objLabels[key] = value
  148. }
  149. for _, label := range remove {
  150. delete(objLabels, label)
  151. }
  152. accessor.SetLabels(objLabels)
  153. if len(resourceVersion) != 0 {
  154. accessor.SetResourceVersion(resourceVersion)
  155. }
  156. return nil
  157. }
  158. func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *LabelOptions) error {
  159. resources, labelArgs, err := cmdutil.GetResourcesAndPairs(args, "label")
  160. if err != nil {
  161. return err
  162. }
  163. if len(resources) < 1 && len(options.Filenames) == 0 {
  164. return cmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
  165. }
  166. if len(labelArgs) < 1 {
  167. return cmdutil.UsageError(cmd, "at least one label update is required")
  168. }
  169. selector := cmdutil.GetFlagString(cmd, "selector")
  170. all := cmdutil.GetFlagBool(cmd, "all")
  171. overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
  172. resourceVersion := cmdutil.GetFlagString(cmd, "resource-version")
  173. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  174. if err != nil {
  175. return err
  176. }
  177. lbls, remove, err := parseLabels(labelArgs)
  178. if err != nil {
  179. return cmdutil.UsageError(cmd, err.Error())
  180. }
  181. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  182. b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  183. ContinueOnError().
  184. NamespaceParam(cmdNamespace).DefaultNamespace().
  185. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  186. SelectorParam(selector).
  187. ResourceTypeOrNameArgs(all, resources...).
  188. Flatten().
  189. Latest()
  190. one := false
  191. r := b.Do().IntoSingular(&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(resourceVersion) > 0 {
  197. return cmdutil.UsageError(cmd, "--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. dataChangeMsg := "not labeled"
  206. if cmdutil.GetDryRunFlag(cmd) {
  207. err = labelFunc(info.Object, overwrite, resourceVersion, lbls, remove)
  208. if err != nil {
  209. return err
  210. }
  211. outputObj = info.Object
  212. } else {
  213. obj, err := cmdutil.MaybeConvertObject(info.Object, info.Mapping.GroupVersionKind.GroupVersion(), info.Mapping)
  214. if err != nil {
  215. return err
  216. }
  217. name, namespace := info.Name, info.Namespace
  218. oldData, err := json.Marshal(obj)
  219. if err != nil {
  220. return err
  221. }
  222. accessor, err := meta.Accessor(obj)
  223. if err != nil {
  224. return err
  225. }
  226. for _, label := range remove {
  227. if _, ok := accessor.GetLabels()[label]; !ok {
  228. fmt.Fprintf(out, "label %q not found.\n", label)
  229. }
  230. }
  231. if err := labelFunc(obj, overwrite, resourceVersion, lbls, remove); err != nil {
  232. return err
  233. }
  234. if cmdutil.ShouldRecord(cmd, info) {
  235. if err := cmdutil.RecordChangeCause(obj, f.Command()); err != nil {
  236. return err
  237. }
  238. }
  239. newData, err := json.Marshal(obj)
  240. if err != nil {
  241. return err
  242. }
  243. if !reflect.DeepEqual(oldData, newData) {
  244. dataChangeMsg = "labeled"
  245. }
  246. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
  247. createdPatch := err == nil
  248. if err != nil {
  249. glog.V(2).Infof("couldn't compute patch: %v", err)
  250. }
  251. mapping := info.ResourceMapping()
  252. client, err := f.ClientForMapping(mapping)
  253. if err != nil {
  254. return err
  255. }
  256. helper := resource.NewHelper(client, mapping)
  257. if createdPatch {
  258. outputObj, err = helper.Patch(namespace, name, api.StrategicMergePatchType, patchBytes)
  259. } else {
  260. outputObj, err = helper.Replace(namespace, name, false, obj)
  261. }
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. outputFormat := cmdutil.GetFlagString(cmd, "output")
  267. if outputFormat != "" {
  268. return f.PrintObject(cmd, mapper, outputObj, out)
  269. }
  270. cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, dataChangeMsg)
  271. return nil
  272. })
  273. }