From f9f8f599d4d95ab4b0e55ae1e803604cab928578 Mon Sep 17 00:00:00 2001 From: gregoryguillou Date: Sun, 5 Jan 2020 12:07:17 +0100 Subject: [PATCH] Remove ignoreNotFound and rely on client --- .../testdata/finalizer_example.go | 2 +- .../project/controllers/cronjob_controller.go | 23 ++++--------------- .../controller/cronjob/cronjob_controller.go | 17 ++------------ .../project/controllers/cronjob_controller.go | 19 ++------------- 4 files changed, 9 insertions(+), 52 deletions(-) diff --git a/docs/book/src/cronjob-tutorial/testdata/finalizer_example.go b/docs/book/src/cronjob-tutorial/testdata/finalizer_example.go index dc95348fd31..fbab9cbaf7a 100644 --- a/docs/book/src/cronjob-tutorial/testdata/finalizer_example.go +++ b/docs/book/src/cronjob-tutorial/testdata/finalizer_example.go @@ -47,7 +47,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. - return ctrl.Result{}, ignoreNotFound(err) + return ctrl.Result{}, client.IgnoreNotFound(err) } // name of our custom finalizer diff --git a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go b/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go index 212002e10bb..6e74a9061d7 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go @@ -30,7 +30,6 @@ import ( "github.com/robfig/cron" kbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - apierrs "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ref "k8s.io/client-go/tools/reference" @@ -71,20 +70,6 @@ type Clock interface { // +kubebuilder:docs-gen:collapse=Clock -/* -We generally want to ignore (not requeue) NotFound errors, since we'll get a -reconciliation request once the object exists, and requeuing in the meantime -won't help. -*/ -func ignoreNotFound(err error) error { - if apierrs.IsNotFound(err) { - return nil - } - return err -} - -// +kubebuilder:docs-gen:collapse=ignoreNotFound - /* Notice that we need a few more RBAC permissions -- since we're creating and managing jobs now, we'll need permissions for those, which means adding @@ -125,7 +110,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. - return ctrl.Result{}, ignoreNotFound(err) + return ctrl.Result{}, client.IgnoreNotFound(err) } /* @@ -280,7 +265,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit { break } - if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil { + if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil { log.Error(err, "unable to delete old failed job", "job", job) } else { log.V(0).Info("deleted old failed job", "job", job) @@ -299,7 +284,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit { break } - if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil { + if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); (err) != nil { log.Error(err, "unable to delete old successful job", "job", job) } else { log.V(0).Info("deleted old successful job", "job", job) @@ -446,7 +431,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { if cronJob.Spec.ConcurrencyPolicy == batch.ReplaceConcurrent { for _, activeJob := range activeJobs { // we don't care if the job was already deleted - if err := r.Delete(ctx, activeJob, client.PropagationPolicy(metav1.DeletePropagationBackground)); ignoreNotFound(err) != nil { + if err := r.Delete(ctx, activeJob, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil { log.Error(err, "unable to delete active job", "job", activeJob) return ctrl.Result{}, err } diff --git a/docs/book/src/migration/testdata/gopath/project-v1/pkg/controller/cronjob/cronjob_controller.go b/docs/book/src/migration/testdata/gopath/project-v1/pkg/controller/cronjob/cronjob_controller.go index f508c6e8d52..ef31e3384b1 100644 --- a/docs/book/src/migration/testdata/gopath/project-v1/pkg/controller/cronjob/cronjob_controller.go +++ b/docs/book/src/migration/testdata/gopath/project-v1/pkg/controller/cronjob/cronjob_controller.go @@ -28,7 +28,6 @@ import ( kbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - apierrs "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/runtime" @@ -108,18 +107,6 @@ type Clock interface { Now() time.Time } -/* -We generally want to ignore (not requeue) NotFound errors, since we'll get a -reconciliation request once the object exists, and requeuing in the meantime -won't help. -*/ -func ignoreNotFound(err error) error { - if apierrs.IsNotFound(err) { - return nil - } - return err -} - var ( scheduledTimeAnnotation = "batch.tutorial.kubebuilder.io/scheduled-at" ) @@ -142,7 +129,7 @@ func (r *ReconcileCronJob) Reconcile(req reconcile.Request) (reconcile.Result, e // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. - return ctrl.Result{}, ignoreNotFound(err) + return ctrl.Result{}, client.IgnoreNotFound(err) } var childJobs kbatch.JobList @@ -372,7 +359,7 @@ func (r *ReconcileCronJob) Reconcile(req reconcile.Request) (reconcile.Result, e if cronJob.Spec.ConcurrencyPolicy == batchv1.ReplaceConcurrent { for _, activeJob := range activeJobs { // we don't care if the job was already deleted - if err := r.Delete(ctx, activeJob); ignoreNotFound(err) != nil { + if err := r.Delete(ctx, activeJob); client.IgnoreNotFound(err) != nil { log.Error(err, "unable to delete active job", "job", activeJob) return ctrl.Result{}, err } diff --git a/docs/book/src/multiversion-tutorial/testdata/project/controllers/cronjob_controller.go b/docs/book/src/multiversion-tutorial/testdata/project/controllers/cronjob_controller.go index d70b1e729fe..5b49d9dd3ad 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/controllers/cronjob_controller.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/controllers/cronjob_controller.go @@ -30,7 +30,6 @@ import ( "github.com/robfig/cron" kbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - apierrs "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ref "k8s.io/client-go/tools/reference" @@ -71,20 +70,6 @@ type Clock interface { // +kubebuilder:docs-gen:collapse=Clock -/* -We generally want to ignore (not requeue) NotFound errors, since we'll get a -reconciliation request once the object exists, and requeuing in the meantime -won't help. -*/ -func ignoreNotFound(err error) error { - if apierrs.IsNotFound(err) { - return nil - } - return err -} - -// +kubebuilder:docs-gen:collapse=ignoreNotFound - /* Notice that we need a few more RBAC permissions -- since we're creating and managing jobs now, we'll need permissions for those, which means adding @@ -125,7 +110,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. - return ctrl.Result{}, ignoreNotFound(err) + return ctrl.Result{}, client.IgnoreNotFound(err) } /* @@ -442,7 +427,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { if cronJob.Spec.ConcurrencyPolicy == batch.ReplaceConcurrent { for _, activeJob := range activeJobs { // we don't care if the job was already deleted - if err := r.Delete(ctx, activeJob); ignoreNotFound(err) != nil { + if err := r.Delete(ctx, activeJob); client.IgnoreNotFound(err) != nil { log.Error(err, "unable to delete active job", "job", activeJob) return ctrl.Result{}, err }