def test_kube_config_out_of_cluster(self, monkeypatch): config = MagicMock() config.load_incluster_config.side_effect = ConfigException() monkeypatch.setattr("prefect.utilities.kubernetes.config", config) batchapi = MagicMock() monkeypatch.setattr( "prefect.utilities.kubernetes.client", MagicMock(BatchV1Api=MagicMock(return_value=batchapi)), ) get_kubernetes_client("job", kubernetes_api_key_secret=None) assert config.load_kube_config.called
def test_kube_config_out_of_cluster(self, monkeypatch): config = MagicMock() config.load_incluster_config.side_effect = ConfigException() monkeypatch.setattr("prefect.tasks.kubernetes.pod.config", config) coreapi = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.pod.client", MagicMock(CoreV1Api=MagicMock(return_value=coreapi)), ) task = CreateNamespacedPod(body={"test": "a"}, kubernetes_api_key_secret=None) task.run(body={"test": "b"}) assert config.load_kube_config.called
def test_kube_config_out_of_cluster(self, monkeypatch): config = MagicMock() config.load_incluster_config.side_effect = ConfigException() monkeypatch.setattr("prefect.tasks.kubernetes.job.config", config) batchapi = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.job.client", MagicMock(BatchV1Api=MagicMock(return_value=batchapi)), ) task = ListNamespacedJob(kube_kwargs={"test": "a"}, kubernetes_api_key_secret=None) task.run(kube_kwargs={"test": "b"}) assert config.load_kube_config.called
def test_run_kubernetes_job_in_cluster(): active_run = mock.Mock() project_name = "mlflow-docker-example" image_tag = "image_tag" image_digest = "5e74a5a" command = ["python train.py --alpha 0.5 --l1-ratio 0.1"] env_vars = {"RUN_ID": "1"} kube_context = None job_template = yaml.safe_load( "apiVersion: batch/v1\n" "kind: Job\n" "metadata:\n" " name: pi-with-ttl\n" " namespace: mlflow\n" "spec:\n" " ttlSecondsAfterFinished: 100\n" " template:\n" " spec:\n" " containers:\n" " - name: pi\n" " image: perl\n" " command: ['perl', '-Mbignum=bpi', '-wle']\n" " restartPolicy: Never\n") with mock.patch("kubernetes.config.load_kube_config") as kube_config_mock: kube_config_mock.side_effect = ConfigException() with mock.patch("kubernetes.config.load_incluster_config" ) as incluster_kube_config_mock: with mock.patch( "kubernetes.client.BatchV1Api.create_namespaced_job" ) as kube_api_mock: submitted_run_obj = kb.run_kubernetes_job( project_name=project_name, active_run=active_run, image_tag=image_tag, image_digest=image_digest, command=command, env_vars=env_vars, job_template=job_template, kube_context=kube_context, ) assert submitted_run_obj._mlflow_run_id == active_run.info.run_id assert submitted_run_obj._job_name.startswith(project_name) assert submitted_run_obj._job_namespace == "mlflow" assert kube_api_mock.call_count == 1 assert kube_config_mock.call_count == 1 assert incluster_kube_config_mock.call_count == 1
def __init__(self): self.log = get_logger(self.__class_name.__name__) if kalytical_config.kalytical_endpoint is None: # This is the API endpoint we send back to the pod for a callback/interaction during pipeline running. It may be behind a load balancer/DNS - i.e. it can't communicate with local host) raise ConfigException( "Config is missing parameter for kalytical API endpoint!") try: # Defaults to the service account asssigned to th epod config.load_incluster_config() except CDonfigException: self.log.warn( f"Could not load kube configuration from pod! Attempting to configure client with local kubeconfig={config.KUBE_CONFIG_DEFAULT_LOCATION}") config.load_kube_config() self._k8s_core_client = client.CoreV1Api() self.log = get_logger(self. __class__.__name__) self._running_job_list = None
def test_kube_config_out_of_cluster(self, monkeypatch): config = MagicMock() config.load_incluster_config.side_effect = ConfigException() monkeypatch.setattr("prefect.tasks.kubernetes.deployment.config", config) extensionsapi = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.deployment.client", MagicMock(ExtensionsV1beta1Api=MagicMock( return_value=extensionsapi)), ) task = ReplaceNamespacedDeployment(body={"test": "a"}, deployment_name="test", kubernetes_api_key_secret=None) task.run(body={"test": "b"}) assert config.load_kube_config.called