forked from 3scale-qe/3scale-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynaconf_loader.py
More file actions
369 lines (305 loc) · 14.4 KB
/
Copy pathdynaconf_loader.py
File metadata and controls
369 lines (305 loc) · 14.4 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""
Custom loader for dynaconf
Some values can be gathered from openshift deployment of 3scale from relevant
configmaps, secrets, routes, etc. These values should be used as
default/fallback in case they aren't defined in config or by env variables.
dynaconf provides interface to custom loader that can be used to gather these
data and have them still available through settings object of dynaconf. It also
provides possibility to set the order oc loaders, this has some limitations
though.
The order of core loaders seems fixed, so custom loader can't be prepended to
them. Anyway this loader requires the config as it needs to know where to find
openshift deployment. That's true for both files and env loader (order of env
loader is defined in config/.env file). At same moment this has to be
overwritten by values from config and env. Therefore the update at the end of
load() is doubled.
"""
import logging
import os
import os.path
import re
from pathlib import Path
from openshift_client import OpenShiftPythonException
from packaging.version import InvalidVersion, Version
from weakget import weakget
from testsuite.openshift.client import OpenShiftClient
log = logging.getLogger(__name__) # pylint: disable=invalid-name
def _route2url(route):
"""Convert host from openshift route to https:// url"""
return f"https://{route['spec']['host']}"
def _testsuite_version():
"""Get testsuite version string"""
path = os.path.join(Path(os.path.abspath(__file__)).parent, "VERSION")
with open(path, encoding="utf8") as version:
return version.read().strip()
def _is_devrelease(namespace):
"""Return true whether installation source should be considered alpha build"""
if re.match(r"3scale-[^-]+-dev", namespace):
return True
if re.match(r"3scale-alpha", namespace):
return True
return False
def _guess_version(ocp, namespace):
"""Attempt to determine version from amp-system imagestream"""
version = None
try:
version = ocp.api_manager.get_annotation("apps.3scale.net/apimanager-threescale-version")
Version(version)
if _is_devrelease(namespace):
version += "-dev"
except (ValueError, IndexError, OpenShiftPythonException, InvalidVersion):
return ".".join(_testsuite_version().split(".")[:2])
return str(version)
def _guess_apicast_operator_version(ocp, settings):
"""Attempt to determine version of apicast operator from subscription"""
if ocp is None:
return 0
version = None
try:
version = ocp.apicast_operator_subscription.model.status.installedCSV.split(".v")[1]
version = version.split("-")[0]
Version(version)
except (ValueError, IndexError, OpenShiftPythonException, InvalidVersion):
# guess version from the cluster-wide operator
try:
_ocp = _apicast_ocp(ocp, settings)
_ocp.project_name = "openshift-operators"
version = _ocp.apicast_operator_subscription.model.status.installedCSV.split(".v")[1]
version = version.split("-")[0]
Version(version)
except (ValueError, IndexError, OpenShiftPythonException, InvalidVersion):
# returning value that is greater than any apicast version
# in result all tests for latest apicast operator will run
return "50035350" # doesn't have to be spot at first glance, this is literal transcript of word "devel"
return str(version)
def _apicast_image(ocp):
"""Find source of amp-apicast image"""
# dc are replaced with deployments in 2.15-dev
try:
lookup = ocp.do_action("get", ["deployment/apicast-production", "-o", "yaml"], parse_output=True)
return lookup.model.spec.template.spec.containers[0].image
except OpenShiftPythonException:
lookup = ocp.do_action("get", ["dc/apicast-production", "-o", "yaml"], parse_output=True)
return lookup.model.spec.template.spec.containers[0].image
def _rhsso_credentials(tools_config, rhsso_config):
"""Search for SSO admin password"""
server_url = tools_config.get("server")
project = tools_config.get("namespace")
token = tools_config.get("token")
kind = rhsso_config.get("kind", "rhbk+ssl")
try:
# is this RHOAM?
tools = OpenShiftClient(project_name="redhat-rhoam-user-sso", server_url=server_url, token=token)
username = tools.secrets["credential-rhssouser"]["ADMIN_USERNAME"].decode("utf-8")
password = tools.secrets["credential-rhssouser"]["ADMIN_PASSWORD"].decode("utf-8")
return username, password
except (OpenShiftPythonException, KeyError):
pass
tools = OpenShiftClient(project_name=project, server_url=server_url, token=token)
if kind.startswith("rhbk"):
try:
# was it deployed as part of tools
username = tools.secrets["rhbk-admin"]["username"].decode("utf-8")
password = tools.secrets["rhbk-admin"]["password"].decode("utf-8")
return username, password
except (OpenShiftPythonException, KeyError):
pass
try:
# was it deployed as part of tools
username = tools.secrets["rhbk-initial-admin"]["username"].decode("utf-8")
password = tools.secrets["rhbk-initial-admin"]["password"].decode("utf-8")
return username, password
except (OpenShiftPythonException, KeyError):
pass
elif kind == "rhsso":
try:
# first RHSSO 7.5 way
username = tools.secrets["credential-sso"]["ADMIN_USERNAME"].decode("utf-8")
password = tools.secrets["credential-sso"]["ADMIN_PASSWORD"].decode("utf-8")
return username, password
except (OpenShiftPythonException, KeyError):
pass
try:
# try 7.4 known deployment if previous fails
username = tools.environ("dc/sso")["SSO_ADMIN_USERNAME"]
password = tools.environ("dc/sso")["SSO_ADMIN_PASSWORD"]
return username, password
except (OpenShiftPythonException, KeyError):
pass
return None, None
def _threescale_operator_ocp(ocp):
try:
ocp.threescale_operator # pylint: disable=pointless-statement
return ocp
except OpenShiftPythonException:
try:
operators = OpenShiftClient("openshift-operators", ocp.server_url, ocp.token)
operators.threescale_operator # pylint: disable=pointless-statement
return operators
except OpenShiftPythonException:
return None
def _apicast_operator_ocp(ocp):
try:
ocp.apicast_operator # pylint: disable=pointless-statement
return ocp
except OpenShiftPythonException:
try:
operators = OpenShiftClient("openshift-operators", ocp.server_url, ocp.token)
operators.apicast_operator # pylint: disable=pointless-statement
return operators
except OpenShiftPythonException:
return None
def _apicast_ocp(ocp, settings):
"""apicast operator can live in different namespace and even openshift"""
settings = weakget(settings)["threescale"]["gateway"]["OperatorApicast"]["openshift"] % {}
# the assumption is that patterns "3scale-{IDENTIFIER}" and "apicast-{IDENTIFIER}" are used
maybe_project_name = ocp.project_name.replace("3scale", "apicast", 1)
if not settings.get("project_name"):
maybe_ocp = OpenShiftClient(
project_name=maybe_project_name, server_url=settings.get("server_url"), token=settings.get("token")
)
if maybe_ocp.project_exists:
return maybe_ocp
return OpenShiftClient(ocp.project_name, ocp.server_url, ocp.token)
return OpenShiftClient(
project_name=settings.get("project_name"), server_url=settings.get("server_url"), token=settings.get("token")
)
def get_routes(ocp):
"""Returns routes grouped by the service they are for"""
mapping = {}
for route in ocp.routes:
mapping.setdefault(route["spec"]["to"]["name"], []).append(route)
for values in mapping.values():
values.sort(key=lambda x: float(x["metadata"].get("labels", {}).get("3scale.net/tenant_id", -1)))
return mapping
def _is_rhoam(client):
"""Returns True, if the current instance is RHOAM. Detects RHOAM by annotations on APIManager object"""
if client.is_operator_deployment:
manager = client.api_manager
if manager.get_annotation("integreatly-name") or manager.get_annotation("integreatly-namespace"):
return True
return False
def _deployment_type(client) -> str:
"""Returns type of 3scale deployment
These are possible (self-explanatory) values:
- on_prem
- rhoam
- saas
"""
if _is_rhoam(client):
return "rhoam"
return "on_prem"
# pylint: disable=unused-argument,too-many-locals
def load(obj, env=None, silent=None, key=None):
"""Reads and loads in to "settings" a single key or all keys from vault
:param obj: the settings instance
:param env: settings env default='DYNACONF'
:param silent: if errors should raise
:param key: if defined load a single key, else load all in env
:return: None
"""
try: # one large try/except block for now with logging at the end
# openshift project: if not set use ENV_FOR_DYNACONF, env NAMESPACE overwrites everything
project = obj.get("env_for_dynaconf")
project = obj.get("openshift", {}).get("projects", {}).get("threescale", {}).get("name", project)
project = os.environ.get("NAMESPACE", project)
ocp_setup = obj.get("openshift", {}).get("servers", {}).get("default", {})
ocp_tools_setup = obj.get("fixtures", {}).get("tools", {})
if not ocp_tools_setup:
ocp_tools_setup = ocp_setup
rhsso_setup = obj.get("rhsso", {})
rhsso_username, rhsso_password = _rhsso_credentials(ocp_tools_setup, rhsso_setup)
ocp = OpenShiftClient(
project_name=project, server_url=ocp_setup.get("server_url"), token=ocp_setup.get("token")
)
apicast_ocp = _apicast_ocp(ocp, obj)
apicast_operator_ocp = _apicast_operator_ocp(apicast_ocp)
threescale_operator_ocp = _threescale_operator_ocp(ocp)
routes = get_routes(ocp)
system_seed = ocp.secrets["system-seed"]
backend_internal_api = ocp.secrets["backend-internal-api"]
admin_url = _route2url(routes["system-provider"][0])
admin_token = system_seed["ADMIN_ACCESS_TOKEN"].decode("utf-8")
master_url = _route2url(routes["system-master"][0])
master_token = system_seed["MASTER_ACCESS_TOKEN"].decode("utf-8")
devel_url = _route2url(routes["system-developer"][0])
superdomain = ocp.config_maps["system-environment"]["THREESCALE_SUPERDOMAIN"]
try:
backend_route = routes["backend-listener"][0]
except (IndexError, KeyError):
# RHOAM changed service name owning the route
backend_route = routes["backend-listener-proxy"][0]
catalogsource = "UNKNOWN"
try:
catalogsource = ocp.do_action("get", ["catalogsource", "-o=jsonpath={.items[0].spec.image}"]).out().strip()
except OpenShiftPythonException:
pass
# all this or nothing
if None in (project, admin_url, admin_token, master_url, master_token, devel_url):
return
# Values gathered in this loader are just fallback defaults, current
# settings needs to be dumped and written again, because a) it doesn't seem
# to be possible to change order of builtin loaders to make this one first;
# b) values from file(s) are needed here anyway. Therefore dump & update
settings = obj.to_dict()
data = {
"openshift": {
"version": ocp.version,
"projects": {"threescale": {"name": project}},
"servers": {"default": {"server_url": ocp.api_url}},
},
"threescale": {
"version": _guess_version(ocp, project),
"apicast_operator_version": _guess_apicast_operator_version(apicast_ocp, obj),
"superdomain": superdomain,
"catalogsource": catalogsource,
"admin": {
"url": admin_url,
"username": system_seed["ADMIN_USER"].decode("utf-8"),
"password": system_seed["ADMIN_PASSWORD"].decode("utf-8"),
"token": admin_token,
},
"master": {
"url": master_url,
"username": system_seed["MASTER_USER"].decode("utf-8"),
"password": system_seed["MASTER_PASSWORD"].decode("utf-8"),
"token": master_token,
},
"devel": {"url": devel_url},
"deployment_type": _deployment_type(ocp),
"gateway": {
"default": {
"portal_endpoint": f"https://{admin_token}@3scale-admin.{superdomain}",
"openshift": ocp,
},
"TemplateApicast": {
"image": _apicast_image(ocp),
},
"OperatorApicast": {
"openshift": {"kind": "OpenShiftClient", "project_name": apicast_ocp.project_name}
},
"WASMGateway": {"backend_host": backend_route["spec"]["host"]},
},
"backend_internal_api": {
"route": backend_route,
"username": backend_internal_api["username"],
"password": backend_internal_api["password"],
},
},
"operators": {
"threescale": {"openshift": threescale_operator_ocp},
"apicast": {"openshift": apicast_operator_ocp},
},
"rhsso": {"password": rhsso_password, "username": rhsso_username},
}
# this overwrites what's already in settings to ensure NAMESPACE is propagated
project_data = {"openshift": {"projects": {"threescale": {"name": project}}}}
settings.update(project_data)
obj.update(data)
obj.update(settings)
log.info("dynamic dynaconf loader successfully got data from openshift")
except Exception as err:
if silent:
log.debug("'%s' appeared with message: %s", type(err).__name__, err, exc_info=True)
return
raise err