Skip to content

Commit 51aedf7

Browse files
committed
fix(controller): metrics sql tpl, gateway pod should not restart, cleanup old rs, update monitor datatbase config
1 parent a38739f commit 51aedf7

7 files changed

Lines changed: 192 additions & 83 deletions

File tree

charts/controller/templates/_helpers.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ env:
9797
- name: DRYCC_TS_USER
9898
valueFrom:
9999
secretKeyRef:
100-
name: database-creds
100+
name: timeseries-creds
101101
key: user
102102
- name: DRYCC_TS_PASSWORD
103103
valueFrom:
104104
secretKeyRef:
105-
name: database-creds
105+
name: timeseries-creds
106106
key: password
107107
- name: DRYCC_DATABASE_MONITOR_URL
108108
value: "postgres://$(DRYCC_TS_USER):$(DRYCC_TS_PASSWORD)@drycc-timeseries-replica.{{.Release.Namespace}}.svc.{{.Values.global.clusterDomain}}:5432/monitor"

charts/controller/templates/controller-clusterrole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ rules:
6969
verbs: ["get"]
7070
- apiGroups: ["apps"]
7171
resources: ["replicasets"]
72-
verbs: ["get", "list", "watch"]
72+
verbs: ["get", "list", "watch", "delete"]
7373
- apiGroups: [""]
7474
resources: ["persistentvolumeclaims"]
7575
verbs: ["get", "list", "watch", "create", "delete", "patch", "update"]
Lines changed: 98 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1+
import json
12
from urllib.parse import quote
23

3-
from social_core.utils import sanitize_redirect, user_is_authenticated, \
4-
user_is_active, partial_pipeline_data, setting_url
4+
from social_core.utils import (
5+
partial_pipeline_data,
6+
sanitize_redirect,
7+
setting_url,
8+
user_is_active,
9+
user_is_authenticated,
10+
)
511

612

7-
def do_auth(backend, redirect_name='next'):
13+
def do_auth(backend, redirect_name="next"):
814
# Save any defined next value into session
915
data = backend.strategy.request_data(merge=False)
1016

1117
# Save extra data into session.
12-
for field_name in backend.setting('FIELDS_STORED_IN_SESSION', []):
18+
for field_name in backend.setting("FIELDS_STORED_IN_SESSION", []):
1319
if field_name in data:
1420
backend.strategy.session_set(field_name, data[field_name])
1521
else:
1622
backend.strategy.session_set(field_name, None)
17-
# uri = None
23+
1824
if redirect_name in data:
1925
# Check and sanitize a user-defined GET/POST next field value
2026
redirect_uri = data[redirect_name]
21-
if backend.setting('SANITIZE_REDIRECTS', True):
22-
allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \
23-
[backend.strategy.request_host()]
27+
if backend.setting("SANITIZE_REDIRECTS", True):
28+
allowed_hosts = backend.setting("ALLOWED_REDIRECT_HOSTS", []) + [
29+
backend.strategy.request_host()
30+
]
2431
redirect_uri = sanitize_redirect(allowed_hosts, redirect_uri)
2532
backend.strategy.session_set(
26-
redirect_name,
27-
redirect_uri or backend.setting('LOGIN_REDIRECT_URL')
33+
redirect_name, redirect_uri or backend.setting("LOGIN_REDIRECT_URL")
2834
)
2935
response = backend.start()
30-
url = response.url.split('?')[1]
36+
url = response.url.split("?")[1]
3137

3238
def form2json(form_data):
3339
from urllib.parse import parse_qs, urlparse
34-
query = urlparse('?' + form_data).query
40+
query = urlparse("?" + form_data).query
3541
params = parse_qs(query)
3642
return {key: params[key][0] for key in params}
3743
from django.core.cache import cache
38-
cache.set("oidc_key_" + data.get('key', ''), form2json(url).get('state'), 60 * 10)
44+
cache.set("oidc_key_" + data.get("key", ""), form2json(url).get("state"), 60 * 10)
3945
return response
4046

4147

42-
def do_complete(backend, login, user=None, redirect_name='next',
43-
*args, **kwargs):
48+
def do_complete(backend, login, user=None, redirect_name="next", *args, **kwargs):
4449
data = backend.strategy.request_data()
4550

