From 6876f1d82a5ed3d0aee3dfd3742a75d0b6a3a971 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Thu, 10 Sep 2020 17:18:44 +0800 Subject: [PATCH] Allow preserve structure in import Signed-off-by: Yujun Zhang --- konfig | 18 +++++++++++------- test/konfig.bats | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/konfig b/konfig index ab03d95..971f335 100755 --- a/konfig +++ b/konfig @@ -35,7 +35,7 @@ USAGE: Merge multiple kubeconfigs into one. -p prevents flattening which will make the result less portable. - konfig import [--save,-s] .. + konfig import [--preserve-structure,-p] [--save,-s] .. Import the given configs into your current kubeconfig (respects KUBECONFIG env var). -s writes the result to your ~/.kube/config @@ -86,20 +86,24 @@ merge() { import_ctx() { local tmpcfg local out="" - if [[ "${1}" = '--save' || "$1" = '-s' ]]; then + local arg="" + if [[ "$1" == '-p' || "$1" == '--preserve-structure' ]]; then + arg="$1" + shift + elif [[ "$1" = '--save' || "$1" = '-s' ]]; then out="${XDG_CACHE_HOME:-$HOME/.kube}/config" - shift 1 - elif [[ "${1}" =~ ^-(.*) ]]; then + shift + elif [[ "$1" =~ ^-(.*) ]]; then error "unrecognized flag \"$1\"" fi tmpcfg=$(mktemp konfig_XXXXXX) TMPFILES+=( "$tmpcfg" ) - $KUBECTL config view --flatten > "$tmpcfg" + $KUBECTL config view --raw > "$tmpcfg" if [[ -z "$out" ]]; then - merge "$tmpcfg" "$@" + merge "$arg" "$tmpcfg" "$@" else - merge "$tmpcfg" "$@" > "$out" + merge "$arg" "$tmpcfg" "$@" > "$out" fi } diff --git a/test/konfig.bats b/test/konfig.bats index c9cb2a8..2a444f7 100644 --- a/test/konfig.bats +++ b/test/konfig.bats @@ -105,6 +105,22 @@ load common [[ $(check_kubeconfig 'testdata/config123-flat') = 'same' ]] } +@test "import single config and preserve structure" { + use_config config1 + run ${COMMAND} import --preserve-structure --save testdata/config-2 + echo "$output" + [[ "$status" -eq 0 ]] + [[ $(check_fixture 'testdata/config12' "$output") = 'same' ]] +} + +@test "import multiple configs and preserve structure" { + use_config config1 + run ${COMMAND} import -p -s testdata/config-2 testdata/config3 + echo "$output" + [[ "$status" -eq 0 ]] + [[ $(check_fixture 'testdata/config123' "$output") = 'same' ]] +} + #### EXPORT @test "exporting with '--kubeconfig' yields original config - I" {