forked from opendatahub-io/opendatahub-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguardrails.py
More file actions
196 lines (168 loc) · 6.49 KB
/
guardrails.py
File metadata and controls
196 lines (168 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from typing import Generator, Any
import pytest
from _pytest.fixtures import FixtureRequest
from kubernetes.dynamic import DynamicClient
from ocp_resources.config_map import ConfigMap
from ocp_resources.deployment import Deployment
from ocp_resources.guardrails_orchestrator import GuardrailsOrchestrator
from ocp_resources.namespace import Namespace
from ocp_resources.pod import Pod
from ocp_resources.resource import ResourceEditor
from ocp_resources.route import Route
from utilities.constants import Labels, Annotations
from utilities.guardrails import check_guardrails_health_endpoint
GUARDRAILS_ORCHESTRATOR_NAME: str = "guardrails-orchestrator"
@pytest.fixture(scope="class")
def guardrails_orchestrator(
request: FixtureRequest,
admin_client: DynamicClient,
model_namespace: Namespace,
) -> Generator[GuardrailsOrchestrator, Any, Any]:
gorch_kwargs = {
"client": admin_client,
"name": GUARDRAILS_ORCHESTRATOR_NAME,
"namespace": model_namespace.name,
"log_level": "DEBUG",
"replicas": 1,
"wait_for_resource": True,
}
if request.param.get("auto_config"):
gorch_kwargs["auto_config"] = request.param.get("auto_config")
if request.param.get("orchestrator_config"):
orchestrator_config = request.getfixturevalue(argname="orchestrator_config")
gorch_kwargs["orchestrator_config"] = orchestrator_config.name
if request.param.get("enable_guardrails_gateway"):
gorch_kwargs["enable_guardrails_gateway"] = True
if request.param.get("guardrails_gateway_config"):
guardrails_gateway_config = request.getfixturevalue(argname="guardrails_gateway_config")
gorch_kwargs["guardrails_gateway_config"] = guardrails_gateway_config.name
if enable_built_in_detectors := request.param.get("enable_built_in_detectors"):
gorch_kwargs["enable_built_in_detectors"] = enable_built_in_detectors
if request.param.get("otel_exporter_config"):
metrics_endpoint = request.getfixturevalue(argname="otelcol_metrics_endpoint")
traces_endpoint = request.getfixturevalue(argname="tempo_traces_endpoint")
gorch_kwargs["otel_exporter"] = {
"otlpProtocol": "grpc",
"otlpMetricsEndpoint": metrics_endpoint,
"otlpTracesEndpoint": traces_endpoint,
"enableMetrics": True,
"enableTracing": True,
}
with GuardrailsOrchestrator(**gorch_kwargs) as gorch:
gorch_deployment = Deployment(name=gorch.name, namespace=gorch.namespace, wait_for_resource=True)
gorch_deployment.wait_for_replicas()
yield gorch
@pytest.fixture(scope="class")
def orchestrator_config(
request: FixtureRequest, admin_client: DynamicClient, model_namespace: Namespace
) -> Generator[ConfigMap, Any, Any]:
with ConfigMap(
client=admin_client,
name="fms-orchestr8-config-nlp",
namespace=model_namespace.name,
data=request.param["orchestrator_config_data"],
) as cm:
yield cm
@pytest.fixture(scope="class")
def guardrails_gateway_config(
request: FixtureRequest, admin_client: DynamicClient, model_namespace: Namespace
) -> Generator[ConfigMap, Any, Any]:
with ConfigMap(
client=admin_client,
name="fms-orchestr8-config-gateway",
namespace=model_namespace.name,
label={Labels.Openshift.APP: "fmstack-nlp"},
data=request.param["guardrails_gateway_config_data"],
) as cm:
yield cm
@pytest.fixture(scope="class")
def guardrails_orchestrator_pod(
admin_client: DynamicClient,
model_namespace: Namespace,
guardrails_orchestrator: GuardrailsOrchestrator,
) -> Pod:
return list(
Pod.get(
namespace=model_namespace.name, label_selector=f"app.kubernetes.io/instance={GUARDRAILS_ORCHESTRATOR_NAME}"
)
)[0]
@pytest.fixture(scope="class")
def guardrails_orchestrator_route(
admin_client: DynamicClient,
model_namespace: Namespace,
guardrails_orchestrator: GuardrailsOrchestrator,
) -> Generator[Route, Any, Any]:
guardrails_orchestrator_route = Route(
name=f"{guardrails_orchestrator.name}",
namespace=guardrails_orchestrator.namespace,
wait_for_resource=True,
ensure_exists=True,
)
with ResourceEditor(
patches={
guardrails_orchestrator_route: {
"metadata": {
"annotations": {Annotations.HaproxyRouterOpenshiftIo.TIMEOUT: "10m"},
}
}
}
):
yield guardrails_orchestrator_route
@pytest.fixture(scope="class")
def guardrails_orchestrator_url(
guardrails_orchestrator_route: Route,
) -> str:
return f"https://{guardrails_orchestrator_route.host}"
@pytest.fixture(scope="class")
def guardrails_orchestrator_health_route(
admin_client: DynamicClient,
model_namespace: Namespace,
guardrails_orchestrator: GuardrailsOrchestrator,
) -> Generator[Route, Any, Any]:
guardrails_orchestrator_health_route = Route(
name=f"{guardrails_orchestrator.name}-health",
namespace=guardrails_orchestrator.namespace,
wait_for_resource=True,
ensure_exists=True,
)
with ResourceEditor(
patches={
guardrails_orchestrator_health_route: {
"metadata": {
"annotations": {Annotations.HaproxyRouterOpenshiftIo.TIMEOUT: "10m"},
}
}
}
):
yield guardrails_orchestrator_health_route
@pytest.fixture
def guardrails_healthcheck(
current_client_token, openshift_ca_bundle_file, guardrails_orchestrator_health_route: Route
) -> None:
check_guardrails_health_endpoint(
token=current_client_token,
host=guardrails_orchestrator_health_route.host,
ca_bundle_file=openshift_ca_bundle_file,
)
@pytest.fixture(scope="class")
def guardrails_orchestrator_gateway_route(
admin_client: DynamicClient,
model_namespace: Namespace,
guardrails_orchestrator: GuardrailsOrchestrator,
) -> Generator[Route, Any, Any]:
guardrails_orchestrator_gateway_route = Route(
name=f"{guardrails_orchestrator.name}-gateway",
namespace=guardrails_orchestrator.namespace,
wait_for_resource=True,
ensure_exists=True,
)
with ResourceEditor(
patches={
guardrails_orchestrator_gateway_route: {
"metadata": {
"annotations": {Annotations.HaproxyRouterOpenshiftIo.TIMEOUT: "10m"},
}
}
}
):
yield guardrails_orchestrator_gateway_route