Skip to content

Commit

Permalink
make deletion reconciler optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ilackarms committed Apr 11, 2020
1 parent e1db1f4 commit 8ff15b9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
13 changes: 11 additions & 2 deletions codegen/templates/code/controller/reconcilers.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ import (

{{- $kindLowerCamel := lower_camel $resource.Kind }}

// Reconcile the {{ $resource.Kind }} Resource
// Reconcile Upsert events for the {{ $resource.Kind }} Resource.
// implemented by the user
type {{ $resource.Kind }}Reconciler interface {
Reconcile{{ $resource.Kind }}(obj *{{ group_import_name $ }}.{{ $resource.Kind }}) (reconcile.Result, error)
}

// Reconcile deletion events for the {{ $resource.Kind }} Resource.
// Deletion receives a reconcile.Request as we cannot guarantee the last state of the object
// before being deleted.
// implemented by the user
type {{ $resource.Kind }}DeletionReconciler interface {
Reconcile{{ $resource.Kind }}Deletion(req reconcile.Request)
}

Expand Down Expand Up @@ -103,7 +110,9 @@ func (r generic{{ $resource.Kind }}Reconciler) Reconcile(object ezkube.Object) (
}

func (r generic{{ $resource.Kind }}Reconciler) ReconcileDeletion(request reconcile.Request) {
r.reconciler.Reconcile{{ $resource.Kind }}Deletion(request)
if deletionReconciler, ok := r.reconciler.({{ $resource.Kind }}DeletionReconciler); ok {
deletionReconciler.Reconcile{{ $resource.Kind }}Deletion(request)
}
}

// generic{{ $resource.Kind }}Finalizer implements a generic reconcile.FinalizingReconciler
Expand Down
15 changes: 12 additions & 3 deletions codegen/test/api/things.test.io/v1/controller/reconcilers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import (
"context"

things_test_io_v1 "github.com/solo-io/skv2/codegen/test/api/things.test.io/v1"
"github.com/solo-io/skv2/pkg/ezkube"

"github.com/pkg/errors"
"github.com/solo-io/skv2/pkg/ezkube"
"github.com/solo-io/skv2/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// Reconcile the Paint Resource
// Reconcile Upsert events for the Paint Resource.
// implemented by the user
type PaintReconciler interface {
ReconcilePaint(obj *things_test_io_v1.Paint) (reconcile.Result, error)
}

// Reconcile deletion events for the Paint Resource.
// Deletion receives a reconcile.Request as we cannot guarantee the last state of the object
// before being deleted.
// implemented by the user
type PaintDeletionReconciler interface {
ReconcilePaintDeletion(req reconcile.Request)
}

Expand Down Expand Up @@ -98,7 +105,9 @@ func (r genericPaintReconciler) Reconcile(object ezkube.Object) (reconcile.Resul
}

func (r genericPaintReconciler) ReconcileDeletion(request reconcile.Request) {
r.reconciler.ReconcilePaintDeletion(request)
if deletionReconciler, ok := r.reconciler.(PaintDeletionReconciler); ok {
deletionReconciler.ReconcilePaintDeletion(request)
}
}

// genericPaintFinalizer implements a generic reconcile.FinalizingReconciler
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Masterminds/sprig/v3 v3.0.0
github.com/coreos/go-systemd/v22 v22.0.0 // indirect
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/fsnotify/fsnotify v1.4.7
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/gertd/go-pluralize v0.1.1
github.com/go-logr/logr v0.1.0
github.com/gobuffalo/envy v1.8.1 // indirect
Expand All @@ -27,8 +27,8 @@ require (
github.com/onsi/gomega v1.8.1
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.2.1
github.com/prometheus/common v0.7.0
github.com/prometheus/client_golang v1.2.1 // indirect
github.com/prometheus/common v0.7.0 // indirect
github.com/pseudomuto/protoc-gen-doc v1.3.0 // indirect
github.com/pseudomuto/protokit v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.5.2
Expand All @@ -37,9 +37,9 @@ require (
github.com/solo-io/go-utils v0.11.7
github.com/solo-io/protoc-gen-ext v0.0.7
github.com/solo-io/solo-kit v0.12.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
go.uber.org/atomic v1.5.0
github.com/spf13/cobra v0.0.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.5.0 // indirect
go.uber.org/zap v1.13.0
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect
golang.org/x/sys v0.0.0-20200117145432-59e60aa80a0c // indirect
Expand All @@ -50,8 +50,8 @@ require (
k8s.io/apimachinery v0.17.2
k8s.io/client-go v8.0.0+incompatible
k8s.io/code-generator v0.17.2
k8s.io/gengo v0.0.0-20190822140433-26a664648505
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f
k8s.io/gengo v0.0.0-20190822140433-26a664648505 // indirect
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f // indirect
sigs.k8s.io/controller-runtime v0.5.1
sigs.k8s.io/yaml v1.1.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,9 @@ k8s.io/code-generator v0.0.0-20191004115455-8e001e5d1894 h1:NMYlxaF7rYQJk2E2IyrU
k8s.io/code-generator v0.0.0-20191004115455-8e001e5d1894/go.mod h1:mJUgkl06XV4kstAnLHAIzJPVCOzVR+ZcfPIv4fUsFCY=
k8s.io/code-generator v0.17.1 h1:e3B1UqRzRUWygp7WD+QTRT3ZUahPIaRKF0OFa7duQwI=
k8s.io/code-generator v0.17.1/go.mod h1:DVmfPQgxQENqDIzVR2ddLXMH34qeszkKSdH/N+s+38s=
k8s.io/code-generator v0.17.2 h1:pTwl3rLB1fUyxmvEzmVPMM0tBSdUehd7z+bDzpj4lPE=
k8s.io/code-generator v0.17.2/go.mod h1:DVmfPQgxQENqDIzVR2ddLXMH34qeszkKSdH/N+s+38s=
k8s.io/code-generator v0.18.1 h1:emaWQKnyNOhMgmp6NKxGjB3p9cCLylM+fAir45F2ZlQ=
k8s.io/component-base v0.0.0-20190918160511-547f6c5d7090/go.mod h1:933PBGtQFJky3TEwYx4aEPZ4IxqhWh3R6DCmzqIn1hA=
k8s.io/component-base v0.0.0-20191004121406-d5138742ad72/go.mod h1:zT8T6A3K4wLlbQkLUC62skjmWoiNJ9B8WUQj3KIvcrQ=
k8s.io/component-base v0.0.0-20191004121439-41066ddd0b23/go.mod h1:9R0GX/ZjvQBIzteo+978HZ8AyiJkJeSPhGreRhUngDg=
Expand Down
6 changes: 5 additions & 1 deletion pkg/reconcile/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type Reconciler interface {
// reconcile an object
// requeue the object if returning an error, or a non-zero "requeue-after" duration
Reconcile(object ezkube.Object) (Result, error)
}

type DeletionReconciler interface {
// we received a reconcile request for an object that was removed from the cache
ReconcileDeletion(request Request)
}
Expand Down Expand Up @@ -115,7 +117,9 @@ func (ec *runnerReconciler) Reconcile(request Request) (reconcile.Result, error)
logger.V(2).Info(fmt.Sprintf("unable to fetch %T", obj), "request", request, "err", err)

// call OnDelete
ec.reconciler.ReconcileDeletion(request)
if deletionReconciler, ok := ec.reconciler.(DeletionReconciler); ok {
deletionReconciler.ReconcileDeletion(request)
}

return reconcile.Result{}, nil
}
Expand Down

0 comments on commit 8ff15b9

Please sign in to comment.