From 5c4b6583e4c9f47e4b78734273becaa2c75f43c8 Mon Sep 17 00:00:00 2001 From: deads2k Date: Fri, 20 Feb 2015 09:03:38 -0500 Subject: [PATCH 1/3] fix kubectl config view to respect serialization formats --- docs/man/man1/kubectl-config-view.1 | 2 +- pkg/kubectl/cmd/config/view.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/man/man1/kubectl-config-view.1 b/docs/man/man1/kubectl-config-view.1 index 934da061c1b..86d4d063e76 100644 --- a/docs/man/man1/kubectl-config-view.1 +++ b/docs/man/man1/kubectl-config-view.1 @@ -172,7 +172,7 @@ $ kubectl config view $ kubectl config view \-\-local // Get the password for the e2e user -$ kubectl config view \-o template \-\-template='\{\{ index . "users" "e2e" "password" \}\}' +$ kubectl config view \-o template \-\-template='\{\{range .users\}\}\{\{ if eq .name "e2e" \}\}\{\{ index .user.password \}\}\{\{end\}\}\{\{end\}\}' .fi .RE diff --git a/pkg/kubectl/cmd/config/view.go b/pkg/kubectl/cmd/config/view.go index 18fc4afe79d..9b198c8e031 100644 --- a/pkg/kubectl/cmd/config/view.go +++ b/pkg/kubectl/cmd/config/view.go @@ -25,6 +25,8 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api/latest" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" ) @@ -45,7 +47,7 @@ $ kubectl config view $ kubectl config view --local // Get the password for the e2e user -$ kubectl config view -o template --template='{{ index . "users" "e2e" "password" }}'` +$ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2e" }}{{ index .user.password }}{{end}}{{end}}'` ) func NewCmdConfigView(out io.Writer, pathOptions *pathOptions) *cobra.Command { @@ -63,10 +65,14 @@ func NewCmdConfigView(out io.Writer, pathOptions *pathOptions) *cobra.Command { if err != nil { glog.FatalDepth(1, err) } + version := cmdutil.OutputVersion(cmd, latest.Version) + printer = kubectl.NewVersionedPrinter(printer, clientcmdapi.Scheme, version) + config, err := options.loadConfig() if err != nil { glog.FatalDepth(1, err) } + err = printer.PrintObj(config, out) if err != nil { glog.FatalDepth(1, err) From 609208b8b5e5bd8c20fce60eb305a84d0e6e0b04 Mon Sep 17 00:00:00 2001 From: deads2k Date: Mon, 6 Apr 2015 14:56:13 -0400 Subject: [PATCH 2/3] update scripts with correct templates --- cluster/aws/util.sh | 2 +- cluster/common.sh | 4 ++-- docs/kubectl-config-view.md | 2 +- examples/openshift-origin/resource-generator.sh | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cluster/aws/util.sh b/cluster/aws/util.sh index 5952f7a7ddc..38ce605d21b 100644 --- a/cluster/aws/util.sh +++ b/cluster/aws/util.sh @@ -274,7 +274,7 @@ function upload-server-tars() { function get-password { # go template to extract the auth-path of the current-context user # Note: we save dot ('.') to $dot because the 'with' action overrides dot - local template='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{$user := index $dot "contexts" $ctx "user"}}{{index $dot "users" $user "auth-path"}}{{end}}' + local template='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "auth-path" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}' local file=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${template}") if [[ ! -z "$file" && -r "$file" ]]; then KUBE_USER=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["User"]') diff --git a/cluster/common.sh b/cluster/common.sh index f00558d1018..9943619bda3 100644 --- a/cluster/common.sh +++ b/cluster/common.sh @@ -91,8 +91,8 @@ function get-kubeconfig-basicauth() { # is missing. # Note: we save dot ('.') to $root because the 'with' action overrides it. # See http://golang.org/pkg/text/template/. - local username='{{$root := .}}{{with index $root "current-context"}}{{with index $root "contexts" .}}{{with index . "user"}}{{with index $root "users" .}}{{index . "username"}}{{end}}{{end}}{{end}}{{end}}' - local password='{{$root := .}}{{with index $root "current-context"}}{{with index $root "contexts" .}}{{with index . "user"}}{{with index $root "users" .}}{{index . "password"}}{{end}}{{end}}{{end}}{{end}}' + local username='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "username" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}' + local password='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "password" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}' KUBE_USER=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${username}") KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${password}") # Handle empty/missing username|password diff --git a/docs/kubectl-config-view.md b/docs/kubectl-config-view.md index afe81c40056..784220bd815 100644 --- a/docs/kubectl-config-view.md +++ b/docs/kubectl-config-view.md @@ -23,7 +23,7 @@ $ kubectl config view $ kubectl config view --local // Get the password for the e2e user -$ kubectl config view -o template --template='{{ index . "users" "e2e" "password" }}' +$ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2e" }}{{ index .user.password }}{{end}}{{end}}' ``` ### Options diff --git a/examples/openshift-origin/resource-generator.sh b/examples/openshift-origin/resource-generator.sh index 87b80323f38..56b953bbe71 100755 --- a/examples/openshift-origin/resource-generator.sh +++ b/examples/openshift-origin/resource-generator.sh @@ -41,19 +41,19 @@ fi TEMPLATE="--template=\"{{ index . \"current-context\" }}\"" CURRENT_CONTEXT=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template=\"{{ index . \"contexts\" ${CURRENT_CONTEXT} \"cluster\" }}\"" +TEMPLATE="--template=\"{{range .contexts}}{{ if eq .name ${CURRENT_CONTEXT} }}{{ .context.cluster }}{{end}}{{end}}\"" CURRENT_CLUSTER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template=\"{{ index . \"contexts\" ${CURRENT_CONTEXT} \"user\" }}\"" +TEMPLATE="--template=\"{{range .contexts}}{{ if eq .name ${CURRENT_CONTEXT} }}{{ .context.user }}{{end}}{{end}}\"" CURRENT_USER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"clusters\" ${CURRENT_CLUSTER} \"certificate-authority\" }}" +TEMPLATE="--template=\"{{range .clusters}}{{ if eq .name ${CURRENT_CLUSTER} }}{{ index . \"cluster\" \"certificate-authority\" }}{{end}}{{end}}\"" CERTIFICATE_AUTHORITY=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"clusters\" ${CURRENT_CLUSTER} \"server\" }}" +TEMPLATE="--template=\"{{range .clusters}}{{ if eq .name ${CURRENT_CLUSTER} }}{{ .cluster.server }}{{end}}{{end}}\"" KUBE_MASTER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"users\" ${CURRENT_USER} \"auth-path\" }}" +TEMPLATE="--template=\"{{range .users}}{{ if eq .name ${CURRENT_USER} }}{{ index . \"user\" \"auth-path\" }}{{end}}{{end}}\"" AUTH_PATH=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) # Build an auth_path file to embed as a secret From 06c9dbd32fcf604d5e8f09d48f3686086061ea88 Mon Sep 17 00:00:00 2001 From: deads2k Date: Tue, 24 Feb 2015 08:54:03 -0500 Subject: [PATCH 3/3] add example for default view serialization --- pkg/kubectl/cmd/config/config_test.go | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/cmd/config/config_test.go b/pkg/kubectl/cmd/config/config_test.go index a528ae8aa27..bcfb9b17062 100644 --- a/pkg/kubectl/cmd/config/config_test.go +++ b/pkg/kubectl/cmd/config/config_test.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "fmt" "io/ioutil" "os" "reflect" @@ -47,6 +48,36 @@ type configCommandTest struct { expectedOutputs []string } +func ExampleView() { + expectedConfig := newRedFederalCowHammerConfig() + test := configCommandTest{ + args: []string{"view"}, + startingConfig: newRedFederalCowHammerConfig(), + expectedConfig: expectedConfig, + } + + output := test.run(nil) + fmt.Printf("%v", output) + // Output: + // apiVersion: v1 + // clusters: + // - cluster: + // server: http://cow.org:8080 + // name: cow-cluster + // contexts: + // - context: + // cluster: cow-cluster + // user: red-user + // name: federal-context + // current-context: "" + // kind: Config + // preferences: {} + // users: + // - name: red-user + // user: + // token: red-token +} + func TestSetCurrentContext(t *testing.T) { expectedConfig := newRedFederalCowHammerConfig() expectedConfig.CurrentContext = "the-new-context" @@ -629,7 +660,7 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (strin return buf.String(), *config } -func (test configCommandTest) run(t *testing.T) { +func (test configCommandTest) run(t *testing.T) string { out, actualConfig := testConfigCommand(test.args, test.startingConfig) testSetNilMapsToEmpties(reflect.ValueOf(&test.expectedConfig)) @@ -645,6 +676,8 @@ func (test configCommandTest) run(t *testing.T) { t.Errorf("expected '%s' in output, got '%s'", expectedOutput, out) } } + + return out } func testSetNilMapsToEmpties(curr reflect.Value) { actualCurrValue := curr