forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admission.go
104 lines (88 loc) · 2.92 KB
/
admission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nodetaint
import (
"fmt"
"io"
"k8s.io/apiserver/pkg/admission"
utilfeature "k8s.io/apiserver/pkg/util/feature"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/features"
)
const (
// PluginName is the name of the plugin.
PluginName = "TaintNodesByCondition"
// TaintNodeNotReady is the not-ready label as specified in the API.
TaintNodeNotReady = "node.kubernetes.io/not-ready"
)
// Register registers a plugin
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return NewPlugin(), nil
})
}
// NewPlugin creates a new NodeTaint admission plugin.
// This plugin identifies requests from nodes
func NewPlugin() *Plugin {
return &Plugin{
Handler: admission.NewHandler(admission.Create),
features: utilfeature.DefaultFeatureGate,
}
}
// Plugin holds state for and implements the admission plugin.
type Plugin struct {
*admission.Handler
// allows overriding for testing
features utilfeature.FeatureGate
}
var (
_ = admission.Interface(&Plugin{})
)
var (
nodeResource = api.Resource("nodes")
)
// Admit is the main function that checks node identity and adds taints as needed.
func (p *Plugin) Admit(a admission.Attributes) error {
// If TaintNodesByCondition is not enabled, we don't need to do anything.
if !p.features.Enabled(features.TaintNodesByCondition) {
return nil
}
// Our job is just to taint nodes.
if a.GetResource().GroupResource() != nodeResource || a.GetSubresource() != "" {
return nil
}
node, ok := a.GetObject().(*api.Node)
if !ok {
return admission.NewForbidden(a, fmt.Errorf("unexpected type %T", a.GetObject()))
}
// Taint node with NotReady taint at creation if TaintNodesByCondition is
// enabled. This is needed to make sure that nodes are added to the cluster
// with the NotReady taint. Otherwise, a new node may receive the taint with
// some delay causing pods to be scheduled on a not-ready node.
// Node controller will remove the taint when the node becomes ready.
addNotReadyTaint(node)
return nil
}
func addNotReadyTaint(node *api.Node) {
notReadyTaint := api.Taint{
Key: TaintNodeNotReady,
Effect: api.TaintEffectNoSchedule,
}
for _, taint := range node.Spec.Taints {
if taint.MatchTaint(notReadyTaint) {
// the taint already exists.
return
}
}
node.Spec.Taints = append(node.Spec.Taints, notReadyTaint)
}