@@ -28,11 +28,13 @@ import (
2828 flag "github.com/spf13/pflag"
2929 "helm.sh/helm/v3/pkg/getter"
3030 corev1 "k8s.io/api/core/v1"
31+ "k8s.io/apimachinery/pkg/labels"
3132 "k8s.io/apimachinery/pkg/runtime"
3233 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3334 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3435 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3536 ctrl "sigs.k8s.io/controller-runtime"
37+ ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
3638 ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3739
3840 "github.com/fluxcd/pkg/git"
@@ -45,16 +47,16 @@ import (
4547 "github.com/fluxcd/pkg/runtime/pprof"
4648 "github.com/fluxcd/pkg/runtime/probes"
4749
48- "github.com/fluxcd/source-controller/internal/digest "
49- "github.com/fluxcd/source-controller/internal/features "
50- "github.com/fluxcd/source-controller/internal/helm/registry"
50+ "github.com/fluxcd/source-controller/api/v1 "
51+ "github.com/fluxcd/source-controller/api/v1beta2 "
52+ // +kubebuilder:scaffold:imports
5153
52- v1 "github.com/fluxcd/source-controller/api/v1"
53- v1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
5454 "github.com/fluxcd/source-controller/controllers"
5555 "github.com/fluxcd/source-controller/internal/cache"
56+ "github.com/fluxcd/source-controller/internal/digest"
57+ "github.com/fluxcd/source-controller/internal/features"
5658 "github.com/fluxcd/source-controller/internal/helm"
57- // +kubebuilder:scaffold:imports
59+ "github.com/fluxcd/source-controller/internal/helm/registry"
5860)
5961
6062const controllerName = "source-controller"
@@ -92,7 +94,6 @@ func main() {
9294 storageAdvAddr string
9395 concurrent int
9496 requeueDependency time.Duration
95- watchAllNamespaces bool
9697 helmIndexLimit int64
9798 helmChartLimit int64
9899 helmChartFileLimit int64
@@ -101,6 +102,7 @@ func main() {
101102 leaderElectionOptions leaderelection.Options
102103 rateLimiterOptions helper.RateLimiterOptions
103104 featureGates feathelper.FeatureGates
105+ watchOptions helper.WatchOptions
104106 helmCacheMaxSize int
105107 helmCacheTTL string
106108 helmCachePurgeInterval string
@@ -121,8 +123,6 @@ func main() {
121123 flag .StringVar (& storageAdvAddr , "storage-adv-addr" , envOrDefault ("STORAGE_ADV_ADDR" , "" ),
122124 "The advertised address of the static file server." )
123125 flag .IntVar (& concurrent , "concurrent" , 2 , "The number of concurrent reconciles per controller." )
124- flag .BoolVar (& watchAllNamespaces , "watch-all-namespaces" , true ,
125- "Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace." )
126126 flag .Int64Var (& helmIndexLimit , "helm-index-max-size" , helm .MaxIndexSize ,
127127 "The max allowed size in bytes of a Helm repository index file." )
128128 flag .Int64Var (& helmChartLimit , "helm-chart-max-size" , helm .MaxChartSize ,
@@ -153,6 +153,7 @@ func main() {
153153 leaderElectionOptions .BindFlags (flag .CommandLine )
154154 rateLimiterOptions .BindFlags (flag .CommandLine )
155155 featureGates .BindFlags (flag .CommandLine )
156+ watchOptions .BindFlags (flag .CommandLine )
156157
157158 flag .Parse ()
158159
@@ -180,10 +181,28 @@ func main() {
180181 helm .MaxChartFileSize = helmChartFileLimit
181182
182183 watchNamespace := ""
183- if ! watchAllNamespaces {
184+ if ! watchOptions . AllNamespaces {
184185 watchNamespace = os .Getenv ("RUNTIME_NAMESPACE" )
185186 }
186187
188+ var newSelectingCache ctrlcache.NewCacheFunc
189+ watchSelector , err := helper .GetWatchSelector (watchOptions )
190+ if err != nil {
191+ setupLog .Error (err , "unable to configure watch label selector" )
192+ os .Exit (1 )
193+ }
194+ if watchSelector != labels .Everything () {
195+ newSelectingCache = ctrlcache .BuilderWithOptions (ctrlcache.Options {
196+ SelectorsByObject : ctrlcache.SelectorsByObject {
197+ & v1.GitRepository {}: {Label : watchSelector },
198+ & v1beta2.HelmRepository {}: {Label : watchSelector },
199+ & v1beta2.HelmChart {}: {Label : watchSelector },
200+ & v1beta2.Bucket {}: {Label : watchSelector },
201+ & v1beta2.OCIRepository {}: {Label : watchSelector },
202+ },
203+ })
204+ }
205+
187206 var disableCacheFor []ctrlclient.Object
188207 shouldCache , err := features .Enabled (features .CacheSecretsAndConfigMaps )
189208 if err != nil {
@@ -209,6 +228,7 @@ func main() {
209228 Namespace : watchNamespace ,
210229 Logger : ctrl .Log ,
211230 ClientDisableCacheFor : disableCacheFor ,
231+ NewCache : newSelectingCache ,
212232 })
213233 if err != nil {
214234 setupLog .Error (err , "unable to start manager" )
0 commit comments