2323import  io .quarkiverse .operatorsdk .bundle .runtime .CSVMetadataHolder .RequiredCRD ;
2424import  io .quarkiverse .operatorsdk .common .*;
2525import  io .quarkiverse .operatorsdk .runtime .BuildTimeOperatorConfiguration ;
26+ import  io .quarkiverse .operatorsdk .runtime .QuarkusControllerConfiguration ;
2627
2728public  class  CsvManifestsBuilder  extends  ManifestsBuilder  {
2829
2930    private  static  final  Logger  log  = Logger .getLogger (CsvManifestsBuilder .class );
3031
31-     private  static  final  String  DEFAULT_INSTALL_MODE  = "AllNamespaces" ;
32+     private  static  final  String  ALL_NAMESPACES  = "AllNamespaces" ;
3233    private  static  final  String  DEPLOYMENT  = "deployment" ;
3334    private  static  final  String  SERVICE_ACCOUNT_KIND  = "ServiceAccount" ;
3435    private  static  final  String  CLUSTER_ROLE_KIND  = "ClusterRole" ;
@@ -37,24 +38,29 @@ public class CsvManifestsBuilder extends ManifestsBuilder {
3738    private  static  final  Logger  LOGGER  = Logger .getLogger (CsvManifestsBuilder .class .getName ());
3839    private  static  final  String  IMAGE_PNG  = "image/png" ;
3940    public  static  final  String  OLM_TARGET_NAMESPACES  = "metadata.annotations['olm.targetNamespaces']" ;
41+     public  static  final  String  OWN_NAMESPACE  = "OwnNamespace" ;
42+     public  static  final  String  SINGLE_NAMESPACE  = "SingleNamespace" ;
43+     public  static  final  String  MULTI_NAMESPACE  = "MultiNamespace" ;
4044    private  ClusterServiceVersionBuilder  csvBuilder ;
4145    private  final  Set <CRDDescription > ownedCRs  = new  HashSet <>();
4246    private  final  Set <CRDDescription > requiredCRs  = new  HashSet <>();
4347    private  final  Path  kubernetesResources ;
4448    private  final  String  deploymentName ;
4549    private  final  List <ReconcilerAugmentedClassInfo > controllers ;
4650
51+     @ SuppressWarnings ("rawtypes" )
4752    public  CsvManifestsBuilder (CSVMetadataHolder  metadata , BuildTimeOperatorConfiguration  operatorConfiguration ,
4853            List <ReconcilerAugmentedClassInfo > controllers ,
49-             Path  mainSourcesRoot , String  deploymentName ) {
54+             Path  mainSourcesRoot , String  deploymentName ,  Map < String ,  QuarkusControllerConfiguration >  controllerConfigs ) {
5055        super (metadata );
5156        this .deploymentName  = deploymentName ;
5257        this .controllers  = controllers ;
5358        this .kubernetesResources  = mainSourcesRoot  != null  ? mainSourcesRoot .resolve ("kubernetes" ) : null ;
5459
5560        csvBuilder  = new  ClusterServiceVersionBuilder ();
5661
57-         final  var  metadataBuilder  = csvBuilder .withNewMetadata ().withName (getName ());
62+         final  var  name  = getName ();
63+         final  var  metadataBuilder  = csvBuilder .withNewMetadata ().withName (name );
5864        if  (metadata .annotations  != null ) {
5965            metadataBuilder .addToAnnotations ("olm.skipRange" , metadata .annotations .skipRange );
6066            metadataBuilder .addToAnnotations ("containerImage" , metadata .annotations .containerImage );
@@ -72,7 +78,7 @@ public CsvManifestsBuilder(CSVMetadataHolder metadata, BuildTimeOperatorConfigur
7278        final  var  csvSpecBuilder  = csvBuilder 
7379                .editOrNewSpec ()
7480                .withDescription (metadata .description )
75-                 .withDisplayName (defaultIfEmpty (metadata .displayName , getName () ))
81+                 .withDisplayName (defaultIfEmpty (metadata .displayName , name ))
7682                .withKeywords (metadata .keywords )
7783                .withReplaces (metadata .replaces )
7884                .withVersion (metadata .version )
@@ -135,14 +141,6 @@ public CsvManifestsBuilder(CSVMetadataHolder metadata, BuildTimeOperatorConfigur
135141            }
136142        }
137143
138-         if  (metadata .installModes  == null  || metadata .installModes .length  == 0 ) {
139-             csvSpecBuilder .addNewInstallMode (true , DEFAULT_INSTALL_MODE );
140-         } else  {
141-             for  (CSVMetadataHolder .InstallMode  installMode  : metadata .installModes ) {
142-                 csvSpecBuilder .addNewInstallMode (installMode .supported , installMode .type );
143-             }
144-         }
145- 
146144        // add owned and required CRD, also collect them 
147145        final  var  nativeApis  = new  ArrayList <GroupVersionKind >();
148146        controllers .forEach (raci  -> {
@@ -181,6 +179,29 @@ public CsvManifestsBuilder(CSVMetadataHolder metadata, BuildTimeOperatorConfigur
181179                            }
182180                        });
183181            }
182+ 
183+             // deal with install modes 
184+             // use watched namespaces information for default install mode 
185+             // fixme: multiple, incompatible controller configurations in the same bundle will result in inconsistent runs 
186+             final  var  config  = controllerConfigs .get (raci .nameOrFailIfUnset ());
187+             if  (config .watchAllNamespaces ()) {
188+                 csvSpecBuilder .withInstallModes (new  InstallMode (true , ALL_NAMESPACES ));
189+             } else  if  (config .watchCurrentNamespace ()) {
190+                 csvSpecBuilder .withInstallModes (new  InstallMode (true , OWN_NAMESPACE ));
191+             } else  {
192+                 final  var  namespaces  = config .getNamespaces ();
193+                 if  (namespaces .size () == 1 ) {
194+                     csvSpecBuilder .withInstallModes (new  InstallMode (true , SINGLE_NAMESPACE ));
195+                 } else  {
196+                     csvSpecBuilder .withInstallModes (new  InstallMode (true , MULTI_NAMESPACE ));
197+                 }
198+             }
199+             // then process metadata 
200+             if  (metadata .installModes  != null ) {
201+                 for  (CSVMetadataHolder .InstallMode  installMode  : metadata .installModes ) {
202+                     csvSpecBuilder .addNewInstallMode (installMode .supported , installMode .type );
203+                 }
204+             }
184205        });
185206
186207        // add required CRDs from CSV metadata 
0 commit comments