|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +import arcaflow |
| 5 | +import yaml |
| 6 | +from krkn_lib.models.telemetry import ScenarioTelemetry |
| 7 | +from krkn_lib.telemetry.ocp import KrknTelemetryOpenshift |
| 8 | +from krkn.scenario_plugins.abstract_scenario_plugin import AbstractScenarioPlugin |
| 9 | +from krkn.scenario_plugins.arcaflow.context_auth import ContextAuth |
| 10 | + |
| 11 | + |
| 12 | +class ArcaflowScenarioPlugin(AbstractScenarioPlugin): |
| 13 | + |
| 14 | + def run( |
| 15 | + self, |
| 16 | + run_uuid: str, |
| 17 | + scenario: str, |
| 18 | + lib_telemetry: KrknTelemetryOpenshift, |
| 19 | + scenario_telemetry: ScenarioTelemetry, |
| 20 | + ) -> int: |
| 21 | + try: |
| 22 | + engine_args = self.build_args(scenario) |
| 23 | + status_code = self.run_workflow( |
| 24 | + engine_args, lib_telemetry.get_lib_kubernetes().get_kubeconfig_path() |
| 25 | + ) |
| 26 | + return status_code |
| 27 | + except Exception as e: |
| 28 | + logging.error("ArcaflowScenarioPlugin exiting due to Exception %s" % e) |
| 29 | + return 1 |
| 30 | + |
| 31 | + def get_scenario_types(self) -> [str]: |
| 32 | + return ["hog_scenarios", "arcaflow_scenario"] |
| 33 | + |
| 34 | + def run_workflow( |
| 35 | + self, engine_args: arcaflow.EngineArgs, kubeconfig_path: str |
| 36 | + ) -> int: |
| 37 | + self.set_arca_kubeconfig(engine_args, kubeconfig_path) |
| 38 | + exit_status = arcaflow.run(engine_args) |
| 39 | + return exit_status |
| 40 | + |
| 41 | + def build_args(self, input_file: str) -> arcaflow.EngineArgs: |
| 42 | + """sets the kubeconfig parsed by setArcaKubeConfig as an input to the arcaflow workflow""" |
| 43 | + current_path = Path().resolve() |
| 44 | + context = f"{current_path}/{Path(input_file).parent}" |
| 45 | + workflow = f"{context}/workflow.yaml" |
| 46 | + config = f"{context}/config.yaml" |
| 47 | + if not os.path.exists(context): |
| 48 | + raise Exception( |
| 49 | + "context folder for arcaflow workflow not found: {}".format(context) |
| 50 | + ) |
| 51 | + if not os.path.exists(input_file): |
| 52 | + raise Exception( |
| 53 | + "input file for arcaflow workflow not found: {}".format(input_file) |
| 54 | + ) |
| 55 | + if not os.path.exists(workflow): |
| 56 | + raise Exception( |
| 57 | + "workflow file for arcaflow workflow not found: {}".format(workflow) |
| 58 | + ) |
| 59 | + if not os.path.exists(config): |
| 60 | + raise Exception( |
| 61 | + "configuration file for arcaflow workflow not found: {}".format(config) |
| 62 | + ) |
| 63 | + |
| 64 | + engine_args = arcaflow.EngineArgs() |
| 65 | + engine_args.context = context |
| 66 | + engine_args.config = config |
| 67 | + engine_args.workflow = workflow |
| 68 | + engine_args.input = f"{current_path}/{input_file}" |
| 69 | + return engine_args |
| 70 | + |
| 71 | + def set_arca_kubeconfig( |
| 72 | + self, engine_args: arcaflow.EngineArgs, kubeconfig_path: str |
| 73 | + ): |
| 74 | + |
| 75 | + context_auth = ContextAuth() |
| 76 | + if not os.path.exists(kubeconfig_path): |
| 77 | + raise Exception("kubeconfig not found in {}".format(kubeconfig_path)) |
| 78 | + |
| 79 | + with open(kubeconfig_path, "r") as stream: |
| 80 | + try: |
| 81 | + kubeconfig = yaml.safe_load(stream) |
| 82 | + context_auth.fetch_auth_data(kubeconfig) |
| 83 | + except Exception as e: |
| 84 | + logging.error( |
| 85 | + "impossible to read kubeconfig file in: {}".format(kubeconfig_path) |
| 86 | + ) |
| 87 | + raise e |
| 88 | + |
| 89 | + kubeconfig_str = self.set_kubeconfig_auth(kubeconfig, context_auth) |
| 90 | + |
| 91 | + with open(engine_args.input, "r") as stream: |
| 92 | + input_file = yaml.safe_load(stream) |
| 93 | + if "input_list" in input_file and isinstance( |
| 94 | + input_file["input_list"], list |
| 95 | + ): |
| 96 | + for index, _ in enumerate(input_file["input_list"]): |
| 97 | + if isinstance(input_file["input_list"][index], dict): |
| 98 | + input_file["input_list"][index]["kubeconfig"] = kubeconfig_str |
| 99 | + else: |
| 100 | + input_file["kubeconfig"] = kubeconfig_str |
| 101 | + stream.close() |
| 102 | + with open(engine_args.input, "w") as stream: |
| 103 | + yaml.safe_dump(input_file, stream) |
| 104 | + |
| 105 | + with open(engine_args.config, "r") as stream: |
| 106 | + config_file = yaml.safe_load(stream) |
| 107 | + if config_file["deployers"]["image"]["deployer_name"] == "kubernetes": |
| 108 | + kube_connection = self.set_kubernetes_deployer_auth( |
| 109 | + config_file["deployers"]["image"]["connection"], context_auth |
| 110 | + ) |
| 111 | + config_file["deployers"]["image"]["connection"] = kube_connection |
| 112 | + with open(engine_args.config, "w") as stream: |
| 113 | + yaml.safe_dump(config_file, stream, explicit_start=True, width=4096) |
| 114 | + |
| 115 | + def set_kubernetes_deployer_auth( |
| 116 | + self, deployer: any, context_auth: ContextAuth |
| 117 | + ) -> any: |
| 118 | + if context_auth.clusterHost is not None: |
| 119 | + deployer["host"] = context_auth.clusterHost |
| 120 | + if context_auth.clientCertificateData is not None: |
| 121 | + deployer["cert"] = context_auth.clientCertificateData |
| 122 | + if context_auth.clientKeyData is not None: |
| 123 | + deployer["key"] = context_auth.clientKeyData |
| 124 | + if context_auth.clusterCertificateData is not None: |
| 125 | + deployer["cacert"] = context_auth.clusterCertificateData |
| 126 | + if context_auth.username is not None: |
| 127 | + deployer["username"] = context_auth.username |
| 128 | + if context_auth.password is not None: |
| 129 | + deployer["password"] = context_auth.password |
| 130 | + if context_auth.bearerToken is not None: |
| 131 | + deployer["bearerToken"] = context_auth.bearerToken |
| 132 | + return deployer |
| 133 | + |
| 134 | + def set_kubeconfig_auth(self, kubeconfig: any, context_auth: ContextAuth) -> str: |
| 135 | + """ |
| 136 | + Builds an arcaflow-compatible kubeconfig representation and returns it as a string. |
| 137 | + In order to run arcaflow plugins in kubernetes/openshift the kubeconfig must contain client certificate/key |
| 138 | + and server certificate base64 encoded within the kubeconfig file itself in *-data fields. That is not always the |
| 139 | + case, infact kubeconfig may contain filesystem paths to those files, this function builds an arcaflow-compatible |
| 140 | + kubeconfig file and returns it as a string that can be safely included in input.yaml |
| 141 | + """ |
| 142 | + |
| 143 | + if "current-context" not in kubeconfig.keys(): |
| 144 | + raise Exception( |
| 145 | + "invalid kubeconfig file, impossible to determine current-context" |
| 146 | + ) |
| 147 | + user_id = None |
| 148 | + cluster_id = None |
| 149 | + user_name = None |
| 150 | + cluster_name = None |
| 151 | + current_context = kubeconfig["current-context"] |
| 152 | + for context in kubeconfig["contexts"]: |
| 153 | + if context["name"] == current_context: |
| 154 | + user_name = context["context"]["user"] |
| 155 | + cluster_name = context["context"]["cluster"] |
| 156 | + if user_name is None: |
| 157 | + raise Exception( |
| 158 | + "user not set for context {} in kubeconfig file".format(current_context) |
| 159 | + ) |
| 160 | + if cluster_name is None: |
| 161 | + raise Exception( |
| 162 | + "cluster not set for context {} in kubeconfig file".format( |
| 163 | + current_context |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + for index, user in enumerate(kubeconfig["users"]): |
| 168 | + if user["name"] == user_name: |
| 169 | + user_id = index |
| 170 | + for index, cluster in enumerate(kubeconfig["clusters"]): |
| 171 | + if cluster["name"] == cluster_name: |
| 172 | + cluster_id = index |
| 173 | + |
| 174 | + if cluster_id is None: |
| 175 | + raise Exception( |
| 176 | + "no cluster {} found in kubeconfig users".format(cluster_name) |
| 177 | + ) |
| 178 | + if "client-certificate" in kubeconfig["users"][user_id]["user"]: |
| 179 | + kubeconfig["users"][user_id]["user"][ |
| 180 | + "client-certificate-data" |
| 181 | + ] = context_auth.clientCertificateDataBase64 |
| 182 | + del kubeconfig["users"][user_id]["user"]["client-certificate"] |
| 183 | + |
| 184 | + if "client-key" in kubeconfig["users"][user_id]["user"]: |
| 185 | + kubeconfig["users"][user_id]["user"][ |
| 186 | + "client-key-data" |
| 187 | + ] = context_auth.clientKeyDataBase64 |
| 188 | + del kubeconfig["users"][user_id]["user"]["client-key"] |
| 189 | + |
| 190 | + if "certificate-authority" in kubeconfig["clusters"][cluster_id]["cluster"]: |
| 191 | + kubeconfig["clusters"][cluster_id]["cluster"][ |
| 192 | + "certificate-authority-data" |
| 193 | + ] = context_auth.clusterCertificateDataBase64 |
| 194 | + del kubeconfig["clusters"][cluster_id]["cluster"]["certificate-authority"] |
| 195 | + kubeconfig_str = yaml.dump(kubeconfig) |
| 196 | + return kubeconfig_str |
0 commit comments