1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-02 15:54:32 +00:00

Ensure controller is scoped to namespace

This commit is contained in:
Darren Shepherd
2020-08-26 15:28:52 -07:00
parent 9a28dd616d
commit d8c3e21805
2 changed files with 20 additions and 4 deletions

View File

@@ -5,12 +5,12 @@ import (
"strings" "strings"
"time" "time"
"github.com/rancher/lasso/pkg/controller"
errors2 "github.com/pkg/errors" errors2 "github.com/pkg/errors"
"github.com/rancher/lasso/pkg/controller"
"github.com/rancher/norman/metrics" "github.com/rancher/norman/metrics"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
) )
@@ -28,13 +28,15 @@ type genericController struct {
controller controller.SharedController controller controller.SharedController
informer cache.SharedIndexInformer informer cache.SharedIndexInformer
name string name string
namespace string
} }
func NewGenericController(name string, controller controller.SharedController) GenericController { func NewGenericController(namespace, name string, controller controller.SharedController) GenericController {
return &genericController{ return &genericController{
controller: controller, controller: controller,
informer: controller.Informer(), informer: controller.Informer(),
name: name, name: name,
namespace: namespace,
} }
} }
@@ -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) { 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) { 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) logrus.Tracef("%s calling handler %s %s", g.name, name, key)
metrics.IncTotalHandlerExecution(g.name, name) metrics.IncTotalHandlerExecution(g.name, name)
result, err := handler(key, obj) result, err := handler(key, obj)
@@ -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 { func ignoreError(err error, checkString bool) bool {
err = errors2.Cause(err) err = errors2.Cause(err)
if errors.IsConflict(err) { if errors.IsConflict(err) {

View File

@@ -220,7 +220,7 @@ func (c {{.schema.ID}}Factory) List() runtime.Object {
} }
func (s *{{.schema.ID}}Client) Controller() {{.schema.CodeName}}Controller { 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}})) s.client.controllerFactory.ForResourceKind({{.schema.CodeName}}GroupVersionResource, {{.schema.CodeName}}GroupVersionKind.Kind, {{.schema | namespaced}}))
return &{{.schema.ID}}Controller{ return &{{.schema.ID}}Controller{