Skip to content

Commit

Permalink
Add optional namespace filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Olshevski committed Dec 28, 2023
1 parent 4bdc8f2 commit 49beafa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/eno-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/Azure/eno/internal/manager"
)

// TODO: Expose leader election options
// TODO: Expose leader election and other manager options

func main() {
if err := run(); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions cmd/eno-reconciler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ func main() {
}
}

// TODO: Label filters, etc.

func run() error {
ctx := ctrl.SetupSignalHandler()
var (
rediscoverWhenNotFound bool
writeBatchInterval time.Duration
discoveryMaxRPS float32

mgrOpts = &manager.Options{
Rest: ctrl.GetConfigOrDie(),
}
)
flag.BoolVar(&rediscoverWhenNotFound, "rediscover-when-not-found", true, "Invalidate discovery cache when any type is not found in the openapi spec. Set this to false on <= k8s 1.14")
flag.DurationVar(&writeBatchInterval, "write-batch-interval", time.Second*5, "The max throughput of composition status updates")
mgrOpts.Bind(flag.CommandLine)
flag.Parse()

zl, err := zap.NewProduction()
Expand All @@ -41,9 +44,7 @@ func run() error {
}
logger := zapr.NewLogger(zl)

mgr, err := manager.New(logger, &manager.Options{
Rest: ctrl.GetConfigOrDie(),
})
mgr, err := manager.New(logger, mgrOpts)
if err != nil {
return fmt.Errorf("constructing manager: %w", err)
}
Expand Down
40 changes: 34 additions & 6 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package manager

import (
"context"
"flag"
"fmt"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -33,23 +36,48 @@ const (

type Options struct {
Rest *rest.Config
Namespace string
HealthProbeAddr string
MetricsAddr string
}

func (o *Options) Bind(set *flag.FlagSet) {
flag.StringVar(&o.Namespace, "namespace", "", "Only reconcile resources in a particular namespace")
}

func New(logger logr.Logger, opts *Options) (ctrl.Manager, error) {
mgr, err := ctrl.NewManager(opts.Rest, manager.Options{
mgrOpts := manager.Options{
Logger: logger,
HealthProbeBindAddress: opts.HealthProbeAddr,
Metrics: server.Options{
BindAddress: opts.MetricsAddr,
},
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Pod{}: {Label: labels.SelectorFromSet(labels.Set{ManagerLabelKey: ManagerLabelValue})},
}

podLabelSelector := labels.SelectorFromSet(labels.Set{ManagerLabelKey: ManagerLabelValue})
if opts.Namespace == "" {
mgrOpts.Cache.ByObject = map[client.Object]cache.ByObject{
&corev1.Pod{}: {Label: podLabelSelector},
}
} else {
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.namespace=%s", opts.Namespace))

mgrOpts.Cache.ByObject = map[client.Object]cache.ByObject{
&corev1.Pod{}: {
Label: podLabelSelector,
Field: fieldSelector,
},
},
})
}

mgrOpts.Cache.DefaultNamespaces = map[string]cache.Config{
opts.Namespace: {
LabelSelector: labels.Everything(),
FieldSelector: fieldSelector,
},
}
}

mgr, err := ctrl.NewManager(opts.Rest, mgrOpts)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 49beafa

Please sign in to comment.