admission.go 2.5 KB

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