import argparse import os from ruamel import yaml def read_dev_args_patch(is_eks: bool): if is_eks: fp = "eks-provider/envs/dev-us-east-1/args-patch.yaml" else: fp = "gke-provider/envs/dev-us-west2/args-patch.yaml" with open(fp, "r") as file: return list(yaml.safe_load_all(file)) def add_args_patch(base_path: str, is_eks: bool): if is_eks: fp = os.path.join(base_path, "args-patch.yaml") else: fp = os.path.join(base_path, "args-patch.yaml") with open(fp, "w") as file: old_docs = read_dev_args_patch(is_eks) for idx in range(0, len(old_docs)): yaml.dump(old_docs[idx], file, default_flow_style=False) if idx != len(old_docs) - 1: file.write("---\n") print(f"add args-patch.yaml to:{fp} success!") def split_br_cdc_config(base_path: str, is_eks: bool): old_config_path = os.path.join(base_path, "config.yaml") with open(old_config_path, "r") as file: old_config = yaml.safe_load(file) new_br_config_path = os.path.join(base_path, "config-br.yaml") with open(new_br_config_path, "w") as file: br_config = {} br_config["region"] = old_config["network"]["region"] br_config["predicate"] = old_config["predicate"] if not is_eks: br_config["gcpProjectID"] = old_config["fullManage"]["gcpProjectID"] yaml.dump(br_config, file, default_flow_style=False) print(f"add config-br.yaml to:{new_br_config_path} success!") new_cdc_config_path = os.path.join(base_path, "config-cdc.yaml") with open(new_cdc_config_path, "w") as file: cdc_config = {} cdc_config["region"] = old_config["network"]["region"] cdc_config["predicate"] = old_config["predicate"] cdc_config["configServiceEndpoint"] = old_config["configServiceEndpoint"] cdc_config["featureGates"] = old_config["featureGates"] yaml.dump(cdc_config, file, default_flow_style=False) print(f"add config-cdc.yaml to:{new_cdc_config_path} success!") def update_dp_deployment(base_path: str, is_eks: bool): y = yaml.YAML() fp = os.path.join(base_path, "dp-deployment.yaml") with open(fp, "r") as file: dp_deployments = list(y.load_all(file)) if not is_eks: for dp_deployment in dp_deployments: if dp_deployment["metadata"]["name"] == "dp-gke-infra-provider-br": volumes = [ { "name": "root-config", "configMap": { "name": "gke-provider-br-config", }, } ] volumes = dp_deployment["spec"]["template"]["spec"]["volumes"] = volumes if dp_deployment["metadata"]["name"] == "dp-gke-infra-provider-cdc": volumes = [ { "name": "root-config", "configMap": { "name": "gke-provider-cdc-config", }, } ] volumes = dp_deployment["spec"]["template"]["spec"]["volumes"] = volumes else: for dp_deployment in dp_deployments: if dp_deployment["metadata"]["name"] == "dp-eks-infra-provider-br": volumes = [ { "name": "root-config", "configMap": { "name": "eks-provider-br-config", }, } ] volumes = dp_deployment["spec"]["template"]["spec"]["volumes"] = volumes if dp_deployment["metadata"]["name"] == "dp-eks-infra-provider-cdc": volumes = [ { "name": "root-config", "configMap": { "name": "eks-provider-cdc-config", }, } ] volumes = dp_deployment["spec"]["template"]["spec"]["volumes"] = volumes with open(fp, "w") as file: for idx in range(0, len(dp_deployments)): y.dump(dp_deployments[idx], file) if idx != len(dp_deployments) - 1: file.write("---\n") print(f"update dp-deployment.yaml to:{fp} success!") def update_kustomization(base_path: str, is_eks: bool): y = yaml.YAML() fp = os.path.join(base_path, "kustomization.yaml") with open(fp, "r") as file: data = y.load(file) if len(data["configMapGenerator"]) == 1: if is_eks: data["configMapGenerator"].append( { "name": "eks-provider-br-config", "files": ["config-br.yaml"], } ) data["configMapGenerator"].append( { "name": "eks-provider-cdc-config", "files": ["config-cdc.yaml"], } ) else: data["configMapGenerator"].append( { "name": "gke-provider-br-config", "files": ["config-br.yaml"], } ) data["configMapGenerator"].append( { "name": "gke-provider-cdc-config", "files": ["config-cdc.yaml"], } ) found_args_patch = False for p in data["patchesStrategicMerge"]: if p == "args-patch.yaml": found_args_patch = True break if not found_args_patch: data["patchesStrategicMerge"].append("args-patch.yaml") with open(fp, "w") as file: y.dump(data, file) print(f"update kustomization.yaml to:{fp} success!") def main(): parser = argparse.ArgumentParser(description="tools") parser.add_argument("--base_path", type=str, help="path to env") parser.add_argument("--provider", type=str, help="provider_type is eks or gke") args = parser.parse_args() print("--------------------") print(f"start to handle {args.base_path}... provider:{args.provider}...") print("--------------------") is_eks = True if args.provider == "eks" else False # add args-patch.yaml add_args_patch(args.base_path, is_eks) # # split br-cdc-config.yaml split_br_cdc_config(args.base_path, is_eks) # # update dp-deployment.yaml update_dp_deployment(args.base_path, is_eks) # update kustomization.yaml update_kustomization(args.base_path, is_eks) if __name__ == "__main__": main()