From 82225fd952e51bbf87c7bbc80ec325d18b45f1bf Mon Sep 17 00:00:00 2001 From: hzxuzhonghu Date: Mon, 12 Feb 2018 12:17:20 +0800 Subject: [PATCH] optimize DefaultTolerationSeconds admission controller --- .../admission/defaulttolerationseconds/BUILD | 1 - .../defaulttolerationseconds/admission.go | 35 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/plugin/pkg/admission/defaulttolerationseconds/BUILD b/plugin/pkg/admission/defaulttolerationseconds/BUILD index 48ca06b3b83..6dbc7d2c9c8 100644 --- a/plugin/pkg/admission/defaulttolerationseconds/BUILD +++ b/plugin/pkg/admission/defaulttolerationseconds/BUILD @@ -25,7 +25,6 @@ go_library( importpath = "k8s.io/kubernetes/plugin/pkg/admission/defaulttolerationseconds", deps = [ "//pkg/apis/core:go_default_library", - "//pkg/apis/core/helper:go_default_library", "//pkg/scheduler/algorithm:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apiserver/pkg/admission:go_default_library", diff --git a/plugin/pkg/admission/defaulttolerationseconds/admission.go b/plugin/pkg/admission/defaulttolerationseconds/admission.go index 41c37511c01..292be403f49 100644 --- a/plugin/pkg/admission/defaulttolerationseconds/admission.go +++ b/plugin/pkg/admission/defaulttolerationseconds/admission.go @@ -24,7 +24,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apiserver/pkg/admission" api "k8s.io/kubernetes/pkg/apis/core" - "k8s.io/kubernetes/pkg/apis/core/helper" "k8s.io/kubernetes/pkg/scheduler/algorithm" ) @@ -39,6 +38,20 @@ var ( defaultUnreachableTolerationSeconds = flag.Int64("default-unreachable-toleration-seconds", 300, "Indicates the tolerationSeconds of the toleration for unreachable:NoExecute"+ " that is added by default to every pod that does not already have such a toleration.") + + notReadyToleration = api.Toleration{ + Key: algorithm.TaintNodeNotReady, + Operator: api.TolerationOpExists, + Effect: api.TaintEffectNoExecute, + TolerationSeconds: defaultNotReadyTolerationSeconds, + } + + unreachableToleration = api.Toleration{ + Key: algorithm.TaintNodeUnreachable, + Operator: api.TolerationOpExists, + Effect: api.TaintEffectNoExecute, + TolerationSeconds: defaultUnreachableTolerationSeconds, + } ) // Register registers a plugin @@ -99,27 +112,13 @@ func (p *Plugin) Admit(attributes admission.Attributes) (err error) { } } - // no change is required, return immediately - if toleratesNodeNotReady && toleratesNodeUnreachable { - return nil - } - if !toleratesNodeNotReady { - helper.AddOrUpdateTolerationInPod(pod, &api.Toleration{ - Key: algorithm.TaintNodeNotReady, - Operator: api.TolerationOpExists, - Effect: api.TaintEffectNoExecute, - TolerationSeconds: defaultNotReadyTolerationSeconds, - }) + pod.Spec.Tolerations = append(pod.Spec.Tolerations, notReadyToleration) } if !toleratesNodeUnreachable { - helper.AddOrUpdateTolerationInPod(pod, &api.Toleration{ - Key: algorithm.TaintNodeUnreachable, - Operator: api.TolerationOpExists, - Effect: api.TaintEffectNoExecute, - TolerationSeconds: defaultUnreachableTolerationSeconds, - }) + pod.Spec.Tolerations = append(pod.Spec.Tolerations, unreachableToleration) } + return nil }