Skip to content

Commit 6d670dc

Browse files
[patch] Prepare Install RBAC Helper functions (#266)
1 parent 543b34f commit 6d670dc

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/mas/devops/tekton.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,3 +954,59 @@ def launchAiServiceUpgradePipeline(dynClient: DynamicClient,
954954

955955
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/aiservice-{aiserviceInstanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{aiserviceInstanceId}-upgrade-{timestamp}"
956956
return pipelineURL
957+
958+
959+
def prepareInstallRBAC(dynClient: DynamicClient, namespace: str, instanceId: str, installRBACDir: str) -> None:
960+
"""
961+
Apply the minimal install RBAC bundle for a MAS instance.
962+
963+
The bundle is defined by the kustomization under cli/rbac/install and creates the install-user and install-pipeline service accounts
964+
and their associated role bindings.
965+
966+
Parameters:
967+
dynClient (DynamicClient): OpenShift Dynamic Client
968+
instanceId (str): MAS instance ID used to render the RBAC templates
969+
installRBACDir (str): Path to the directory containing the RBAC kustomization and templates
970+
971+
Returns:
972+
None
973+
974+
Raises:
975+
FileNotFoundError: If the RBAC bundle directory or kustomization file does not exists
976+
"""
977+
kustomizationFile = path.join(installRBACDir, "kustomization.yaml")
978+
if not path.isfile(kustomizationFile):
979+
logger.error(f"Cannot find kustomization file for install RBAC at {kustomizationFile}")
980+
raise FileNotFoundError(f"Cannot find kustomization file for install RBAC at {kustomizationFile}")
981+
982+
with open(kustomizationFile, "r") as file:
983+
kustomization = yaml.safe_load(file)
984+
985+
env = Environment()
986+
for resourcePath in kustomization.get("resources", []):
987+
manifestFile = path.join(installRBACDir, resourcePath)
988+
if not path.isfile(manifestFile):
989+
logger.error(f"Cannot find RBAC manifest file at {manifestFile}")
990+
raise FileNotFoundError(f"Cannot find RBAC manifest file at {manifestFile}")
991+
992+
with open(manifestFile, "r") as file:
993+
template = env.from_string(file.read())
994+
renderedManifest = template.render(mas_instance_id=instanceId)
995+
logger.debug(f"Applying RBAC manifest {manifestFile} for instance {instanceId}:\n{renderedManifest}")
996+
997+
for resourceBody in yaml.safe_load_all(renderedManifest):
998+
if resourceBody is None:
999+
continue
1000+
1001+
apiVersion = resourceBody["apiVersion"]
1002+
kind = resourceBody["kind"]
1003+
metadata = resourceBody.get("metadata", {})
1004+
name = metadata.get("name", "<unnamed>")
1005+
namespace = metadata.get("namespace")
1006+
1007+
logger.debug(f"Applying RBAC resource {kind}/{name} in namespace {namespace} for instance {instanceId}")
1008+
resourceAPI = dynClient.resources.get(api_version=apiVersion, kind=kind)
1009+
if namespace:
1010+
resourceAPI.apply(body=resourceBody, namespace=namespace)
1011+
else:
1012+
resourceAPI.apply(body=resourceBody)

0 commit comments

Comments
 (0)