@@ -850,3 +850,59 @@ def launchAiServiceUpgradePipeline(dynClient: DynamicClient,
850850
851851 pipelineURL = f"{ getConsoleURL (dynClient )} /k8s/ns/aiservice-{ aiserviceInstanceId } -pipelines/tekton.dev~v1beta1~PipelineRun/{ aiserviceInstanceId } -upgrade-{ timestamp } "
852852 return pipelineURL
853+
854+
855+ def prepareInstallRBAC (dynClient : DynamicClient , namespace : str , instanceId : str , installRBACDir : str ) -> None :
856+ """
857+ Apply the minimal install RBAC bundle for a MAS instance.
858+
859+ The bundle is defined by the kustomization under cli/rbac/install and creates the install-user and install-pipeline service accounts
860+ and their associated role bindings.
861+
862+ Parameters:
863+ dynClient (DynamicClient): OpenShift Dynamic Client
864+ instanceId (str): MAS instance ID used to render the RBAC templates
865+ installRBACDir (str): Path to the directory containing the RBAC kustomization and templates
866+
867+ Returns:
868+ None
869+
870+ Raises:
871+ FileNotFoundError: If the RBAC bundle directory or kustomization file does not exists
872+ """
873+ kustomizationFile = path .join (installRBACDir , "kustomization.yaml" )
874+ if not path .isfile (kustomizationFile ):
875+ logger .error (f"Cannot find kustomization file for install RBAC at { kustomizationFile } " )
876+ raise FileNotFoundError (f"Cannot find kustomization file for install RBAC at { kustomizationFile } " )
877+
878+ with open (kustomizationFile , "r" ) as file :
879+ kustomization = yaml .safe_load (file )
880+
881+ env = Environment ()
882+ for resourcePath in kustomization .get ("resources" , []):
883+ manifestFile = path .join (installRBACDir , resourcePath )
884+ if not path .isfile (manifestFile ):
885+ logger .error (f"Cannot find RBAC manifest file at { manifestFile } " )
886+ raise FileNotFoundError (f"Cannot find RBAC manifest file at { manifestFile } " )
887+
888+ with open (manifestFile , "r" ) as file :
889+ template = env .from_string (file .read ())
890+ renderedManifest = template .render (mas_instance_id = instanceId )
891+ logger .debug (f"Applying RBAC manifest { manifestFile } for instance { instanceId } :\n { renderedManifest } " )
892+
893+ for resourceBody in yaml .safe_load_all (renderedManifest ):
894+ if resourceBody is None :
895+ continue
896+
897+ apiVersion = resourceBody .get ["apiVersion" ]
898+ kind = resourceBody .get ["kind" ]
899+ metadata = resourceBody .get ("metadata" , {})
900+ name = metadata .get ("name" , "<unnamed>" )
901+ namespace = metadata .get ("namespace" )
902+
903+ logger .debug (f"Applying RBAC resource { kind } /{ name } in namespace { namespace } for instance { instanceId } " )
904+ resourceAPI = dynClient .resources .get (api_version = apiVersion , kind = kind )
905+ if namespace :
906+ resourceAPI .apply (body = resourceBody , namespace = namespace )
907+ else :
908+ resourceAPI .apply (body = resourceBody )
0 commit comments