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

chore: Limit cache with selector app=kubernetes-image-puller #90

Merged
merged 1 commit into from
Jan 17, 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
40 changes: 39 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ package main
import (
"flag"
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"os"

"sigs.k8s.io/controller-runtime/pkg/cache"
// 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"
Expand Down Expand Up @@ -90,6 +94,12 @@ func main() {
"the manager will watch and manage resources in all Namespaces")
}

cacheFunction, err := getCacheFunc()
if err != nil {
setupLog.Error(err, "failed to create cache function")
os.Exit(1)
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand All @@ -98,6 +108,7 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: "06ff708d.eclipse.che",
Namespace: watchNamespace,
NewCache: cacheFunction,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -131,3 +142,30 @@ func main() {
os.Exit(1)
}
}

// getCacheFunc allows to limit cache for objects by label selector.
func getCacheFunc() (cache.NewCacheFunc, error) {
kubernetesImagePullerApp, err := labels.NewRequirement("app", selection.Equals, []string{"kubernetes-image-puller"})
if err != nil {
return nil, err
}
kubernetesImagePullerAppSelector := labels.NewSelector().Add(*kubernetesImagePullerApp)

setupLog.Info("Cache with selector", "app", "kubernetes-image-puller")

selectors := cache.SelectorsByObject{
&appsv1.Deployment{}: {
Label: kubernetesImagePullerAppSelector,
},
&corev1.Pod{}: {
Label: kubernetesImagePullerAppSelector,
},
&corev1.ConfigMap{}: {
Label: kubernetesImagePullerAppSelector,
},
}

return cache.BuilderWithOptions(cache.Options{
SelectorsByObject: selectors,
}), nil
}