admission.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2019 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 nodetaint
  14. import (
  15. "fmt"
  16. "io"
  17. "k8s.io/apiserver/pkg/admission"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. "k8s.io/component-base/featuregate"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/features"
  22. )
  23. const (
  24. // PluginName is the name of the plugin.
  25. PluginName = "TaintNodesByCondition"
  26. // TaintNodeNotReady is the not-ready label as specified in the API.
  27. TaintNodeNotReady = "node.kubernetes.io/not-ready"
  28. )
  29. // Register registers a plugin
  30. func Register(plugins *admission.Plugins) {
  31. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  32. return NewPlugin(), nil
  33. })
  34. }
  35. // NewPlugin creates a new NodeTaint admission plugin.
  36. // This plugin identifies requests from nodes
  37. func NewPlugin() *Plugin {
  38. return &Plugin{
  39. Handler: admission.NewHandler(admission.Create),
  40. features: utilfeature.DefaultFeatureGate,
  41. }
  42. }
  43. // Plugin holds state for and implements the admission plugin.
  44. type Plugin struct {
  45. *admission.Handler
  46. // allows overriding for testing
  47. features featuregate.FeatureGate
  48. }
  49. var (
  50. _ = admission.Interface(&Plugin{})
  51. )
  52. var (
  53. nodeResource = api.Resource("nodes")
  54. )
  55. // Admit is the main function that checks node identity and adds taints as needed.
  56. func (p *Plugin) Admit(a admission.Attributes, o admission.ObjectInterfaces) error {
  57. // If TaintNodesByCondition is not enabled, we don't need to do anything.
  58. if !p.features.Enabled(features.TaintNodesByCondition) {
  59. return nil
  60. }
  61. // Our job is just to taint nodes.
  62. if a.GetResource().GroupResource() != nodeResource || a.GetSubresource() != "" {
  63. return nil
  64. }
  65. node, ok := a.GetObject().(*api.Node)
  66. if !ok {
  67. return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
  68. }
  69. // Taint node with NotReady taint at creation if TaintNodesByCondition is
  70. // enabled. This is needed to make sure that nodes are added to the cluster
  71. // with the NotReady taint. Otherwise, a new node may receive the taint with
  72. // some delay causing pods to be scheduled on a not-ready node.
  73. // Node controller will remove the taint when the node becomes ready.
  74. addNotReadyTaint(node)
  75. return nil
  76. }
  77. func addNotReadyTaint(node *api.Node) {
  78. notReadyTaint := api.Taint{
  79. Key: TaintNodeNotReady,
  80. Effect: api.TaintEffectNoSchedule,
  81. }
  82. for _, taint := range node.Spec.Taints {
  83. if taint.MatchTaint(notReadyTaint) {
  84. // the taint already exists.
  85. return
  86. }
  87. }
  88. node.Spec.Taints = append(node.Spec.Taints, notReadyTaint)
  89. }