Skip to content

Commit

Permalink
Merge pull request #22 from yujunz/master
Browse files Browse the repository at this point in the history
Allow preserve structure in import
  • Loading branch information
corneliusweig authored Sep 14, 2020
2 parents 6a34288 + 6876f1d commit e4d30f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 11 additions & 7 deletions konfig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ USAGE:
Merge multiple kubeconfigs into one.
-p prevents flattening which will make the result less portable.
konfig import [--save,-s] <CONFIG>..
konfig import [--preserve-structure,-p] [--save,-s] <CONFIG>..
Import the given configs into your current kubeconfig (respects KUBECONFIG env var).
-s writes the result to your ~/.kube/config
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions test/konfig.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit e4d30f0

Please sign in to comment.