@@ -25,6 +25,7 @@ import (
2525
2626 "github.com/spf13/cobra"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+ apimachruntime "k8s.io/apimachinery/pkg/runtime"
2829 "k8s.io/client-go/rest"
2930 ctrl "sigs.k8s.io/controller-runtime"
3031 "sigs.k8s.io/controller-runtime/pkg/cache"
@@ -45,6 +46,10 @@ import (
4546 "github.com/operator-framework/operator-sdk/internal/helm/watches"
4647 "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
4748 sdkVersion "github.com/operator-framework/operator-sdk/internal/version"
49+ "helm.sh/helm/v3/pkg/chart/loader"
50+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
51+ "k8s.io/apimachinery/pkg/labels"
52+ "k8s.io/apimachinery/pkg/selection"
4853)
4954
5055var log = logf .Log .WithName ("cmd" )
@@ -136,6 +141,49 @@ func run(cmd *cobra.Command, f *flags.Flags) {
136141 // Set default manager options
137142 options = f .ToManagerOptions (options )
138143
144+ if options .Scheme == nil {
145+ options .Scheme = apimachruntime .NewScheme ()
146+ }
147+
148+ ws , err := watches .Load (f .WatchesFile )
149+ if err != nil {
150+ log .Error (err , "Failed to create new manager factories." )
151+ os .Exit (1 )
152+ }
153+
154+ selectorsByObject := cache.SelectorsByObject {}
155+ chartNames := make ([]string , 0 , len (ws ))
156+ for _ , w := range ws {
157+ options .Scheme .AddKnownTypeWithName (w .GroupVersionKind , & unstructured.Unstructured {})
158+
159+ crObj := & unstructured.Unstructured {}
160+ crObj .SetGroupVersionKind (w .GroupVersionKind )
161+ sel , err := metav1 .LabelSelectorAsSelector (& w .Selector )
162+ if err != nil {
163+ log .Error (err , "Unable to parse selector" )
164+ os .Exit (1 )
165+ }
166+ selectorsByObject [crObj ] = cache.ObjectSelector {Label : sel }
167+
168+ chrt , err := loader .LoadDir (w .ChartDir )
169+ if err != nil {
170+ log .Error (err , "Unable to load chart" , "directory" , w .ChartDir )
171+ os .Exit (1 )
172+ }
173+ chartNames = append (chartNames , chrt .Name ())
174+ }
175+
176+ req , err := labels .NewRequirement ("helm.sdk.operatorframework.io/chart" , selection .In , chartNames )
177+ if err != nil {
178+ log .Error (err , "Unable to create label requirement for cache default selector" )
179+ os .Exit (1 )
180+ }
181+ defaultSelector := labels .NewSelector ().Add (* req )
182+ options .NewCache = cache .BuilderWithOptions (cache.Options {
183+ SelectorsByObject : selectorsByObject ,
184+ DefaultSelector : cache.ObjectSelector {Label : defaultSelector },
185+ })
186+
139187 if options .NewClient == nil {
140188 options .NewClient = func (cache cache.Cache , config * rest.Config , options client.Options , uncachedObjects ... client.Object ) (client.Client , error ) {
141189 // Create the Client for Write operations.
@@ -189,11 +237,6 @@ func run(cmd *cobra.Command, f *flags.Flags) {
189237 os .Exit (1 )
190238 }
191239
192- ws , err := watches .Load (f .WatchesFile )
193- if err != nil {
194- log .Error (err , "Failed to create new manager factories." )
195- os .Exit (1 )
196- }
197240 acg , err := helmClient .NewActionConfigGetter (mgr .GetConfig (), mgr .GetRESTMapper (), mgr .GetLogger ())
198241 if err != nil {
199242 log .Error (err , "Failed to create Helm action config getter" )
0 commit comments