Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure controller is scoped to namespace #383

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions controller/generic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"
"time"

"github.com/rancher/lasso/pkg/controller"

errors2 "github.com/pkg/errors"
"github.com/rancher/lasso/pkg/controller"
"github.com/rancher/norman/metrics"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
Expand All @@ -28,13 +28,15 @@ type genericController struct {
controller controller.SharedController
informer cache.SharedIndexInformer
name string
namespace string
}

func NewGenericController(name string, controller controller.SharedController) GenericController {
func NewGenericController(namespace, name string, controller controller.SharedController) GenericController {
return &genericController{
controller: controller,
informer: controller.Informer(),
name: name,
namespace: namespace,
}
}

Expand All @@ -52,6 +54,9 @@ func (g *genericController) EnqueueAfter(namespace, name string, after time.Dura

func (g *genericController) AddHandler(ctx context.Context, name string, handler HandlerFunc) {
g.controller.RegisterHandler(ctx, name, controller.SharedControllerHandlerFunc(func(key string, obj runtime.Object) (runtime.Object, error) {
if !isNamespace(g.namespace, obj) {
return obj, nil
}
logrus.Tracef("%s calling handler %s %s", g.name, name, key)
metrics.IncTotalHandlerExecution(g.name, name)
result, err := handler(key, obj)
Expand All @@ -67,6 +72,17 @@ func (g *genericController) AddHandler(ctx context.Context, name string, handler
}))
}

func isNamespace(namespace string, obj runtime.Object) bool {
if namespace == "" {
return true
}
meta, err := meta.Accessor(obj)
if err != nil {
return false
}
return meta.GetNamespace() == namespace
}

func ignoreError(err error, checkString bool) bool {
err = errors2.Cause(err)
if errors.IsConflict(err) {
Expand Down
2 changes: 1 addition & 1 deletion generator/controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c {{.schema.ID}}Factory) List() runtime.Object {
}

func (s *{{.schema.ID}}Client) Controller() {{.schema.CodeName}}Controller {
genericController := controller.NewGenericController({{.schema.CodeName}}GroupVersionKind.Kind+"Controller",
genericController := controller.NewGenericController(s.ns, {{.schema.CodeName}}GroupVersionKind.Kind+"Controller",
s.client.controllerFactory.ForResourceKind({{.schema.CodeName}}GroupVersionResource, {{.schema.CodeName}}GroupVersionKind.Kind, {{.schema | namespaced}}))

return &{{.schema.ID}}Controller{
Expand Down