Skip to content

Commit

Permalink
Merge pull request Kuadrant#361 from pehala/backend_reorg
Browse files Browse the repository at this point in the history
Reorganize backend classes
  • Loading branch information
pehala authored Mar 12, 2024
2 parents 3eb83fe + 9acb45c commit e3ef956
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 86 deletions.
15 changes: 15 additions & 0 deletions testsuite/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Module containing all the Backends"""

from abc import abstractmethod

from testsuite.gateway import Referencable
from testsuite.lifecycle import LifecycleObject


class Backend(LifecycleObject, Referencable):
"""Backend (workload) deployed in Kubernetes"""

@property
@abstractmethod
def url(self):
"""Returns internal URL for this backend"""
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Httpbin related classes"""
"""Httpbin implementation of Backend"""

from functools import cached_property

from testsuite.openshift.backend import Backend
from testsuite.backend import Backend
from testsuite.openshift import Selector
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.deployment import Deployment
from testsuite.openshift.service import Service, ServicePort


class Httpbin(Backend):
"""Httpbin deployed in OpenShift"""
"""Httpbin deployed in Kubernetes as Backend"""

def __init__(self, openshift: OpenShiftClient, name, label, replicas=1) -> None:
super().__init__()
Expand Down
69 changes: 69 additions & 0 deletions testsuite/backend/mockserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Mockserver implementation as Backend"""

from testsuite.backend import Backend
from testsuite.openshift import Selector
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.deployment import Deployment, ContainerResources
from testsuite.openshift.service import Service, ServicePort


class MockserverBackend(Backend):
"""Mockserver deployed as backend in Kubernetes"""

PORT = 8080

def __init__(self, openshift: OpenShiftClient, name: str, label: str):
self.openshift = openshift
self.name = name
self.label = label

self.deployment = None
self.service = None

@property
def reference(self):
return {
"group": "",
"kind": "Service",
"port": self.PORT,
"name": self.name,
"namespace": self.openshift.project,
}

@property
def url(self):
return f"{self.name}.{self.openshift.project}.svc.cluster.local"

def commit(self):
match_labels = {"app": self.label, "deployment": self.name}
self.deployment = Deployment.create_instance(
self.openshift,
self.name,
container_name="mockserver",
image="quay.io/mganisin/mockserver:latest",
ports={"api": 1080},
selector=Selector(matchLabels=match_labels),
labels={"app": self.label},
resources=ContainerResources(limits_memory="2G"),
lifecycle={"postStart": {"exec": {"command": ["/bin/sh", "init-mockserver"]}}},
)
self.deployment.commit()
self.deployment.wait_for_ready()

self.service = Service.create_instance(
self.openshift,
self.name,
selector=match_labels,
ports=[ServicePort(name="1080-tcp", port=self.PORT, targetPort="api")],
labels={"app": self.label},
)
self.service.commit()

def delete(self):
with self.openshift.context:
if self.service:
self.service.delete()
self.service = None
if self.deployment:
self.deployment.delete()
self.deployment = None
4 changes: 2 additions & 2 deletions testsuite/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

if TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.httpbin import Httpbin
from testsuite.backend import Backend


class Referencable(ABC):
Expand Down Expand Up @@ -164,7 +164,7 @@ def remove_all_hostnames(self):
"""Remove all hostnames from the Route"""

@abstractmethod
def add_backend(self, backend: "Httpbin", prefix):
def add_backend(self, backend: "Backend", prefix):
"""Adds another backend to the Route, with specific prefix"""

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions testsuite/gateway/envoy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import yaml

from testsuite.openshift.httpbin import Httpbin
from testsuite.backend import Backend
from testsuite.openshift import modify
from testsuite.openshift.config_map import ConfigMap

Expand Down Expand Up @@ -108,7 +108,7 @@ def create_instance(
)

@modify
def add_backend(self, backend: Httpbin, prefix: str):
def add_backend(self, backend: Backend, prefix: str):
"""Adds backend to the EnvoyConfig"""
config = yaml.safe_load(self["envoy.yaml"])
config["static_resources"]["clusters"].append(yaml.safe_load(CLUSTER.replace("${backend_url}", backend.url)))
Expand Down
4 changes: 2 additions & 2 deletions testsuite/gateway/envoy/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if typing.TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.httpbin import Httpbin
from testsuite.backend import Backend
from testsuite.policy.authorization.auth_config import AuthConfig


Expand All @@ -28,7 +28,7 @@ def __init__(self, openshift, gateway) -> None:
self.auth_configs: list["AuthConfig"] = []
self.hostnames: list[str] = []

def add_backend(self, backend: "Httpbin", prefix="/"):
def add_backend(self, backend: "Backend", prefix="/"):
self.gateway.config.add_backend(backend, prefix)
self.gateway.rollout()

Expand Down
8 changes: 4 additions & 4 deletions testsuite/gateway/gateway_api/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from testsuite.utils import asdict

if typing.TYPE_CHECKING:
from testsuite.openshift.httpbin import Httpbin
from testsuite.backend import Backend


class HTTPRoute(OpenShiftObject, GatewayRoute):
Expand Down Expand Up @@ -74,9 +74,9 @@ def remove_all_hostnames(self):
self.model.spec.hostnames = []

@modify
def add_rule(self, backend: "Httpbin", *route_matches: RouteMatch):
def add_rule(self, backend: "Backend", *route_matches: RouteMatch):
"""Adds rule to the Route"""
rules = {"backendRefs": [backend.reference]}
rules: dict[str, typing.Any] = {"backendRefs": [backend.reference]}
matches = list(route_matches)
if len(matches) == 0:
matches.append(RouteMatch(path=PathMatch(type=MatchType.PATH_PREFIX, value="/")))
Expand All @@ -90,7 +90,7 @@ def remove_all_rules(self):
self.model.spec.rules = []

@modify
def add_backend(self, backend: "Httpbin", prefix="/"):
def add_backend(self, backend: "Backend", prefix="/"):
self.model.spec.rules.append(
{"backendRefs": [backend.reference], "matches": [{"path": {"value": prefix, "type": "PathPrefix"}}]}
)
Expand Down
63 changes: 0 additions & 63 deletions testsuite/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

from testsuite.utils import ContentType
from testsuite.httpx import KuadrantClient
from testsuite.openshift import Selector
from testsuite.openshift.backend import Backend
from testsuite.openshift.service import Service, ServicePort
from testsuite.openshift.deployment import Deployment, ContainerResources
from testsuite.openshift.client import OpenShiftClient


class Mockserver:
Expand Down Expand Up @@ -77,61 +72,3 @@ def retrieve_requests(self, expectation_id):
params={"type": "REQUESTS", "format": "JSON"},
json={"path": "/" + expectation_id},
).json()


class MockserverBackend(Backend):
"""Mockserver deployed as backend in Openshift"""

PORT = 8080

def __init__(self, openshift: OpenShiftClient, name: str, label: str):
self.openshift = openshift
self.name = name
self.label = label

self.deployment = None
self.service = None

@property
def reference(self):
return {
"group": "",
"kind": "Service",
"port": self.PORT,
"name": self.name,
"namespace": self.openshift.project,
}

def commit(self):
match_labels = {"app": self.label, "deployment": self.name}
self.deployment = Deployment.create_instance(
self.openshift,
self.name,
container_name="mockserver",
image="quay.io/mganisin/mockserver:latest",
ports={"api": 1080},
selector=Selector(matchLabels=match_labels),
labels={"app": self.label},
resources=ContainerResources(limits_memory="2G"),
lifecycle={"postStart": {"exec": {"command": ["/bin/sh", "init-mockserver"]}}},
)
self.deployment.commit()
self.deployment.wait_for_ready()

self.service = Service.create_instance(
self.openshift,
self.name,
selector=match_labels,
ports=[ServicePort(name="1080-tcp", port=self.PORT, targetPort="api")],
labels={"app": self.label},
)
self.service.commit()

def delete(self):
with self.openshift.context:
if self.service:
self.service.delete()
self.service = None
if self.deployment:
self.deployment.delete()
self.deployment = None
8 changes: 0 additions & 8 deletions testsuite/openshift/backend.py

This file was deleted.

2 changes: 1 addition & 1 deletion testsuite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from testsuite.gateway import Gateway, GatewayRoute, Hostname, Exposer
from testsuite.oidc import OIDCProvider
from testsuite.oidc.auth0 import Auth0Provider
from testsuite.openshift.httpbin import Httpbin
from testsuite.backend.httpbin import Httpbin
from testsuite.oidc.rhsso import RHSSO
from testsuite.gateway.envoy import Envoy
from testsuite.gateway.envoy.route import EnvoyVirtualRoute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest

from testsuite.openshift.secret import Secret
from testsuite.mockserver import Mockserver, MockserverBackend
from testsuite.mockserver import Mockserver
from testsuite.backend.mockserver import MockserverBackend
from testsuite.policy.dns_policy import HealthCheck, AdditionalHeadersRef

pytestmark = [pytest.mark.mgc]
Expand Down

0 comments on commit e3ef956

Please sign in to comment.