Skip to content

Commit f489e81

Browse files
Merge pull request #1954 from tiraboschi/deleteVAP
Add delete support for ValidatingAdmissionPolicies and ValidatingAdmissionPolicyBindings
2 parents eb4177e + d28bebc commit f489e81

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed

pkg/operator/resource/resourceapply/admissionregistration.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,51 @@ func ApplyValidatingAdmissionPolicyBindingV1(ctx context.Context, client admissi
415415
cache.UpdateCachedResourceMetadata(requiredOriginal, actual)
416416
return actual, true, nil
417417
}
418+
419+
func DeleteValidatingAdmissionPolicyV1beta1(ctx context.Context, client admissionregistrationclientv1beta1.ValidatingAdmissionPoliciesGetter, recorder events.Recorder, required *admissionregistrationv1beta1.ValidatingAdmissionPolicy) (*admissionregistrationv1beta1.ValidatingAdmissionPolicy, bool, error) {
420+
err := client.ValidatingAdmissionPolicies().Delete(ctx, required.Name, metav1.DeleteOptions{})
421+
if err != nil && apierrors.IsNotFound(err) {
422+
return nil, false, nil
423+
}
424+
if err != nil {
425+
return nil, false, err
426+
}
427+
resourcehelper.ReportDeleteEvent(recorder, required, err)
428+
return nil, true, nil
429+
}
430+
431+
func DeleteValidatingAdmissionPolicyBindingV1beta1(ctx context.Context, client admissionregistrationclientv1beta1.ValidatingAdmissionPolicyBindingsGetter, recorder events.Recorder, required *admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding) (*admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding, bool, error) {
432+
err := client.ValidatingAdmissionPolicyBindings().Delete(ctx, required.Name, metav1.DeleteOptions{})
433+
if err != nil && apierrors.IsNotFound(err) {
434+
return nil, false, nil
435+
}
436+
if err != nil {
437+
return nil, false, err
438+
}
439+
resourcehelper.ReportDeleteEvent(recorder, required, err)
440+
return nil, true, nil
441+
}
442+
443+
func DeleteValidatingAdmissionPolicyV1(ctx context.Context, client admissionregistrationclientv1.ValidatingAdmissionPoliciesGetter, recorder events.Recorder, required *admissionregistrationv1.ValidatingAdmissionPolicy) (*admissionregistrationv1.ValidatingAdmissionPolicy, bool, error) {
444+
err := client.ValidatingAdmissionPolicies().Delete(ctx, required.Name, metav1.DeleteOptions{})
445+
if err != nil && apierrors.IsNotFound(err) {
446+
return nil, false, nil
447+
}
448+
if err != nil {
449+
return nil, false, err
450+
}
451+
resourcehelper.ReportDeleteEvent(recorder, required, err)
452+
return nil, true, nil
453+
}
454+
455+
func DeleteValidatingAdmissionPolicyBindingV1(ctx context.Context, client admissionregistrationclientv1.ValidatingAdmissionPolicyBindingsGetter, recorder events.Recorder, required *admissionregistrationv1.ValidatingAdmissionPolicyBinding) (*admissionregistrationv1.ValidatingAdmissionPolicyBinding, bool, error) {
456+
err := client.ValidatingAdmissionPolicyBindings().Delete(ctx, required.Name, metav1.DeleteOptions{})
457+
if err != nil && apierrors.IsNotFound(err) {
458+
return nil, false, nil
459+
}
460+
if err != nil {
461+
return nil, false, err
462+
}
463+
resourcehelper.ReportDeleteEvent(recorder, required, err)
464+
return nil, true, nil
465+
}

pkg/operator/resource/resourceapply/admissionregistration_test.go

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func TestDeleteValidatingConfiguration(t *testing.T) {
448448
}
449449
}
450450

