-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbacklog_controller.py
More file actions
185 lines (156 loc) · 6.56 KB
/
backlog_controller.py
File metadata and controls
185 lines (156 loc) · 6.56 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
import datetime
import http
import logging
import math
import dateutil.parser
import kubernetes.client.rest
import urllib3.exceptions
import ci.log
import k8s.backlog
import k8s.logging
import k8s.model
import k8s.util
import odg.extensions_cfg
import odg.util
import paths
logger = logging.getLogger(__name__)
ci.log.configure_default_logging()
k8s.logging.configure_kubernetes_logging()
def on_backlog_change(
backlog_controller_cfg: odg.extensions_cfg.BacklogControllerConfig,
metadata: dict,
namespace: str,
kubernetes_api: k8s.util.KubernetesApi,
):
service = metadata.get('labels').get(k8s.model.LABEL_SERVICE)
labels = {
k8s.model.LABEL_SERVICE: service,
}
label_selector = k8s.util.create_label_selector(labels=labels)
backlog_crds = kubernetes_api.custom_kubernetes_api.list_namespaced_custom_object(
group=k8s.model.BacklogItemCrd.DOMAIN,
version=k8s.model.BacklogItemCrd.VERSION,
plural=k8s.model.BacklogItemCrd.PLURAL_NAME,
namespace=namespace,
label_selector=label_selector,
).get('items')
running_pod_names = []
now = datetime.datetime.now(tz=datetime.timezone.utc)
for backlog_crd in backlog_crds:
crd_name = backlog_crd.get('metadata').get('name')
labels = backlog_crd.get('metadata').get('labels')
annotations = backlog_crd.get('metadata').get('annotations')
label_claimed = labels.get(k8s.backlog.LABEL_CLAIMED)
if not (label_claimed and k8s.util.label_is_true(label=label_claimed)):
continue
claimed_by = annotations.get(k8s.backlog.ANNOTATION_CLAIMED_BY)
claimed_at = dateutil.parser.parse(annotations.get(k8s.backlog.ANNOTATION_CLAIMED_AT))
if not running_pod_names:
running_pod_names = [
pod.metadata.name
for pod in kubernetes_api.core_kubernetes_api.list_namespaced_pod(
namespace=namespace,
label_selector=label_selector,
).items
]
if claimed_by and claimed_by not in running_pod_names:
logger.warning(
f'the pod {claimed_by} which claimed the backlog item {crd_name} '
'is not available anymore',
)
k8s.backlog.remove_claim(
namespace=namespace,
kubernetes_api=kubernetes_api,
backlog_crd=backlog_crd,
)
elif claimed_at.tzinfo and now - claimed_at >= datetime.timedelta(
minutes=backlog_controller_cfg.remove_claim_after_minutes,
):
logger.warning(
f'the backlog item {crd_name} was claimed for more than '
f'{backlog_controller_cfg.remove_claim_after_minutes} minutes by pod {claimed_by}',
)
k8s.backlog.remove_claim(
namespace=namespace,
kubernetes_api=kubernetes_api,
backlog_crd=backlog_crd,
)
if service == odg.extensions_cfg.Services.ISSUE_REPLICATOR:
# only allow up-scaling to 1 for issue replicator because of github's secondary rate limits
max_replicas = 1
elif service == odg.extensions_cfg.Services.BLACKDUCK:
# limit parallel BlackDuck API load
max_replicas = 2
else:
max_replicas = backlog_controller_cfg.max_replicas
items_per_replica = backlog_controller_cfg.backlog_items_per_replica
desired_replicas = min(math.ceil(len(backlog_crds) / items_per_replica), max_replicas)
k8s.util.scale_replicas(
service=service,
namespace=namespace,
kubernetes_api=kubernetes_api,
desired_replicas=desired_replicas,
)
def main():
parsed_arguments = odg.util.parse_args(
arguments=(
odg.util.Arguments.K8S_CFG_NAME,
odg.util.Arguments.KUBECONFIG,
odg.util.Arguments.K8S_NAMESPACE,
odg.util.Arguments.EXTENSIONS_CFG_PATH,
),
)
kubernetes_api = odg.util.kubernetes_api(parsed_arguments)
namespace = parsed_arguments.k8s_namespace
k8s.logging.init_logging_thread(
service=odg.extensions_cfg.Services.BACKLOG_CONTROLLER,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
if not (extensions_cfg_path := parsed_arguments.extensions_cfg_path):
extensions_cfg_path = paths.extensions_cfg_path()
extensions_cfg = odg.extensions_cfg.ExtensionsConfiguration.from_file(extensions_cfg_path)
backlog_controller_cfg = extensions_cfg.backlog_controller
resource_version = ''
while True:
try:
for event in kubernetes.watch.Watch().stream(
kubernetes_api.custom_kubernetes_api.list_namespaced_custom_object,
group=k8s.model.BacklogItemCrd.DOMAIN,
version=k8s.model.BacklogItemCrd.VERSION,
namespace=namespace,
plural=k8s.model.BacklogItemCrd.PLURAL_NAME,
resource_version=resource_version,
timeout_seconds=0,
):
if (type := str(event['type'])) == 'MODIFIED':
continue
metadata = event['object'].get('metadata')
resource_version = metadata['resourceVersion']
name = metadata['name']
logger.debug(f'identified modification {type=} of backlog item {name}')
on_backlog_change(
backlog_controller_cfg=backlog_controller_cfg,
metadata=metadata,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
except kubernetes.client.rest.ApiException as e:
if e.status == http.HTTPStatus.GONE:
resource_version = ''
logger.info('API resource watching expired, will start new watch')
else:
raise e
except urllib3.exceptions.ProtocolError:
# this is a known error which has no impact on the functionality, thus rather be
# degregated to a warning or even info
# [ref](https://github.com/kiwigrid/k8s-sidecar/issues/233#issuecomment-1332358459)
resource_version = ''
logger.info('API resource watching received protocol error, will start new watch')
except urllib3.exceptions.MaxRetryError as e:
if not isinstance(e.reason, urllib3.exceptions.ProtocolError):
raise
resource_version = ''
logger.info('API resource watching received protocol error, will start new watch')
if __name__ == '__main__':
main()