|
| 1 | +/* |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | +// +kubebuilder:docs-gen:collapse=Apache License |
| 16 | + |
| 17 | +/* |
| 18 | +First, we start out with some standard imports. |
| 19 | +As before, we need the core controller-runtime library, as well as |
| 20 | +the client package, and the package for our API types. |
| 21 | +*/ |
| 22 | +package controllers |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + |
| 27 | + "github.com/go-logr/logr" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + batchv1 "tutorial.kubebuilder.io/project/api/v1" |
| 32 | +) |
| 33 | + |
| 34 | +// +kubebuilder:docs-gen:collapse=Imports |
| 35 | + |
| 36 | +/* |
| 37 | +The code snippet below shows skeleton code for implementing a finalizer. |
| 38 | +*/ |
| 39 | + |
| 40 | +func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { |
| 41 | + ctx := context.Background() |
| 42 | + log := r.Log.WithValues("cronjob", req.NamespacedName) |
| 43 | + |
| 44 | + var cronJob batch.CronJob |
| 45 | + if err := r.Get(ctx, req.NamespacedName, &cronJob); err != nil { |
| 46 | + log.Error(err, "unable to fetch CronJob") |
| 47 | + // we'll ignore not-found errors, since they can't be fixed by an immediate |
| 48 | + // requeue (we'll need to wait for a new notification), and we can get them |
| 49 | + // on deleted requests. |
| 50 | + return ctrl.Result{}, ignoreNotFound(err) |
| 51 | + } |
| 52 | + |
| 53 | + // name of our custom finalizer |
| 54 | + myFinalizerName := "storage.finalizers.tutorial.kubebuilder.io" |
| 55 | + |
| 56 | + // examine DeletionTimestamp to determine if object is under deletion |
| 57 | + if cronJob.ObjectMeta.DeletionTimestamp.IsZero() { |
| 58 | + // The object is not being deleted, so if it does not have our finalizer, |
| 59 | + // then lets add the finalizer and update the object. This is equivalent |
| 60 | + // registering our finalizer. |
| 61 | + if !containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) { |
| 62 | + cronJob.ObjectMeta.Finalizers = append(cronJob.ObjectMeta.Finalizers, myFinalizerName) |
| 63 | + if err := r.Update(context.Background(), cronJob); err != nil { |
| 64 | + return ctrl.Result{}, err |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + // The object is being deleted |
| 69 | + if containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) { |
| 70 | + // our finalizer is present, so lets handle any external dependency |
| 71 | + if err := r.deleteExternalResources(cronJob); err != nil { |
| 72 | + // if fail to delete the external dependency here, return with error |
| 73 | + // so that it can be retried |
| 74 | + return ctrl.Result{}, err |
| 75 | + } |
| 76 | + |
| 77 | + // remove our finalizer from the list and update it. |
| 78 | + cronJob.ObjectMeta.Finalizers = removeString(cronJob.ObjectMeta.Finalizers, myFinalizerName) |
| 79 | + if err := r.Update(context.Background(), cronJob); err != nil { |
| 80 | + return ctrl.Result{}, err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ctrl.Result{}, err |
| 85 | + } |
| 86 | + |
| 87 | + // rest of the reconciler code |
| 88 | +} |
| 89 | + |
| 90 | +func (r *Reconciler) deleteExternalResources(cronJob *batch.CronJob) error { |
| 91 | + // |
| 92 | + // delete any external resources associated with the cronJob |
| 93 | + // |
| 94 | + // Ensure that delete implementation is idempotent and safe to invoke |
| 95 | + // multiple types for same object. |
| 96 | +} |
| 97 | + |
| 98 | +// Helper functions to check and remove string from a slice of strings. |
| 99 | +func containsString(slice []string, s string) bool { |
| 100 | + for _, item := range slice { |
| 101 | + if item == s { |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + return false |
| 106 | +} |
| 107 | + |
| 108 | +func removeString(slice []string, s string) (result []string) { |
| 109 | + for _, item := range slice { |
| 110 | + if item == s { |
| 111 | + continue |
| 112 | + } |
| 113 | + result = append(result, item) |
| 114 | + } |
| 115 | + return |
| 116 | +} |
0 commit comments