@@ -31,9 +31,9 @@ import (
31
31
apierrors "k8s.io/apimachinery/pkg/api/errors"
32
32
"k8s.io/apiserver/pkg/admission"
33
33
genericadmissioninitailizer "k8s.io/apiserver/pkg/admission/initializer"
34
- utilfeature "k8s.io/apiserver/pkg/util/feature"
35
34
"k8s.io/client-go/informers"
36
35
nodev1beta1listers "k8s.io/client-go/listers/node/v1beta1"
36
+ "k8s.io/component-base/featuregate"
37
37
api "k8s.io/kubernetes/pkg/apis/core"
38
38
node "k8s.io/kubernetes/pkg/apis/node"
39
39
nodev1beta1 "k8s.io/kubernetes/pkg/apis/node/v1beta1"
@@ -58,16 +58,27 @@ func Register(plugins *admission.Plugins) {
58
58
type RuntimeClass struct {
59
59
* admission.Handler
60
60
runtimeClassLister nodev1beta1listers.RuntimeClassLister
61
+
62
+ inspectedFeatures bool
63
+ runtimeClassEnabled bool
64
+ podOverheadEnabled bool
61
65
}
62
66
63
67
var _ admission.MutationInterface = & RuntimeClass {}
64
68
var _ admission.ValidationInterface = & RuntimeClass {}
65
69
66
70
var _ genericadmissioninitailizer.WantsExternalKubeInformerFactory = & RuntimeClass {}
67
71
72
+ // InspectFeatureGates allows setting bools without taking a dep on a global variable
73
+ func (r * RuntimeClass ) InspectFeatureGates (featureGates featuregate.FeatureGate ) {
74
+ r .runtimeClassEnabled = featureGates .Enabled (features .RuntimeClass )
75
+ r .podOverheadEnabled = featureGates .Enabled (features .PodOverhead )
76
+ r .inspectedFeatures = true
77
+ }
78
+
68
79
// SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
69
80
func (r * RuntimeClass ) SetExternalKubeInformerFactory (f informers.SharedInformerFactory ) {
70
- if ! utilfeature . DefaultFeatureGate . Enabled ( features . RuntimeClass ) {
81
+ if ! r . runtimeClassEnabled {
71
82
return
72
83
}
73
84
runtimeClassInformer := f .Node ().V1beta1 ().RuntimeClasses ()
@@ -77,7 +88,10 @@ func (r *RuntimeClass) SetExternalKubeInformerFactory(f informers.SharedInformer
77
88
78
89
// ValidateInitialization implements the WantsExternalKubeInformerFactory interface.
79
90
func (r * RuntimeClass ) ValidateInitialization () error {
80
- if ! utilfeature .DefaultFeatureGate .Enabled (features .RuntimeClass ) {
91
+ if ! r .inspectedFeatures {
92
+ return fmt .Errorf ("InspectFeatureGates was not called" )
93
+ }
94
+ if ! r .runtimeClassEnabled {
81
95
return nil
82
96
}
83
97
if r .runtimeClassLister == nil {
@@ -88,7 +102,7 @@ func (r *RuntimeClass) ValidateInitialization() error {
88
102
89
103
// Admit makes an admission decision based on the request attributes
90
104
func (r * RuntimeClass ) Admit (ctx context.Context , attributes admission.Attributes , o admission.ObjectInterfaces ) error {
91
- if ! utilfeature . DefaultFeatureGate . Enabled ( features . RuntimeClass ) {
105
+ if ! r . runtimeClassEnabled {
92
106
return nil
93
107
}
94
108
@@ -101,7 +115,7 @@ func (r *RuntimeClass) Admit(ctx context.Context, attributes admission.Attribute
101
115
if err != nil {
102
116
return err
103
117
}
104
- if utilfeature . DefaultFeatureGate . Enabled ( features . PodOverhead ) {
118
+ if r . podOverheadEnabled {
105
119
if err := setOverhead (attributes , pod , runtimeClass ); err != nil {
106
120
return err
107
121
}
@@ -116,7 +130,7 @@ func (r *RuntimeClass) Admit(ctx context.Context, attributes admission.Attribute
116
130
117
131
// Validate makes sure that pod adhere's to RuntimeClass's definition
118
132
func (r * RuntimeClass ) Validate (ctx context.Context , attributes admission.Attributes , o admission.ObjectInterfaces ) error {
119
- if ! utilfeature . DefaultFeatureGate . Enabled ( features . RuntimeClass ) {
133
+ if ! r . runtimeClassEnabled {
120
134
return nil
121
135
}
122
136
@@ -129,7 +143,7 @@ func (r *RuntimeClass) Validate(ctx context.Context, attributes admission.Attrib
129
143
if err != nil {
130
144
return err
131
145
}
132
- if utilfeature . DefaultFeatureGate . Enabled ( features . PodOverhead ) {
146
+ if r . podOverheadEnabled {
133
147
if err := validateOverhead (attributes , pod , runtimeClass ); err != nil {
134
148
return err
135
149
}
0 commit comments