Created
September 19, 2018 10:39
-
-
Save daviddyball/349abfff3d7a127d7d35f1f269411aad to your computer and use it in GitHub Desktop.
Kubebuilder Finalizer Attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (r *MyReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) { | |
instance := &v1beta1.MyInstance{} | |
err := r.Get(context.TODO(), request.NamespacedName, instance) | |
if err != nil { | |
if errors.IsNotFound(err) { | |
// Object not found, return | |
return reconcile.Result{}, nil | |
} | |
return reconcile.Result{}, err | |
} | |
if instance.ObjectMeta.DeletionTimestamp != nil { | |
// Instance is scheduled for deletion, but probably has finalizers | |
switch instance.ObjectMeta.Finalizers[0] { | |
case "delete.remote.resources": | |
_, err := SomeFunctionToDeleteRemoteResources(instance) | |
if err != nil { | |
return reconcile.Result{}, err | |
} | |
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers[:0], instance.ObjectMeta.Finalizers[1:]...) | |
r.Update(context.TODO(), instance) | |
return reconcile.Result{}, nil | |
default: | |
log.Fatalf("Unknown finalizer \"%s\". Cannot process deletion event", current) | |
return reconcile.Result{}, nil | |
} | |
} | |
// Create Remote Resources | |
_, err := SomeFunctionToCreateRemoteResources(instance) | |
if err != nil { | |
return reconcile.Result{}, err | |
} | |
// Add Finalizers to Prevent Deletion without cleaning up remote resources | |
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, "delete.remote.resources") | |
r.Update(context.TODO(), instance) | |
return reconcile.Result{}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment