|
1 | 1 | import logging
|
2 | 2 | from datetime import datetime
|
3 |
| -from typing import Optional, Dict, Any, List, Mapping |
| 3 | +from typing import Optional, Dict, Any, List, Mapping, Tuple, Generator |
4 | 4 | import re
|
5 | 5 |
|
6 | 6 | import kopf
|
7 | 7 | from kubernetes.client import CoreV1Api, CustomObjectsApi, exceptions, V1ObjectMeta, rest, V1Secret
|
8 | 8 |
|
9 | 9 | from os_utils import get_replace_existing, get_version
|
10 | 10 | from consts import CREATE_BY_ANNOTATION, LAST_SYNC_ANNOTATION, VERSION_ANNOTATION, BLACK_LISTED_ANNOTATIONS, \
|
11 |
| - CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL |
| 11 | + BLACK_LISTED_LABELS, CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL |
12 | 12 |
|
13 | 13 |
|
14 | 14 | def patch_clustersecret_status(
|
@@ -286,27 +286,36 @@ def create_secret_metadata(
|
286 | 286 | Kubernetes metadata object with ClusterSecret annotations.
|
287 | 287 | """
|
288 | 288 |
|
289 |
| - _labels = { |
| 289 | + def filter_dict( |
| 290 | + prefixes: List[str], |
| 291 | + base: Dict[str, str], |
| 292 | + source: Optional[Mapping[str, str]] = None |
| 293 | + ) -> Generator[Tuple[str, str]]: |
| 294 | + """ Remove potential useless / dangerous annotations and labels""" |
| 295 | + for item in base.items(): |
| 296 | + yield item |
| 297 | + if source is not None: |
| 298 | + for item in source.items(): |
| 299 | + key, _ = item |
| 300 | + if not any(key.startswith(prefix) for prefix in prefixes): |
| 301 | + yield item |
| 302 | + |
| 303 | + base_labels = { |
290 | 304 | CLUSTER_SECRET_LABEL: 'true'
|
291 | 305 | }
|
292 |
| - _labels.update(labels or {}) |
293 |
| - |
294 |
| - _annotations = { |
| 306 | + base_annotations = { |
295 | 307 | CREATE_BY_ANNOTATION: CREATE_BY_AUTHOR,
|
296 | 308 | VERSION_ANNOTATION: get_version(),
|
297 | 309 | LAST_SYNC_ANNOTATION: datetime.now().isoformat(),
|
298 | 310 | }
|
299 |
| - _annotations.update(annotations or {}) |
300 |
| - |
301 |
| - # Remove potential useless / dangerous annotations |
302 |
| - _annotations = {key: value for key, value in _annotations.items() if |
303 |
| - not any(key.startswith(prefix) for prefix in BLACK_LISTED_ANNOTATIONS)} |
304 | 311 |
|
| 312 | + _annotations = filter_dict(BLACK_LISTED_ANNOTATIONS, base_annotations, annotations) |
| 313 | + _labels = filter_dict(BLACK_LISTED_LABELS, base_labels, labels) |
305 | 314 | return V1ObjectMeta(
|
306 | 315 | name=name,
|
307 | 316 | namespace=namespace,
|
308 |
| - annotations=_annotations, |
309 |
| - labels=_labels, |
| 317 | + annotations=dict(_annotations), |
| 318 | + labels=dict(_labels), |
310 | 319 | )
|
311 | 320 |
|
312 | 321 |
|
|
0 commit comments