Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cosmos/operators/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def __init__(self, profile_config: ProfileConfig | None = None, **kwargs: Any) -
pass

AbstractDbtBase.__init__(self, **base_kwargs)

container_resources = operator_kwargs.get("container_resources")
if isinstance(container_resources, dict):
Comment thread
tatiana marked this conversation as resolved.
operator_kwargs["container_resources"] = k8s.V1ResourceRequirements(**container_resources)
KubernetesPodOperator.__init__(self, **operator_kwargs)

def build_env_args(self, env: dict[str, str | bytes | PathLike[Any]]) -> None:
Expand Down
35 changes: 35 additions & 0 deletions tests/operators/test_kubernetes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch

import kubernetes.client as k8s
import pytest
from airflow import __version__ as airflow_version
from airflow.models import DAG, TaskInstance
Expand Down Expand Up @@ -461,3 +462,37 @@ def test_kubernetes_default_args():
assert dbt_run_operation.image == image
assert dbt_run_operation.project_dir == DBT_ROOT_PATH / "jaffle_shop"
assert dbt_run_operation.profile_config.target_name == profile_config.target_name


def test_kubernetes_pod_container_resources():
"""Test that the container_resources are converted to V1ResourceRequirements in the operator."""
resources = {
"requests": {"cpu": "100m", "memory": "128Mi"},
"limits": {"cpu": "500m", "memory": "512Mi"},
}
a_operator = DbtRunOperationKubernetesOperator(
task_id="run_macro_command",
macro_name="macro",
project_dir=DBT_ROOT_PATH / "jaffle_shop",
container_resources=resources,
)
assert isinstance(a_operator.container_resources, k8s.V1ResourceRequirements)
assert a_operator.container_resources.to_dict()["requests"] == resources["requests"]
assert a_operator.container_resources.to_dict()["limits"] == resources["limits"]

b_operator = DbtRunOperationKubernetesOperator(
task_id="run_macro_command",
macro_name="macro",
project_dir=DBT_ROOT_PATH / "jaffle_shop",
container_resources=k8s.V1ResourceRequirements(**resources),
)
assert isinstance(b_operator.container_resources, k8s.V1ResourceRequirements)
assert b_operator.container_resources.to_dict()["requests"] == resources["requests"]
assert b_operator.container_resources.to_dict()["limits"] == resources["limits"]

c_operator = DbtRunOperationKubernetesOperator(
task_id="run_macro_command",
macro_name="macro",
project_dir=DBT_ROOT_PATH / "jaffle_shop",
)
assert c_operator.container_resources is None