451-
func TestApplyValidatingAdmissionPolicyConfiguration(t *testing.T) {
451+
func TestApplyValidatingAdmissionPolicy(t *testing.T) {
452452
defaultPolicy := &admissionregistrationv1beta1.ValidatingAdmissionPolicy{}
453453
defaultPolicy.SetName("test")
454454
createEvent := "ValidatingAdmissionPolicyCreated"
@@ -562,6 +562,138 @@ func TestApplyValidatingAdmissionPolicyConfiguration(t *testing.T) {
562562
}
563563
}
564564

565+
func TestDeleteValidatingAdmissionPolicy(t *testing.T) {
566+
defaultPolicy := &admissionregistrationv1.ValidatingAdmissionPolicy{}
567+
defaultPolicy.SetName("test")
568+
deleteEvent := "ValidatingAdmissionPolicyDeleted"
569+
570+
tests := []struct {
571+
name string
572+
expectModified bool
573+
existing func() *admissionregistrationv1.ValidatingAdmissionPolicy
574+
input func() *admissionregistrationv1.ValidatingAdmissionPolicy
575+
checkUpdated func(*admissionregistrationv1.ValidatingAdmissionPolicy) error
576+
expectedEvents []string
577+
}{
578+
{
579+
name: "Should delete policy if it exists",
580+
expectModified: true,
581+
input: func() *admissionregistrationv1.ValidatingAdmissionPolicy {
582+
policy := defaultPolicy.DeepCopy()
583+
return policy
584+
},
585+
existing: func() *admissionregistrationv1.ValidatingAdmissionPolicy {
586+
policy := defaultPolicy.DeepCopy()
587+
return policy
588+
},
589+
expectedEvents: []string{deleteEvent},
590+
},
591+
{
592+
name: "Should do nothing if policy does not exist",
593+
expectModified: false,
594+
input: func() *admissionregistrationv1.ValidatingAdmissionPolicy {
595+
policy := defaultPolicy.DeepCopy()
596+
return policy
597+
},
598+
expectedEvents: []string{},
599+
},
600+
}
601+
602+
for _, test := range tests {
603+
t.Run(test.name, func(t *testing.T) {
604+
605+
existingHooks := []runtime.Object{}
606+
if test.existing != nil {
607+
existingHooks = append(existingHooks, test.existing())
608+
}
609+
client := fake.NewSimpleClientset(existingHooks...)
610+
recorder := events.NewInMemoryRecorder("test", clocktesting.NewFakePassiveClock(time.Now()))
611+
612+
testApply := func(expectModify bool) {
613+
updatedHook, modified, err := DeleteValidatingAdmissionPolicyV1(
614+
context.TODO(),
615+
client.AdmissionregistrationV1(),
616+
recorder, test.input())
617+
if err != nil {
618+
t.Fatal(err)
619+
}
620+
if expectModify != modified {
621+
t.Errorf("expected modified to be equal %v, got %v: %#v", expectModify, modified, updatedHook)
622+
}
623+
}
624+
625+
testApply(test.expectModified)
626+
assertEvents(t, test.name, test.expectedEvents, recorder.Events())
627+
})
628+
}
629+
}
630+
631+
func TestDeleteValidatingAdmissionPolicyBinding(t *testing.T) {
632+
defaultPolicyBinding := &admissionregistrationv1.ValidatingAdmissionPolicyBinding{}
633+
defaultPolicyBinding.SetName("test")
634+
deleteEvent := "ValidatingAdmissionPolicyBindingDeleted"
635+
636+
tests := []struct {
637+
name string
638+
expectModified bool
639+
existing func() *admissionregistrationv1.ValidatingAdmissionPolicyBinding
640+
input func() *admissionregistrationv1.ValidatingAdmissionPolicyBinding
641+
checkUpdated func(*admissionregistrationv1.ValidatingAdmissionPolicyBinding) error
642+
expectedEvents []string
643+
}{
644+
{
645+
name: "Should delete policy binding if it exists",
646+
expectModified: true,
647+
input: func() *admissionregistrationv1.ValidatingAdmissionPolicyBinding {
648+
policyBinding := defaultPolicyBinding.DeepCopy()
649+
return policyBinding
650+
},
651+
existing: func() *admissionregistrationv1.ValidatingAdmissionPolicyBinding {
652+
policyBinding := defaultPolicyBinding.DeepCopy()
653+
return policyBinding
654+
},
655+
expectedEvents: []string{deleteEvent},
656+
},
657+
{
658+
name: "Should do nothing if policy binding does not exist",
659+
expectModified: false,
660+
input: func() *admissionregistrationv1.ValidatingAdmissionPolicyBinding {
661+
policyBinding := defaultPolicyBinding.DeepCopy()
662+
return policyBinding
663+
},
664+
expectedEvents: []string{},
665+
},
666+
}
667+
668+
for _, test := range tests {
669+
t.Run(test.name, func(t *testing.T) {
670+
671+
existingHooks := []runtime.Object{}
672+
if test.existing != nil {
673+
existingHooks = append(existingHooks, test.existing())
674+
}
675+
client := fake.NewSimpleClientset(existingHooks...)
676+
recorder := events.NewInMemoryRecorder("test", clocktesting.NewFakePassiveClock(time.Now()))
677+
678+
testApply := func(expectModify bool) {
679+
updatedHook, modified, err := DeleteValidatingAdmissionPolicyBindingV1(
680+
context.TODO(),
681+
client.AdmissionregistrationV1(),
682+
recorder, test.input())
683+
if err != nil {
684+
t.Fatal(err)
685+
}
686+
if expectModify != modified {
687+
t.Errorf("expected modified to be equal %v, got %v: %#v", expectModify, modified, updatedHook)
688+
}
689+
}
690+
691+
testApply(test.expectModified)
692+
assertEvents(t, test.name, test.expectedEvents, recorder.Events())
693+
})
694+
}
695+
}
696+
565697
func assertEvents(t *testing.T, testCase string, expectedReasons []string, events []*corev1.Event) {
566698
if len(expectedReasons) != len(events) {
567699
t.Errorf(

pkg/operator/resource/resourceapply/generic.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,30 @@ func DeleteAll(ctx context.Context, clients *ClientHolder, recorder events.Recor
380380
} else {
381381
_, result.Changed, result.Error = DeleteValidatingWebhookConfiguration(ctx, clients.kubeClient.AdmissionregistrationV1(), recorder, t)
382382
}
383+
case *admissionregistrationv1beta1.ValidatingAdmissionPolicy:
384+
if clients.kubeClient == nil {
385+
result.Error = fmt.Errorf("missing kubeClient")
386+
} else {
387+
_, result.Changed, result.Error = DeleteValidatingAdmissionPolicyV1beta1(ctx, clients.kubeClient.AdmissionregistrationV1beta1(), recorder, t)
388+
}
389+
case *admissionregistrationv1beta1.ValidatingAdmissionPolicyBinding:
390+
if clients.kubeClient == nil {
391+
result.Error = fmt.Errorf("missing kubeClient")
392+
} else {
393+
_, result.Changed, result.Error = DeleteValidatingAdmissionPolicyBindingV1beta1(ctx, clients.kubeClient.AdmissionregistrationV1beta1(), recorder, t)
394+
}
395+
case *admissionregistrationv1.ValidatingAdmissionPolicy:
396+
if clients.kubeClient == nil {
397+
result.Error = fmt.Errorf("missing kubeClient")
398+
} else {
399+
_, result.Changed, result.Error = DeleteValidatingAdmissionPolicyV1(ctx, clients.kubeClient.AdmissionregistrationV1(), recorder, t)
400+
}
401+
case *admissionregistrationv1.ValidatingAdmissionPolicyBinding:
402+
if clients.kubeClient == nil {
403+
result.Error = fmt.Errorf("missing kubeClient")
404+
} else {
405+
_, result.Changed, result.Error = DeleteValidatingAdmissionPolicyBindingV1(ctx, clients.kubeClient.AdmissionregistrationV1(), recorder, t)
406+
}
383407
case *storagev1.CSIDriver:
384408
if clients.kubeClient == nil {
385409
result.Error = fmt.Errorf("missing kubeClient")

0 commit comments

Comments
 (0)