...
1
16
17 package nodetaint
18
19 import (
20 "context"
21 "fmt"
22 "io"
23
24 v1 "k8s.io/api/core/v1"
25 "k8s.io/apiserver/pkg/admission"
26 api "k8s.io/kubernetes/pkg/apis/core"
27 )
28
29 const (
30
31 PluginName = "TaintNodesByCondition"
32 )
33
34
35 func Register(plugins *admission.Plugins) {
36 plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
37 return NewPlugin(), nil
38 })
39 }
40
41
42
43 func NewPlugin() *Plugin {
44 return &Plugin{
45 Handler: admission.NewHandler(admission.Create),
46 }
47 }
48
49
50 type Plugin struct {
51 *admission.Handler
52 }
53
54 var (
55 _ = admission.Interface(&Plugin{})
56 )
57
58 var (
59 nodeResource = api.Resource("nodes")
60 )
61
62
63 func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
64
65 if a.GetResource().GroupResource() != nodeResource || a.GetSubresource() != "" {
66 return nil
67 }
68
69 node, ok := a.GetObject().(*api.Node)
70 if !ok {
71 return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
72 }
73
74
75
76
77
78
79 addNotReadyTaint(node)
80 return nil
81 }
82
83 func addNotReadyTaint(node *api.Node) {
84 notReadyTaint := api.Taint{
85 Key: v1.TaintNodeNotReady,
86 Effect: api.TaintEffectNoSchedule,
87 }
88 for _, taint := range node.Spec.Taints {
89 if taint.MatchTaint(notReadyTaint) {
90
91 return
92 }
93 }
94 node.Spec.Taints = append(node.Spec.Taints, notReadyTaint)
95 }
96
View as plain text