Skip to content

Commit 3b2ec45

Browse files
committed
Address review feedback
1 parent 14df149 commit 3b2ec45

File tree

2 files changed

+122
-133
lines changed

2 files changed

+122
-133
lines changed

tests/model_serving/model_server/maas_billing/conftest.py

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
from timeout_sampler import TimeoutSampler
1313
from utilities.llmd_utils import create_llmisvc
1414
from utilities.llmd_constants import ModelStorage, ContainerImages
15+
from ocp_resources.gateway_gateway_networking_k8s_io import Gateway
16+
from ocp_resources.data_science_cluster import DataScienceCluster
1517
from utilities.constants import (
1618
MAAS_GATEWAY_NAMESPACE,
1719
MAAS_RATE_LIMIT_POLICY_NAME,
1820
MAAS_TOKEN_RATE_LIMIT_POLICY_NAME,
19-
Timeout,
2021
)
2122

2223
from ocp_resources.infrastructure import Infrastructure
@@ -28,8 +29,7 @@
2829
from utilities.general import wait_for_oauth_openshift_deployment
2930
from ocp_resources.secret import Secret
3031
from tests.model_serving.model_server.maas_billing.utils import get_total_tokens
31-
from utilities.infra import get_data_science_cluster
32-
from utilities.constants import DscComponents
32+
from utilities.constants import DscComponents, MAAS_GATEWAY_NAME
3333
from utilities.resources.rate_limit_policy import RateLimitPolicy
3434
from utilities.resources.token_rate_limit_policy import TokenRateLimitPolicy
3535
from tests.model_serving.model_server.maas_billing.utils import (
@@ -44,10 +44,9 @@
4444
maas_gateway_rate_limits_patched,
4545
detect_maas_control_plane_namespace,
4646
get_tier_mapping_configmap,
47-
ensure_maas_gateway_api,
48-
ensure_maas_usage_policies,
4947
endpoints_have_ready_addresses,
5048
gateway_probe_reaches_maas_api,
49+
maas_gateway_listeners,
5150
)
5251

