Skip to content

Commit

Permalink
devbox cache improve. (#5122)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie authored Oct 8, 2024
1 parent 6489ce6 commit 9817f6f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions controllers/devbox/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
Expand Down Expand Up @@ -133,6 +138,11 @@ func main() {
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}

cacheObjLabelSelector := labels.SelectorFromSet(map[string]string{
"app.kubernetes.io/managed-by": "sealos",
"app.kubernetes.io/part-of": "devbox",
})

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsServerOptions,
Expand All @@ -151,6 +161,15 @@ func main() {
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,

NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.ByObject = map[client.Object]cache.ByObject{
&corev1.Service{}: {Label: cacheObjLabelSelector},
&corev1.Pod{}: {Label: cacheObjLabelSelector},
&corev1.Secret{}: {Label: cacheObjLabelSelector},
}
return cache.New(config, opts)
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit 9817f6f

Please sign in to comment.