Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to customize the blocked labels #152

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
IMG_NAMESPACE = flag5
IMG_NAME = clustersecret
IMG_FQNAME = $(IMG_NAMESPACE)/$(IMG_NAME)
IMG_VERSION = 0.0.12
IMG_VERSION = 0.0.13

.PHONY: container push clean
all: container
Expand Down
4 changes: 2 additions & 2 deletions charts/cluster-secret/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: cluster-secret
description: ClusterSecret Operator
kubeVersion: '>= 1.25.0-0'
type: application
version: 0.4.5
version: 0.5.0
icon: https://clustersecret.com/assets/csninjasmall.png
sources:
- https://github.com/zakkg3/ClusterSecret
appVersion: "0.0.12"
appVersion: "0.0.13"
maintainers:
- email: zakkg3@gmail.com
name: zakkg3
1 change: 1 addition & 0 deletions charts/cluster-secret/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ spec:
{{- end }}
containers:
- env:
{{- .Values.env | toYaml | nindent 8 }}
- name: KUBERNETES_CLUSTER_DOMAIN
value: {{ .Values.kubernetesClusterDomain }}
- name: CLUSTER_SECRET_VERSION
Expand Down
4 changes: 4 additions & 0 deletions charts/cluster-secret/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ image:
# It can also be replaced, just set value to true.
replace_existing: 'false'

env:
- name: BLOCKED_LABELS
value: app.kubernetes.io # a comma (,) separated list

kubernetesClusterDomain: cluster.local

nodeSelector: {}
Expand Down
5 changes: 3 additions & 2 deletions src/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

CLUSTER_SECRET_LABEL = "clustersecret.io"

BLACK_LISTED_ANNOTATIONS = ["kopf.zalando.org", "kubectl.kubernetes.io"]
BLACK_LISTED_LABELS = ["app.kubernetes.io"]
BLOCKED_ANNOTATIONS = ["kopf.zalando.org", "kubectl.kubernetes.io"]

BLOCKED_LABELS = ["app.kubernetes.io"]
10 changes: 5 additions & 5 deletions src/kubernetes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import kopf
from kubernetes.client import CoreV1Api, CustomObjectsApi, exceptions, V1ObjectMeta, rest, V1Secret

from os_utils import get_replace_existing, get_version
from consts import CREATE_BY_ANNOTATION, LAST_SYNC_ANNOTATION, VERSION_ANNOTATION, BLACK_LISTED_ANNOTATIONS, \
BLACK_LISTED_LABELS, CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL
from os_utils import get_blocked_labels, get_replace_existing, get_version
from consts import CREATE_BY_ANNOTATION, LAST_SYNC_ANNOTATION, VERSION_ANNOTATION, BLOCKED_ANNOTATIONS, \
CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL


def patch_clustersecret_status(
Expand Down Expand Up @@ -309,8 +309,8 @@ def filter_dict(
LAST_SYNC_ANNOTATION: datetime.now().isoformat(),
}

_annotations = filter_dict(BLACK_LISTED_ANNOTATIONS, base_annotations, annotations)
_labels = filter_dict(BLACK_LISTED_LABELS, base_labels, labels)
_annotations = filter_dict(BLOCKED_ANNOTATIONS, base_annotations, annotations)
_labels = filter_dict(get_blocked_labels(), base_labels, labels)
return V1ObjectMeta(
name=name,
namespace=namespace,
Expand Down
15 changes: 14 additions & 1 deletion src/os_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import os
from functools import cache

from consts import BLOCKED_LABELS


@cache
def get_version() -> str:
"""
Wrapper for CLUSTER_SECRET_VERSION variable environment
"""
return os.getenv('CLUSTER_SECRET_VERSION', '0')


@cache
def get_replace_existing() -> bool:

replace_existing = os.getenv('REPLACE_EXISTING', 'false')
return replace_existing.lower() == 'true'


@cache
def get_blocked_labels() -> list[str]:
if blocked_labels := os.getenv('BLOCKED_LABELS'):
return [label.strip() for label in blocked_labels.split(',')]

return BLOCKED_LABELS


@cache
def in_cluster() -> bool:
"""
Whether we are running in cluster (on the pod) or outside (debug mode.)
Expand Down
18 changes: 9 additions & 9 deletions src/tests/test_kubernetes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from kubernetes.client import V1ObjectMeta

from consts import CREATE_BY_ANNOTATION, LAST_SYNC_ANNOTATION, VERSION_ANNOTATION, BLACK_LISTED_ANNOTATIONS, \
BLACK_LISTED_LABELS, CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL
from consts import CREATE_BY_ANNOTATION, LAST_SYNC_ANNOTATION, VERSION_ANNOTATION, BLOCKED_ANNOTATIONS, \
CREATE_BY_AUTHOR, CLUSTER_SECRET_LABEL
from kubernetes_utils import get_ns_list, create_secret_metadata
from os_utils import get_version
from os_utils import get_version, get_blocked_labels

USER_NAMESPACE_COUNT = 10
initial_namespaces = ['default', 'kube-node-lease', 'kube-public', 'kube-system']
Expand Down Expand Up @@ -99,9 +99,9 @@ def test_create_secret_metadata(self) -> None:
(LAST_SYNC_ANNOTATION, is_iso_format)
]

attributes_black_lists = dict(
labels=BLACK_LISTED_LABELS,
annotations=BLACK_LISTED_ANNOTATIONS,
attributes_blocked_lists = dict(
labels=get_blocked_labels(),
annotations=BLOCKED_ANNOTATIONS,
)

test_cases: list[Tuple[dict[str, str], dict[str, str]]] = [
Expand Down Expand Up @@ -140,15 +140,15 @@ def test_create_secret_metadata(self) -> None:

self.assertIsInstance(obj=subject, cls=V1ObjectMeta, msg='returned value has correct type')

for attribute, black_list in attributes_black_lists.items():
for attribute, blocked_list in attributes_blocked_lists.items():
attribute_object = subject.__getattribute__(attribute)
self.assertIsNotNone(obj=attribute_object, msg=f'attribute "{attribute}" is not None')

for key in attribute_object.keys():
self.assertIsInstance(obj=key, cls=str, msg=f'the {attribute} key is a string')
for black_listed_label_prefix in black_list:
for blocked_listed_label_prefix in blocked_list:
self.assertFalse(
expr=key.startswith(black_listed_label_prefix),
expr=key.startswith(blocked_listed_label_prefix),
msg=f'{attribute} key does not match black listed prefix'
)

Expand Down
Loading