forked from opendatahub-io/opendatahub-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
158 lines (145 loc) · 5.87 KB
/
utils.py
File metadata and controls
158 lines (145 loc) · 5.87 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
from typing import Any
from kubernetes.dynamic import DynamicClient
from ocp_resources.namespace import Namespace
from ocp_resources.service import Service
from ocp_resources.model_registry import ModelRegistry
from kubernetes.dynamic.exceptions import ResourceNotFoundError
from tests.model_registry.constants import MR_DB_IMAGE_DIGEST
from utilities.exceptions import ProtocolNotSupportedError, TooManyServicesError
from utilities.constants import Protocols, Annotations
ADDRESS_ANNOTATION_PREFIX: str = "routing.opendatahub.io/external-address-"
def get_mr_service_by_label(client: DynamicClient, ns: Namespace, mr_instance: ModelRegistry) -> Service:
"""
Args:
client (DynamicClient): OCP Client to use.
ns (Namespace): Namespace object where to find the Service
mr_instance (ModelRegistry): Model Registry instance
Returns:
Service: The matching Service
Raises:
ResourceNotFoundError: if no service is found.
"""
if svc := [
svcs
for svcs in Service.get(
dyn_client=client,
namespace=ns.name,
label_selector=f"app={mr_instance.name},component=model-registry",
)
]:
if len(svc) == 1:
return svc[0]
raise TooManyServicesError(svc)
raise ResourceNotFoundError(f"{mr_instance.name} has no Service")
def get_endpoint_from_mr_service(svc: Service, protocol: str) -> str:
if protocol in (Protocols.REST, Protocols.GRPC):
return svc.instance.metadata.annotations[f"{ADDRESS_ANNOTATION_PREFIX}{protocol}"]
else:
raise ProtocolNotSupportedError(protocol)
def get_model_registry_deployment_template_dict(secret_name: str, resource_name: str) -> dict[str, Any]:
return {
"metadata": {
"labels": {
"name": resource_name,
"sidecar.istio.io/inject": "false",
}
},
"spec": {
"containers": [
{
"env": [
{
"name": "MYSQL_USER",
"valueFrom": {
"secretKeyRef": {
"key": "database-user",
"name": secret_name,
}
},
},
{
"name": "MYSQL_PASSWORD",
"valueFrom": {
"secretKeyRef": {
"key": "database-password",
"name": secret_name,
}
},
},
{
"name": "MYSQL_ROOT_PASSWORD",
"valueFrom": {
"secretKeyRef": {
"key": "database-password",
"name": secret_name,
}
},
},
{
"name": "MYSQL_DATABASE",
"valueFrom": {
"secretKeyRef": {
"key": "database-name",
"name": secret_name,
}
},
},
],
"args": [
"--datadir",
"/var/lib/mysql/datadir",
"--default-authentication-plugin=mysql_native_password",
],
"image": MR_DB_IMAGE_DIGEST,
"imagePullPolicy": "IfNotPresent",
"livenessProbe": {
"exec": {
"command": [
"/bin/bash",
"-c",
"mysqladmin -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} ping",
]
},
"initialDelaySeconds": 15,
"periodSeconds": 10,
"timeoutSeconds": 5,
},
"name": "mysql",
"ports": [{"containerPort": 3306, "protocol": "TCP"}],
"readinessProbe": {
"exec": {
"command": [
"/bin/bash",
"-c",
'mysql -D ${MYSQL_DATABASE} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "SELECT 1"',
]
},
"initialDelaySeconds": 10,
"timeoutSeconds": 5,
},
"securityContext": {"capabilities": {}, "privileged": False},
"terminationMessagePath": "/dev/termination-log",
"volumeMounts": [
{
"mountPath": "/var/lib/mysql",
"name": f"{resource_name}-data",
}
],
}
],
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"volumes": [
{
"name": f"{resource_name}-data",
"persistentVolumeClaim": {"claimName": resource_name},
}
],
},
}
def get_model_registry_db_label_dict(db_resource_name: str) -> dict[str, str]:
return {
Annotations.KubernetesIo.NAME: db_resource_name,
Annotations.KubernetesIo.INSTANCE: db_resource_name,
Annotations.KubernetesIo.PART_OF: db_resource_name,
}