5352
LOGGER = get_logger(name=__name__)
@@ -577,7 +576,7 @@ def maas_inference_service_tinyllama(
577576
},
578577
service_account=model_service_account.name,
579578
wait=False,
580-
timeout=Timeout.TIMEOUT_15MIN,
579+
timeout=900,
581580
) as llm_service:
582581
with patch_llmisvc_with_maas_router(
583582
llm_service=llm_service,
@@ -589,7 +588,7 @@ def maas_inference_service_tinyllama(
589588
llm_service.wait_for_condition(
590589
condition="Ready",
591590
status="True",
592-
timeout=Timeout.TIMEOUT_15MIN,
591+
timeout=900,
593592
)
594593

595594
LOGGER.info(
@@ -637,47 +636,40 @@ def maas_gateway_api_hostname(admin_client: DynamicClient) -> str:
637636

638637
@pytest.fixture(scope="session")
639638
def maas_gateway_and_policies(
640-
admin_client: DynamicClient,
641-
maas_gateway_api_hostname: str,
642-
) -> Generator[None, None, None]:
643-
"""
644-
Ensure MaaS Gateway + Kuadrant policies exist once per test session.
645-
"""
646-
with (
647-
ensure_maas_gateway_api(
648-
admin_client=admin_client,
649-
hostname=maas_gateway_api_hostname,
650-
),
651-
ensure_maas_usage_policies(
652-
admin_client=admin_client,
653-
),
654-
):
655-
yield
639+
maas_gateway_api: None,
640+
maas_usage_policies: None,
641+
) -> None:
642+
yield
656643

657644

658645
@pytest.fixture(scope="session")
659646
def maas_controller_enabled_latest(
660-
admin_client: DynamicClient,
647+
dsc_resource: DataScienceCluster,
661648
maas_gateway_and_policies: None,
662-
):
663-
dsc_resource = get_data_science_cluster(client=admin_client)
664-
dsc_resource.get()
665-
666-
original_components = dsc_resource.instance.spec.components
667-
668-
kserve = original_components[DscComponents.KSERVE]
669-
maas = kserve["modelsAsService"]
670-
if maas["managementState"] == "Managed":
671-
dsc_resource.wait_for_condition(condition="ModelsAsServiceReady", status="True", timeout=Timeout.TIMEOUT_15MIN)
649+
) -> Generator[DataScienceCluster, None, None]:
650+
original_components = dsc_resource.instance.spec.components or {}
651+
kserve = original_components.get(DscComponents.KSERVE, {}) or {}
652+
maas = kserve.get("modelsAsService", {}) or {}
653+
654+
if maas.get("managementState") == DscComponents.ManagementState.MANAGED:
655+
dsc_resource.wait_for_condition(
656+
condition="ModelsAsServiceReady",
657+
status="True",
658+
timeout=300,
659+
)
672660
yield dsc_resource
661+
return
673662

674-
component_patch = {DscComponents.KSERVE: {"modelsAsService": {"managementState": "Managed"}}}
663+
component_patch = {
664+
DscComponents.KSERVE: {"modelsAsService": {"managementState": DscComponents.ManagementState.MANAGED}}
665+
}
675666

676667
with ResourceEditor(patches={dsc_resource: {"spec": {"components": component_patch}}}):
677-
dsc_resource.wait_for_condition(condition="ModelsAsServiceReady", status="True", timeout=Timeout.TIMEOUT_15MIN)
678-
dsc_resource.wait_for_condition(condition="Ready", status="True", timeout=Timeout.TIMEOUT_15MIN)
668+
dsc_resource.wait_for_condition(condition="ModelsAsServiceReady", status="True", timeout=900)
669+
dsc_resource.wait_for_condition(condition="Ready", status="True", timeout=600)
679670
yield dsc_resource
680-
dsc_resource.wait_for_condition(condition="Ready", status="True", timeout=Timeout.TIMEOUT_15MIN)
671+
672+
dsc_resource.wait_for_condition(condition="Ready", status="True", timeout=600)
681673

682674

683675
@pytest.fixture(scope="session")
@@ -719,16 +711,17 @@ def maas_api_ready(
719711
client=admin_client,
720712
name="maas-api",
721713
namespace="opendatahub",
714+
ensure_exists=True,
722715
)
723716
maas_api_deployment.wait_for_condition(
724717
condition="Available",
725718
status="True",
726-
timeout=Timeout.TIMEOUT_10MIN,
719+
timeout=600,
727720
)
728721

729722
endpoints_ready = False
730723
for endpoints_ready_sample in TimeoutSampler(
731-
wait_timeout=Timeout.TIMEOUT_5MIN,
724+
wait_timeout=300,
732725
sleep=5,
733726
func=endpoints_have_ready_addresses,
734727
admin_client=admin_client,
@@ -748,7 +741,7 @@ def maas_api_ready(
748741
last_response_body: str | None = None
749742

750743
for gateway_probe_sample in TimeoutSampler(
751-
wait_timeout=Timeout.TIMEOUT_5MIN,
744+
wait_timeout=300,
752745
sleep=5,
753746
func=gateway_probe_reaches_maas_api,
754747
http_session=request_session_http,
@@ -795,3 +788,83 @@ def maas_token_ratelimit_policy(
795788
namespace=MAAS_GATEWAY_NAMESPACE,
796789
ensure_exists=True,
797790
)
791+
792+
793+
@pytest.fixture(scope="session")
794+
def maas_gateway_api(
795+
admin_client: DynamicClient,
796+
maas_gateway_api_hostname: str,
797+
) -> Generator[None, None, None]:
798+
"""
799+
Ensure MaaS Gateway exists once per test session.
800+
"""
801+
gateway = Gateway(
802+
client=admin_client,
803+
name=MAAS_GATEWAY_NAME,
804+
namespace=MAAS_GATEWAY_NAMESPACE,
805+
gateway_class_name="openshift-default",
806+
listeners=maas_gateway_listeners(hostname=maas_gateway_api_hostname),
807+
annotations={"opendatahub.io/managed": "false"},
808+
label={
809+
"app.kubernetes.io/name": "maas",
810+
"app.kubernetes.io/instance": MAAS_GATEWAY_NAME,
811+
"app.kubernetes.io/component": "gateway",
812+
"opendatahub.io/managed": "false",
813+
},
814+
ensure_exists=False,
815+
wait_for_resource=True,
816+
teardown=True,
817+
)
818+
819+
with gateway:
820+
yield
821+
822+
823+
@pytest.fixture(scope="session")
824+
def maas_usage_policies(
825+
admin_client: DynamicClient,
826+
maas_gateway_api: None,
827+
) -> Generator[None, None, None]:
828+
"""
829+
Ensure MaaS Kuadrant policies exist once per test session.
830+
"""
831+
target_ref = {
832+
"group": "gateway.networking.k8s.io",
833+
"kind": "Gateway",
834+
"name": MAAS_GATEWAY_NAME,
835+
}
836+
837+
request_policy = RateLimitPolicy(
838+
client=admin_client,
839+
name=MAAS_RATE_LIMIT_POLICY_NAME,
840+
namespace=MAAS_GATEWAY_NAMESPACE,
841+
target_ref=target_ref,
842+
limits={
843+
"bootstrap": {
844+
"counters": [{"expression": "auth.identity.userid"}],
845+
"rates": [{"limit": 1000, "window": "1m"}],
846+
}
847+
},
848+
ensure_exists=False,
849+
wait_for_resource=True,
850+
teardown=True,
851+
)
852+
853+
token_policy = TokenRateLimitPolicy(
854+
client=admin_client,
855+
name=MAAS_TOKEN_RATE_LIMIT_POLICY_NAME,
856+
namespace=MAAS_GATEWAY_NAMESPACE,
857+
target_ref=target_ref,
858+
limits={
859+
"bootstrap": {
860+
"counters": [{"expression": "auth.identity.userid"}],
861+
"rates": [{"limit": 1000000, "window": "1m"}],
862+
}
863+
},
864+
ensure_exists=False,
865+
wait_for_resource=True,
866+
teardown=True,
867+
)
868+
869+
with request_policy, token_policy:
870+
yield

tests/model_serving/model_server/maas_billing/utils.py

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
from ocp_resources.resource import ResourceEditor
1717
from utilities.resources.rate_limit_policy import RateLimitPolicy
1818
from utilities.resources.token_rate_limit_policy import TokenRateLimitPolicy
19-
from ocp_resources.gateway_gateway_networking_k8s_io import Gateway
19+
20+
# from ocp_resources.gateway_gateway_networking_k8s_io import Gateway
2021
from ocp_resources.endpoints import Endpoints
2122
from ocp_resources.config_map import ConfigMap
2223
from ocp_resources.namespace import Namespace
2324
from utilities.constants import (
2425
MAAS_GATEWAY_NAME,
2526
MAAS_GATEWAY_NAMESPACE,
26-
MAAS_RATE_LIMIT_POLICY_NAME,
27-
MAAS_TOKEN_RATE_LIMIT_POLICY_NAME,
2827
)
2928

3029

@@ -472,92 +471,6 @@ def maas_gateway_listeners(hostname: str) -> List[Dict[str, Any]]:
472471
]
473472

474473

475-
@contextmanager
476-
def ensure_maas_gateway_api(
477-
*,
478-
admin_client: DynamicClient,
479-
hostname: str,
480-
) -> Generator[None, None, None]:
481-
gateway = Gateway(
482-
client=admin_client,
483-
name=MAAS_GATEWAY_NAME,
484-
namespace=MAAS_GATEWAY_NAMESPACE,
485-
gateway_class_name="openshift-default",
486-
listeners=maas_gateway_listeners(hostname=hostname),
487-
annotations={"opendatahub.io/managed": "false"},
488-
label={
489-
"app.kubernetes.io/name": "maas",
490-
"app.kubernetes.io/instance": MAAS_GATEWAY_NAME,
491-
"app.kubernetes.io/component": "gateway",
492-
"opendatahub.io/managed": "false",
493-
},
494-
ensure_exists=False,
495-
wait_for_resource=True,
496-
teardown=True,
497-
)
498-
499-
LOGGER.info(f"Creating MaaS Gateway: {MAAS_GATEWAY_NAMESPACE}/{MAAS_GATEWAY_NAME} (hostname={hostname})")
500-
with gateway:
501-
yield
502-
503-
504-
@contextmanager
505-
def ensure_maas_usage_policies(*, admin_client: DynamicClient) -> Generator[None, None, None]:
506-
target_ref = {
507-
"group": "gateway.networking.k8s.io",
508-
"kind": "Gateway",
509-
"name": MAAS_GATEWAY_NAME,
510-
}
511-
512-
request_policy = RateLimitPolicy(
513-
client=admin_client,
514-
name=MAAS_RATE_LIMIT_POLICY_NAME,
515-
namespace=MAAS_GATEWAY_NAMESPACE,
516-
target_ref=target_ref,
517-
limits=_minimal_ratelimit_limits(),
518-
ensure_exists=False,
519-
wait_for_resource=True,
520-
teardown=True,
521-
)
522-
523-
token_policy = TokenRateLimitPolicy(
524-
client=admin_client,
525-
name=MAAS_TOKEN_RATE_LIMIT_POLICY_NAME,
526-
namespace=MAAS_GATEWAY_NAMESPACE,
527-
target_ref=target_ref,
528-
limits=_minimal_token_ratelimit_limits(),
529-
ensure_exists=False,
530-
wait_for_resource=True,
531-
teardown=True,
532-
)
533-
534-
LOGGER.info(
535-
f"Creating MaaS usage policies in {MAAS_GATEWAY_NAMESPACE}: "
536-
f"{MAAS_RATE_LIMIT_POLICY_NAME}, {MAAS_TOKEN_RATE_LIMIT_POLICY_NAME}"
537-
)
538-
539-
with request_policy, token_policy:
540-
yield
541-
542-
543-
def _minimal_ratelimit_limits() -> Dict[str, Any]:
544-
return {
545-
"bootstrap": {
546-
"counters": [{"expression": "auth.identity.userid"}],
547-
"rates": [{"limit": 1000, "window": "1m"}],
548-
}
549-
}
550-
551-
552-
def _minimal_token_ratelimit_limits() -> Dict[str, Any]:
553-
return {
554-
"bootstrap": {
555-
"counters": [{"expression": "auth.identity.userid"}],
556-
"rates": [{"limit": 1000000, "window": "1m"}],
557-
}
558-
}
559-
560-
561474
def detect_maas_control_plane_namespace(admin_client: DynamicClient) -> str:
562475
"""
563476
Detect the namespace that contains MaaS configuration objects
@@ -610,9 +523,12 @@ def endpoints_have_ready_addresses(
610523
namespace=namespace,
611524
ensure_exists=True,
612525
)
613-
endpoints.get()
614-
subsets = endpoints.instance.subsets or []
615-
return any((subset.addresses or []) for subset in subsets)
526+
527+
subsets = endpoints.instance.subsets
528+
if not subsets:
529+
return False
530+
531+
return any(subset.addresses for subset in subsets)
616532

617533

618534
def gateway_probe_reaches_maas_api(

0 commit comments

Comments
 (0)