@@ -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 -
266316func (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
308358func (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
314364func (r * OpenStackReconciler ) applyOperator (ctx context.Context , instance * operatorv1beta1.OpenStack ) error {
0 commit comments