Skip to content

Commit e0aa70d

Browse files
Merge pull request #1760 from mpatlasov/STOR-1767-support-deletion-of-ValidatingWebhookConfiguration
STOR-1767: Support deletion of ValidatingWebhookConfiguration
2 parents 737dc0f + 8bdcc1d commit e0aa70d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

pkg/operator/resource/resourceapply/admissionregistration.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ func ApplyValidatingWebhookConfigurationImproved(ctx context.Context, client adm
152152
return actual, true, nil
153153
}
154154

155+
func DeleteValidatingWebhookConfiguration(ctx context.Context, client admissionregistrationclientv1.ValidatingWebhookConfigurationsGetter, recorder events.Recorder, required *admissionregistrationv1.ValidatingWebhookConfiguration) (*admissionregistrationv1.ValidatingWebhookConfiguration, bool, error) {
156+
err := client.ValidatingWebhookConfigurations().Delete(ctx, required.Name, metav1.DeleteOptions{})
157+
if err != nil && apierrors.IsNotFound(err) {
158+
return nil, false, nil
159+
}
160+
if err != nil {
161+
return nil, false, err
162+
}
163+
reportDeleteEvent(recorder, required, err)
164+
return nil, true, nil
165+
}
166+
155167
// copyValidatingWebhookCABundle populates webhooks[].clientConfig.caBundle fields from existing resource if it was set before
156168
// and is not set in present. This provides upgrade compatibility with service-ca-bundle operator.
157169
func copyValidatingWebhookCABundle(from, to *admissionregistrationv1.ValidatingWebhookConfiguration) {

pkg/operator/resource/resourceapply/admissionregistration_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,70 @@ func TestApplyValidatingConfiguration(t *testing.T) {
382382
}
383383
}
384384

385+
func TestDeleteValidatingConfiguration(t *testing.T) {
386+
defaultHook := &admissionregistrationv1.ValidatingWebhookConfiguration{}
387+
defaultHook.SetName("test")
388+
deleteEvent := "ValidatingWebhookConfigurationDeleted"
389+
390+
tests := []struct {
391+
name string
392+
expectModified bool
393+
existing func() *admissionregistrationv1.ValidatingWebhookConfiguration
394+
input func() *admissionregistrationv1.ValidatingWebhookConfiguration
395+
expectedEvents []string
396+
}{
397+
{
398+
name: "Should delete webhook if it exists",
399+
expectModified: true,
400+
input: func() *admissionregistrationv1.ValidatingWebhookConfiguration {
401+
hook := defaultHook.DeepCopy()
402+
return hook
403+
},
404+
existing: func() *admissionregistrationv1.ValidatingWebhookConfiguration {
405+
hook := defaultHook.DeepCopy()
406+
return hook
407+
},
408+
expectedEvents: []string{deleteEvent},
409+
},
410+
{
411+
name: "Should do nothing if webhook does not exist",
412+
expectModified: false,
413+
input: func() *admissionregistrationv1.ValidatingWebhookConfiguration {
414+
hook := defaultHook.DeepCopy()
415+
return hook
416+
},
417+
expectedEvents: []string{},
418+
},
419+
}
420+
421+
for _, test := range tests {
422+
t.Run(test.name, func(t *testing.T) {
423+
existingHooks := []runtime.Object{}
424+
if test.existing != nil {
425+
existingHooks = append(existingHooks, test.existing())
426+
}
427+
client := fake.NewSimpleClientset(existingHooks...)
428+
recorder := events.NewInMemoryRecorder("test")
429+
430+
testApply := func(expectModify bool) {
431+
updatedHook, modified, err := DeleteValidatingWebhookConfiguration(
432+
context.TODO(),
433+
client.AdmissionregistrationV1(),
434+
recorder, test.input())
435+
if err != nil {
436+
t.Fatal(err)
437+
}
438+
if expectModify != modified {
439+
t.Errorf("expected modified to be equal %v, got %v: %#v", expectModify, modified, updatedHook)
440+
}
441+
}
442+
443+
testApply(test.expectModified)
444+
assertEvents(t, test.name, test.expectedEvents, recorder.Events())
445+
})
446+
}
447+
}
448+
385449
func TestApplyValidatingAdmissionPolicyConfiguration(t *testing.T) {
386450
defaultPolicy := &admissionregistrationv1beta1.ValidatingAdmissionPolicy{}
387451
defaultPolicy.SetName("test")

pkg/operator/resource/resourceapply/generic.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func DeleteAll(ctx context.Context, clients *ClientHolder, recorder events.Recor
335335
} else {
336336
_, result.Changed, result.Error = DeleteStorageClass(ctx, clients.kubeClient.StorageV1(), recorder, t)
337337
}
338+
case *admissionregistrationv1.ValidatingWebhookConfiguration:
339+
if clients.kubeClient == nil {
340+
result.Error = fmt.Errorf("missing kubeClient")
341+
} else {
342+
_, result.Changed, result.Error = DeleteValidatingWebhookConfiguration(ctx, clients.kubeClient.AdmissionregistrationV1(), recorder, t)
343+
}
338344
case *storagev1.CSIDriver:
339345
if clients.kubeClient == nil {
340346
result.Error = fmt.Errorf("missing kubeClient")

0 commit comments

Comments
 (0)