Skip to content

Commit

Permalink
Make nodeNotReady taint name configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar committed Jun 13, 2018
1 parent 6441bff commit 3fd1e11
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
54 changes: 25 additions & 29 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import (
)

const (
// TaintNodeNotReadyWorkload defines a taint key indicating that a node
// is not ready to receive workloads.
// This taint should be set on all nodes at startup and be removed by
// the kube-node-ready-controller once the required system pods are
// running on the node.
TaintNodeNotReadyWorkload = "node.alpha.kubernetes.io/notReady-workload"
// ConfigMapSelectorsKey defines the key name of the config map where
// the pod selector definition is defined.
ConfigMapSelectorsKey = "pod_selectors"
Expand All @@ -31,17 +25,18 @@ const (
// resources defined by selectors.
type NodeController struct {
kubernetes.Interface
selectors []*PodSelector
ignoreNodeLabels map[string]string
interval time.Duration
configMap string
namespace string
nodeReadyHooks []Hook
nodeStartUpObeserver NodeStartUpObeserver
selectors []*PodSelector
ignoreNodeLabels map[string]string
interval time.Duration
configMap string
namespace string
nodeReadyHooks []Hook
nodeStartUpObeserver NodeStartUpObeserver
taintNodeNotReadyName string
}

// NewNodeController initializes a new NodeController.
func NewNodeController(selectors []*PodSelector, ignoreNodeLabels map[string]string, interval time.Duration, configMap string, hooks []Hook, nodeStartUpObeserver NodeStartUpObeserver) (*NodeController, error) {
func NewNodeController(selectors []*PodSelector, ignoreNodeLabels map[string]string, taintNodeNotReadyName string, interval time.Duration, configMap string, hooks []Hook, nodeStartUpObeserver NodeStartUpObeserver) (*NodeController, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
Expand All @@ -53,13 +48,14 @@ func NewNodeController(selectors []*PodSelector, ignoreNodeLabels map[string]str
}

controller := &NodeController{
Interface: client,
selectors: selectors,
ignoreNodeLabels: ignoreNodeLabels,
interval: interval,
configMap: configMap,
nodeReadyHooks: hooks,
nodeStartUpObeserver: nodeStartUpObeserver,
Interface: client,
selectors: selectors,
ignoreNodeLabels: ignoreNodeLabels,
interval: interval,
configMap: configMap,
nodeReadyHooks: hooks,
nodeStartUpObeserver: nodeStartUpObeserver,
taintNodeNotReadyName: taintNodeNotReadyName,
}

if controller.configMap != "" {
Expand Down Expand Up @@ -190,7 +186,7 @@ func (n *NodeController) setNodeReady(node *v1.Node, ready bool) error {
if ready {
var newTaints []v1.Taint
for _, taint := range node.Spec.Taints {
if taint.Key != TaintNodeNotReadyWorkload {
if taint.Key != n.taintNodeNotReadyName {
newTaints = append(newTaints, taint)
}
}
Expand All @@ -203,7 +199,7 @@ func (n *NodeController) setNodeReady(node *v1.Node, ready bool) error {
}
log.WithFields(log.Fields{
"action": "removed",
"taint": TaintNodeNotReadyWorkload,
"taint": n.taintNodeNotReadyName,
"node": node.ObjectMeta.Name,
}).Info("")

Expand All @@ -221,9 +217,9 @@ func (n *NodeController) setNodeReady(node *v1.Node, ready bool) error {
}
}
} else { // else add the taint if the node is not ready
if !hasTaint(node) {
if !hasTaint(node, n.taintNodeNotReadyName) {
taint := v1.Taint{
Key: TaintNodeNotReadyWorkload,
Key: n.taintNodeNotReadyName,
Effect: v1.TaintEffectNoSchedule,
}
node.Spec.Taints = append(node.Spec.Taints, taint)
Expand All @@ -233,7 +229,7 @@ func (n *NodeController) setNodeReady(node *v1.Node, ready bool) error {
}
log.WithFields(log.Fields{
"action": "added",
"taint": TaintNodeNotReadyWorkload,
"taint": n.taintNodeNotReadyName,
"node": node.ObjectMeta.Name,
}).Info("")
}
Expand Down Expand Up @@ -263,10 +259,10 @@ func (n *NodeController) getConfig() error {
return nil
}

// hasTaint returns true if the node has the taint TaintNodeNotReadyWorkload.
func hasTaint(node *v1.Node) bool {
// hasTaint returns true if the node has the taint.
func hasTaint(node *v1.Node, taintName string) bool {
for _, taint := range node.Spec.Taints {
if taint.Key == TaintNodeNotReadyWorkload {
if taint.Key == taintName {
return true
}
}
Expand Down
18 changes: 11 additions & 7 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"k8s.io/client-go/kubernetes/fake"
)

const namespace = "default"
const (
namespace = "default"
taintNodeNotReadyName = "notReady"
)

func setupMockKubernetes(t *testing.T, node *v1.Node, config *v1.ConfigMap) kubernetes.Interface {
client := fake.NewSimpleClientset()
Expand Down Expand Up @@ -59,7 +62,7 @@ func TestRunOnce(t *testing.T) {
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: TaintNodeNotReadyWorkload,
Key: taintNodeNotReadyName,
},
{
Key: "foo",
Expand Down Expand Up @@ -107,7 +110,7 @@ func TestRun(t *testing.T) {
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: TaintNodeNotReadyWorkload,
Key: taintNodeNotReadyName,
},
{
Key: "foo",
Expand Down Expand Up @@ -193,7 +196,7 @@ func TestSetNodeReady(t *testing.T) {
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: TaintNodeNotReadyWorkload,
Key: taintNodeNotReadyName,
},
{
Key: "foo",
Expand Down Expand Up @@ -222,7 +225,8 @@ func TestSetNodeReady(t *testing.T) {
} {
t.Run(tc.msg, func(t *testing.T) {
controller := &NodeController{
Interface: setupMockKubernetes(t, tc.node, nil),
Interface: setupMockKubernetes(t, tc.node, nil),
taintNodeNotReadyName: taintNodeNotReadyName,
}
_ = controller.setNodeReady(tc.node, tc.ready)

Expand All @@ -231,11 +235,11 @@ func TestSetNodeReady(t *testing.T) {
t.Errorf("should not fail: %s", err)
}

if tc.ready && hasTaint(n) {
if tc.ready && hasTaint(n, taintNodeNotReadyName) {
t.Errorf("node should not have taint when ready")
}

if !tc.ready && !hasTaint(n) {
if !tc.ready && !hasTaint(n, taintNodeNotReadyName) {
t.Errorf("node should have taint when not ready")
}
})
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

const (
defaultInterval = "15s"
defaultMetricsAddress = ":7979"
defaultInterval = "15s"
defaultMetricsAddress = ":7979"
defaultTaintNodeNotReadyName = "node.alpha.kubernetes.io/notReady-workload"
)

var (
Expand All @@ -32,6 +33,7 @@ var (
ASGLifecycleHook string
IgnoreNodeLabels []string
EnableNodeStartUpMetrics bool
TaintNodeNotReadyName string
}
)

Expand All @@ -50,6 +52,8 @@ func init() {
StringVar(&config.ASGLifecycleHook)
kingpin.Flag("enable-node-startup-metrics", "Enable node startup duration metrics.").
BoolVar(&config.EnableNodeStartUpMetrics)
kingpin.Flag("not-ready-taint-name", "Name of the taint set for not ready nodes.").
StringVar(&config.TaintNodeNotReadyName)
}

func main() {
Expand Down Expand Up @@ -85,6 +89,7 @@ func main() {
controller, err := NewNodeController(
config.PodSelectors,
ignoreLabels,
config.TaintNodeNotReadyName,
config.Interval,
config.ConfigMap,
hooks,
Expand Down

0 comments on commit 3fd1e11

Please sign in to comment.