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