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

Introduce object selector to restrict controller list watch #59

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
promapi "github.com/prometheus/client_golang/api"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -102,6 +104,8 @@ func main() {
probeAddr string
enableLeaderElection bool
reconcileConcurrency int
objectLabelSelector string
objectFieldSelector string

enableAdmissionWebhookServer bool

Expand All @@ -119,6 +123,8 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&reconcileConcurrency, "reconcile-concurrency", 1, "The reconciliation concurrency of each controller.")
flag.StringVar(&objectLabelSelector, "object-label-selector", "", "The label selector to restrict controllers' list watch for objects.")
flag.StringVar(&objectFieldSelector, "object-field-selector", "", "The field selector to restrict controllers' list watch for objects.")
flag.BoolVar(&enableAdmissionWebhookServer, "serve-admission-webhooks", true, "Enable admission webhook servers.")
flag.StringVar(&grpcServerAddr, "grpc-server-bind-address", ":9090",
"The address the gRPC server binds to. Set 0 to disable the gRPC server.")
Expand Down Expand Up @@ -165,6 +171,25 @@ func main() {
cfg := ctrl.GetConfigOrDie()
rest.AddUserAgent(cfg, util.UserAgent)

var (
cacheObjectSelector = cache.ObjectSelector{}
err error
)
if objectLabelSelector != "" {
cacheObjectSelector.Label, err = labels.Parse(objectLabelSelector)
if err != nil {
setupLog.Error(err, "failed to parse object label selector", "selector", objectLabelSelector)
os.Exit(1)
}
}
if objectFieldSelector != "" {
cacheObjectSelector.Field, err = fields.ParseSelector(objectFieldSelector)
if err != nil {
setupLog.Error(err, "failed to parse object field selector", "selector", objectFieldSelector)
os.Exit(1)
}
}

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand All @@ -185,6 +210,10 @@ func main() {
"HorizontalPortrait.autoscaling.kapacitystack.io": reconcileConcurrency,
},
},
NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.DefaultSelector = cacheObjectSelector
return cache.New(config, opts)
},
})
if err != nil {
setupLog.Error(err, "unable to build manager")
Expand Down