Skip to content

Commit

Permalink
Merge pull request #231 from AvihaiSam/fix/folder-anotation-changes
Browse files Browse the repository at this point in the history
support dest_folder state
increments #minor as respecting dest_folder state is a new feature.
  • Loading branch information
jekkel authored Jan 17, 2023
2 parents 8cb4ee9 + 9ef8168 commit 749c90e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
echo "Installing resources..."
kubectl apply -f "test/resources/resources.yaml"
pods=("sidecar" "sidecar-5xx")
resources=("sample-configmap" "sample-secret-binary" "absolute-configmap" "relative-configmap" "url-configmap-500" "url-configmap-basic-auth" "sample-configmap")
resources=("sample-configmap" "sample-secret-binary" "absolute-configmap" "relative-configmap" "change-dir-configmap" "url-configmap-500" "url-configmap-basic-auth" "sample-configmap")
for p in ${pods[*]}; do
for r in ${resources[*]}; do
wait_for_pod_log $p $r
Expand All @@ -126,6 +126,7 @@ jobs:
kubectl cp sidecar:/tmp/script_result /tmp/script_result
kubectl cp sidecar:/tmp/absolute/absolute.txt /tmp/absolute.txt
kubectl cp sidecar:/tmp/relative/relative.txt /tmp/relative.txt
kubectl cp sidecar:/tmp/orig-dir/change-dir.txt /tmp/change-dir.txt
kubectl cp sidecar:/tmp/500.txt /tmp/500.txt || true
kubectl cp sidecar:/tmp/secured.txt /tmp/secured.txt
Expand All @@ -138,6 +139,7 @@ jobs:
# absolute path in configmap points to /tmp in 'absolute-configmap'
kubectl cp sidecar-5xx:/tmp/absolute/absolute.txt /tmp/5xx/absolute.txt
kubectl cp sidecar-5xx:/tmp-5xx/relative/relative.txt /tmp/5xx/relative.txt
kubectl cp sidecar-5xx:/tmp-5xx/orig-dir/change-dir.txt /tmp/5xx/change-dir.txt
kubectl cp sidecar-5xx:/tmp-5xx/500.txt /tmp/5xx/500.txt
kubectl cp sidecar-5xx:/tmp-5xx/secured.txt /tmp/5xx/secured.txt
Expand All @@ -152,7 +154,7 @@ jobs:
echo "Updating resources..."
kubectl apply -f "test/resources/change_resources.yaml"
pods=("sidecar" "sidecar-5xx")
resources=("sample-configmap" "sample-secret-binary" "absolute-configmap" "relative-configmap" "url-configmap-500" "url-configmap-basic-auth" "sample-configmap")
resources=("sample-configmap" "sample-secret-binary" "absolute-configmap" "relative-configmap" "change-dir-configmap" "url-configmap-500" "url-configmap-basic-auth" "sample-configmap")
for p in ${pods[*]}; do
for r in ${resources[*]}; do
wait_for_pod_log $p $r
Expand All @@ -169,6 +171,7 @@ jobs:
diff test/kubelogo.png /tmp/secret-kubelogo.png &&
echo -n "This absolutely exists" | diff - /tmp/absolute.txt &&
echo -n "This relatively exists" | diff - /tmp/relative.txt &&
echo -n "This change-dir exists" | diff - /tmp/change-dir.txt &&
echo -n "allowed" | diff - /tmp/secured.txt &&
echo -n '{"detail":"Not authenticated"}' | diff - /tmp/5xx/secured.txt &&
[ ! -f /tmp/500.txt ] && echo "No 5xx file created" &&
Expand All @@ -178,11 +181,13 @@ jobs:
diff test/kubelogo.png /tmp/5xx/secret-kubelogo.png &&
echo -n "This absolutely exists" | diff - /tmp/5xx/absolute.txt &&
echo -n "This relatively exists" | diff - /tmp/5xx/relative.txt &&
echo -n "500" | diff - /tmp/5xx/500.txt &&
echo -n "This change-dir exists" | diff - /tmp/5xx/change-dir.txt &&
echo -n "500" | diff - /tmp/5xx/500.txt &&
ls /tmp/5xx/script_result &&
kubectl exec sidecar -- sh -c "ls /tmp/" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/hello.world" && kubectl exec sidecar -- sh -c "test -e /tmp/change-hello.world" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/cm-kubelogo.png" && kubectl exec sidecar -- sh -c "test -e /tmp/change-cm-kubelogo.png" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/secret-kubelogo.png" && kubectl exec sidecar -- sh -c "test -e /tmp/change-secret-kubelogo.png" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/absolute/absolute.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/absolute/change-absolute.txt" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/relative/relative.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/relative/change-relative.txt"
kubectl exec sidecar -- sh -c "! test -e /tmp/relative/relative.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/relative/change-relative.txt" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/orig-dir/change-dir.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/new-dir/change-dir.txt"
28 changes: 18 additions & 10 deletions src/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

_resources_version_map = {}
_resources_object_map = {}
_resources_dest_folder_map = {}

