Skip to content

Commit f252a5e

Browse files
[patch] Prepare Install RBAC Helper functions
1 parent 2d8fd1c commit f252a5e

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/mas/devops/tekton.py

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

0 commit comments

Comments
 (0)