Skip to content

Commit 5023035

Browse files
authored
Merge pull request #205 from bentoml/fix-role
fix: fix ns watch
2 parents 4b479e6 + f35241b commit 5023035

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

controllers/resources/bentorequest_controller.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ const (
101101
// BentoRequestReconciler reconciles a BentoRequest object
102102
type BentoRequestReconciler struct {
103103
client.Client
104-
Scheme *runtime.Scheme
105-
Recorder record.EventRecorder
104+
Scheme *runtime.Scheme
105+
Recorder record.EventRecorder
106+
NamespaceMap map[string]bool
106107
}
107108

108109
//+kubebuilder:rbac:groups=resources.yatai.ai,resources=bentorequests,verbs=get;list;watch;create;update;patch;delete
@@ -3431,10 +3432,17 @@ func (r *BentoRequestReconciler) SetupWithManager(mgr ctrl.Manager) error {
34313432
logs.Info("yatai component registration is disabled")
34323433
}
34333434

3435+
namespaceFilter := predicate.NewPredicateFuncs(func(object client.Object) bool {
3436+
if r.NamespaceMap != nil {
3437+
return r.NamespaceMap[object.GetNamespace()]
3438+
}
3439+
return true
3440+
})
3441+
34343442
err := ctrl.NewControllerManagedBy(mgr).
34353443
For(&resourcesv1alpha1.BentoRequest{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
3436-
Owns(&resourcesv1alpha1.Bento{}).
3437-
Owns(&batchv1.Job{}).
3444+
Owns(&resourcesv1alpha1.Bento{}, builder.WithPredicates(namespaceFilter)).
3445+
Owns(&batchv1.Job{}, builder.WithPredicates(namespaceFilter)).
34383446
Complete(r)
34393447
return errors.Wrap(err, "failed to setup BentoRequest controller")
34403448
}

helm/yatai-image-builder/templates/deployment.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ spec:
3838
- --metrics-bind-address=:8080
3939
- --leader-elect
4040
- --skip-check={{ .Values.internal.skipCheck }}
41+
- --watch-namespaces={{ .Values.global.deploymentNamespaces | join "," }}
42+
- --watch-all-namespaces={{ .Values.global.monitorAllNamespaces }}
4143
command:
4244
- /manager
4345
env:

main.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"os"
23+
"strings"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
@@ -30,6 +31,7 @@ import (
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3233
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/cache"
3335
"sigs.k8s.io/controller-runtime/pkg/healthz"
3436
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3537

@@ -59,12 +61,16 @@ func main() {
5961
var enableLeaderElection bool
6062
var probeAddr string
6163
var skipCheck bool
64+
var watchNamespaces string
65+
var watchAllNamespaces bool
6266
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6367
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6468
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6569
"Enable leader election for controller manager. "+
6670
"Enabling this will ensure there is only one active controller manager.")
6771
flag.BoolVar(&skipCheck, "skip-check", false, "Skip check")
72+
flag.StringVar(&watchNamespaces, "watch-namespaces", "", "Watch namespaces")
73+
flag.BoolVar(&watchAllNamespaces, "watch-all-namespaces", false, "Watch all namespaces")
6874
opts := zap.Options{
6975
Development: true,
7076
}
@@ -73,7 +79,7 @@ func main() {
7379

7480
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
7581

76-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
82+
crtlOptions := ctrl.Options{
7783
Scheme: scheme,
7884
HealthProbeBindAddress: probeAddr,
7985
LeaderElection: enableLeaderElection,
@@ -89,16 +95,35 @@ func main() {
8995
// if you are doing or is intended to do any operation such as perform cleanups
9096
// after the manager stops then its usage might be unsafe.
9197
// LeaderElectionReleaseOnCancel: true,
92-
})
98+
}
99+
100+
watchNamespacesList := strings.Split(watchNamespaces, ",")
101+
namespaceMap := map[string]cache.Config{}
102+
namespaceMapBool := map[string]bool{}
103+
for _, namespace := range watchNamespacesList {
104+
namespaceMap[namespace] = cache.Config{}
105+
namespaceMapBool[namespace] = true
106+
}
107+
108+
if !watchAllNamespaces {
109+
crtlOptions.Cache = cache.Options{
110+
DefaultNamespaces: namespaceMap,
111+
}
112+
} else {
113+
namespaceMapBool = nil
114+
}
115+
116+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), crtlOptions)
93117
if err != nil {
94118
setupLog.Error(err, "unable to start manager")
95119
os.Exit(1)
96120
}
97121

98122
if err = (&resourcescontrollers.BentoRequestReconciler{
99-
Client: mgr.GetClient(),
100-
Scheme: mgr.GetScheme(),
101-
Recorder: mgr.GetEventRecorderFor("yatai-image-builder"),
123+
Client: mgr.GetClient(),
124+
Scheme: mgr.GetScheme(),
125+
Recorder: mgr.GetEventRecorderFor("yatai-image-builder"),
126+
NamespaceMap: namespaceMapBool,
102127
}).SetupWithManager(mgr); err != nil {
103128
setupLog.Error(err, "unable to create controller", "controller", "BentoRequest")
104129
os.Exit(1)

0 commit comments

Comments
 (0)