diff --git a/.circleci/test-in-cluster.sh b/.circleci/test-in-cluster.sh index 0ef85f00..f49e7119 100755 --- a/.circleci/test-in-cluster.sh +++ b/.circleci/test-in-cluster.sh @@ -88,11 +88,15 @@ KIND_CONFIG="${CWD}/kind-config.yaml" kubectl cp sidecar:/tmp/cm-kubelogo.png /tmp/cm-kubelogo.png kubectl cp sidecar:/tmp/secret-kubelogo.png /tmp/secret-kubelogo.png 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 log "Verifying file content..." echo -n "Hello World!" | diff - /tmp/hello.world \ && diff ${CWD}/kubelogo.png /tmp/cm-kubelogo.png \ && diff ${CWD}/kubelogo.png /tmp/secret-kubelogo.png \ + && echo -n "This absolutely exists" | diff - /tmp/absolute.txt \ + && echo -n "This relatively exists" | diff - /tmp/relative.txt \ && ls /tmp/script_result } diff --git a/.circleci/test/resources.yaml b/.circleci/test/resources.yaml index c7099ab3..53695fb4 100644 --- a/.circleci/test/resources.yaml +++ b/.circleci/test/resources.yaml @@ -29,3 +29,27 @@ data: script.sh: |- #!/bin/sh touch /tmp/script_result +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: absolute-configmap + labels: + findme: "yup" + annotations: + k8s-sidecar-target-directory: /tmp/absolute/ +data: + absolute.txt: |- + This absolutely exists +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: relative-configmap + labels: + findme: "yup" + annotations: + k8s-sidecar-target-directory: relative +data: + relative.txt: |- + This relatively exists diff --git a/README.md b/README.md index f069990c..ef9dd242 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ If the filename ends with `.url` suffix, the content will be processed as an URL - type: string - `FOLDER_ANNOTATION` - - description: The annotation the sidecar will look for in configmaps to override the destination folder for files, defaults to "k8s-sidecar-target-directory" + - description: The annotation the sidecar will look for in configmaps to override the destination folder for files, defaults to "k8s-sidecar-target-directory". The annotation _value_ can be either an absolute or a relative path. Relative paths will be relative to `FOLDER`. - required: false - type: string @@ -170,4 +170,4 @@ If the filename ends with `.url` suffix, the content will be processed as an URL - `KUBECONFIG` - description: if this is given and points to a file or `~/.kube/config` is mounted k8s config will be loaded from this file, otherwise "incluster" k8s configuration is tried. - required: false - - type: string \ No newline at end of file + - type: string diff --git a/sidecar/resources.py b/sidecar/resources.py index a0c68dc5..e40d9c26 100755 --- a/sidecar/resources.py +++ b/sidecar/resources.py @@ -54,7 +54,11 @@ def _get_file_data_and_name(full_filename, content, content_type=CONTENT_TYPE_TE def _get_destination_folder(metadata, default_folder, folder_annotation): if metadata.annotations and folder_annotation in metadata.annotations.keys(): - dest_folder = metadata.annotations[folder_annotation] + folder_annotation = metadata.annotations[folder_annotation] + if os.path.isabs(folder_annotation): + dest_folder = folder_annotation + else: + dest_folder = os.path.join(default_folder, folder_annotation) print(f"{timestamp()} Found a folder override annotation, " f"placing the {metadata.name} in: {dest_folder}") return dest_folder