Debian Codegraph @

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

raw
1 /*
2 Copyright 2024 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package generic
18
19 import (
20 "context"
21 "fmt"
22 "time"
23
24 corev1 "k8s.io/api/core/v1"
25 "k8s.io/apimachinery/pkg/api/errors"
26 "k8s.io/apimachinery/pkg/api/meta"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/apimachinery/pkg/runtime/schema"
31 "k8s.io/apimachinery/pkg/runtime/serializer"
32 "k8s.io/apimachinery/pkg/types"
33 "k8s.io/apimachinery/pkg/util/uuid"
34 "k8s.io/apimachinery/pkg/util/wait"
35 "k8s.io/apimachinery/pkg/watch"
36 "k8s.io/client-go/dynamic"
37 dynamicfake "k8s.io/client-go/dynamic/fake"
38 "k8s.io/client-go/informers"
39 "k8s.io/client-go/kubernetes"
40 "k8s.io/client-go/kubernetes/fake"
41 clienttesting "k8s.io/client-go/testing"
42 "k8s.io/client-go/tools/cache"
43 "k8s.io/component-base/featuregate"
44
45 "k8s.io/apiserver/pkg/admission"
46 "k8s.io/apiserver/pkg/admission/initializer"
47 "k8s.io/apiserver/pkg/authorization/authorizer"
48 )
49
50 // PolicyTestContext is everything you need to unit test a policy plugin
51 type PolicyTestContext[P runtime.Object, B runtime.Object, E Evaluator] struct {
52 context.Context
53 Plugin *Plugin[PolicyHook[P, B, E]]
54 Source Source[PolicyHook[P, B, E]]
55 Start func() error
56
57 scheme *runtime.Scheme
58 restMapper *meta.DefaultRESTMapper
59 policyGVR schema.GroupVersionResource
60 bindingGVR schema.GroupVersionResource
61
62 policyGVK schema.GroupVersionKind
63 bindingGVK schema.GroupVersionKind
64
65 nativeTracker clienttesting.ObjectTracker
66 policyAndBindingTracker clienttesting.ObjectTracker
67 unstructuredTracker clienttesting.ObjectTracker
68 }
69
70 func NewPolicyTestContext[P, B runtime.Object, E Evaluator](
71 newPolicyAccessor func(P) PolicyAccessor,
72 newBindingAccessor func(B) BindingAccessor,
73 compileFunc func(P) E,
74 dispatcher dispatcherFactory[PolicyHook[P, B, E]],
75 initialObjects []runtime.Object,
76 paramMappings []meta.RESTMapping,
77 ) (*PolicyTestContext[P, B, E], func(), error) {
78 var Pexample P
79 var Bexample B
80
81 // Create a fake resource and kind for the provided policy and binding types
82 fakePolicyGVR := schema.GroupVersionResource{
83 Group: "policy.example.com",
84 Version: "v1",
85 Resource: "fakepolicies",
86 }
87 fakeBindingGVR := schema.GroupVersionResource{
88 Group: "policy.example.com",
89 Version: "v1",
90 Resource: "fakebindings",
91 }
92 fakePolicyGVK := fakePolicyGVR.GroupVersion().WithKind("FakePolicy")
93 fakeBindingGVK := fakeBindingGVR.GroupVersion().WithKind("FakeBinding")
94
95 policySourceTestScheme, err := func() (*runtime.Scheme, error) {
96 scheme := runtime.NewScheme()
97
98 if err := fake.AddToScheme(scheme); err != nil {
99 return nil, err
100 }
101
102 scheme.AddKnownTypeWithName(fakePolicyGVK, Pexample)
103 scheme.AddKnownTypeWithName(fakeBindingGVK, Bexample)
104 scheme.AddKnownTypeWithName(fakePolicyGVK.GroupVersion().WithKind(fakePolicyGVK.Kind+"List"), &FakeList[P]{})
105 scheme.AddKnownTypeWithName(fakeBindingGVK.GroupVersion().WithKind(fakeBindingGVK.Kind+"List"), &FakeList[B]{})
106
107 for _, mapping := range paramMappings {
108 // Skip if it is in the scheme already
109 if scheme.Recognizes(mapping.GroupVersionKind) {
110 continue
111 }
112 scheme.AddKnownTypeWithName(mapping.GroupVersionKind, &unstructured.Unstructured{})
113 scheme.AddKnownTypeWithName(mapping.GroupVersionKind.GroupVersion().WithKind(mapping.GroupVersionKind.Kind+"List"), &unstructured.UnstructuredList{})
114 }
115
116 return scheme, nil
117 }()
118 if err != nil {
119 return nil, nil, err
120 }
121
122 fakeRestMapper := func() *meta.DefaultRESTMapper {
123 res := meta.NewDefaultRESTMapper([]schema.GroupVersion{
124 {
125 Group: "",
126 Version: "v1",
127 },
128 })
129
130 res.Add(fakePolicyGVK, meta.RESTScopeRoot)
131 res.Add(fakeBindingGVK, meta.RESTScopeRoot)
132 res.Add(corev1.SchemeGroupVersion.WithKind("ConfigMap"), meta.RESTScopeNamespace)
133
134 for _, mapping := range paramMappings {
135 res.AddSpecific(mapping.GroupVersionKind, mapping.Resource, mapping.Resource, mapping.Scope)
136 }
137
138 return res
139 }()
140
141 nativeClient := fake.NewSimpleClientset()
142 dynamicClient := dynamicfake.NewSimpleDynamicClient(policySourceTestScheme)
143 fakeInformerFactory := informers.NewSharedInformerFactory(nativeClient, 30*time.Second)
144
145 // Make an object tracker specifically for our policies and bindings
146 policiesAndBindingsTracker := clienttesting.NewObjectTracker(
147 policySourceTestScheme,
148 serializer.NewCodecFactory(policySourceTestScheme).UniversalDecoder())
149
150 // Make an informer for our policies and bindings
151
152 policyInformer := cache.NewSharedIndexInformer(
153 &cache.ListWatch{
154 ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
155 return policiesAndBindingsTracker.List(fakePolicyGVR, fakePolicyGVK, "")
156 },
157 WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
158 return policiesAndBindingsTracker.Watch(fakePolicyGVR, "")
159 },
160 },
161 Pexample,
162 30*time.Second,
163 cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
164 )
165 bindingInformer := cache.NewSharedIndexInformer(
166 &cache.ListWatch{
167 ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
168 return policiesAndBindingsTracker.List(fakeBindingGVR, fakeBindingGVK, "")
169 },
170 WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
171 return policiesAndBindingsTracker.Watch(fakeBindingGVR, "")
172 },
173 },
174 Bexample,
175 30*time.Second,
176 cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
177 )
178
179 var source Source[PolicyHook[P, B, E]]
180 plugin := NewPlugin[PolicyHook[P, B, E]](
181 admission.NewHandler(admission.Connect, admission.Create, admission.Delete, admission.Update),
182 func(sif informers.SharedInformerFactory, i1 kubernetes.Interface, i2 dynamic.Interface, r meta.RESTMapper) Source[PolicyHook[P, B, E]] {
183 source = NewPolicySource[P, B, E](
184 policyInformer,
185 bindingInformer,
186 newPolicyAccessor,
187 newBindingAccessor,
188 compileFunc,
189 sif,
190 i2,
191 r,
192 )
193 return source
194 }, dispatcher)
195 plugin.SetEnabled(true)
196
197 featureGate := featuregate.NewFeatureGate()
198 testContext, testCancel := context.WithCancel(context.Background())
199 genericInitializer := initializer.New(
200 nativeClient,
201 dynamicClient,
202 fakeInformerFactory,
203 fakeAuthorizer{},
204 featureGate,
205 testContext.Done(),
206 fakeRestMapper,
207 )
208 genericInitializer.Initialize(plugin)
209 plugin.SetRESTMapper(fakeRestMapper)
210
211 if err := plugin.ValidateInitialization(); err != nil {
212 testCancel()
213 return nil, nil, err
214 }
215
216 res := &PolicyTestContext[P, B, E]{
217 Context: testContext,
218 Plugin: plugin,
219 Source: source,
220
221 restMapper: fakeRestMapper,
222 scheme: policySourceTestScheme,
223 policyGVK: fakePolicyGVK,
224 bindingGVK: fakeBindingGVK,
225 policyGVR: fakePolicyGVR,
226 bindingGVR: fakeBindingGVR,
227 nativeTracker: nativeClient.Tracker(),
228 policyAndBindingTracker: policiesAndBindingsTracker,
229 unstructuredTracker: dynamicClient.Tracker(),
230 }
231
232 for _, obj := range initialObjects {
233 err := res.updateOne(obj)
234 if err != nil {
235 testCancel()
236 return nil, nil, err
237 }
238 }
239
240 res.Start = func() error {
241 fakeInformerFactory.Start(res.Done())
242 go policyInformer.Run(res.Done())
243 go bindingInformer.Run(res.Done())
244
245 if !cache.WaitForCacheSync(res.Done(), res.Source.HasSynced) {
246 return fmt.Errorf("timed out waiting for initial cache sync")
247 }
248 return nil
249 }
250 return res, testCancel, nil
251 }
252
253 // UpdateAndWait updates the given object in the test, or creates it if it doesn't exist
254 // Depending upon object type, waits afterward until the object is synced
255 // by the policy source
256 //
257 // Be aware the UpdateAndWait will modify the ResourceVersion of the
258 // provided objects.
259 func (p *PolicyTestContext[P, B, E]) UpdateAndWait(objects ...runtime.Object) error {
260 return p.update(true, objects...)
261 }
262
263 // Update updates the given object in the test, or creates it if it doesn't exist
264 //
265 // Be aware the Update will modify the ResourceVersion of the
266 // provided objects.
267 func (p *PolicyTestContext[P, B, E]) Update(objects ...runtime.Object) error {
268 return p.update(false, objects...)
269 }
270
271 // Objects the given object in the test, or creates it if it doesn't exist
272 // Depending upon object type, waits afterward until the object is synced
273 // by the policy source
274 func (p *PolicyTestContext[P, B, E]) update(wait bool, objects ...runtime.Object) error {
275 for _, object := range objects {
276 if err := p.updateOne(object); err != nil {
277 return err
278 }
279 }
280
281 if wait {
282 timeoutCtx, timeoutCancel := context.WithTimeout(p, 3*time.Second)
283 defer timeoutCancel()
284
285 for _, object := range objects {
286 if err := p.WaitForReconcile(timeoutCtx, object); err != nil {
287 return fmt.Errorf("error waiting for reconcile of %v: %v", object, err)
288 }
289 }
290 }
291 return nil
292 }
293
294 // Depending upon object type, waits afterward until the object is synced
295 // by the policy source. Note that policies that are not bound are skipped,
296 // so you should not try to wait for an unbound policy. Create both the binding
297 // and policy, then wait.
298 func (p *PolicyTestContext[P, B, E]) WaitForReconcile(timeoutCtx context.Context, object runtime.Object) error {
299 if !p.Source.HasSynced() {
300 return nil
301 }
302
303 objectMeta, err := meta.Accessor(object)
304 if err != nil {
305 return err
306 }
307
308 objectGVK, _, err := p.inferGVK(object)
309 if err != nil {
310 return err
311 }
312
313 switch objectGVK {
314 case p.policyGVK:
315 return wait.PollUntilContextCancel(timeoutCtx, 100*time.Millisecond, true, func(ctx context.Context) (done bool, err error) {
316 policies := p.Source.Hooks()
317 for _, policy := range policies {
318 policyMeta, err := meta.Accessor(policy.Policy)
319 if err != nil {
320 return true, err
321 } else if policyMeta.GetName() == objectMeta.GetName() && policyMeta.GetResourceVersion() == objectMeta.GetResourceVersion() {
322 return true, nil
323 }
324 }
325 return false, nil
326 })
327 case p.bindingGVK:
328 return wait.PollUntilContextCancel(timeoutCtx, 100*time.Millisecond, true, func(ctx context.Context) (done bool, err error) {
329 policies := p.Source.Hooks()
330 for _, policy := range policies {
331 for _, binding := range policy.Bindings {
332 bindingMeta, err := meta.Accessor(binding)
333 if err != nil {
334 return true, err
335 } else if bindingMeta.GetName() == objectMeta.GetName() && bindingMeta.GetResourceVersion() == objectMeta.GetResourceVersion() {
336 return true, nil
337 }
338 }
339 }
340 return false, nil
341 })
342
343 default:
344 // Do nothing, params are visible immediately
345 // Loop until one of the params is visible via get of the param informer
346 return wait.PollUntilContextCancel(timeoutCtx, 100*time.Millisecond, true, func(ctx context.Context) (done bool, err error) {
347 informer, scope := p.Source.(*policySource[P, B, E]).getParamInformer(objectGVK)
348 if informer == nil {
349 // Informer does not exist yet, keep waiting for sync
350 return false, nil
351 }
352
353 if !cache.WaitForCacheSync(timeoutCtx.Done(), informer.Informer().HasSynced) {
354 return false, fmt.Errorf("timed out waiting for cache sync of param informer")
355 }
356
357 var lister cache.GenericNamespaceLister = informer.Lister()
358 if scope == meta.RESTScopeNamespace {
359 lister = informer.Lister().ByNamespace(objectMeta.GetNamespace())
360 }
361
362 fetched, err := lister.Get(objectMeta.GetName())
363 if err != nil {
364 if errors.IsNotFound(err) {
365 return false, nil
366 }
367 return true, err
368 }
369
370 // Ensure RV matches
371 fetchedMeta, err := meta.Accessor(fetched)
372 if err != nil {
373 return true, err
374 } else if fetchedMeta.GetResourceVersion() != objectMeta.GetResourceVersion() {
375 return false, nil
376 }
377
378 return true, nil
379 })
380 }
381 }
382
383 func (p *PolicyTestContext[P, B, E]) waitForDelete(ctx context.Context, objectGVK schema.GroupVersionKind, name types.NamespacedName) error {
384 srce := p.Source.(*policySource[P, B, E])
385
386 return wait.PollUntilContextCancel(ctx, 100*time.Millisecond, true, func(ctx context.Context) (done bool, err error) {
387 switch objectGVK {
388 case p.policyGVK:
389 for _, hook := range p.Source.Hooks() {
390 accessor := srce.newPolicyAccessor(hook.Policy)
391 if accessor.GetName() == name.Name && accessor.GetNamespace() == name.Namespace {
392 return false, nil
393 }
394 }
395
396 return true, nil
397 case p.bindingGVK:
398 for _, hook := range p.Source.Hooks() {
399 for _, binding := range hook.Bindings {
400 accessor := srce.newBindingAccessor(binding)
401 if accessor.GetName() == name.Name && accessor.GetNamespace() == name.Namespace {
402 return false, nil
403 }
404 }
405 }
406 return true, nil
407 default:
408 // Do nothing, params are visible immediately
409 // Loop until one of the params is visible via get of the param informer
410 informer, scope := p.Source.(*policySource[P, B, E]).getParamInformer(objectGVK)
411 if informer == nil {
412 return true, nil
413 }
414
415 var lister cache.GenericNamespaceLister = informer.Lister()
416 if scope == meta.RESTScopeNamespace {
417 lister = informer.Lister().ByNamespace(name.Namespace)
418 }
419
420 _, err = lister.Get(name.Name)
421 if err != nil {
422 if errors.IsNotFound(err) {
423 return true, nil
424 }
425 return false, err
426 }
427 return false, nil
428 }
429 })
430 }
431
432 func (p *PolicyTestContext[P, B, E]) updateOne(object runtime.Object) error {
433 objectMeta, err := meta.Accessor(object)
434 if err != nil {
435 return err
436 }
437 objectMeta.SetResourceVersion(string(uuid.NewUUID()))
438 objectGVK, gvr, err := p.inferGVK(object)
439 if err != nil {
440 return err
441 }
442
443 switch objectGVK {
444 case p.policyGVK:
445 err := p.policyAndBindingTracker.Update(p.policyGVR, object, objectMeta.GetNamespace())
446 if errors.IsNotFound(err) {
447 err = p.policyAndBindingTracker.Create(p.policyGVR, object, objectMeta.GetNamespace())
448 }
449
450 return err
451 case p.bindingGVK:
452 err := p.policyAndBindingTracker.Update(p.bindingGVR, object, objectMeta.GetNamespace())
453 if errors.IsNotFound(err) {
454 err = p.policyAndBindingTracker.Create(p.bindingGVR, object, objectMeta.GetNamespace())
455 }
456
457 return err
458 default:
459 if _, ok := object.(*unstructured.Unstructured); ok {
460 if err := p.unstructuredTracker.Create(gvr, object, objectMeta.GetNamespace()); err != nil {
461 if errors.IsAlreadyExists(err) {
462 return p.unstructuredTracker.Update(gvr, object, objectMeta.GetNamespace())
463 }
464 return err
465 }
466 return nil
467 } else if err := p.nativeTracker.Create(gvr, object, objectMeta.GetNamespace()); err != nil {
468 if errors.IsAlreadyExists(err) {
469 return p.nativeTracker.Update(gvr, object, objectMeta.GetNamespace())
470 }
471 }
472 return nil
473 }
474 }
475
476 // Depending upon object type, waits afterward until the object is synced
477 // by the policy source
478 func (p *PolicyTestContext[P, B, E]) DeleteAndWait(object ...runtime.Object) error {
479 for _, object := range object {
480 if err := p.deleteOne(object); err != nil && !errors.IsNotFound(err) {
481 return err
482 }
483 }
484
485 timeoutCtx, timeoutCancel := context.WithTimeout(p, 3*time.Second)
486 defer timeoutCancel()
487
488 for _, object := range object {
489 accessor, err := meta.Accessor(object)
490 if err != nil {
491 return err
492 }
493
494 objectGVK, _, err := p.inferGVK(object)
495 if err != nil {
496 return err
497 }
498
499 if err := p.waitForDelete(
500 timeoutCtx,
501 objectGVK,
502 types.NamespacedName{Name: accessor.GetName(), Namespace: accessor.GetNamespace()}); err != nil {
503 return err
504 }
505 }
506 return nil
507 }
508
509 func (p *PolicyTestContext[P, B, E]) deleteOne(object runtime.Object) error {
510 objectMeta, err := meta.Accessor(object)
511 if err != nil {
512 return err
513 }
514 objectMeta.SetResourceVersion(string(uuid.NewUUID()))
515 objectGVK, gvr, err := p.inferGVK(object)
516 if err != nil {
517 return err
518 }
519
520 switch objectGVK {
521 case p.policyGVK:
522 return p.policyAndBindingTracker.Delete(p.policyGVR, objectMeta.GetNamespace(), objectMeta.GetName())
523 case p.bindingGVK:
524 return p.policyAndBindingTracker.Delete(p.bindingGVR, objectMeta.GetNamespace(), objectMeta.GetName())
525 default:
526 if _, ok := object.(*unstructured.Unstructured); ok {
527 return p.unstructuredTracker.Delete(gvr, objectMeta.GetNamespace(), objectMeta.GetName())
528 }
529 return p.nativeTracker.Delete(gvr, objectMeta.GetNamespace(), objectMeta.GetName())
530 }
531 }
532
533 func (p *PolicyTestContext[P, B, E]) Dispatch(
534 new, old runtime.Object,
535 operation admission.Operation,
536 ) error {
537 if old == nil && new == nil {
538 return fmt.Errorf("both old and new objects cannot be nil")
539 }
540
541 nonNilObject := new
542 if nonNilObject == nil {
543 nonNilObject = old
544 }
545
546 gvk, gvr, err := p.inferGVK(nonNilObject)
547 if err != nil {
548 return err
549 }
550
551 nonNilMeta, err := meta.Accessor(nonNilObject)
552 if err != nil {
553 return err
554 }
555
556 return p.Plugin.Dispatch(
557 p,
558 admission.NewAttributesRecord(
559 new,
560 old,
561 gvk,
562 nonNilMeta.GetName(),
563 nonNilMeta.GetNamespace(),
564 gvr,
565 "",
566 operation,
567 nil,
568 false,
569 nil,
570 ), admission.NewObjectInterfacesFromScheme(p.scheme))
571 }
572
573 func (p *PolicyTestContext[P, B, E]) inferGVK(object runtime.Object) (schema.GroupVersionKind, schema.GroupVersionResource, error) {
574 objectGVK := object.GetObjectKind().GroupVersionKind()
575 if objectGVK.Empty() {
576 // If the object doesn't have a GVK, ask the schema for preferred GVK
577 knownKinds, _, err := p.scheme.ObjectKinds(object)
578 if err != nil {
579 return schema.GroupVersionKind{}, schema.GroupVersionResource{}, err
580 } else if len(knownKinds) == 0 {
581 return schema.GroupVersionKind{}, schema.GroupVersionResource{}, fmt.Errorf("no known GVKs for object in schema: %T", object)
582 }
583 toTake := 0
584
585 // Prefer GVK if it is our fake policy or binding
586 for i, knownKind := range knownKinds {
587 if knownKind == p.policyGVK || knownKind == p.bindingGVK {
588 toTake = i
589 break
590 }
591 }
592
593 objectGVK = knownKinds[toTake]
594 }
595
596 // Make sure GVK is known to the fake rest mapper. To prevent cryptic error
597 mapping, err := p.restMapper.RESTMapping(objectGVK.GroupKind(), objectGVK.Version)
598 if err != nil {
599 return schema.GroupVersionKind{}, schema.GroupVersionResource{}, err
600 }
601 return objectGVK, mapping.Resource, nil
602 }
603
604 type FakeList[T runtime.Object] struct {
605 metav1.TypeMeta
606 metav1.ListMeta
607 Items []T
608 }
609
610 func (fl *FakeList[P]) DeepCopyObject() runtime.Object {
611 copiedItems := make([]P, len(fl.Items))
612 for i, item := range fl.Items {
613 copiedItems[i] = item.DeepCopyObject().(P)
614 }
615 return &FakeList[P]{
616 TypeMeta: fl.TypeMeta,
617 ListMeta: fl.ListMeta,
618 Items: copiedItems,
619 }
620 }
621
622 type fakeAuthorizer struct{}
623
624 func (f fakeAuthorizer) Authorize(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) {
625 return authorizer.DecisionAllow, "", nil
626 }