# Get logger
logger = get_logger()
Expand Down Expand Up @@ -123,7 +124,7 @@ def list_resources(label, label_value, target_folder, request_url, request_metho
files_changed |= _process_config_map(dest_folder, item, resource, unique_filenames, enable_5xx, True)
else:
files_changed = _process_secret(dest_folder, item, resource, unique_filenames, enable_5xx, True)

if script and files_changed:
execute(script)

Expand All @@ -135,10 +136,12 @@ def _process_secret(dest_folder, secret, resource, unique_filenames, enable_5xx,
files_changed = False

old_secret = _resources_object_map.get(secret.metadata.namespace + secret.metadata.name) or copy.deepcopy(secret)
old_dest_folder = _resources_dest_folder_map.get(secret.metadata.namespace + secret.metadata.name) or dest_folder
if is_removed:
_resources_object_map.pop(secret.metadata.namespace + secret.metadata.name, None)
else:
_resources_object_map[secret.metadata.namespace + secret.metadata.name] = copy.deepcopy(secret)
_resources_dest_folder_map[secret.metadata.namespace + secret.metadata.name] = dest_folder

if secret.data is None:
logger.warning(f"No data field in {resource}")
Expand All @@ -154,11 +157,12 @@ def _process_secret(dest_folder, secret, resource, unique_filenames, enable_5xx,
enable_5xx,
is_removed)
if old_secret.data is not None and not is_removed:
for key in set(old_secret.data.keys()) & set(secret.data or {}):
old_secret.data.pop(key)
if old_dest_folder == dest_folder:
for key in set(old_secret.data.keys()) & set(secret.data or {}):
old_secret.data.pop(key)
files_changed |= _iterate_data(
old_secret.data,
dest_folder,
old_dest_folder,
old_secret.metadata,
resource,
unique_filenames,
Expand All @@ -172,10 +176,12 @@ def _process_config_map(dest_folder, config_map, resource, unique_filenames, ena
files_changed = False

old_config_map = _resources_object_map.get(config_map.metadata.namespace + config_map.metadata.name) or copy.deepcopy(config_map)
old_dest_folder = _resources_dest_folder_map.get(config_map.metadata.namespace + config_map.metadata.name) or dest_folder
if is_removed:
_resources_object_map.pop(config_map.metadata.namespace + config_map.metadata.name, None)
else:
_resources_object_map[config_map.metadata.namespace + config_map.metadata.name] = copy.deepcopy(config_map)
_resources_dest_folder_map[config_map.metadata.namespace + config_map.metadata.name] = dest_folder

if config_map.data is None and config_map.binary_data is None:
logger.warning(f"No data/binaryData field in {resource}")
Expand All @@ -192,11 +198,12 @@ def _process_config_map(dest_folder, config_map, resource, unique_filenames, ena
enable_5xx,
is_removed)
if old_config_map.data is not None and not is_removed:
for key in set(old_config_map.data.keys()) & set(config_map.data or {}):
old_config_map.data.pop(key)
if old_dest_folder == dest_folder:
for key in set(old_config_map.data.keys()) & set(config_map.data or {}):
old_config_map.data.pop(key)
files_changed |= _iterate_data(
old_config_map.data,
dest_folder,
old_dest_folder,
old_config_map.metadata,
resource,
unique_filenames,
Expand All @@ -215,11 +222,12 @@ def _process_config_map(dest_folder, config_map, resource, unique_filenames, ena
enable_5xx,
is_removed)
if old_config_map.binary_data is not None and not is_removed:
for key in set(old_config_map.binary_data.keys()) & set(config_map.binary_data or {}):
old_config_map.binary_data.pop(key)
if old_dest_folder == dest_folder:
for key in set(old_config_map.binary_data.keys()) & set(config_map.binary_data or {}):
old_config_map.binary_data.pop(key)
files_changed |= _iterate_data(
old_config_map.binary_data,
dest_folder,
old_dest_folder,
old_config_map.metadata,
resource,
unique_filenames,
Expand Down
14 changes: 13 additions & 1 deletion test/resources/change_resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ metadata:
k8s-sidecar-target-directory: relative
data:
change-relative.txt: |-
This relatively exists
This relatively exists
---
apiVersion: v1
kind: ConfigMap
metadata:
name: change-dir-configmap
labels:
findme: "yup"
annotations:
k8s-sidecar-target-directory: new-dir
data:
change-dir.txt: |-
This change-dir exists
14 changes: 13 additions & 1 deletion test/resources/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ data:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: change-dir-configmap
labels:
findme: "yup"
annotations:
k8s-sidecar-target-directory: orig-dir
data:
change-dir.txt: |-
This change-dir exists
---
apiVersion: v1
kind: ConfigMap
metadata:
name: url-configmap-500
labels:
Expand All @@ -59,4 +71,4 @@ metadata:
labels:
findme: "yup"
data:
secured.txt.url: "http://dummy-server/secured"
secured.txt.url: "http://dummy-server/secured"

0 comments on commit 749c90e

Please sign in to comment.