|
| 1 | +from dockerutil import build_and_push_image |
| 2 | +from dockerfile_generator import render |
| 3 | +from kubernetes import client, config |
| 4 | +from kubernetes.client.rest import ApiException |
| 5 | +from dev_config import DevConfig, load_config |
| 6 | +from typing import Dict, Optional |
| 7 | +import yaml |
| 8 | +import io |
| 9 | +import os |
| 10 | +import time |
| 11 | + |
| 12 | + |
| 13 | +def _load_operator_service_account() -> Optional[Dict]: |
| 14 | + return load_yaml_from_file("deploy/service_account.yaml") |
| 15 | + |
| 16 | + |
| 17 | +def _load_operator_role() -> Optional[Dict]: |
| 18 | + return load_yaml_from_file("deploy/role.yaml") |
| 19 | + |
| 20 | + |
| 21 | +def _load_operator_role_binding() -> Optional[Dict]: |
| 22 | + return load_yaml_from_file("deploy/role_binding.yaml") |
| 23 | + |
| 24 | + |
| 25 | +def _load_operator_deployment() -> Optional[Dict]: |
| 26 | + return load_yaml_from_file("deploy/operator.yaml") |
| 27 | + |
| 28 | + |
| 29 | +def _load_mongodb_crd() -> Optional[Dict]: |
| 30 | + return load_yaml_from_file("deploy/crds/mongodb.com_mongodbs_crd.yaml") |
| 31 | + |
| 32 | + |
| 33 | +def load_yaml_from_file(path: str) -> Optional[Dict]: |
| 34 | + with open(path, "r") as f: |
| 35 | + return yaml.full_load(f.read()) |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +def _ensure_crds(): |
| 40 | + """ |
| 41 | + ensure_crds makes sure that all the required CRDs have been created |
| 42 | + """ |
| 43 | + crdv1 = client.ApiextensionsV1beta1Api() |
| 44 | + crd = _load_mongodb_crd() |
| 45 | + |
| 46 | + ignore_if_doesnt_exist( |
| 47 | + lambda: crdv1.delete_custom_resource_definition("mongodbs.mongodb.com") |
| 48 | + ) |
| 49 | + |
| 50 | + # TODO: fix this, when calling create_custom_resource_definition, we get the error |
| 51 | + # ValueError("Invalid value for `conditions`, must not be `None`") |
| 52 | + # but the crd is still successfully created |
| 53 | + try: |
| 54 | + crdv1.create_custom_resource_definition(body=crd) |
| 55 | + except ValueError as e: |
| 56 | + pass |
| 57 | + |
| 58 | + print("Ensured CRDs") |
| 59 | + |
| 60 | + |
| 61 | +def build_and_push_operator(repo_url: str, tag: str, path: str): |
| 62 | + """ |
| 63 | + build_and_push_operator creates the Dockerfile for the operator |
| 64 | + and pushes it to the target repo |
| 65 | + """ |
| 66 | + return build_and_push_image(repo_url, tag, path, "operator") |
| 67 | + |
| 68 | + |
| 69 | +def _ignore_error_codes(fn, codes): |
| 70 | + try: |
| 71 | + fn() |
| 72 | + except ApiException as e: |
| 73 | + if e.status not in codes: |
| 74 | + raise |
| 75 | + |
| 76 | + |
| 77 | +def ignore_if_already_exists(fn): |
| 78 | + """ |
| 79 | + ignore_if_already_exists accepts a function and calls it, |
| 80 | + ignoring an Kubernetes API conflict errors |
| 81 | + """ |
| 82 | + |
| 83 | + return _ignore_error_codes(fn, [409]) |
| 84 | + |
| 85 | + |
| 86 | +def ignore_if_doesnt_exist(fn): |
| 87 | + """ |
| 88 | + ignore_if_doesnt_exist accepts a function and calls it, |
| 89 | + ignoring an Kubernetes API not found errors |
| 90 | + """ |
| 91 | + return _ignore_error_codes(fn, [404]) |
| 92 | + |
| 93 | + |
| 94 | +def deploy_operator(): |
| 95 | + """ |
| 96 | + deploy_operator ensures the CRDs are created, and als creates all the required ServiceAccounts, Roles |
| 97 | + and RoleBindings for the operator, and then creates the operator deployment. |
| 98 | + """ |
| 99 | + appsv1 = client.AppsV1Api() |
| 100 | + corev1 = client.CoreV1Api() |
| 101 | + rbacv1 = client.RbacAuthorizationV1Api() |
| 102 | + |
| 103 | + dev_config = load_config() |
| 104 | + _ensure_crds() |
| 105 | + |
| 106 | + ignore_if_already_exists( |
| 107 | + lambda: rbacv1.create_namespaced_role( |
| 108 | + dev_config.namespace, _load_operator_role() |
| 109 | + ) |
| 110 | + ) |
| 111 | + ignore_if_already_exists( |
| 112 | + lambda: rbacv1.create_namespaced_role_binding( |
| 113 | + dev_config.namespace, _load_operator_role_binding() |
| 114 | + ) |
| 115 | + ) |
| 116 | + ignore_if_already_exists( |
| 117 | + lambda: corev1.create_namespaced_service_account( |
| 118 | + dev_config.namespace, _load_operator_service_account() |
| 119 | + ) |
| 120 | + ) |
| 121 | + ignore_if_already_exists( |
| 122 | + lambda: appsv1.create_namespaced_deployment( |
| 123 | + dev_config.namespace, _load_operator_deployment() |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +def main(): |
| 129 | + config.load_kube_config() |
| 130 | + dev_config = load_config() |
| 131 | + build_and_push_operator( |
| 132 | + dev_config.repo_url, f"{dev_config.repo_url}/mongodb-kubernetes-operator", "." |
| 133 | + ) |
| 134 | + deploy_operator() |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
0 commit comments