77
88 "helm.sh/helm/v3/pkg/chart"
99
10+ "github.com/operator-framework/api/pkg/operators/v1alpha1"
11+
12+ "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
1013 "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
1114 "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/render"
1215)
@@ -18,11 +21,27 @@ type BundleToHelmChartConverter struct {
1821}
1922
2023func (r * BundleToHelmChartConverter ) ToHelmChart (bundle source.BundleSource , installNamespace string , watchNamespace string ) (* chart.Chart , error ) {
24+ config := make (map [string ]string )
25+
26+ if watchNamespace != "" {
27+ config ["watchNamespace" ] = watchNamespace
28+ }
29+
2130 rv1 , err := bundle .GetBundle ()
2231 if err != nil {
2332 return nil , err
2433 }
2534
35+ var opts []render.Option
36+
37+ opts = append (opts , render .WithCertificateProvider (r .CertificateProvider ))
38+
39+ targetNsOpt , err := r .buildTargetNamespacesOption (config , & rv1 , installNamespace )
40+ if err != nil {
41+ return nil , err
42+ }
43+ opts = append (opts , targetNsOpt )
44+
2645 if len (rv1 .CSV .Spec .APIServiceDefinitions .Owned ) > 0 {
2746 return nil , fmt .Errorf ("unsupported bundle: apiServiceDefintions are not supported" )
2847 }
@@ -39,11 +58,7 @@ func (r *BundleToHelmChartConverter) ToHelmChart(bundle source.BundleSource, ins
3958 return nil , fmt .Errorf ("unsupported bundle: webhookDefinitions are not supported" )
4059 }
4160
42- objs , err := r .BundleRenderer .Render (
43- rv1 , installNamespace ,
44- render .WithTargetNamespaces (watchNamespace ),
45- render .WithCertificateProvider (r .CertificateProvider ),
46- )
61+ objs , err := r .BundleRenderer .Render (rv1 , installNamespace , opts ... )
4762
4863 if err != nil {
4964 return nil , fmt .Errorf ("error rendering bundle: %w" , err )
@@ -77,3 +92,55 @@ func (r *BundleToHelmChartConverter) ToHelmChart(bundle source.BundleSource, ins
7792
7893 return chrt , nil
7994}
95+
96+ // buildTargetNamespacesOption creates the target namespaces option based on configuration
97+ func (r * BundleToHelmChartConverter ) buildTargetNamespacesOption (config map [string ]string , rv1 * bundle.RegistryV1 , installNamespace string ) (render.Option , error ) {
98+ if watchNs , exists := config ["watchNamespace" ]; exists {
99+ if watchNs == "" {
100+ return render .WithTargetNamespaces ("" ), nil
101+ }
102+ return render .WithTargetNamespaces (watchNs ), nil
103+ }
104+
105+ // No watchNamespace configured - derive default
106+ defaultWatchNs , err := r .deriveDefaultWatchNamespace (rv1 , installNamespace )
107+ if err != nil {
108+ return nil , fmt .Errorf ("error deriving default watch namespace: %w" , err )
109+ }
110+
111+ if defaultWatchNs == "" {
112+ return render .WithTargetNamespaces ("" ), nil
113+ }
114+ return render .WithTargetNamespaces (defaultWatchNs ), nil
115+ }
116+
117+ // deriveDefaultWatchNamespace determines the default watch namespace based on bundle's supported install modes
118+ func (r * BundleToHelmChartConverter ) deriveDefaultWatchNamespace (rv1 * bundle.RegistryV1 , installNamespace string ) (string , error ) {
119+ supportedModes := make (map [v1alpha1.InstallModeType ]bool )
120+ for _ , installMode := range rv1 .CSV .Spec .InstallModes {
121+ if installMode .Supported {
122+ supportedModes [installMode .Type ] = true
123+ }
124+ }
125+
126+ // Priority order for defaults:
127+ // 1. If supports AllNamespaces -> "" (AllNamespaces)
128+ // 2. If supports OwnNamespace -> installNamespace (OwnNamespace)
129+ // 3. If only supports SingleNamespace or MultiNamespace -> error (watchNamespace needs to be explicitly set, cannot derive default)
130+ // 4. No supported modes -> error
131+
132+ if supportedModes [v1alpha1 .InstallModeTypeAllNamespaces ] {
133+ return "" , nil // Empty string means AllNamespaces
134+ }
135+ if supportedModes [v1alpha1 .InstallModeTypeOwnNamespace ] {
136+ return installNamespace , nil // OwnNamespace
137+ }
138+ if supportedModes [v1alpha1 .InstallModeTypeSingleNamespace ] {
139+ return "" , fmt .Errorf ("config parameter watchNamespace required: bundle only supports SingleNamespace install mode" )
140+ }
141+ if supportedModes [v1alpha1 .InstallModeTypeMultiNamespace ] {
142+ return "" , fmt .Errorf ("bundle only supports MultiNamespace install mode" )
143+ }
144+
145+ return "" , fmt .Errorf ("bundle has no supported install modes" )
146+ }
0 commit comments