Skip to content

Commit e523d7a

Browse files
Merge pull request #1286 from dprince/webhook_cleanup
webhook cleanup on init resource delete
2 parents c0465d9 + b686d24 commit e523d7a

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

controllers/operator/openstack_controller.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (r *OpenStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
138138
return ctrl.Result{}, err
139139
}
140140

141-
versionHelper, err := helper.NewHelper(
141+
openstackHelper, err := helper.NewHelper(
142142
instance,
143143
r.Client,
144144
r.Kclient,
@@ -177,13 +177,18 @@ func (r *OpenStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
177177
condition.RestoreLastTransitionTimes(
178178
&instance.Status.Conditions, savedConditions)
179179

180-
err := versionHelper.PatchInstance(ctx, instance)
180+
err := openstackHelper.PatchInstance(ctx, instance)
181181
if err != nil {
182182
_err = err
183183
return
184184
}
185185
}()
186186

187+
// If we're not deleting this and the object doesn't have our finalizer, add it.
188+
if instance.DeletionTimestamp.IsZero() && controllerutil.AddFinalizer(instance, openstackHelper.GetFinalizer()) || isNewInstance {
189+
return ctrl.Result{}, err
190+
}
191+
187192
cl := condition.CreateList(
188193
condition.UnknownCondition(operatorv1beta1.OpenStackOperatorReadyCondition, condition.InitReason, string(operatorv1beta1.OpenStackOperatorReadyInitMessage)),
189194
)
@@ -219,6 +224,10 @@ func (r *OpenStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
219224
}
220225
}
221226

227+
if !instance.DeletionTimestamp.IsZero() {
228+
return r.reconcileDelete(ctx, instance, openstackHelper)
229+
}
230+
222231
// TODO: cleanup obsolete resources here (remove old CSVs, etc)
223232
/*
224233
if err := r.cleanupObsoleteResources(ctx); err != nil {
@@ -262,6 +271,47 @@ func (r *OpenStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
262271

263272
}
264273

274+
func (r *OpenStackReconciler) reconcileDelete(ctx context.Context, instance *operatorv1beta1.OpenStack, helper *helper.Helper) (ctrl.Result, error) {
275+
Log := r.GetLogger(ctx)
276+
Log.Info("Reconciling OpenStack initialization resource delete")
277+
278+
// validating webhook cleanup
279+
valWebhooks, err := r.Kclient.AdmissionregistrationV1().ValidatingWebhookConfigurations().List(ctx, metav1.ListOptions{
280+
LabelSelector: "openstack.openstack.org/managed=true",
281+
})
282+
if err != nil {
283+
return ctrl.Result{}, errors.Wrap(err, "failed listing validating webhook configurations")
284+
}
285+
for _, webhook := range valWebhooks.Items {
286+
err := r.Kclient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Delete(ctx, webhook.Name, metav1.DeleteOptions{})
287+
if err != nil {
288+
return ctrl.Result{}, errors.Wrap(err, "failed to cleanup webhook")
289+
}
290+
fmt.Println("Found ValidatingWebhookConfiguration:", webhook.Name)
291+
292+
}
293+
294+
// mutating webhook cleanup
295+
mutWebhooks, err := r.Kclient.AdmissionregistrationV1().MutatingWebhookConfigurations().List(ctx, metav1.ListOptions{
296+
LabelSelector: "openstack.openstack.org/managed=true",
297+
})
298+
if err != nil {
299+
return ctrl.Result{}, errors.Wrap(err, "failed listing validating webhook configurations")
300+
}
301+
for _, webhook := range mutWebhooks.Items {
302+
err := r.Kclient.AdmissionregistrationV1().MutatingWebhookConfigurations().Delete(ctx, webhook.Name, metav1.DeleteOptions{})
303+
if err != nil {
304+
return ctrl.Result{}, errors.Wrap(err, "failed to cleanup webhook")
305+
}
306+
fmt.Println("Found MutatingWebhookConfiguration:", webhook.Name)
307+
308+
}
309+
310+
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
311+
312+
return ctrl.Result{}, nil
313+
}
314+
265315
// countDeployments -
266316
func (r *OpenStackReconciler) countDeployments(ctx context.Context, instance *operatorv1beta1.OpenStack) (int, error) {
267317
deployments := &appsv1.DeploymentList{}
@@ -308,7 +358,7 @@ func (r *OpenStackReconciler) applyCRDs(ctx context.Context, instance *operatorv
308358
func (r *OpenStackReconciler) applyRBAC(ctx context.Context, instance *operatorv1beta1.OpenStack) error {
309359
data := bindata.MakeRenderData()
310360
data.Data["OperatorNamespace"] = instance.Namespace
311-
return r.renderAndApply(ctx, instance, data, "rbac", false)
361+
return r.renderAndApply(ctx, instance, data, "rbac", true)
312362
}
313363

314364
func (r *OpenStackReconciler) applyOperator(ctx context.Context, instance *operatorv1beta1.OpenStack) error {

pkg/operator/bindata/merge.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ func mergeLabels(current, updated *uns.Unstructured) {
198198
if gvk.Group == "apiextensions.k8s.io" && gvk.Kind == "CustomResourceDefinition" {
199199
curLabels["openstack.openstack.org/crd"] = ""
200200
}
201+
// Validating/Mutating webhooks aren't namespaced meaning we can't own them directly
202+
// via the initialization resource. This adds a custom label so that at least we
203+
// can identify them for cleanup via a finalizer
204+
if gvk.Group == "admissionregistration.k8s.io" && (gvk.Kind == "MutatingWebhookConfiguration" || gvk.Kind == "ValidatingWebhookConfiguration") {
205+
curLabels["openstack.openstack.org/managed"] = "true"
206+
}
201207

202208
updated.SetLabels(curLabels)
203209
}

0 commit comments

Comments
 (0)