mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-25 12:17:52 +00:00
feat: update taint nodes by condition to GA
This commit is contained in:
@@ -7,10 +7,7 @@ go_library(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/featuregate:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -20,12 +17,10 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/featuregate:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@@ -22,10 +22,7 @@ import (
|
||||
"io"
|
||||
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
"k8s.io/component-base/featuregate"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -46,16 +43,13 @@ func Register(plugins *admission.Plugins) {
|
||||
// This plugin identifies requests from nodes
|
||||
func NewPlugin() *Plugin {
|
||||
return &Plugin{
|
||||
Handler: admission.NewHandler(admission.Create),
|
||||
features: utilfeature.DefaultFeatureGate,
|
||||
Handler: admission.NewHandler(admission.Create),
|
||||
}
|
||||
}
|
||||
|
||||
// Plugin holds state for and implements the admission plugin.
|
||||
type Plugin struct {
|
||||
*admission.Handler
|
||||
// allows overriding for testing
|
||||
features featuregate.FeatureGate
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -68,11 +62,6 @@ var (
|
||||
|
||||
// Admit is the main function that checks node identity and adds taints as needed.
|
||||
func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) 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
|
||||
@@ -83,11 +72,11 @@ func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.
|
||||
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.
|
||||
// Taint node with NotReady taint at creation. 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
|
||||
}
|
||||
|
@@ -26,25 +26,9 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
"k8s.io/component-base/featuregate"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
var (
|
||||
enableTaintNodesByCondition = featuregate.NewFeatureGate()
|
||||
disableTaintNodesByCondition = featuregate.NewFeatureGate()
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := enableTaintNodesByCondition.Add(map[featuregate.Feature]featuregate.FeatureSpec{features.TaintNodesByCondition: {Default: true}}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := disableTaintNodesByCondition.Add(map[featuregate.Feature]featuregate.FeatureSpec{features.TaintNodesByCondition: {Default: false}}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_nodeTaints(t *testing.T) {
|
||||
var (
|
||||
mynode = &user.DefaultInfo{Name: "system:node:mynode", Groups: []string{"system:nodes"}}
|
||||
@@ -63,7 +47,6 @@ func Test_nodeTaints(t *testing.T) {
|
||||
name string
|
||||
node api.Node
|
||||
oldNode api.Node
|
||||
features featuregate.FeatureGate
|
||||
operation admission.Operation
|
||||
options runtime.Object
|
||||
expectedTaints []api.Taint
|
||||
@@ -71,23 +54,13 @@ func Test_nodeTaints(t *testing.T) {
|
||||
{
|
||||
name: "notReady taint is added on creation",
|
||||
node: myNodeObj,
|
||||
features: enableTaintNodesByCondition,
|
||||
operation: admission.Create,
|
||||
options: &metav1.CreateOptions{},
|
||||
expectedTaints: []api.Taint{notReadyTaint},
|
||||
},
|
||||
{
|
||||
name: "NotReady taint is not added when TaintNodesByCondition is disabled",
|
||||
node: myNodeObj,
|
||||
features: disableTaintNodesByCondition,
|
||||
operation: admission.Create,
|
||||
options: &metav1.CreateOptions{},
|
||||
expectedTaints: nil,
|
||||
},
|
||||
{
|
||||
name: "already tainted node is not tainted again",
|
||||
node: myTaintedNodeObj,
|
||||
features: enableTaintNodesByCondition,
|
||||
operation: admission.Create,
|
||||
options: &metav1.CreateOptions{},
|
||||
expectedTaints: []api.Taint{notReadyTaint},
|
||||
@@ -95,7 +68,6 @@ func Test_nodeTaints(t *testing.T) {
|
||||
{
|
||||
name: "NotReady taint is added to an unready node as well",
|
||||
node: myUnreadyNodeObj,
|
||||
features: enableTaintNodesByCondition,
|
||||
operation: admission.Create,
|
||||
options: &metav1.CreateOptions{},
|
||||
expectedTaints: []api.Taint{notReadyTaint},
|
||||
@@ -105,9 +77,6 @@ func Test_nodeTaints(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
attributes := admission.NewAttributesRecord(&tt.node, &tt.oldNode, nodeKind, myNodeObj.Namespace, myNodeObj.Name, resource, "", tt.operation, tt.options, false, mynode)
|
||||
c := NewPlugin()
|
||||
if tt.features != nil {
|
||||
c.features = tt.features
|
||||
}
|
||||
err := c.Admit(context.TODO(), attributes, nil)
|
||||
if err != nil {
|
||||
t.Errorf("nodePlugin.Admit() error = %v", err)
|
||||
|
@@ -12,7 +12,6 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//pkg/scheduler/api:go_default_library",
|
||||
"//plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
@@ -21,11 +20,9 @@ go_test(
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/admission/testing:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@@ -29,13 +29,10 @@ import (
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer"
|
||||
admissiontesting "k8s.io/apiserver/pkg/admission/testing"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
|
||||
pluginapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
|
||||
)
|
||||
@@ -87,8 +84,6 @@ func TestPodAdmission(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.TaintNodesByCondition, true)()
|
||||
|
||||
tests := []struct {
|
||||
pod *api.Pod
|
||||
defaultClusterTolerations []api.Toleration
|
||||
|
Reference in New Issue
Block a user