4651
is_authenticated = user_is_authenticated(user)
@@ -56,8 +61,9 @@ def do_complete(backend, login, user=None, redirect_name='next',
5661

5762
# pop redirect value before the session is trashed on login(), but after
5863
# the pipeline so that the pipeline can change the redirect if needed
59-
redirect_value = backend.strategy.session_get(redirect_name, '') or \
60-
data.get(redirect_name, '')
64+
redirect_value = backend.strategy.session_get(redirect_name, "") or data.get(
65+
redirect_name, ""
66+
)
6167

6268
# check if the output value is something else than a user and just
6369
# return it to the client
@@ -67,54 +73,99 @@ def do_complete(backend, login, user=None, redirect_name='next',
6773

6874
if is_authenticated:
6975
if not user:
70-
url = setting_url(backend, redirect_value, 'LOGIN_REDIRECT_URL')
76+
url = setting_url(backend, redirect_value, "LOGIN_REDIRECT_URL")
7177
else:
72-
url = setting_url(backend, redirect_value,
73-
'NEW_ASSOCIATION_REDIRECT_URL',
74-
'LOGIN_REDIRECT_URL')
78+
url = setting_url(
79+
backend,
80+
redirect_value,
81+
"NEW_ASSOCIATION_REDIRECT_URL",
82+
"LOGIN_REDIRECT_URL",
83+
)
7584
elif user:
7685
if user_is_active(user):
7786
# catch is_new/social_user in case login() resets the instance
78-
is_new = getattr(user, 'is_new', False)
87+
is_new = getattr(user, "is_new", False)
7988
social_user = user.social_user
8089
login(backend, user, social_user)
8190
# store last login backend name in session
82-
backend.strategy.session_set('social_auth_last_login_backend',
83-
social_user.provider)
91+
backend.strategy.session_set(
92+
"social_auth_last_login_backend", social_user.provider
93+
)
8494

8595
if is_new:
86-
url = setting_url(backend,
87-
'NEW_USER_REDIRECT_URL',
88-
redirect_value,
89-
'LOGIN_REDIRECT_URL')
96+
url = setting_url(
97+
backend,
98+
"NEW_USER_REDIRECT_URL",
99+
redirect_value,
100+
"LOGIN_REDIRECT_URL",
101+
)
90102
else:
91-
url = setting_url(backend, redirect_value,
92-
'LOGIN_REDIRECT_URL')
103+
url = setting_url(backend, redirect_value, "LOGIN_REDIRECT_URL")
93104
else:
94-
if backend.setting('INACTIVE_USER_LOGIN', False):
105+
if backend.setting("INACTIVE_USER_LOGIN", False):
95106
social_user = user.social_user
96107
login(backend, user, social_user)
97-
url = setting_url(backend, 'INACTIVE_USER_URL', 'LOGIN_ERROR_URL',
98-
'LOGIN_URL')
108+
url = setting_url(
109+
backend, "INACTIVE_USER_URL", "LOGIN_ERROR_URL", "LOGIN_URL"
110+
)
99111
else:
100-
url = setting_url(backend, 'LOGIN_ERROR_URL', 'LOGIN_URL')
112+
url = setting_url(backend, "LOGIN_ERROR_URL", "LOGIN_URL")
101113

102114
if redirect_value and redirect_value != url:
103115
redirect_value = quote(redirect_value)
104-
url += ('&' if '?' in url else '?') + \
105-
'{0}={1}'.format(redirect_name, redirect_value)
106-
107-
if backend.setting('SANITIZE_REDIRECTS', True):
108-
allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \
109-
[backend.strategy.request_host()]
110-
url = sanitize_redirect(allowed_hosts, url) or \
111-
backend.setting('LOGIN_REDIRECT_URL')
116+
url += ("&" if "?" in url else "?") + f"{redirect_name}={redirect_value}"
117+
118+
if backend.setting("SANITIZE_REDIRECTS", True):
119+
allowed_hosts = backend.setting("ALLOWED_REDIRECT_HOSTS", []) + [
120+
backend.strategy.request_host()
121+
]
122+
url = sanitize_redirect(allowed_hosts, url) or backend.setting(
123+
"LOGIN_REDIRECT_URL"
124+
)
125+
112126
response = backend.strategy.redirect(url)
113-
social_auth = user.social_auth.filter(provider='drycc').\
114-
order_by('-modified').last()
127+
social_auth = user.social_auth.filter(provider="drycc").\
128+
order_by("-modified").last()
129+
if social_auth and social_auth.extra_data:
130+
extra_data = json.loads(social_auth.extra_data) if \
131+
isinstance(social_auth.extra_data, str) else social_auth.extra_data
115132
from django.core.cache import cache
116-
cache.set("oidc_state_" + data.get('state'),
117-
{'token': social_auth.extra_data.get('id_token', 'fail'),
118-
'username': user.username},
133+
cache.set("oidc_state_" + data.get("state"),
134+
{"token": extra_data.get("id_token", "fail"),
135+
"username": user.username},
119136
60 * 10)
120137
return response
138+
139+
140+
def do_disconnect(
141+
backend, user, association_id=None, redirect_name="next", *args, **kwargs
142+
):
143+
partial = partial_pipeline_data(backend, user, *args, **kwargs)
144+
if partial:
145+
if association_id and not partial.kwargs.get("association_id"):
146+
partial.extend_kwargs({"association_id": association_id})
147+
response = backend.disconnect(*partial.args, **partial.kwargs)
148+
# clean partial data after usage
149+
backend.strategy.clean_partial_pipeline(partial.token)
150+
else:
151+
response = backend.disconnect(
152+
user=user, association_id=association_id, *args, **kwargs
153+
)
154+
155+
if isinstance(response, dict):
156+
url = backend.strategy.absolute_uri(
157+
backend.strategy.request_data().get(redirect_name, "")
158+
or backend.setting("DISCONNECT_REDIRECT_URL")
159+
or backend.setting("LOGIN_REDIRECT_URL")
160+
)
161+
if backend.setting("SANITIZE_REDIRECTS", True):
162+
allowed_hosts = backend.setting("ALLOWED_REDIRECT_HOSTS", []) + [
163+
backend.strategy.request_host()
164+
]
165+
url = (
166+
sanitize_redirect(allowed_hosts, url)
167+
or backend.setting("DISCONNECT_REDIRECT_URL")
168+
or backend.setting("LOGIN_REDIRECT_URL")
169+
)
170+
response = backend.strategy.redirect(url)
171+
return response

rootfs/api/models/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def cleanup_old(self):
401401
if scale_type in app_settings.canaries:
402402
names.append(self._get_job_id(scale_type, True))
403403
names.append(self._get_job_id(scale_type, False))
404-
deployments = self._scheduler.deployments.get(self.id).json()["items"]
404+
labels = {'heritage': 'drycc'}
405+
deployments = self._scheduler.deployments.get(self.id, labels=labels).json()["items"]
405406
if deployments is not None:
406407
for deployment in deployments:
407408
name = deployment['metadata']['name']

rootfs/api/models/release.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,40 @@ def cleanup_old(self): # noqa
165165
"""
166166
Cleanup any old resources from Kubernetes
167167
168-
This includes any RCs that are no longer considered the latest release (just a safety net)
168+
This includes any RSs that are no longer considered the latest release (just a safety net)
169169
Secrets no longer tied to any ReplicaSet
170170
Stray pods no longer relevant to the latest release
171171
"""
172172
latest_version = 'v{}'.format(self.version)
173173
self.app.log(
174-
'Cleaning up RCs for releases older than {} (latest)'.format(latest_version),
174+
'Cleaning up RSs for releases older than {} (latest)'.format(latest_version),
175175
level=logging.DEBUG
176176
)
177177

178178
# Cleanup controllers
179179
labels = {'heritage': 'drycc'}
180-
controller_removal = []
181-
controllers = self._scheduler.rc.get(self.app.id, labels=labels).json()['items']
182-
if not controllers:
183-
controllers = []
184-
for controller in controllers:
185-
current_version = controller['metadata']['labels']['version']
180+
replica_sets_removal = []
181+
replica_sets = self._scheduler.rs.get(self.app.id, labels=labels).json()['items']
182+
if not replica_sets:
183+
replica_sets = []
184+
for replica_set in replica_sets:
185+
current_version = replica_set['metadata']['labels']['version']
186186
# skip the latest release
187187
if current_version == latest_version:
188188
continue
189189

190190
# aggregate versions together to removal all at once
191-
if current_version not in controller_removal:
192-
controller_removal.append(current_version)
191+
if current_version not in replica_sets_removal:
192+
replica_sets_removal.append(current_version)
193193

194-
if controller_removal:
194+
if replica_sets_removal:
195195
self.app.log(
196-
'Found the following versions to cleanup: {}'.format(', '.join(controller_removal)), # noqa
196+
'Found the following versions to cleanup: {}'.format(', '.join(replica_sets_removal)), # noqa
197197
level=logging.DEBUG
198198
)
199199

200200
# this is RC related
201-
for version in controller_removal:
201+
for version in replica_sets_removal:
202202
self._delete_release_in_scheduler(self.app.id, version)
203203

204204
# handle Deployments specific cleanups
@@ -269,7 +269,7 @@ def _delete_release_in_scheduler(self, namespace, version):
269269
"""
270270
Deletes a specific release in k8s based on ReplicationController
271271
272-
Scale RCs to 0 then delete RCs and the version specific
272+
Scale RSs to 0 then delete RSs and the version specific
273273
secret that container the env var
274274
"""
275275
labels = {
@@ -281,14 +281,14 @@ def _delete_release_in_scheduler(self, namespace, version):
281281
# see if the app config has deploy timeout preference, otherwise use global
282282
timeout = self.config.values.get('DRYCC_DEPLOY_TIMEOUT', settings.DRYCC_DEPLOY_TIMEOUT)
283283

284-
controllers = self._scheduler.rc.get(namespace, labels=labels).json()['items']
285-
if not controllers:
286-
controllers = []
287-
for controller in controllers:
288-
# Deployment takes care of this in the API, RC does not
289-
# Have the RC scale down pods and delete itself
290-
self._scheduler.rc.scale(namespace, controller['metadata']['name'], 0, timeout)
291-
self._scheduler.rc.delete(namespace, controller['metadata']['name'])
284+
replica_sets = self._scheduler.rs.get(namespace, labels=labels).json()['items']
285+
if not replica_sets:
286+
replica_sets = []
287+
for replica_set in replica_sets:
288+
# Deployment takes care of this in the API, RS does not
289+
# Have the RS scale down pods and delete itself
290+
self._scheduler.rs.scale(namespace, replica_set['metadata']['name'], 0, timeout)
291+
self._scheduler.rs.delete(namespace, replica_set['metadata']['name'])
292292

293293
def save(self, *args, **kwargs): # noqa
294294
if not self.summary:

rootfs/api/monitor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
ON kubernetes_pod_network.tag_id = kubernetes_pod_network_tag.tag_id
1515
WHERE
1616
namespace in ({namespace_range})
17-
AND time < to_timestamp({start})
18-
AND time > to_timestamp({stop})
17+
AND time > to_timestamp({start})
18+
AND time < to_timestamp({stop})
1919
GROUP by namespace, pod_name
2020
"""
2121

@@ -34,8 +34,8 @@
3434
WHERE
3535
namespace='{namespace}'
3636
AND container_name='{container_name}'
37-
AND time < to_timestamp({start})
38-
AND time > to_timestamp({stop})
37+
AND time > to_timestamp({start})
38+
AND time < to_timestamp({stop})
3939
GROUP BY namespace, pod_name, container_name, kubernetes_pod_container.tag_id
4040
) AS container
4141
GROUP BY namespace, container_name
@@ -56,8 +56,8 @@
5656
WHERE
5757
namespace='{namespace}'
5858
AND container_name='{container_name}'
59-
AND time < to_timestamp({start})
60-
AND time > to_timestamp({stop})
59+
AND time > to_timestamp({start})
60+
AND time < to_timestamp({stop})
6161
GROUP BY namespace, pod_name, container_name, timestamp
6262
"""
6363

@@ -76,8 +76,8 @@
7676
WHERE
7777
namespace='{namespace}'
7878
AND container_name='{container_name}'
79-
AND time < to_timestamp({start})
80-
AND time > to_timestamp({stop})
79+
AND time > to_timestamp({start})
80+
AND time < to_timestamp({stop})
8181
GROUP BY namespace, pod_name, container_name, timestamp
8282
"""
8383

@@ -95,8 +95,8 @@
9595
WHERE
9696
namespace='{namespace}'
9797
AND pod_name like '{pod_name_prefix}%'
98-
AND time < to_timestamp({start})
99-
AND time > to_timestamp({stop})
98+
AND time > to_timestamp({start})
99+
AND time < to_timestamp({stop})
100100
GROUP by namespace, pod_name, timestamp
101101
"""
102102

0 commit comments

Comments
 (0)