Skip to content

Commit 7040c8e

Browse files
authored
Add configurable taint effect (#539)
Add TAINT_EFFECT env var and --taint-effect flag to configure the node taint effect Defaults to NoSchedule, garbage values will log a warning and default to NoSchedule. closes #273
1 parent ceadee9 commit 7040c8e

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const (
6767
metadataTriesDefault = 3
6868
cordonOnly = "CORDON_ONLY"
6969
taintNode = "TAINT_NODE"
70+
taintEffectDefault = "NoSchedule"
71+
taintEffect = "TAINT_EFFECT"
7072
jsonLoggingConfigKey = "JSON_LOGGING"
7173
jsonLoggingDefault = false
7274
logLevelConfigKey = "LOG_LEVEL"
@@ -123,6 +125,7 @@ type Config struct {
123125
MetadataTries int
124126
CordonOnly bool
125127
TaintNode bool
128+
TaintEffect string
126129
JsonLogging bool
127130
LogLevel string
128131
UptimeFromFile string
@@ -175,6 +178,7 @@ func ParseCliArgs() (config Config, err error) {
175178
flag.IntVar(&config.MetadataTries, "metadata-tries", getIntEnv(metadataTriesConfigKey, metadataTriesDefault), "The number of times to try requesting metadata. If you would like 2 retries, set metadata-tries to 3.")
176179
flag.BoolVar(&config.CordonOnly, "cordon-only", getBoolEnv(cordonOnly, false), "If true, nodes will be cordoned but not drained when an interruption event occurs.")
177180
flag.BoolVar(&config.TaintNode, "taint-node", getBoolEnv(taintNode, false), "If true, nodes will be tainted when an interruption event occurs.")
181+
flag.StringVar(&config.TaintEffect, "taint-effect", getEnv(taintEffect, taintEffectDefault), "Sets the effect when a node is tainted.")
178182
flag.BoolVar(&config.JsonLogging, "json-logging", getBoolEnv(jsonLoggingConfigKey, jsonLoggingDefault), "If true, use JSON-formatted logs instead of human readable logs.")
179183
flag.StringVar(&config.LogLevel, "log-level", getEnv(logLevelConfigKey, logLevelDefault), "Sets the log level (INFO, DEBUG, or ERROR)")
180184
flag.StringVar(&config.UptimeFromFile, "uptime-from-file", getEnv(uptimeFromFileConfigKey, uptimeFromFileDefault), "If specified, read system uptime from the file path (useful for testing).")
@@ -249,6 +253,7 @@ func (c Config) PrintJsonConfigArgs() {
249253
Int("metadata_tries", c.MetadataTries).
250254
Bool("cordon_only", c.CordonOnly).
251255
Bool("taint_node", c.TaintNode).
256+
Str("taint_effect", c.TaintEffect).
252257
Bool("json_logging", c.JsonLogging).
253258
Str("log_level", c.LogLevel).
254259
Str("webhook_proxy", c.WebhookProxy).
@@ -292,6 +297,7 @@ func (c Config) PrintHumanConfigArgs() {
292297
"\tmetadata-tries: %d,\n"+
293298
"\tcordon-only: %t,\n"+
294299
"\ttaint-node: %t,\n"+
300+
"\ttaint-effect: %s,\n"+
295301
"\tjson-logging: %t,\n"+
296302
"\tlog-level: %s,\n"+
297303
"\twebhook-proxy: %s,\n"+
@@ -326,6 +332,7 @@ func (c Config) PrintHumanConfigArgs() {
326332
c.MetadataTries,
327333
c.CordonOnly,
328334
c.TaintNode,
335+
c.TaintEffect,
329336
c.JsonLogging,
330337
c.LogLevel,
331338
c.WebhookProxy,

pkg/node/node.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (n Node) TaintSpotItn(nodeName string, eventID string) error {
310310
eventID = eventID[:maxTaintValueLength]
311311
}
312312

313-
return addTaint(k8sNode, n, SpotInterruptionTaint, eventID, corev1.TaintEffectNoSchedule)
313+
return addTaint(k8sNode, n, SpotInterruptionTaint, eventID)
314314
}
315315

316316
// TaintASGLifecycleTermination adds the spot termination notice taint onto a node
@@ -328,7 +328,7 @@ func (n Node) TaintASGLifecycleTermination(nodeName string, eventID string) erro
328328
eventID = eventID[:maxTaintValueLength]
329329
}
330330

331-
return addTaint(k8sNode, n, ASGLifecycleTerminationTaint, eventID, corev1.TaintEffectNoSchedule)
331+
return addTaint(k8sNode, n, ASGLifecycleTerminationTaint, eventID)
332332
}
333333

334334
// TaintRebalanceRecommendation adds the rebalance recommendation notice taint onto a node
@@ -346,7 +346,7 @@ func (n Node) TaintRebalanceRecommendation(nodeName string, eventID string) erro
346346
eventID = eventID[:maxTaintValueLength]
347347
}
348348

349-
return addTaint(k8sNode, n, RebalanceRecommendationTaint, eventID, corev1.TaintEffectNoSchedule)
349+
return addTaint(k8sNode, n, RebalanceRecommendationTaint, eventID)
350350
}
351351

352352
// LogPods logs all the pod names on a node
@@ -388,7 +388,7 @@ func (n Node) TaintScheduledMaintenance(nodeName string, eventID string) error {
388388
eventID = eventID[:maxTaintValueLength]
389389
}
390390

391-
return addTaint(k8sNode, n, ScheduledMaintenanceTaint, eventID, corev1.TaintEffectNoSchedule)
391+
return addTaint(k8sNode, n, ScheduledMaintenanceTaint, eventID)
392392
}
393393

394394
// RemoveNTHTaints removes NTH-specific taints from a node
@@ -540,7 +540,22 @@ func jsonPatchEscape(value string) string {
540540
return strings.Replace(value, "/", "~1", -1)
541541
}
542542

543-
func addTaint(node *corev1.Node, nth Node, taintKey string, taintValue string, effect corev1.TaintEffect) error {
543+
func getTaintEffect(effect string) corev1.TaintEffect {
544+
switch effect {
545+
case "PreferNoSchedule":
546+
return corev1.TaintEffectPreferNoSchedule
547+
case "NoExecute":
548+
return corev1.TaintEffectNoExecute
549+
default:
550+
log.Warn().Msgf("Unknown taint effect: %s", effect)
551+
fallthrough
552+
case "NoSchedule":
553+
return corev1.TaintEffectNoSchedule
554+
}
555+
}
556+
557+
func addTaint(node *corev1.Node, nth Node, taintKey string, taintValue string) error {
558+
effect := getTaintEffect(nth.nthConfig.TaintEffect)
544559
if nth.nthConfig.DryRun {
545560
log.Info().Msgf("Would have added taint (%s=%s:%s) to node %s, but dry-run flag was set", taintKey, taintValue, effect, nth.nthConfig.NodeName)
546561
return nil

0 commit comments

Comments
 (0)