@@ -1022,3 +1022,59 @@ def launchAiServiceUpgradePipeline(dynClient: DynamicClient,
10221022
10231023 pipelineURL = f"{ getConsoleURL (dynClient )} /k8s/ns/aiservice-{ aiserviceInstanceId } -pipelines/tekton.dev~v1beta1~PipelineRun/{ aiserviceInstanceId } -upgrade-{ timestamp } "
10241024 return pipelineURL
1025+
1026+
1027+ def prepareInstallRBAC (dynClient : DynamicClient , namespace : str , instanceId : str , installRBACDir : str ) -> None :
1028+ """
1029+ Apply the minimal install RBAC bundle for a MAS instance.
1030+
1031+ The bundle is defined by the kustomization under cli/rbac/install and creates the install-user and install-pipeline service accounts
1032+ and their associated role bindings.
1033+
1034+ Parameters:
1035+ dynClient (DynamicClient): OpenShift Dynamic Client
1036+ instanceId (str): MAS instance ID used to render the RBAC templates
1037+ installRBACDir (str): Path to the directory containing the RBAC kustomization and templates
1038+
1039+ Returns:
1040+ None
1041+
1042+ Raises:
1043+ FileNotFoundError: If the RBAC bundle directory or kustomization file does not exists
1044+ """
1045+ kustomizationFile = path .join (installRBACDir , "kustomization.yaml" )
1046+ if not path .isfile (kustomizationFile ):
1047+ logger .error (f"Cannot find kustomization file for install RBAC at { kustomizationFile } " )
1048+ raise FileNotFoundError (f"Cannot find kustomization file for install RBAC at { kustomizationFile } " )
1049+
1050+ with open (kustomizationFile , "r" ) as file :
1051+ kustomization = yaml .safe_load (file )
1052+
1053+ env = Environment ()
1054+ for resourcePath in kustomization .get ("resources" , []):
1055+ manifestFile = path .join (installRBACDir , resourcePath )
1056+ if not path .isfile (manifestFile ):
1057+ logger .error (f"Cannot find RBAC manifest file at { manifestFile } " )
1058+ raise FileNotFoundError (f"Cannot find RBAC manifest file at { manifestFile } " )
1059+
1060+ with open (manifestFile , "r" ) as file :
1061+ template = env .from_string (file .read ())
1062+ renderedManifest = template .render (mas_instance_id = instanceId )
1063+ logger .debug (f"Applying RBAC manifest { manifestFile } for instance { instanceId } :\n { renderedManifest } " )
1064+
1065+ for resourceBody in yaml .safe_load_all (renderedManifest ):
1066+ if resourceBody is None :
1067+ continue
1068+
1069+ apiVersion = resourceBody ["apiVersion" ]
1070+ kind = resourceBody ["kind" ]
1071+ metadata = resourceBody .get ("metadata" , {})
1072+ name = metadata .get ("name" , "<unnamed>" )
1073+ namespace = metadata .get ("namespace" )
1074+
1075+ logger .debug (f"Applying RBAC resource { kind } /{ name } in namespace { namespace } for instance { instanceId } " )
1076+ resourceAPI = dynClient .resources .get (api_version = apiVersion , kind = kind )
1077+ if namespace :
1078+ resourceAPI .apply (body = resourceBody , namespace = namespace )
1079+ else :
1080+ resourceAPI .apply (body = resourceBody )
0 commit comments