Skip to content

Commit 1c5ce10

Browse files
committed
Avoid conflicts when deployed by ArgoCD
This change introduces a black list for labels like the one already existing for metadata. The list contains one entry for the prefix "app.kubernetes.io". The label "app.kubernetes.io/instance" is per default used by ArgoCD to track resources, which causes copied Secrets to be potentially deleted again by ArgoCD. Also labels with prefix "app.kubernetes.io" are in general very specific to the resources in their respective namespace and therefore shouldn't probably be automatically copied to resources in other namespaces anyway. In order to avoid code duplication the filtering is delegated to an embedded function filter_dict. Signed-off-by: Max Harmathy <[email protected]>
1 parent cee6cb0 commit 1c5ce10

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/consts.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99

1010
CLUSTER_SECRET_LABEL = "clustersecret.io"
1111

12-
BLACK_LISTED_ANNOTATIONS = ["kopf.zalando.org", "kubectl.kubernetes.io"]
12+
BLACK_LISTED_ANNOTATIONS = ["kopf.zalando.org", "kubectl.kubernetes.io"]
13+
BLACK_LISTED_LABELS = ["app.kubernetes.io"]

src/kubernetes_utils.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import logging
22
from datetime import datetime
3-
from typing import Optional, Dict, Any, List, Mapping
3+
from typing import Optional, Dict, Any, List, Mapping, Tuple, Generator
44
import re
55

66
import kopf
77
from kubernetes.client import CoreV1Api, CustomObjectsApi, exceptions, V1ObjectMeta, rest, V1Secret
88

99
from os_utils import get_replace_existing, get_version
1010
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
1212

1313

1414
def patch_clustersecret_status(
@@ -286,27 +286,36 @@ def create_secret_metadata(
286286
Kubernetes metadata object with ClusterSecret annotations.
287287
"""
288288

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 = {
290304
CLUSTER_SECRET_LABEL: 'true'
291305
}
292-
_labels.update(labels or {})
293-
294-
_annotations = {
306+
base_annotations = {
295307
CREATE_BY_ANNOTATION: CREATE_BY_AUTHOR,
296308
VERSION_ANNOTATION: get_version(),
297309
LAST_SYNC_ANNOTATION: datetime.now().isoformat(),
298310
}
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)}
304311

312+
_annotations = filter_dict(BLACK_LISTED_ANNOTATIONS, base_annotations, annotations)
313+
_labels = filter_dict(BLACK_LISTED_LABELS, base_labels, labels)
305314
return V1ObjectMeta(
306315
name=name,
307316
namespace=namespace,
308-
annotations=_annotations,
309-
labels=_labels,
317+
annotations=dict(_annotations),
318+
labels=dict(_labels),
310319
)
311320

312321

0 commit comments

Comments
 (0)