2
0
mirror of https://github.com/nofusscomputing/kubernetes-manifest-tools.git synced 2025-08-14 00:37:27 +00:00

feat: Add dockerfile and deps

ref: #1 nofusscomputing/kubernetes#2
This commit is contained in:
2025-06-18 22:04:29 +09:30
parent b40d4836cc
commit bf53743fc9
3 changed files with 183 additions and 0 deletions

47
Dockerfile Normal file
View File

@ -0,0 +1,47 @@
ARG KUBECTL_SLICE_VER=v1.4.2
ARG ALPINE_VERSION=3.22
ARG PYTHON_VERSON=3.13.5
FROM harbor.earth.nww/docker/alpine:${ALPINE_VERSION} AS build
ARG KUBECTL_SLICE_VER
RUN wget --tries=5 https://github.com/patrickdappollonio/kubectl-slice/releases/download/${KUBECTL_SLICE_VER}/kubectl-slice_linux_x86_64.tar.gz; \
tar -x -f kubectl-slice_linux_x86_64.tar.gz; \
chmod +x kubectl-slice;
FROM harbor.earth.nww/docker/python:${PYTHON_VERSON}-alpine${ALPINE_VERSION}
LABEL \
org.opencontainers.image.author="No Fuss Computing" \
org.opencontainers.image.vendor="No Fuss Computing" \
org.opencontainers.image.title="kubectl-split and format" \
org.opencontainers.image.description="Split a kubernetes manifest file into single manifests and format" \
maintainer="No Fuss Computing" \
io.artifacthub.package.license="MIT"
COPY --from=build /kubectl-slice /bin/kubectl-slice
ADD includes/ /
RUN \
chmod +x /entrypoint.sh; \
mv /bin/yaml-format.py /bin/yaml-format; \
chmod +x /bin/yaml-format; \
apk update --no-cache; \
apk upgrade --no-cache; \
pip install --no-cache-dir ruamel.yaml==0.18.14;
WORKDIR /workdir
ENTRYPOINT [ "/entrypoint.sh" ]

View File

@ -0,0 +1,50 @@
#!/usr/bin/env python3
import sys
import os
from ruamel.yaml import YAML
def debug_print(msg):
if os.getenv('is_debug', '').lower() == 'true':
print(f"[DEBUG] {msg}", file=sys.stdout)
def format_yaml_file(input_path, output_path):
yaml = YAML()
yaml.explicit_start = True # Add ---
yaml.indent(mapping=2, sequence=4, offset=2)
debug_print(f"Reading file: {input_path}")
with open(input_path, 'r') as f:
data = yaml.load(f)
debug_print(f"YAML type: {type(data)}")
with open(output_path, 'w') as f:
yaml.dump(data, f)
print(f"[INFO] Wrote formatted: {output_path}", file=sys.stdout)
def main():
if len(sys.argv) != 3:
print(f"[ERROR] Usage: {sys.argv[0]} <input_dir> <output_dir>", file=sys.stderr)
sys.exit(1)
input_dir, output_dir = sys.argv[1], sys.argv[2]
if not os.path.isdir(input_dir):
print(f"[ERROR] Input '{input_dir}' is not a directory", file=sys.stderr)
sys.exit(1)
os.makedirs(output_dir, exist_ok=True)
debug_print(f"Created output dir: {output_dir}")
for filename in os.listdir(input_dir):
if filename.endswith(('.yaml', '.yml')):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
try:
format_yaml_file(input_path, output_path)
except Exception as e:
print(f"[ERROR] Failed on {input_path}: {e}", file=sys.stderr)
if __name__ == "__main__":
main()

86
includes/entrypoint.sh Normal file
View File

@ -0,0 +1,86 @@
#!/bin/sh
set -e
export TMP_OUTPUT_DIR="/tmp/yaml-files";
if [ -d "/workspace/github" ]; then
export NFC_GIT_PROVIDER=github;
export NFC_WORKDIR="/github/workspace";
if [ "${ACTIONS_STEP_DEBUG:-}" = "true" ] || [ "${RUNNER_DEBUG:-}" = "1" ]; then
export KUBECTL_SLICE_DEBUG="true"
fi;
elif [ -n "${CI_PROJECT_DIR}" ]; then
export NFC_GIT_PROVIDER=gitlab;
export NFC_WORKDIR="${CI_PROJECT_DIR}"
else
export NFC_WORKDIR="/workdir"
fi;
cd $NFC_WORKDIR;
if [ ! -n "${KUBECTL_SLICE_INPUT_FILE}" ]; then
echo "var KUBECTL_SLICE_INPUT_FILE must be set to the input file.";
exit 1;
fi;
if [ ! -n "${KUBECTL_SLICE_TEMPLATE}" ]; then
export KUBECTL_SLICE_TEMPLATE='{{ .kind }}-{{ .metadata.name | dottodash | replace ":" "-" }}.yaml';
fi;
if [ ! -n "${KUBECTL_SLICE_OUTPUT_DIR}" ]; then
export NFC_OUTPUT_DIR="${PWD}";
else
export NFC_OUTPUT_DIR="${KUBECTL_SLICE_OUTPUT_DIR}";
fi;
if printf '%s\n' "${KUBECTL_SLICE_INPUT_FILE}" | grep -Eq '^https?://|^ftp://'; then
wget ${KUBECTL_SLICE_INPUT_FILE} -O /tmp/manifest.yaml;
export KUBECTL_SLICE_INPUT_FILE="/tmp/manifest.yaml";
fi
mkdir -p $TMP_OUTPUT_DIR;
kubectl-slice --output-dir $TMP_OUTPUT_DIR
if [ "${NFC_FORMAT_YAML:-}" = "true" ]; then
echo "adding yaml headers '---' to output files";
# sed -i "1s/^/---\n/" ${KUBECTL_SLICE_OUTPUT_DIR}*.yaml;
yaml-format $TMP_OUTPUT_DIR $NFC_OUTPUT_DIR
fi;