taint.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. Copyright 2016 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. "fmt"
  16. "io"
  17. "strings"
  18. "encoding/json"
  19. "github.com/golang/glog"
  20. "github.com/renstrom/dedent"
  21. "github.com/spf13/cobra"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/meta"
  24. "k8s.io/kubernetes/pkg/kubectl"
  25. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  26. "k8s.io/kubernetes/pkg/kubectl/resource"
  27. "k8s.io/kubernetes/pkg/runtime"
  28. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  29. "k8s.io/kubernetes/pkg/util/sets"
  30. "k8s.io/kubernetes/pkg/util/strategicpatch"
  31. "k8s.io/kubernetes/pkg/util/validation"
  32. )
  33. // TaintOptions have the data required to perform the taint operation
  34. type TaintOptions struct {
  35. resources []string
  36. taintsToAdd []api.Taint
  37. taintsToRemove []api.Taint
  38. builder *resource.Builder
  39. selector string
  40. overwrite bool
  41. all bool
  42. f *cmdutil.Factory
  43. out io.Writer
  44. cmd *cobra.Command
  45. }
  46. var (
  47. taint_long = dedent.Dedent(`
  48. Update the taints on one or more nodes.
  49. A taint consists of a key, value, and effect. As an argument here, it is expressed as key=value:effect.
  50. The key must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to %[1]d characters.
  51. The value must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to %[1]d characters.
  52. The effect must be NoSchedule or PreferNoSchedule.
  53. Currently taint can only apply to node.`)
  54. taint_example = dedent.Dedent(`
  55. # Update node 'foo' with a taint with key 'dedicated' and value 'special-user' and effect 'NoSchedule'.
  56. # If a taint with that key and effect already exists, its value is replaced as specified.
  57. kubectl taint nodes foo dedicated=special-user:NoSchedule
  58. # Remove from node 'foo' the taint with key 'dedicated' and effect 'NoSchedule' if one exists.
  59. kubectl taint nodes foo dedicated:NoSchedule-
  60. # Remove from node 'foo' all the taints with key 'dedicated'
  61. kubectl taint nodes foo dedicated-`)
  62. )
  63. func NewCmdTaint(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  64. options := &TaintOptions{}
  65. validArgs := []string{"node"}
  66. argAliases := kubectl.ResourceAliases(validArgs)
  67. cmd := &cobra.Command{
  68. Use: "taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 ... KEY_N=VAL_N:TAINT_EFFECT_N",
  69. Short: "Update the taints on one or more nodes",
  70. Long: fmt.Sprintf(taint_long, validation.DNS1123SubdomainMaxLength, validation.LabelValueMaxLength),
  71. Example: taint_example,
  72. Run: func(cmd *cobra.Command, args []string) {
  73. if err := options.Complete(f, out, cmd, args); err != nil {
  74. cmdutil.CheckErr(err)
  75. }
  76. if err := options.Validate(args); err != nil {
  77. cmdutil.CheckErr(cmdutil.UsageError(cmd, err.Error()))
  78. }
  79. if err := options.RunTaint(); err != nil {
  80. cmdutil.CheckErr(err)
  81. }
  82. },
  83. ValidArgs: validArgs,
  84. ArgAliases: argAliases,
  85. }
  86. cmdutil.AddValidateFlags(cmd)
  87. cmdutil.AddPrinterFlags(cmd)
  88. cmdutil.AddInclude3rdPartyFlags(cmd)
  89. cmd.Flags().StringVarP(&options.selector, "selector", "l", "", "Selector (label query) to filter on")
  90. cmd.Flags().BoolVar(&options.overwrite, "overwrite", false, "If true, allow taints to be overwritten, otherwise reject taint updates that overwrite existing taints.")
  91. cmd.Flags().BoolVar(&options.all, "all", false, "select all nodes in the cluster")
  92. return cmd
  93. }
  94. func deleteTaint(taints []api.Taint, taintToDelete api.Taint) ([]api.Taint, error) {
  95. newTaints := []api.Taint{}
  96. found := false
  97. for _, taint := range taints {
  98. if taint.Key == taintToDelete.Key &&
  99. (len(taintToDelete.Effect) == 0 || taint.Effect == taintToDelete.Effect) {
  100. found = true
  101. continue
  102. }
  103. newTaints = append(newTaints, taint)
  104. }
  105. if !found {
  106. return nil, fmt.Errorf("taint key=\"%s\" and effect=\"%s\" not found.", taintToDelete.Key, taintToDelete.Effect)
  107. }
  108. return newTaints, nil
  109. }
  110. // reorganizeTaints returns the updated set of taints, taking into account old taints that were not updated,
  111. // old taints that were updated, old taints that were deleted, and new taints.
  112. func reorganizeTaints(accessor meta.Object, overwrite bool, taintsToAdd []api.Taint, taintsToRemove []api.Taint) ([]api.Taint, error) {
  113. newTaints := append([]api.Taint{}, taintsToAdd...)
  114. var oldTaints []api.Taint
  115. var err error
  116. annotations := accessor.GetAnnotations()
  117. if annotations != nil {
  118. if oldTaints, err = api.GetTaintsFromNodeAnnotations(annotations); err != nil {
  119. return nil, err
  120. }
  121. }
  122. // add taints that already existing but not updated to newTaints
  123. for _, oldTaint := range oldTaints {
  124. existsInNew := false
  125. for _, taint := range newTaints {
  126. if taint.Key == oldTaint.Key && taint.Effect == oldTaint.Effect {
  127. existsInNew = true
  128. break
  129. }
  130. }
  131. if !existsInNew {
  132. newTaints = append(newTaints, oldTaint)
  133. }
  134. }
  135. allErrs := []error{}
  136. for _, taintToRemove := range taintsToRemove {
  137. newTaints, err = deleteTaint(newTaints, taintToRemove)
  138. if err != nil {
  139. allErrs = append(allErrs, err)
  140. }
  141. }
  142. return newTaints, utilerrors.NewAggregate(allErrs)
  143. }
  144. func parseTaints(spec []string) ([]api.Taint, []api.Taint, error) {
  145. var taints, taintsToRemove []api.Taint
  146. uniqueTaints := map[api.TaintEffect]sets.String{}
  147. for _, taintSpec := range spec {
  148. if strings.Index(taintSpec, "=") != -1 && strings.Index(taintSpec, ":") != -1 {
  149. parts := strings.Split(taintSpec, "=")
  150. if len(parts) != 2 || len(parts[1]) == 0 || len(validation.IsQualifiedName(parts[0])) > 0 {
  151. return nil, nil, fmt.Errorf("invalid taint spec: %v", taintSpec)
  152. }
  153. parts2 := strings.Split(parts[1], ":")
  154. errs := validation.IsValidLabelValue(parts2[0])
  155. if len(parts2) != 2 || len(errs) != 0 {
  156. return nil, nil, fmt.Errorf("invalid taint spec: %v, %s", taintSpec, strings.Join(errs, "; "))
  157. }
  158. if parts2[1] != string(api.TaintEffectNoSchedule) && parts2[1] != string(api.TaintEffectPreferNoSchedule) {
  159. return nil, nil, fmt.Errorf("invalid taint spec: %v, unsupported taint effect", taintSpec)
  160. }
  161. effect := api.TaintEffect(parts2[1])
  162. newTaint := api.Taint{
  163. Key: parts[0],
  164. Value: parts2[0],
  165. Effect: effect,
  166. }
  167. // validate if taint is unique by <key, effect>
  168. if len(uniqueTaints[newTaint.Effect]) > 0 && uniqueTaints[newTaint.Effect].Has(newTaint.Key) {
  169. return nil, nil, fmt.Errorf("duplicated taints with the same key and effect: %v", newTaint)
  170. }
  171. // add taint to existingTaints for uniqueness check
  172. if len(uniqueTaints[newTaint.Effect]) == 0 {
  173. uniqueTaints[newTaint.Effect] = sets.String{}
  174. }
  175. uniqueTaints[newTaint.Effect].Insert(newTaint.Key)
  176. taints = append(taints, newTaint)
  177. } else if strings.HasSuffix(taintSpec, "-") {
  178. taintKey := taintSpec[:len(taintSpec)-1]
  179. var effect api.TaintEffect
  180. if strings.Index(taintKey, ":") != -1 {
  181. parts := strings.Split(taintKey, ":")
  182. taintKey = parts[0]
  183. effect = api.TaintEffect(parts[1])
  184. }
  185. taintsToRemove = append(taintsToRemove, api.Taint{Key: taintKey, Effect: effect})
  186. } else {
  187. return nil, nil, fmt.Errorf("unknown taint spec: %v", taintSpec)
  188. }
  189. }
  190. return taints, taintsToRemove, nil
  191. }
  192. // Complete adapts from the command line args and factory to the data required.
  193. func (o *TaintOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) {
  194. namespace, _, err := f.DefaultNamespace()
  195. if err != nil {
  196. return err
  197. }
  198. // retrieves resource and taint args from args
  199. // also checks args to verify that all resources are specified before taints
  200. taintArgs := []string{}
  201. metTaintArg := false
  202. for _, s := range args {
  203. isTaint := strings.Contains(s, "=") || strings.HasSuffix(s, "-")
  204. switch {
  205. case !metTaintArg && isTaint:
  206. metTaintArg = true
  207. fallthrough
  208. case metTaintArg && isTaint:
  209. taintArgs = append(taintArgs, s)
  210. case !metTaintArg && !isTaint:
  211. o.resources = append(o.resources, s)
  212. case metTaintArg && !isTaint:
  213. return fmt.Errorf("all resources must be specified before taint changes: %s", s)
  214. }
  215. }
  216. if len(o.resources) < 1 {
  217. return fmt.Errorf("one or more resources must be specified as <resource> <name>")
  218. }
  219. if len(taintArgs) < 1 {
  220. return fmt.Errorf("at least one taint update is required")
  221. }
  222. if o.taintsToAdd, o.taintsToRemove, err = parseTaints(taintArgs); err != nil {
  223. return cmdutil.UsageError(cmd, err.Error())
  224. }
  225. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  226. o.builder = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  227. ContinueOnError().
  228. NamespaceParam(namespace).DefaultNamespace()
  229. if o.all {
  230. o.builder = o.builder.SelectAllParam(o.all).ResourceTypes("node")
  231. } else {
  232. if len(o.resources) < 2 {
  233. return fmt.Errorf("at least one resource name must be specified since 'all' parameter is not set")
  234. }
  235. o.builder = o.builder.ResourceNames("node", o.resources[1:]...)
  236. }
  237. o.builder = o.builder.SelectorParam(o.selector).
  238. Flatten().
  239. Latest()
  240. o.f = f
  241. o.out = out
  242. o.cmd = cmd
  243. return nil
  244. }
  245. // Validate checks to the TaintOptions to see if there is sufficient information run the command.
  246. func (o TaintOptions) Validate(args []string) error {
  247. resourceType := strings.ToLower(o.resources[0])
  248. validResources, isValidResource := append(kubectl.ResourceAliases([]string{"node"}), "node"), false
  249. for _, validResource := range validResources {
  250. if resourceType == validResource {
  251. isValidResource = true
  252. break
  253. }
  254. }
  255. if !isValidResource {
  256. return fmt.Errorf("invalid resource type %s, only %q are supported", o.resources[0], validResources)
  257. }
  258. // check the format of taint args and checks removed taints aren't in the new taints list
  259. var conflictTaints []string
  260. for _, taintAdd := range o.taintsToAdd {
  261. for _, taintRemove := range o.taintsToRemove {
  262. if taintAdd.Key != taintRemove.Key {
  263. continue
  264. }
  265. if len(taintRemove.Effect) == 0 || taintAdd.Effect == taintRemove.Effect {
  266. conflictTaint := fmt.Sprintf("{\"%s\":\"%s\"}", taintRemove.Key, taintRemove.Effect)
  267. conflictTaints = append(conflictTaints, conflictTaint)
  268. }
  269. }
  270. }
  271. if len(conflictTaints) > 0 {
  272. return fmt.Errorf("can not both modify and remove the following taint(s) in the same command: %s", strings.Join(conflictTaints, ", "))
  273. }
  274. return nil
  275. }
  276. // RunTaint does the work
  277. func (o TaintOptions) RunTaint() error {
  278. r := o.builder.Do()
  279. if err := r.Err(); err != nil {
  280. return err
  281. }
  282. return r.Visit(func(info *resource.Info, err error) error {
  283. if err != nil {
  284. return err
  285. }
  286. obj, err := info.Mapping.ConvertToVersion(info.Object, info.Mapping.GroupVersionKind.GroupVersion())
  287. if err != nil {
  288. return err
  289. }
  290. name, namespace := info.Name, info.Namespace
  291. oldData, err := json.Marshal(obj)
  292. if err != nil {
  293. return err
  294. }
  295. if err := o.updateTaints(obj); err != nil {
  296. return err
  297. }
  298. newData, err := json.Marshal(obj)
  299. if err != nil {
  300. return err
  301. }
  302. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
  303. createdPatch := err == nil
  304. if err != nil {
  305. glog.V(2).Infof("couldn't compute patch: %v", err)
  306. }
  307. mapping := info.ResourceMapping()
  308. client, err := o.f.ClientForMapping(mapping)
  309. if err != nil {
  310. return err
  311. }
  312. helper := resource.NewHelper(client, mapping)
  313. var outputObj runtime.Object
  314. if createdPatch {
  315. outputObj, err = helper.Patch(namespace, name, api.StrategicMergePatchType, patchBytes)
  316. } else {
  317. outputObj, err = helper.Replace(namespace, name, false, obj)
  318. }
  319. if err != nil {
  320. return err
  321. }
  322. mapper, _ := o.f.Object(cmdutil.GetIncludeThirdPartyAPIs(o.cmd))
  323. outputFormat := cmdutil.GetFlagString(o.cmd, "output")
  324. if outputFormat != "" {
  325. return o.f.PrintObject(o.cmd, mapper, outputObj, o.out)
  326. }
  327. cmdutil.PrintSuccess(mapper, false, o.out, info.Mapping.Resource, info.Name, "tainted")
  328. return nil
  329. })
  330. }
  331. // validateNoTaintOverwrites validates that when overwrite is false, to-be-updated taints don't exist in the node taint list (yet)
  332. func validateNoTaintOverwrites(accessor meta.Object, taints []api.Taint) error {
  333. annotations := accessor.GetAnnotations()
  334. if annotations == nil {
  335. return nil
  336. }
  337. allErrs := []error{}
  338. oldTaints, err := api.GetTaintsFromNodeAnnotations(annotations)
  339. if err != nil {
  340. allErrs = append(allErrs, err)
  341. return utilerrors.NewAggregate(allErrs)
  342. }
  343. for _, taint := range taints {
  344. for _, oldTaint := range oldTaints {
  345. if taint.Key == oldTaint.Key && taint.Effect == oldTaint.Effect {
  346. allErrs = append(allErrs, fmt.Errorf("Node '%s' already has a taint with key (%s) and effect (%v), and --overwrite is false", accessor.GetName(), taint.Key, taint.Effect))
  347. break
  348. }
  349. }
  350. }
  351. return utilerrors.NewAggregate(allErrs)
  352. }
  353. // updateTaints updates taints of obj
  354. func (o TaintOptions) updateTaints(obj runtime.Object) error {
  355. accessor, err := meta.Accessor(obj)
  356. if err != nil {
  357. return err
  358. }
  359. if !o.overwrite {
  360. if err := validateNoTaintOverwrites(accessor, o.taintsToAdd); err != nil {
  361. return err
  362. }
  363. }
  364. annotations := accessor.GetAnnotations()
  365. if annotations == nil {
  366. annotations = make(map[string]string)
  367. }
  368. newTaints, err := reorganizeTaints(accessor, o.overwrite, o.taintsToAdd, o.taintsToRemove)
  369. if err != nil {
  370. return err
  371. }
  372. taintsData, err := json.Marshal(newTaints)
  373. if err != nil {
  374. return err
  375. }
  376. annotations[api.TaintsAnnotationKey] = string(taintsData)
  377. accessor.SetAnnotations(annotations)
  378. return nil
  379. }