Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rename service name for event streams in k8s #672

Merged
merged 1 commit into from
Feb 15, 2024
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
2 changes: 1 addition & 1 deletion src/aap_eda/services/activation/engine/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ContainerEngine(ABC):
"""Abstract interface to connect to the deployment backend."""

@abstractmethod
def __init__(self, activation_id: str):
def __init__(self, activation_id: str, resource_prefix: str):
...

@abstractmethod
Expand Down
6 changes: 3 additions & 3 deletions src/aap_eda/services/activation/engine/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from .podman import Engine as PodmanEngine


def new_container_engine(activation_id: str):
def new_container_engine(activation_id: str, resource_prefix: str):
"""Activation service factory."""
"""Returns an activation object based on the deployment type"""
# TODO: deployment type should not be plain strings
if settings.DEPLOYMENT_TYPE == "podman":
return PodmanEngine(activation_id)
return PodmanEngine(activation_id, resource_prefix)
if settings.DEPLOYMENT_TYPE == "k8s":
return KubernetesEngine(activation_id)
return KubernetesEngine(activation_id, resource_prefix)
raise exceptions.InvalidDeploymentTypeError("Wrong deployment type")
8 changes: 5 additions & 3 deletions src/aap_eda/services/activation/engine/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Engine(ContainerEngine):
def __init__(
self,
activation_id: str,
resource_prefix: str,
client=None,
) -> None:
if client:
Expand All @@ -91,19 +92,20 @@ def __init__(
self.client = get_k8s_client()

self._set_namespace()
self.secret_name = f"activation-secret-{activation_id}"
self.resource_prefix = resource_prefix.replace("_", "-")
hsong-rh marked this conversation as resolved.
Show resolved Hide resolved
self.secret_name = f"{self.resource_prefix}-secret-{activation_id}"
self.job_name = None
self.pod_name = None

def start(self, request: ContainerRequest, log_handler: LogHandler) -> str:
# TODO : Should this be compatible with the previous version
# Previous Version
self.job_name = (
f"activation-job-{request.activation_id}"
f"{self.resource_prefix}-job-{request.activation_id}"
f"-{request.activation_instance_id}"
)
self.pod_name = (
f"activation-pod-{request.activation_id}"
f"{self.resource_prefix}-pod-{request.activation_id}"
f"-{request.activation_instance_id}"
)

Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/services/activation/engine/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Engine(ContainerEngine):
def __init__(
self,
_activation_id: str,
_resource_prefix: str = None,
client=None,
) -> None:
try:
Expand Down
4 changes: 3 additions & 1 deletion src/aap_eda/services/activation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def __init__(
if container_engine:
self.container_engine = container_engine
else:
self.container_engine = new_container_engine(db_instance.id)
self.container_engine = new_container_engine(
db_instance.id, self.db_instance_type
)

self.container_logger_class = container_logger_class

Expand Down
61 changes: 56 additions & 5 deletions tests/integration/services/activation/engine/test_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from kubernetes.config.config_exception import ConfigException

from aap_eda.core import models
from aap_eda.core.enums import ActivationStatus
from aap_eda.core.enums import ActivationStatus, ProcessParentType
from aap_eda.services.activation.db_log_handler import DBLogger
from aap_eda.services.activation.engine.common import (
AnsibleRulebookCmdLine,
Expand Down Expand Up @@ -267,23 +267,49 @@ def kubernetes_engine(init_data):
with mock.patch("builtins.open", mock.mock_open(read_data="aap-eda")):
engine = Engine(
activation_id=str(activation_id),
resource_prefix=ProcessParentType.ACTIVATION,
client=mock.Mock(),
)

yield engine


@pytest.mark.django_db
def test_engine_init(init_data):
@pytest.fixture
def kubernetes_event_stream_engine(init_data):
activation_id = init_data.activation.id
with mock.patch("builtins.open", mock.mock_open(read_data="aap-eda")):
engine = Engine(
activation_id=str(activation_id),
resource_prefix=ProcessParentType.EVENT_STREAM,
client=mock.Mock(),
)

assert engine.namespace == "aap-eda"
assert engine.secret_name == f"activation-secret-{activation_id}"
yield engine


@pytest.mark.parametrize(
"resource_prefixes",
[
{ProcessParentType.ACTIVATION: "activation"},
{ProcessParentType.EVENT_STREAM: "event-stream"},
],
)
@pytest.mark.django_db
def test_engine_init(init_data, resource_prefixes):
activation_id = init_data.activation.id
with mock.patch("builtins.open", mock.mock_open(read_data="aap-eda")):
for prefix in resource_prefixes:
engine = Engine(
activation_id=str(activation_id),
resource_prefix=prefix,
client=mock.Mock(),
)

assert engine.namespace == "aap-eda"
assert (
engine.secret_name
== f"{resource_prefixes[prefix]}-secret-{activation_id}"
)


@pytest.mark.django_db
Expand All @@ -298,6 +324,7 @@ def test_engine_init_with_exception(init_data):
):
Engine(
activation_id=str(activation_id),
resource_prefix=ProcessParentType.ACTIVATION,
client=mock.Mock(),
)

Expand Down Expand Up @@ -348,6 +375,30 @@ def test_engine_start(init_data, kubernetes_engine):
)


@pytest.mark.django_db
def test_event_stream_engine_start(init_data, kubernetes_event_stream_engine):
engine = kubernetes_event_stream_engine
request = get_request(init_data)
log_handler = DBLogger(init_data.activation_instance.id)

with mock.patch("aap_eda.services.activation.engine.kubernetes.watch"):
engine.start(request, log_handler)

assert engine.job_name == (
f"event-stream-job-{init_data.activation.id}-"
f"{init_data.activation_instance.id}"
)
assert engine.pod_name == (
f"event-stream-pod-{init_data.activation.id}-"
f"{init_data.activation_instance.id}"
)

assert models.RulebookProcessLog.objects.count() == 5
assert models.RulebookProcessLog.objects.last().log.endswith(
f"Job {engine.job_name} is running"
)


@pytest.mark.django_db
def test_engine_start_with_create_job_exception(init_data, kubernetes_engine):
engine = kubernetes_engine
Expand Down
Loading