Merge pull request #6476 from deads2k/deads-fix-kubeconfig-serialization

fix kubeconfig serialization
This commit is contained in:
Jeff Lowdermilk 2015-04-09 09:41:26 -07:00
commit ca6f1a1bc7
7 changed files with 51 additions and 12 deletions

View File

@ -274,7 +274,7 @@ function upload-server-tars() {
function get-password { function get-password {
# go template to extract the auth-path of the current-context user # go template to extract the auth-path of the current-context user
# Note: we save dot ('.') to $dot because the 'with' action overrides dot # 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}") local file=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${template}")
if [[ ! -z "$file" && -r "$file" ]]; then if [[ ! -z "$file" && -r "$file" ]]; then
KUBE_USER=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["User"]') KUBE_USER=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["User"]')

View File

@ -91,8 +91,8 @@ function get-kubeconfig-basicauth() {
# is missing. # is missing.
# Note: we save dot ('.') to $root because the 'with' action overrides it. # Note: we save dot ('.') to $root because the 'with' action overrides it.
# See http://golang.org/pkg/text/template/. # 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 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='{{$root := .}}{{with index $root "current-context"}}{{with index $root "contexts" .}}{{with index . "user"}}{{with index $root "users" .}}{{index . "password"}}{{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_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}") KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${password}")
# Handle empty/missing username|password # Handle empty/missing username|password

View File

@ -23,7 +23,7 @@ $ kubectl config view
$ kubectl config view --local $ kubectl config view --local
// Get the password for the e2e user // 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 ### Options

View File

@ -172,7 +172,7 @@ $ kubectl config view
$ kubectl config view \-\-local $ kubectl config view \-\-local
// Get the password for the e2e user // 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 .fi
.RE .RE

View File

@ -41,19 +41,19 @@ fi
TEMPLATE="--template=\"{{ index . \"current-context\" }}\"" TEMPLATE="--template=\"{{ index . \"current-context\" }}\""
CURRENT_CONTEXT=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) 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}" ) 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}" ) 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}" ) 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}" ) 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}" ) AUTH_PATH=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" )
# Build an auth_path file to embed as a secret # Build an auth_path file to embed as a secret

View File

@ -18,6 +18,7 @@ package config
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
@ -48,6 +49,36 @@ type configCommandTest struct {
expectedOutputs []string 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) { func TestSetCurrentContext(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.CurrentContext = "the-new-context" expectedConfig.CurrentContext = "the-new-context"
@ -631,7 +662,7 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (strin
return buf.String(), *config 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) out, actualConfig := testConfigCommand(test.args, test.startingConfig)
testSetNilMapsToEmpties(reflect.ValueOf(&test.expectedConfig)) testSetNilMapsToEmpties(reflect.ValueOf(&test.expectedConfig))
@ -647,6 +678,8 @@ func (test configCommandTest) run(t *testing.T) {
t.Errorf("expected '%s' in output, got '%s'", expectedOutput, out) t.Errorf("expected '%s' in output, got '%s'", expectedOutput, out)
} }
} }
return out
} }
func testSetNilMapsToEmpties(curr reflect.Value) { func testSetNilMapsToEmpties(curr reflect.Value) {
actualCurrValue := curr actualCurrValue := curr

View File

@ -25,6 +25,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api" 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" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
@ -45,7 +47,7 @@ $ kubectl config view
$ kubectl config view --local $ kubectl config view --local
// Get the password for the e2e user // 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 { 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 { if err != nil {
glog.FatalDepth(1, err) glog.FatalDepth(1, err)
} }
version := cmdutil.OutputVersion(cmd, latest.Version)
printer = kubectl.NewVersionedPrinter(printer, clientcmdapi.Scheme, version)
config, err := options.loadConfig() config, err := options.loadConfig()
if err != nil { if err != nil {
glog.FatalDepth(1, err) glog.FatalDepth(1, err)
} }
err = printer.PrintObj(config, out) err = printer.PrintObj(config, out)
if err != nil { if err != nil {
glog.FatalDepth(1, err) glog.FatalDepth(1, err)