Skip to content

Commit

Permalink
use finalizer function
Browse files Browse the repository at this point in the history
  • Loading branch information
kanolato committed Aug 11, 2020
1 parent f7a3b65 commit f3fb525
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/book/src/cronjob-tutorial/testdata/finalizer_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) {
cronJob.ObjectMeta.Finalizers = append(cronJob.ObjectMeta.Finalizers, myFinalizerName)
if !containsString(cronJob.GetFinalizers(), myFinalizerName) {
cronJob.SetFinalizers(append(cronJob.GetFinalizers(), myFinalizerName))
if err := r.Update(context.Background(), cronJob); err != nil {
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) {
if containsString(cronJob.GetFinalizers(), myFinalizerName) {
// our finalizer is present, so lets handle any external dependency
if err := r.deleteExternalResources(cronJob); err != nil {
// if fail to delete the external dependency here, return with error
Expand All @@ -75,7 +75,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

// remove our finalizer from the list and update it.
cronJob.ObjectMeta.Finalizers = removeString(cronJob.ObjectMeta.Finalizers, myFinalizerName)
cronJob.SetFinalizers(removeString(cronJob.GetFinalizers(), myFinalizerName))
if err := r.Update(context.Background(), cronJob); err != nil {
return ctrl.Result{}, err
}
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module sigs.k8s.io/kubebuilder
go 1.13

require (
github.com/go-logr/logr v0.2.0
github.com/gobuffalo/flect v0.2.1
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v0.0.7
github.com/spf13/pflag v1.0.5
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b
k8s.io/apimachinery v0.18.6
k8s.io/client-go v11.0.0+incompatible
sigs.k8s.io/controller-runtime v0.6.2
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit f3fb525

Please sign in to comment.