Skip to content

Commit 335128b

Browse files
Merge pull request #133178 from liggitt/psa-emulation
make admission and pod-security-admission checks be informed by emulation version Kubernetes-commit: d3cb6b539dfaeb24e9f48827a9a76a23470c4607
2 parents bf8cdb9 + c8c846a commit 335128b

File tree

7 files changed

+28
-7
lines changed

7 files changed

+28
-7
lines changed

pkg/admission/initializer/initializer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"k8s.io/client-go/dynamic"
2424
"k8s.io/client-go/informers"
2525
"k8s.io/client-go/kubernetes"
26+
"k8s.io/component-base/compatibility"
2627
"k8s.io/component-base/featuregate"
2728
)
2829

@@ -32,6 +33,7 @@ type pluginInitializer struct {
3233
externalInformers informers.SharedInformerFactory
3334
authorizer authorizer.Authorizer
3435
featureGates featuregate.FeatureGate
36+
effectiveVersion compatibility.EffectiveVersion
3537
stopCh <-chan struct{}
3638
restMapper meta.RESTMapper
3739
}
@@ -45,6 +47,7 @@ func New(
4547
extInformers informers.SharedInformerFactory,
4648
authz authorizer.Authorizer,
4749
featureGates featuregate.FeatureGate,
50+
effectiveVersion compatibility.EffectiveVersion,
4851
stopCh <-chan struct{},
4952
restMapper meta.RESTMapper,
5053
) pluginInitializer {
@@ -54,6 +57,7 @@ func New(
5457
externalInformers: extInformers,
5558
authorizer: authz,
5659
featureGates: featureGates,
60+
effectiveVersion: effectiveVersion,
5761
stopCh: stopCh,
5862
restMapper: restMapper,
5963
}
@@ -68,6 +72,9 @@ func (i pluginInitializer) Initialize(plugin admission.Interface) {
6872
}
6973

7074
// Second tell the plugin about enabled features, so it can decide whether to start informers or not
75+
if wants, ok := plugin.(WantsEffectiveVersion); ok {
76+
wants.InspectEffectiveVersion(i.effectiveVersion)
77+
}
7178
if wants, ok := plugin.(WantsFeatures); ok {
7279
wants.InspectFeatureGates(i.featureGates)
7380
}

pkg/admission/initializer/initializer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
// TestWantsAuthorizer ensures that the authorizer is injected
3535
// when the WantsAuthorizer interface is implemented by a plugin.
3636
func TestWantsAuthorizer(t *testing.T) {
37-
target := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, nil, nil)
37+
target := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, nil, nil, nil)
3838
wantAuthorizerAdmission := &WantAuthorizerAdmission{}
3939
target.Initialize(wantAuthorizerAdmission)
4040
if wantAuthorizerAdmission.auth == nil {
@@ -46,7 +46,7 @@ func TestWantsAuthorizer(t *testing.T) {
4646
// when the WantsExternalKubeClientSet interface is implemented by a plugin.
4747
func TestWantsExternalKubeClientSet(t *testing.T) {
4848
cs := &fake.Clientset{}
49-
target := initializer.New(cs, nil, nil, &TestAuthorizer{}, nil, nil, nil)
49+
target := initializer.New(cs, nil, nil, &TestAuthorizer{}, nil, nil, nil, nil)
5050
wantExternalKubeClientSet := &WantExternalKubeClientSet{}
5151
target.Initialize(wantExternalKubeClientSet)
5252
if wantExternalKubeClientSet.cs != cs {
@@ -59,7 +59,7 @@ func TestWantsExternalKubeClientSet(t *testing.T) {
5959
func TestWantsExternalKubeInformerFactory(t *testing.T) {
6060
cs := &fake.Clientset{}
6161
sf := informers.NewSharedInformerFactory(cs, time.Duration(1)*time.Second)
62-
target := initializer.New(cs, nil, sf, &TestAuthorizer{}, nil, nil, nil)
62+
target := initializer.New(cs, nil, sf, &TestAuthorizer{}, nil, nil, nil, nil)
6363
wantExternalKubeInformerFactory := &WantExternalKubeInformerFactory{}
6464
target.Initialize(wantExternalKubeInformerFactory)
6565
if wantExternalKubeInformerFactory.sf != sf {
@@ -71,7 +71,7 @@ func TestWantsExternalKubeInformerFactory(t *testing.T) {
7171
// when the WantsShutdownSignal interface is implemented by a plugin.
7272
func TestWantsShutdownNotification(t *testing.T) {
7373
stopCh := make(chan struct{})
74-
target := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, stopCh, nil)
74+
target := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, nil, stopCh, nil)
7575
wantDrainedNotification := &WantDrainedNotification{}
7676
target.Initialize(wantDrainedNotification)
7777
if wantDrainedNotification.stopCh == nil {
@@ -153,7 +153,7 @@ func (t *TestAuthorizer) Authorize(ctx context.Context, a authorizer.Attributes)
153153
}
154154

155155
func TestRESTMapperAdmissionPlugin(t *testing.T) {
156-
initializer := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, nil, &doNothingRESTMapper{})
156+
initializer := initializer.New(nil, nil, nil, &TestAuthorizer{}, nil, nil, nil, &doNothingRESTMapper{})
157157
wantsRESTMapperAdmission := &WantsRESTMapperAdmissionPlugin{}
158158
initializer.Initialize(wantsRESTMapperAdmission)
159159

pkg/admission/initializer/interfaces.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/client-go/dynamic"
2727
"k8s.io/client-go/informers"
2828
"k8s.io/client-go/kubernetes"
29+
"k8s.io/component-base/compatibility"
2930
"k8s.io/component-base/featuregate"
3031
)
3132

@@ -73,6 +74,12 @@ type WantsFeatures interface {
7374
admission.InitializationValidator
7475
}
7576

77+
// WantsEffectiveVersion defines a function which passes the effective version for inspection by an admission plugin.
78+
type WantsEffectiveVersion interface {
79+
InspectEffectiveVersion(compatibility.EffectiveVersion)
80+
admission.InitializationValidator
81+
}
82+
7683
type WantsDynamicClient interface {
7784
SetDynamicClient(dynamic.Interface)
7885
admission.InitializationValidator

pkg/admission/plugin/namespace/lifecycle/admission_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/google/go-cmp/cmp"
27+
2728
v1 "k8s.io/api/core/v1"
2829
"k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -53,7 +54,7 @@ func newHandlerForTestWithClock(c clientset.Interface, cacheClock clock.Clock) (
5354
if err != nil {
5455
return nil, f, err
5556
}
56-
pluginInitializer := kubeadmission.New(c, nil, f, nil, nil, nil, nil)
57+
pluginInitializer := kubeadmission.New(c, nil, f, nil, nil, nil, nil, nil)
5758
pluginInitializer.Initialize(handler)
5859
err = admission.ValidateInitialization(handler)
5960
return handler, f, err

pkg/admission/plugin/policy/generic/policy_test_context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"k8s.io/apiserver/pkg/admission"
4646
"k8s.io/apiserver/pkg/admission/initializer"
4747
"k8s.io/apiserver/pkg/authorization/authorizer"
48+
"k8s.io/apiserver/pkg/util/compatibility"
4849
)
4950

5051
// Logger allows t.Testing and b.Testing to be passed to PolicyTestContext
@@ -203,13 +204,15 @@ func NewPolicyTestContext[P, B runtime.Object, E Evaluator](
203204
plugin.SetEnabled(true)
204205

205206
featureGate := featuregate.NewFeatureGate()
207+
effectiveVersion := compatibility.DefaultBuildEffectiveVersion()
206208
testContext, testCancel := context.WithCancel(context.Background())
207209
genericInitializer := initializer.New(
208210
nativeClient,
209211
dynamicClient,
210212
fakeInformerFactory,
211213
fakeAuthorizer{},
212214
featureGate,
215+
effectiveVersion,
213216
testContext.Done(),
214217
fakeRestMapper,
215218
)

pkg/server/options/admission.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"k8s.io/client-go/informers"
4545
"k8s.io/client-go/kubernetes"
4646
"k8s.io/client-go/restmapper"
47+
"k8s.io/component-base/compatibility"
4748
"k8s.io/component-base/featuregate"
4849
)
4950

@@ -130,6 +131,7 @@ func (a *AdmissionOptions) ApplyTo(
130131
kubeClient kubernetes.Interface,
131132
dynamicClient dynamic.Interface,
132133
features featuregate.FeatureGate,
134+
effectiveVersion compatibility.EffectiveVersion,
133135
pluginInitializers ...admission.PluginInitializer,
134136
) error {
135137
if a == nil {
@@ -154,7 +156,7 @@ func (a *AdmissionOptions) ApplyTo(
154156
discoveryClient := cacheddiscovery.NewMemCacheClient(kubeClient.Discovery())
155157
discoveryRESTMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
156158
genericInitializer := initializer.New(kubeClient, dynamicClient, informers, c.Authorization.Authorizer, features,
157-
c.DrainedNotify(), discoveryRESTMapper)
159+
effectiveVersion, c.DrainedNotify(), discoveryRESTMapper)
158160
initializersChain := admission.PluginInitializers{genericInitializer}
159161
initializersChain = append(initializersChain, pluginInitializers...)
160162

pkg/server/options/recommended.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func (o *RecommendedOptions) ApplyTo(config *server.RecommendedConfig) error {
141141
return err
142142
}
143143
if err := o.Admission.ApplyTo(&config.Config, config.SharedInformerFactory, kubeClient, dynamicClient, o.FeatureGate,
144+
config.EffectiveVersion,
144145
initializers...); err != nil {
145146
return err
146147
}

0 commit comments

Comments
 (0)