def _ensure_crds() -> None: """ ensure_crds makes sure that all the required CRDs have been created """ crdv1 = client.ApiextensionsV1beta1Api() crd = _load_mongodb_crd() k8s_conditions.ignore_if_doesnt_exist( lambda: crdv1.delete_custom_resource_definition("mongodb.mongodb.com")) # Make sure that the CRD has being deleted before trying to create it again if not k8s_conditions.wait( lambda: crdv1.list_custom_resource_definition( field_selector="metadata.name==mongodb.mongodb.com"), lambda crd_list: len(crd_list.items) == 0, timeout=5, sleep_time=0.5, ): raise Exception( "Execution timed out while waiting for the CRD to be deleted") # TODO: fix this, when calling create_custom_resource_definition, we get the error # ValueError("Invalid value for `conditions`, must not be `None`") # but the crd is still successfully created try: crdv1.create_custom_resource_definition(body=crd) except ValueError as e: pass print("Ensured CRDs")
def main() -> int: args = parse_args() config.load_kube_config() dev_config = load_config(args.config_file, Distro.from_string(args.distro)) create_kube_config(args.config_file) try: build_and_push_images(args, dev_config) prepare_and_run_test(args, dev_config) finally: if not args.skip_dump_diagnostic: dump_diagnostic.dump_all(dev_config.namespace) corev1 = client.CoreV1Api() if not k8s_conditions.wait( lambda: corev1.read_namespaced_pod(TEST_POD_NAME, dev_config. namespace), lambda pod: pod.status.phase == "Succeeded", sleep_time=5, timeout=60, exceptions_to_ignore=ApiException, ): return 1 return 0
def create_test_runner_pod( test: str, config_file: str, tag: str, perform_cleanup: str, test_runner_image_name: str, ) -> None: """ create_test_runner_pod creates the pod which will run all of the tests. """ dev_config = load_config(config_file) corev1 = client.CoreV1Api() pod_body = _get_testrunner_pod_body( test, config_file, tag, perform_cleanup, test_runner_image_name ) if not k8s_conditions.wait( lambda: corev1.list_namespaced_pod( dev_config.namespace, field_selector=f"metadata.name=={TEST_RUNNER_NAME}", ), lambda pod_list: len(pod_list.items) == 0, timeout=30, sleep_time=0.5, ): raise Exception( "Execution timed out while waiting for the existing pod to be deleted" ) if not k8s_conditions.call_eventually_succeeds( lambda: corev1.create_namespaced_pod(dev_config.namespace, body=pod_body), sleep_time=10, timeout=60, exceptions_to_ignore=ApiException, ): raise Exception("Could not create test_runner pod!")
def wait_for_pod_to_be_running(corev1, name, namespace): print("Waiting for pod to be running") if not k8s_conditions.wait( lambda: corev1.read_namespaced_pod(name, namespace), lambda pod: pod.status.phase == "Running", sleep_time=5, timeout=50, exceptions_to_ignore=ApiException, ): raise Exception("Pod never got into Running state!")
def create_test_runner_pod(test: str): """ create_test_runner_pod creates the pod which will run all of the tests. """ dev_config = load_config() corev1 = client.CoreV1Api() pod_body = _get_testrunner_pod_body(test) if not k8s_conditions.wait( lambda: corev1.list_namespaced_pod( dev_config.namespace, field_selector=f"metadata.name=={TEST_RUNNER_NAME}"), lambda pod_list: len(pod_list.items) == 0, timeout=10, sleep_time=0.5, ): raise Exception( "Execution timed out while waiting for the existing pod to be deleted" ) return corev1.create_namespaced_pod(dev_config.namespace, body=pod_body)
def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None: corev1 = client.CoreV1Api() test_pod = { "kind": "Pod", "metadata": { "name": TEST_POD_NAME, "namespace": dev_config.namespace, }, "spec": { "restartPolicy": "Never", "serviceAccountName": "e2e-test", "containers": [{ "name": TEST_POD_NAME, "image": f"{dev_config.repo_url}/{dev_config.e2e_image}:{args.tag}", "imagePullPolicy": "Always", "volumeMounts": [{ "mountPath": "/etc/config", "name": "kube-config-volume" }], "env": [ { "name": "CLUSTER_WIDE", "value": f"{args.cluster_wide}", }, { "name": "OPERATOR_IMAGE", "value": f"{dev_config.repo_url}/{dev_config.operator_image}:{args.tag}", }, { "name": "AGENT_IMAGE", "value": f"{dev_config.repo_url}/{dev_config.agent_image}:{args.tag}", }, { "name": "TEST_NAMESPACE", "value": dev_config.namespace, }, { "name": "VERSION_UPGRADE_HOOK_IMAGE", "value": f"{dev_config.repo_url}/{dev_config.version_upgrade_hook_image}:{args.tag}", }, { "name": "READINESS_PROBE_IMAGE", "value": f"{dev_config.repo_url}/{dev_config.readiness_probe_image}:{args.tag}", }, { "name": "PERFORM_CLEANUP", "value": f"{args.perform_cleanup}", }, ], "command": [ "go", "test", "-v", "-timeout=30m", "-failfast", f"./test/e2e/{args.test}", ], }], "volumes": [{ "name": "kube-config-volume", "configMap": { "name": "kube-config", }, }], }, } if not k8s_conditions.wait( lambda: corev1.list_namespaced_pod( dev_config.namespace, field_selector=f"metadata.name=={TEST_POD_NAME}", ), lambda pod_list: len(pod_list.items) == 0, timeout=30, sleep_time=0.5, ): raise Exception( "Execution timed out while waiting for the existing pod to be deleted" ) if not k8s_conditions.call_eventually_succeeds( lambda: corev1.create_namespaced_pod(dev_config.namespace, body=test_pod), sleep_time=10, timeout=60, exceptions_to_ignore=ApiException, ): raise Exception("Could not create test_runner pod!")
def create_test_pod(args: argparse.Namespace, dev_config: DevConfig) -> None: corev1 = client.CoreV1Api() test_pod = { "kind": "Pod", "metadata": { "name": TEST_POD_NAME, "namespace": dev_config.namespace, }, "spec": { "restartPolicy": "Never", "serviceAccountName": "mongodb-kubernetes-operator", "containers": [{ "name": TEST_POD_NAME, "image": f"{dev_config.repo_url}/{dev_config.e2e_image}:{args.tag}", "imagePullPolicy": "Always", "volumeMounts": [{ "mountPath": "/etc/config", "name": "kube-config-volume" }], "env": [ { "name": "CLUSTER_WIDE", "value": f"{args.cluster_wide}", }, { "name": "OPERATOR_IMAGE", "value": f"{dev_config.repo_url}/{dev_config.operator_image}:{args.tag}", }, { "name": "TEST_NAMESPACE", "value": dev_config.namespace, }, { "name": "VERSION_UPGRADE_HOOK_IMAGE", # TODO: this needs to come from somewhere else "value": "quay.io/mongodb/mongodb-kubernetes-operator-version-upgrade-post-start-hook:1.0.2", }, { "name": "PERFORM_CLEANUP", "value": f"{args.perform_cleanup}", }, ], "command": [ "/bin/operator-sdk", "test", "local", f"./test/e2e/{args.test}", "--operator-namespace", dev_config.namespace, "--verbose", "--kubeconfig", "/etc/config/kubeconfig", "--go-test-flags", "-timeout=60m", ], }], "volumes": [{ "name": "kube-config-volume", "configMap": { "name": "kube-config", }, }], }, } if not k8s_conditions.wait( lambda: corev1.list_namespaced_pod( dev_config.namespace, field_selector=f"metadata.name=={TEST_POD_NAME}", ), lambda pod_list: len(pod_list.items) == 0, timeout=30, sleep_time=0.5, ): raise Exception( "Execution timed out while waiting for the existing pod to be deleted" ) if not k8s_conditions.call_eventually_succeeds( lambda: corev1.create_namespaced_pod(dev_config.namespace, body=test_pod), sleep_time=10, timeout=60, exceptions_to_ignore=ApiException, ): raise Exception("Could not create test_runner pod!")