switch kubeconfig types to internal map[string]*struct

This commit is contained in:
deads2k 2015-06-29 14:39:48 -04:00
parent 33278e7910
commit 55f574c267
17 changed files with 181 additions and 190 deletions

View File

@ -43,10 +43,10 @@ func MinifyConfig(config *Config) error {
return fmt.Errorf("cannot locate context %v", config.CurrentContext) return fmt.Errorf("cannot locate context %v", config.CurrentContext)
} }
newContexts := map[string]Context{} newContexts := map[string]*Context{}
newContexts[config.CurrentContext] = currContext newContexts[config.CurrentContext] = currContext
newClusters := map[string]Cluster{} newClusters := map[string]*Cluster{}
if len(currContext.Cluster) > 0 { if len(currContext.Cluster) > 0 {
if _, exists := config.Clusters[currContext.Cluster]; !exists { if _, exists := config.Clusters[currContext.Cluster]; !exists {
return fmt.Errorf("cannot locate cluster %v", currContext.Cluster) return fmt.Errorf("cannot locate cluster %v", currContext.Cluster)
@ -55,7 +55,7 @@ func MinifyConfig(config *Config) error {
newClusters[currContext.Cluster] = config.Clusters[currContext.Cluster] newClusters[currContext.Cluster] = config.Clusters[currContext.Cluster]
} }
newAuthInfos := map[string]AuthInfo{} newAuthInfos := map[string]*AuthInfo{}
if len(currContext.AuthInfo) > 0 { if len(currContext.AuthInfo) > 0 {
if _, exists := config.AuthInfos[currContext.AuthInfo]; !exists { if _, exists := config.AuthInfos[currContext.AuthInfo]; !exists {
return fmt.Errorf("cannot locate user %v", currContext.AuthInfo) return fmt.Errorf("cannot locate user %v", currContext.AuthInfo)

View File

@ -38,13 +38,13 @@ func newMergedConfig(certFile, certContent, keyFile, keyContent, caFile, caConte
} }
return Config{ return Config{
AuthInfos: map[string]AuthInfo{ AuthInfos: map[string]*AuthInfo{
"red-user": {Token: "red-token", ClientCertificateData: []byte(certContent), ClientKeyData: []byte(keyContent)}, "red-user": {Token: "red-token", ClientCertificateData: []byte(certContent), ClientKeyData: []byte(keyContent)},
"blue-user": {Token: "blue-token", ClientCertificate: certFile, ClientKey: keyFile}}, "blue-user": {Token: "blue-token", ClientCertificate: certFile, ClientKey: keyFile}},
Clusters: map[string]Cluster{ Clusters: map[string]*Cluster{
"cow-cluster": {Server: "http://cow.org:8080", CertificateAuthorityData: []byte(caContent)}, "cow-cluster": {Server: "http://cow.org:8080", CertificateAuthorityData: []byte(caContent)},
"chicken-cluster": {Server: "http://chicken.org:8080", CertificateAuthority: caFile}}, "chicken-cluster": {Server: "http://chicken.org:8080", CertificateAuthority: caFile}},
Contexts: map[string]Context{ Contexts: map[string]*Context{
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"}, "federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"},
"shaker-context": {AuthInfo: "blue-user", Cluster: "chicken-cluster"}}, "shaker-context": {AuthInfo: "blue-user", Cluster: "chicken-cluster"}},
CurrentContext: "federal-context", CurrentContext: "federal-context",

View File

@ -33,21 +33,21 @@ type Config struct {
// Preferences holds general information to be use for cli interactions // Preferences holds general information to be use for cli interactions
Preferences Preferences `json:"preferences"` Preferences Preferences `json:"preferences"`
// Clusters is a map of referencable names to cluster configs // Clusters is a map of referencable names to cluster configs
Clusters map[string]Cluster `json:"clusters"` Clusters map[string]*Cluster `json:"clusters"`
// AuthInfos is a map of referencable names to user configs // AuthInfos is a map of referencable names to user configs
AuthInfos map[string]AuthInfo `json:"users"` AuthInfos map[string]*AuthInfo `json:"users"`
// Contexts is a map of referencable names to context configs // Contexts is a map of referencable names to context configs
Contexts map[string]Context `json:"contexts"` Contexts map[string]*Context `json:"contexts"`
// CurrentContext is the name of the context that you would like to use by default // CurrentContext is the name of the context that you would like to use by default
CurrentContext string `json:"current-context"` CurrentContext string `json:"current-context"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
Extensions map[string]runtime.EmbeddedObject `json:"extensions,omitempty"` Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"`
} }
type Preferences struct { type Preferences struct {
Colors bool `json:"colors,omitempty"` Colors bool `json:"colors,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
Extensions map[string]runtime.EmbeddedObject `json:"extensions,omitempty"` Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"`
} }
// Cluster contains information about how to communicate with a kubernetes cluster // Cluster contains information about how to communicate with a kubernetes cluster
@ -65,7 +65,7 @@ type Cluster struct {
// CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority // CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority
CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"` CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
Extensions map[string]runtime.EmbeddedObject `json:"extensions,omitempty"` Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"`
} }
// AuthInfo contains information that describes identity information. This is use to tell the kubernetes cluster who you are. // AuthInfo contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
@ -87,7 +87,7 @@ type AuthInfo struct {
// Password is the password for basic authentication to the kubernetes cluster. // Password is the password for basic authentication to the kubernetes cluster.
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
Extensions map[string]runtime.EmbeddedObject `json:"extensions,omitempty"` Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"`
} }
// Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with) // Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with)
@ -101,36 +101,36 @@ type Context struct {
// Namespace is the default namespace to use on unspecified requests // Namespace is the default namespace to use on unspecified requests
Namespace string `json:"namespace,omitempty"` Namespace string `json:"namespace,omitempty"`
// Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
Extensions map[string]runtime.EmbeddedObject `json:"extensions,omitempty"` Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"`
} }
// NewConfig is a convenience function that returns a new Config object with non-nil maps // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
Preferences: *NewPreferences(), Preferences: *NewPreferences(),
Clusters: make(map[string]Cluster), Clusters: make(map[string]*Cluster),
AuthInfos: make(map[string]AuthInfo), AuthInfos: make(map[string]*AuthInfo),
Contexts: make(map[string]Context), Contexts: make(map[string]*Context),
Extensions: make(map[string]runtime.EmbeddedObject), Extensions: make(map[string]*runtime.EmbeddedObject),
} }
} }
// NewConfig is a convenience function that returns a new Config object with non-nil maps // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewContext() *Context { func NewContext() *Context {
return &Context{Extensions: make(map[string]runtime.EmbeddedObject)} return &Context{Extensions: make(map[string]*runtime.EmbeddedObject)}
} }
// NewConfig is a convenience function that returns a new Config object with non-nil maps // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewCluster() *Cluster { func NewCluster() *Cluster {
return &Cluster{Extensions: make(map[string]runtime.EmbeddedObject)} return &Cluster{Extensions: make(map[string]*runtime.EmbeddedObject)}
} }
// NewConfig is a convenience function that returns a new Config object with non-nil maps // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewAuthInfo() *AuthInfo { func NewAuthInfo() *AuthInfo {
return &AuthInfo{Extensions: make(map[string]runtime.EmbeddedObject)} return &AuthInfo{Extensions: make(map[string]*runtime.EmbeddedObject)}
} }
// NewConfig is a convenience function that returns a new Config object with non-nil maps // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewPreferences() *Preferences { func NewPreferences() *Preferences {
return &Preferences{Extensions: make(map[string]runtime.EmbeddedObject)} return &Preferences{Extensions: make(map[string]*runtime.EmbeddedObject)}
} }

View File

@ -42,35 +42,35 @@ func ExampleEmptyConfig() {
func ExampleOfOptionsConfig() { func ExampleOfOptionsConfig() {
defaultConfig := NewConfig() defaultConfig := NewConfig()
defaultConfig.Preferences.Colors = true defaultConfig.Preferences.Colors = true
defaultConfig.Clusters["alfa"] = Cluster{ defaultConfig.Clusters["alfa"] = &Cluster{
Server: "https://alfa.org:8080", Server: "https://alfa.org:8080",
APIVersion: "v1beta2", APIVersion: "v1beta2",
InsecureSkipTLSVerify: true, InsecureSkipTLSVerify: true,
CertificateAuthority: "path/to/my/cert-ca-filename", CertificateAuthority: "path/to/my/cert-ca-filename",
} }
defaultConfig.Clusters["bravo"] = Cluster{ defaultConfig.Clusters["bravo"] = &Cluster{
Server: "https://bravo.org:8080", Server: "https://bravo.org:8080",
APIVersion: "v1beta1", APIVersion: "v1beta1",
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
} }
defaultConfig.AuthInfos["white-mage-via-cert"] = AuthInfo{ defaultConfig.AuthInfos["white-mage-via-cert"] = &AuthInfo{
ClientCertificate: "path/to/my/client-cert-filename", ClientCertificate: "path/to/my/client-cert-filename",
ClientKey: "path/to/my/client-key-filename", ClientKey: "path/to/my/client-key-filename",
} }
defaultConfig.AuthInfos["red-mage-via-token"] = AuthInfo{ defaultConfig.AuthInfos["red-mage-via-token"] = &AuthInfo{
Token: "my-secret-token", Token: "my-secret-token",
} }
defaultConfig.Contexts["bravo-as-black-mage"] = Context{ defaultConfig.Contexts["bravo-as-black-mage"] = &Context{
Cluster: "bravo", Cluster: "bravo",
AuthInfo: "black-mage-via-file", AuthInfo: "black-mage-via-file",
Namespace: "yankee", Namespace: "yankee",
} }
defaultConfig.Contexts["alfa-as-black-mage"] = Context{ defaultConfig.Contexts["alfa-as-black-mage"] = &Context{
Cluster: "alfa", Cluster: "alfa",
AuthInfo: "black-mage-via-file", AuthInfo: "black-mage-via-file",
Namespace: "zulu", Namespace: "zulu",
} }
defaultConfig.Contexts["alfa-as-white-mage"] = Context{ defaultConfig.Contexts["alfa-as-white-mage"] = &Context{
Cluster: "alfa", Cluster: "alfa",
AuthInfo: "white-mage-via-cert", AuthInfo: "white-mage-via-cert",
} }

View File

@ -57,19 +57,19 @@ func init() {
return err return err
} }
out.Clusters = make(map[string]api.Cluster) out.Clusters = make(map[string]*api.Cluster)
if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil { if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
return err return err
} }
out.AuthInfos = make(map[string]api.AuthInfo) out.AuthInfos = make(map[string]*api.AuthInfo)
if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil { if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
return err return err
} }
out.Contexts = make(map[string]api.Context) out.Contexts = make(map[string]*api.Context)
if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
return err return err
} }
out.Extensions = make(map[string]runtime.EmbeddedObject) out.Extensions = make(map[string]*runtime.EmbeddedObject)
if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
return err return err
} }
@ -99,18 +99,18 @@ func init() {
} }
return nil return nil
}, },
func(in *[]NamedCluster, out *map[string]api.Cluster, s conversion.Scope) error { func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error {
for _, curr := range *in { for _, curr := range *in {
newCluster := api.NewCluster() newCluster := api.NewCluster()
if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil { if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil {
return err return err
} }
(*out)[curr.Name] = *newCluster (*out)[curr.Name] = newCluster
} }
return nil return nil
}, },
func(in *map[string]api.Cluster, out *[]NamedCluster, s conversion.Scope) error { func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error {
allKeys := make([]string, 0, len(*in)) allKeys := make([]string, 0, len(*in))
for key := range *in { for key := range *in {
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
@ -120,7 +120,7 @@ func init() {
for _, key := range allKeys { for _, key := range allKeys {
newCluster := (*in)[key] newCluster := (*in)[key]
oldCluster := &Cluster{} oldCluster := &Cluster{}
if err := s.Convert(&newCluster, oldCluster, 0); err != nil { if err := s.Convert(newCluster, oldCluster, 0); err != nil {
return err return err
} }
@ -130,18 +130,18 @@ func init() {
return nil return nil
}, },
func(in *[]NamedAuthInfo, out *map[string]api.AuthInfo, s conversion.Scope) error { func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error {
for _, curr := range *in { for _, curr := range *in {
newAuthInfo := api.NewAuthInfo() newAuthInfo := api.NewAuthInfo()
if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil { if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil {
return err return err
} }
(*out)[curr.Name] = *newAuthInfo (*out)[curr.Name] = newAuthInfo
} }
return nil return nil
}, },
func(in *map[string]api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error { func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error {
allKeys := make([]string, 0, len(*in)) allKeys := make([]string, 0, len(*in))
for key := range *in { for key := range *in {
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
@ -151,7 +151,7 @@ func init() {
for _, key := range allKeys { for _, key := range allKeys {
newAuthInfo := (*in)[key] newAuthInfo := (*in)[key]
oldAuthInfo := &AuthInfo{} oldAuthInfo := &AuthInfo{}
if err := s.Convert(&newAuthInfo, oldAuthInfo, 0); err != nil { if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil {
return err return err
} }
@ -161,18 +161,18 @@ func init() {
return nil return nil
}, },
func(in *[]NamedContext, out *map[string]api.Context, s conversion.Scope) error { func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error {
for _, curr := range *in { for _, curr := range *in {
newContext := api.NewContext() newContext := api.NewContext()
if err := s.Convert(&curr.Context, newContext, 0); err != nil { if err := s.Convert(&curr.Context, newContext, 0); err != nil {
return err return err
} }
(*out)[curr.Name] = *newContext (*out)[curr.Name] = newContext
} }
return nil return nil
}, },
func(in *map[string]api.Context, out *[]NamedContext, s conversion.Scope) error { func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error {
allKeys := make([]string, 0, len(*in)) allKeys := make([]string, 0, len(*in))
for key := range *in { for key := range *in {
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
@ -182,7 +182,7 @@ func init() {
for _, key := range allKeys { for _, key := range allKeys {
newContext := (*in)[key] newContext := (*in)[key]
oldContext := &Context{} oldContext := &Context{}
if err := s.Convert(&newContext, oldContext, 0); err != nil { if err := s.Convert(newContext, oldContext, 0); err != nil {
return err return err
} }
@ -192,18 +192,18 @@ func init() {
return nil return nil
}, },
func(in *[]NamedExtension, out *map[string]runtime.EmbeddedObject, s conversion.Scope) error { func(in *[]NamedExtension, out *map[string]*runtime.EmbeddedObject, s conversion.Scope) error {
for _, curr := range *in { for _, curr := range *in {
newExtension := &runtime.EmbeddedObject{} newExtension := &runtime.EmbeddedObject{}
if err := s.Convert(&curr.Extension, newExtension, 0); err != nil { if err := s.Convert(&curr.Extension, newExtension, 0); err != nil {
return err return err
} }
(*out)[curr.Name] = *newExtension (*out)[curr.Name] = newExtension
} }
return nil return nil
}, },
func(in *map[string]runtime.EmbeddedObject, out *[]NamedExtension, s conversion.Scope) error { func(in *map[string]*runtime.EmbeddedObject, out *[]NamedExtension, s conversion.Scope) error {
allKeys := make([]string, 0, len(*in)) allKeys := make([]string, 0, len(*in))
for key := range *in { for key := range *in {
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
@ -213,7 +213,7 @@ func init() {
for _, key := range allKeys { for _, key := range allKeys {
newExtension := (*in)[key] newExtension := (*in)[key]
oldExtension := &runtime.RawExtension{} oldExtension := &runtime.RawExtension{}
if err := s.Convert(&newExtension, oldExtension, 0); err != nil { if err := s.Convert(newExtension, oldExtension, 0); err != nil {
return err return err
} }

View File

@ -32,14 +32,14 @@ func createValidTestConfig() *clientcmdapi.Config {
) )
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: server, Server: server,
APIVersion: latest.Version, APIVersion: latest.Version,
} }
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
Token: token, Token: token,
} }
config.Contexts["clean"] = clientcmdapi.Context{ config.Contexts["clean"] = &clientcmdapi.Context{
Cluster: "clean", Cluster: "clean",
AuthInfo: "clean", AuthInfo: "clean",
} }
@ -87,16 +87,16 @@ func TestCertificateData(t *testing.T) {
keyData := []byte("key-data") keyData := []byte("key-data")
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: "https://localhost:8443", Server: "https://localhost:8443",
APIVersion: latest.Version, APIVersion: latest.Version,
CertificateAuthorityData: caData, CertificateAuthorityData: caData,
} }
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
ClientCertificateData: certData, ClientCertificateData: certData,
ClientKeyData: keyData, ClientKeyData: keyData,
} }
config.Contexts["clean"] = clientcmdapi.Context{ config.Contexts["clean"] = &clientcmdapi.Context{
Cluster: "clean", Cluster: "clean",
AuthInfo: "clean", AuthInfo: "clean",
} }
@ -120,15 +120,15 @@ func TestBasicAuthData(t *testing.T) {
password := "mypass" password := "mypass"
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: "https://localhost:8443", Server: "https://localhost:8443",
APIVersion: latest.Version, APIVersion: latest.Version,
} }
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
Username: username, Username: username,
Password: password, Password: password,
} }
config.Contexts["clean"] = clientcmdapi.Context{ config.Contexts["clean"] = &clientcmdapi.Context{
Cluster: "clean", Cluster: "clean",
AuthInfo: "clean", AuthInfo: "clean",
} }

View File

@ -226,14 +226,14 @@ func ResolveLocalPaths(filename string, config *clientcmdapi.Config) error {
return fmt.Errorf("Could not determine the absolute path of config file %s: %v", filename, err) return fmt.Errorf("Could not determine the absolute path of config file %s: %v", filename, err)
} }
resolvedClusters := make(map[string]clientcmdapi.Cluster) resolvedClusters := make(map[string]*clientcmdapi.Cluster)
for key, cluster := range config.Clusters { for key, cluster := range config.Clusters {
cluster.CertificateAuthority = resolveLocalPath(configDir, cluster.CertificateAuthority) cluster.CertificateAuthority = resolveLocalPath(configDir, cluster.CertificateAuthority)
resolvedClusters[key] = cluster resolvedClusters[key] = cluster
} }
config.Clusters = resolvedClusters config.Clusters = resolvedClusters
resolvedAuthInfos := make(map[string]clientcmdapi.AuthInfo) resolvedAuthInfos := make(map[string]*clientcmdapi.AuthInfo)
for key, authInfo := range config.AuthInfos { for key, authInfo := range config.AuthInfos {
authInfo.ClientCertificate = resolveLocalPath(configDir, authInfo.ClientCertificate) authInfo.ClientCertificate = resolveLocalPath(configDir, authInfo.ClientCertificate)
authInfo.ClientKey = resolveLocalPath(configDir, authInfo.ClientKey) authInfo.ClientKey = resolveLocalPath(configDir, authInfo.ClientKey)

View File

@ -34,43 +34,43 @@ import (
var ( var (
testConfigAlfa = clientcmdapi.Config{ testConfigAlfa = clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"red-user": {Token: "red-token"}}, "red-user": {Token: "red-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"cow-cluster": {Server: "http://cow.org:8080"}}, "cow-cluster": {Server: "http://cow.org:8080"}},
Contexts: map[string]clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}}, "federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}},
} }
testConfigBravo = clientcmdapi.Config{ testConfigBravo = clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"}}, "black-user": {Token: "black-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"}}, "pig-cluster": {Server: "http://pig.org:8080"}},
Contexts: map[string]clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"queen-anne-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}}, "queen-anne-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
} }
testConfigCharlie = clientcmdapi.Config{ testConfigCharlie = clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"green-user": {Token: "green-token"}}, "green-user": {Token: "green-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"horse-cluster": {Server: "http://horse.org:8080"}}, "horse-cluster": {Server: "http://horse.org:8080"}},
Contexts: map[string]clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"shaker-context": {AuthInfo: "green-user", Cluster: "horse-cluster", Namespace: "chisel-ns"}}, "shaker-context": {AuthInfo: "green-user", Cluster: "horse-cluster", Namespace: "chisel-ns"}},
} }
testConfigDelta = clientcmdapi.Config{ testConfigDelta = clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"blue-user": {Token: "blue-token"}}, "blue-user": {Token: "blue-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"chicken-cluster": {Server: "http://chicken.org:8080"}}, "chicken-cluster": {Server: "http://chicken.org:8080"}},
Contexts: map[string]clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"gothic-context": {AuthInfo: "blue-user", Cluster: "chicken-cluster", Namespace: "plane-ns"}}, "gothic-context": {AuthInfo: "blue-user", Cluster: "chicken-cluster", Namespace: "plane-ns"}},
} }
testConfigConflictAlfa = clientcmdapi.Config{ testConfigConflictAlfa = clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"red-user": {Token: "a-different-red-token"}, "red-user": {Token: "a-different-red-token"},
"yellow-user": {Token: "yellow-token"}}, "yellow-user": {Token: "yellow-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"cow-cluster": {Server: "http://a-different-cow.org:8080", InsecureSkipTLSVerify: true}, "cow-cluster": {Server: "http://a-different-cow.org:8080", InsecureSkipTLSVerify: true},
"donkey-cluster": {Server: "http://donkey.org:8080", InsecureSkipTLSVerify: true}}, "donkey-cluster": {Server: "http://donkey.org:8080", InsecureSkipTLSVerify: true}},
CurrentContext: "federal-context", CurrentContext: "federal-context",
@ -176,21 +176,21 @@ func TestConflictingCurrentContext(t *testing.T) {
func TestResolveRelativePaths(t *testing.T) { func TestResolveRelativePaths(t *testing.T) {
pathResolutionConfig1 := clientcmdapi.Config{ pathResolutionConfig1 := clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"relative-user-1": {ClientCertificate: "relative/client/cert", ClientKey: "../relative/client/key"}, "relative-user-1": {ClientCertificate: "relative/client/cert", ClientKey: "../relative/client/key"},
"absolute-user-1": {ClientCertificate: "/absolute/client/cert", ClientKey: "/absolute/client/key"}, "absolute-user-1": {ClientCertificate: "/absolute/client/cert", ClientKey: "/absolute/client/key"},
}, },
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"relative-server-1": {CertificateAuthority: "../relative/ca"}, "relative-server-1": {CertificateAuthority: "../relative/ca"},
"absolute-server-1": {CertificateAuthority: "/absolute/ca"}, "absolute-server-1": {CertificateAuthority: "/absolute/ca"},
}, },
} }
pathResolutionConfig2 := clientcmdapi.Config{ pathResolutionConfig2 := clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"relative-user-2": {ClientCertificate: "relative/client/cert2", ClientKey: "../relative/client/key2"}, "relative-user-2": {ClientCertificate: "relative/client/cert2", ClientKey: "../relative/client/key2"},
"absolute-user-2": {ClientCertificate: "/absolute/client/cert2", ClientKey: "/absolute/client/key2"}, "absolute-user-2": {ClientCertificate: "/absolute/client/cert2", ClientKey: "/absolute/client/key2"},
}, },
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"relative-server-2": {CertificateAuthority: "../relative/ca2"}, "relative-server-2": {CertificateAuthority: "../relative/ca2"},
"absolute-server-2": {CertificateAuthority: "/absolute/ca2"}, "absolute-server-2": {CertificateAuthority: "/absolute/ca2"},
}, },

View File

@ -95,15 +95,15 @@ func Validate(config clientcmdapi.Config) error {
} }
for contextName, context := range config.Contexts { for contextName, context := range config.Contexts {
validationErrors = append(validationErrors, validateContext(contextName, context, config)...) validationErrors = append(validationErrors, validateContext(contextName, *context, config)...)
} }
for authInfoName, authInfo := range config.AuthInfos { for authInfoName, authInfo := range config.AuthInfos {
validationErrors = append(validationErrors, validateAuthInfo(authInfoName, authInfo)...) validationErrors = append(validationErrors, validateAuthInfo(authInfoName, *authInfo)...)
} }
for clusterName, clusterInfo := range config.Clusters { for clusterName, clusterInfo := range config.Clusters {
validationErrors = append(validationErrors, validateClusterInfo(clusterName, clusterInfo)...) validationErrors = append(validationErrors, validateClusterInfo(clusterName, *clusterInfo)...)
} }
return newErrConfigurationInvalid(validationErrors) return newErrConfigurationInvalid(validationErrors)
@ -131,9 +131,9 @@ func ConfirmUsable(config clientcmdapi.Config, passedContextName string) error {
} }
if exists { if exists {
validationErrors = append(validationErrors, validateContext(contextName, context, config)...) validationErrors = append(validationErrors, validateContext(contextName, *context, config)...)
validationErrors = append(validationErrors, validateAuthInfo(context.AuthInfo, config.AuthInfos[context.AuthInfo])...) validationErrors = append(validationErrors, validateAuthInfo(context.AuthInfo, *config.AuthInfos[context.AuthInfo])...)
validationErrors = append(validationErrors, validateClusterInfo(context.Cluster, config.Clusters[context.Cluster])...) validationErrors = append(validationErrors, validateClusterInfo(context.Cluster, *config.Clusters[context.Cluster])...)
} }
return newErrConfigurationInvalid(validationErrors) return newErrConfigurationInvalid(validationErrors)

View File

@ -28,25 +28,25 @@ import (
func TestConfirmUsableBadInfoButOkConfig(t *testing.T) { func TestConfirmUsableBadInfoButOkConfig(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["missing ca"] = clientcmdapi.Cluster{ config.Clusters["missing ca"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
CertificateAuthority: "missing", CertificateAuthority: "missing",
} }
config.AuthInfos["error"] = clientcmdapi.AuthInfo{ config.AuthInfos["error"] = &clientcmdapi.AuthInfo{
Username: "anything", Username: "anything",
Token: "here", Token: "here",
} }
config.Contexts["dirty"] = clientcmdapi.Context{ config.Contexts["dirty"] = &clientcmdapi.Context{
Cluster: "missing ca", Cluster: "missing ca",
AuthInfo: "error", AuthInfo: "error",
} }
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
} }
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
Token: "here", Token: "here",
} }
config.Contexts["clean"] = clientcmdapi.Context{ config.Contexts["clean"] = &clientcmdapi.Context{
Cluster: "clean", Cluster: "clean",
AuthInfo: "clean", AuthInfo: "clean",
} }
@ -64,15 +64,15 @@ func TestConfirmUsableBadInfoButOkConfig(t *testing.T) {
} }
func TestConfirmUsableBadInfoConfig(t *testing.T) { func TestConfirmUsableBadInfoConfig(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["missing ca"] = clientcmdapi.Cluster{ config.Clusters["missing ca"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
CertificateAuthority: "missing", CertificateAuthority: "missing",
} }
config.AuthInfos["error"] = clientcmdapi.AuthInfo{ config.AuthInfos["error"] = &clientcmdapi.AuthInfo{
Username: "anything", Username: "anything",
Token: "here", Token: "here",
} }
config.Contexts["first"] = clientcmdapi.Context{ config.Contexts["first"] = &clientcmdapi.Context{
Cluster: "missing ca", Cluster: "missing ca",
AuthInfo: "error", AuthInfo: "error",
} }
@ -150,7 +150,7 @@ func TestIsConfigurationInvalid(t *testing.T) {
func TestValidateMissingReferencesConfig(t *testing.T) { func TestValidateMissingReferencesConfig(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.CurrentContext = "anything" config.CurrentContext = "anything"
config.Contexts["anything"] = clientcmdapi.Context{Cluster: "missing", AuthInfo: "missing"} config.Contexts["anything"] = &clientcmdapi.Context{Cluster: "missing", AuthInfo: "missing"}
test := configValidationTest{ test := configValidationTest{
config: config, config: config,
expectedErrorSubstring: []string{"user \"missing\" was not found for context \"anything\"", "cluster \"missing\" was not found for context \"anything\""}, expectedErrorSubstring: []string{"user \"missing\" was not found for context \"anything\"", "cluster \"missing\" was not found for context \"anything\""},
@ -162,7 +162,7 @@ func TestValidateMissingReferencesConfig(t *testing.T) {
func TestValidateEmptyContext(t *testing.T) { func TestValidateEmptyContext(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.CurrentContext = "anything" config.CurrentContext = "anything"
config.Contexts["anything"] = clientcmdapi.Context{} config.Contexts["anything"] = &clientcmdapi.Context{}
test := configValidationTest{ test := configValidationTest{
config: config, config: config,
expectedErrorSubstring: []string{"user was not specified for context \"anything\"", "cluster was not specified for context \"anything\""}, expectedErrorSubstring: []string{"user was not specified for context \"anything\"", "cluster was not specified for context \"anything\""},
@ -174,7 +174,7 @@ func TestValidateEmptyContext(t *testing.T) {
func TestValidateEmptyClusterInfo(t *testing.T) { func TestValidateEmptyClusterInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["empty"] = clientcmdapi.Cluster{} config.Clusters["empty"] = &clientcmdapi.Cluster{}
test := configValidationTest{ test := configValidationTest{
config: config, config: config,
expectedErrorSubstring: []string{"no server found for"}, expectedErrorSubstring: []string{"no server found for"},
@ -185,7 +185,7 @@ func TestValidateEmptyClusterInfo(t *testing.T) {
} }
func TestValidateMissingCAFileClusterInfo(t *testing.T) { func TestValidateMissingCAFileClusterInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["missing ca"] = clientcmdapi.Cluster{ config.Clusters["missing ca"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
CertificateAuthority: "missing", CertificateAuthority: "missing",
} }
@ -199,7 +199,7 @@ func TestValidateMissingCAFileClusterInfo(t *testing.T) {
} }
func TestValidateCleanClusterInfo(t *testing.T) { func TestValidateCleanClusterInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
} }
test := configValidationTest{ test := configValidationTest{
@ -214,7 +214,7 @@ func TestValidateCleanWithCAClusterInfo(t *testing.T) {
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["clean"] = clientcmdapi.Cluster{ config.Clusters["clean"] = &clientcmdapi.Cluster{
Server: "anything", Server: "anything",
CertificateAuthority: tempFile.Name(), CertificateAuthority: tempFile.Name(),
} }
@ -228,7 +228,7 @@ func TestValidateCleanWithCAClusterInfo(t *testing.T) {
func TestValidateEmptyAuthInfo(t *testing.T) { func TestValidateEmptyAuthInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["error"] = clientcmdapi.AuthInfo{} config.AuthInfos["error"] = &clientcmdapi.AuthInfo{}
test := configValidationTest{ test := configValidationTest{
config: config, config: config,
} }
@ -238,7 +238,7 @@ func TestValidateEmptyAuthInfo(t *testing.T) {
} }
func TestValidateCertFilesNotFoundAuthInfo(t *testing.T) { func TestValidateCertFilesNotFoundAuthInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["error"] = clientcmdapi.AuthInfo{ config.AuthInfos["error"] = &clientcmdapi.AuthInfo{
ClientCertificate: "missing", ClientCertificate: "missing",
ClientKey: "missing", ClientKey: "missing",
} }
@ -255,7 +255,7 @@ func TestValidateCertDataOverridesFiles(t *testing.T) {
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
ClientCertificate: tempFile.Name(), ClientCertificate: tempFile.Name(),
ClientCertificateData: []byte("certdata"), ClientCertificateData: []byte("certdata"),
ClientKey: tempFile.Name(), ClientKey: tempFile.Name(),
@ -274,7 +274,7 @@ func TestValidateCleanCertFilesAuthInfo(t *testing.T) {
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
ClientCertificate: tempFile.Name(), ClientCertificate: tempFile.Name(),
ClientKey: tempFile.Name(), ClientKey: tempFile.Name(),
} }
@ -287,7 +287,7 @@ func TestValidateCleanCertFilesAuthInfo(t *testing.T) {
} }
func TestValidateCleanTokenAuthInfo(t *testing.T) { func TestValidateCleanTokenAuthInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["clean"] = clientcmdapi.AuthInfo{ config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
Token: "any-value", Token: "any-value",
} }
test := configValidationTest{ test := configValidationTest{
@ -300,7 +300,7 @@ func TestValidateCleanTokenAuthInfo(t *testing.T) {
func TestValidateMultipleMethodsAuthInfo(t *testing.T) { func TestValidateMultipleMethodsAuthInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.AuthInfos["error"] = clientcmdapi.AuthInfo{ config.AuthInfos["error"] = &clientcmdapi.AuthInfo{
Token: "token", Token: "token",
Username: "username", Username: "username",
} }
@ -319,7 +319,7 @@ type configValidationTest struct {
} }
func (c configValidationTest) testContext(contextName string, t *testing.T) { func (c configValidationTest) testContext(contextName string, t *testing.T) {
errs := validateContext(contextName, c.config.Contexts[contextName], *c.config) errs := validateContext(contextName, *c.config.Contexts[contextName], *c.config)
if len(c.expectedErrorSubstring) != 0 { if len(c.expectedErrorSubstring) != 0 {
if len(errs) == 0 { if len(errs) == 0 {
@ -379,7 +379,7 @@ func (c configValidationTest) testConfig(t *testing.T) {
} }
} }
func (c configValidationTest) testCluster(clusterName string, t *testing.T) { func (c configValidationTest) testCluster(clusterName string, t *testing.T) {
errs := validateClusterInfo(clusterName, c.config.Clusters[clusterName]) errs := validateClusterInfo(clusterName, *c.config.Clusters[clusterName])
if len(c.expectedErrorSubstring) != 0 { if len(c.expectedErrorSubstring) != 0 {
if len(errs) == 0 { if len(errs) == 0 {
@ -399,7 +399,7 @@ func (c configValidationTest) testCluster(clusterName string, t *testing.T) {
} }
func (c configValidationTest) testAuthInfo(authInfoName string, t *testing.T) { func (c configValidationTest) testAuthInfo(authInfoName string, t *testing.T) {
errs := validateAuthInfo(authInfoName, c.config.AuthInfos[authInfoName]) errs := validateAuthInfo(authInfoName, *c.config.AuthInfos[authInfoName])
if len(c.expectedErrorSubstring) != 0 { if len(c.expectedErrorSubstring) != 0 {
if len(errs) == 0 { if len(errs) == 0 {

View File

@ -34,11 +34,11 @@ import (
func newRedFederalCowHammerConfig() clientcmdapi.Config { func newRedFederalCowHammerConfig() clientcmdapi.Config {
return clientcmdapi.Config{ return clientcmdapi.Config{
AuthInfos: map[string]clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"red-user": {Token: "red-token"}}, "red-user": {Token: "red-token"}},
Clusters: map[string]clientcmdapi.Cluster{ Clusters: map[string]*clientcmdapi.Cluster{
"cow-cluster": {Server: "http://cow.org:8080"}}, "cow-cluster": {Server: "http://cow.org:8080"}},
Contexts: map[string]clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"}}, "federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"}},
CurrentContext: "federal-context", CurrentContext: "federal-context",
} }
@ -108,10 +108,7 @@ func TestSetNonExistantContext(t *testing.T) {
func TestSetIntoExistingStruct(t *testing.T) { func TestSetIntoExistingStruct(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
a := expectedConfig.AuthInfos["red-user"] expectedConfig.AuthInfos["red-user"].Password = "new-path-value"
authInfo := &a
authInfo.Password = "new-path-value"
expectedConfig.AuthInfos["red-user"] = *authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set", "users.red-user.password", "new-path-value"}, args: []string{"set", "users.red-user.password", "new-path-value"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -123,10 +120,7 @@ func TestSetIntoExistingStruct(t *testing.T) {
func TestSetWithPathPrefixIntoExistingStruct(t *testing.T) { func TestSetWithPathPrefixIntoExistingStruct(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
cc := expectedConfig.Clusters["cow-clusters"] expectedConfig.Clusters["cow-cluster"].Server = "http://cow.org:8080/foo/baz"
cinfo := &cc
cinfo.Server = "http://cow.org:8080/foo/baz"
expectedConfig.Clusters["cow-cluster"] = *cinfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set", "clusters.cow-cluster.server", "http://cow.org:8080/foo/baz"}, args: []string{"set", "clusters.cow-cluster.server", "http://cow.org:8080/foo/baz"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -164,7 +158,7 @@ func TestUnsetStruct(t *testing.T) {
func TestUnsetField(t *testing.T) { func TestUnsetField(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.AuthInfos["red-user"] = *clientcmdapi.NewAuthInfo() expectedConfig.AuthInfos["red-user"] = clientcmdapi.NewAuthInfo()
test := configCommandTest{ test := configCommandTest{
args: []string{"unset", "users.red-user.token"}, args: []string{"unset", "users.red-user.token"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -178,7 +172,7 @@ func TestSetIntoNewStruct(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
cluster := clientcmdapi.NewCluster() cluster := clientcmdapi.NewCluster()
cluster.Server = "new-server-value" cluster.Server = "new-server-value"
expectedConfig.Clusters["big-cluster"] = *cluster expectedConfig.Clusters["big-cluster"] = cluster
test := configCommandTest{ test := configCommandTest{
args: []string{"set", "clusters.big-cluster.server", "new-server-value"}, args: []string{"set", "clusters.big-cluster.server", "new-server-value"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -192,7 +186,7 @@ func TestSetBoolean(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
cluster := clientcmdapi.NewCluster() cluster := clientcmdapi.NewCluster()
cluster.InsecureSkipTLSVerify = true cluster.InsecureSkipTLSVerify = true
expectedConfig.Clusters["big-cluster"] = *cluster expectedConfig.Clusters["big-cluster"] = cluster
test := configCommandTest{ test := configCommandTest{
args: []string{"set", "clusters.big-cluster.insecure-skip-tls-verify", "true"}, args: []string{"set", "clusters.big-cluster.insecure-skip-tls-verify", "true"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -206,7 +200,7 @@ func TestSetIntoNewConfig(t *testing.T) {
expectedConfig := *clientcmdapi.NewConfig() expectedConfig := *clientcmdapi.NewConfig()
context := clientcmdapi.NewContext() context := clientcmdapi.NewContext()
context.AuthInfo = "fake-user" context.AuthInfo = "fake-user"
expectedConfig.Contexts["new-context"] = *context expectedConfig.Contexts["new-context"] = context
test := configCommandTest{ test := configCommandTest{
args: []string{"set", "contexts.new-context.user", "fake-user"}, args: []string{"set", "contexts.new-context.user", "fake-user"},
startingConfig: *clientcmdapi.NewConfig(), startingConfig: *clientcmdapi.NewConfig(),
@ -218,7 +212,7 @@ func TestSetIntoNewConfig(t *testing.T) {
func TestNewEmptyAuth(t *testing.T) { func TestNewEmptyAuth(t *testing.T) {
expectedConfig := *clientcmdapi.NewConfig() expectedConfig := *clientcmdapi.NewConfig()
expectedConfig.AuthInfos["the-user-name"] = *clientcmdapi.NewAuthInfo() expectedConfig.AuthInfos["the-user-name"] = clientcmdapi.NewAuthInfo()
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "the-user-name"}, args: []string{"set-credentials", "the-user-name"},
startingConfig: *clientcmdapi.NewConfig(), startingConfig: *clientcmdapi.NewConfig(),
@ -232,7 +226,7 @@ func TestAdditionalAuth(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
authInfo := clientcmdapi.NewAuthInfo() authInfo := clientcmdapi.NewAuthInfo()
authInfo.Token = "token" authInfo.Token = "token"
expectedConfig.AuthInfos["another-user"] = *authInfo expectedConfig.AuthInfos["another-user"] = authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -250,7 +244,7 @@ func TestEmbedClientCert(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
authInfo := clientcmdapi.NewAuthInfo() authInfo := clientcmdapi.NewAuthInfo()
authInfo.ClientCertificateData = fakeData authInfo.ClientCertificateData = fakeData
expectedConfig.AuthInfos["another-user"] = *authInfo expectedConfig.AuthInfos["another-user"] = authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=" + fakeCertFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=" + fakeCertFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"},
@ -269,7 +263,7 @@ func TestEmbedClientKey(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
authInfo := clientcmdapi.NewAuthInfo() authInfo := clientcmdapi.NewAuthInfo()
authInfo.ClientKeyData = fakeData authInfo.ClientKeyData = fakeData
expectedConfig.AuthInfos["another-user"] = *authInfo expectedConfig.AuthInfos["another-user"] = authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagKeyFile + "=" + fakeKeyFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagKeyFile + "=" + fakeKeyFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"},
@ -296,7 +290,7 @@ func TestEmptyTokenAndCertAllowed(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
authInfo := clientcmdapi.NewAuthInfo() authInfo := clientcmdapi.NewAuthInfo()
authInfo.ClientCertificate = "cert-file" authInfo.ClientCertificate = "cert-file"
expectedConfig.AuthInfos["another-user"] = *authInfo expectedConfig.AuthInfos["another-user"] = authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert-file", "--" + clientcmd.FlagBearerToken + "="}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert-file", "--" + clientcmd.FlagBearerToken + "="},
@ -312,7 +306,7 @@ func TestTokenAndCertAllowed(t *testing.T) {
authInfo := clientcmdapi.NewAuthInfo() authInfo := clientcmdapi.NewAuthInfo()
authInfo.Token = "token" authInfo.Token = "token"
authInfo.ClientCertificate = "cert-file" authInfo.ClientCertificate = "cert-file"
expectedConfig.AuthInfos["another-user"] = *authInfo expectedConfig.AuthInfos["another-user"] = authInfo
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert-file", "--" + clientcmd.FlagBearerToken + "=token"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert-file", "--" + clientcmd.FlagBearerToken + "=token"},
startingConfig: newRedFederalCowHammerConfig(), startingConfig: newRedFederalCowHammerConfig(),
@ -343,10 +337,10 @@ func TestBasicClearsToken(t *testing.T) {
authInfoWithBasic.Password = "mypass" authInfoWithBasic.Password = "mypass"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.AuthInfos["another-user"] = *authInfoWithToken startingConfig.AuthInfos["another-user"] = authInfoWithToken
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.AuthInfos["another-user"] = *authInfoWithBasic expectedConfig.AuthInfos["another-user"] = authInfoWithBasic
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagUsername + "=myuser", "--" + clientcmd.FlagPassword + "=mypass"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagUsername + "=myuser", "--" + clientcmd.FlagPassword + "=mypass"},
@ -366,10 +360,10 @@ func TestTokenClearsBasic(t *testing.T) {
authInfoWithToken.Token = "token" authInfoWithToken.Token = "token"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.AuthInfos["another-user"] = *authInfoWithBasic startingConfig.AuthInfos["another-user"] = authInfoWithBasic
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.AuthInfos["another-user"] = *authInfoWithToken expectedConfig.AuthInfos["another-user"] = authInfoWithToken
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"},
@ -395,10 +389,10 @@ func TestTokenLeavesCert(t *testing.T) {
authInfoWithTokenAndCerts.ClientKeyData = []byte("keydata") authInfoWithTokenAndCerts.ClientKeyData = []byte("keydata")
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.AuthInfos["another-user"] = *authInfoWithCerts startingConfig.AuthInfos["another-user"] = authInfoWithCerts
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.AuthInfos["another-user"] = *authInfoWithTokenAndCerts expectedConfig.AuthInfos["another-user"] = authInfoWithTokenAndCerts
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagBearerToken + "=token"},
@ -419,10 +413,10 @@ func TestCertLeavesToken(t *testing.T) {
authInfoWithTokenAndCerts.ClientKey = "key" authInfoWithTokenAndCerts.ClientKey = "key"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.AuthInfos["another-user"] = *authInfoWithToken startingConfig.AuthInfos["another-user"] = authInfoWithToken
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.AuthInfos["another-user"] = *authInfoWithTokenAndCerts expectedConfig.AuthInfos["another-user"] = authInfoWithTokenAndCerts
test := configCommandTest{ test := configCommandTest{
args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert", "--" + clientcmd.FlagKeyFile + "=key"}, args: []string{"set-credentials", "another-user", "--" + clientcmd.FlagCertFile + "=cert", "--" + clientcmd.FlagKeyFile + "=key"},
@ -441,10 +435,10 @@ func TestCAClearsInsecure(t *testing.T) {
clusterInfoWithCA.CertificateAuthority = "cafile" clusterInfoWithCA.CertificateAuthority = "cafile"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.Clusters["another-cluster"] = *clusterInfoWithInsecure startingConfig.Clusters["another-cluster"] = clusterInfoWithInsecure
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.Clusters["another-cluster"] = *clusterInfoWithCA expectedConfig.Clusters["another-cluster"] = clusterInfoWithCA
test := configCommandTest{ test := configCommandTest{
args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=cafile"}, args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=cafile"},
@ -463,10 +457,10 @@ func TestCAClearsCAData(t *testing.T) {
clusterInfoWithCA.CertificateAuthority = "cafile" clusterInfoWithCA.CertificateAuthority = "cafile"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.Clusters["another-cluster"] = *clusterInfoWithCAData startingConfig.Clusters["another-cluster"] = clusterInfoWithCAData
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.Clusters["another-cluster"] = *clusterInfoWithCA expectedConfig.Clusters["another-cluster"] = clusterInfoWithCA
test := configCommandTest{ test := configCommandTest{
args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=cafile", "--" + clientcmd.FlagInsecure + "=false"}, args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=cafile", "--" + clientcmd.FlagInsecure + "=false"},
@ -486,10 +480,10 @@ func TestInsecureClearsCA(t *testing.T) {
clusterInfoWithCA.CertificateAuthorityData = []byte("cadata") clusterInfoWithCA.CertificateAuthorityData = []byte("cadata")
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.Clusters["another-cluster"] = *clusterInfoWithCA startingConfig.Clusters["another-cluster"] = clusterInfoWithCA
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.Clusters["another-cluster"] = *clusterInfoWithInsecure expectedConfig.Clusters["another-cluster"] = clusterInfoWithInsecure
test := configCommandTest{ test := configCommandTest{
args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagInsecure + "=true"}, args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagInsecure + "=true"},
@ -513,10 +507,10 @@ func TestCADataClearsCA(t *testing.T) {
clusterInfoWithCA.CertificateAuthority = "cafile" clusterInfoWithCA.CertificateAuthority = "cafile"
startingConfig := newRedFederalCowHammerConfig() startingConfig := newRedFederalCowHammerConfig()
startingConfig.Clusters["another-cluster"] = *clusterInfoWithCA startingConfig.Clusters["another-cluster"] = clusterInfoWithCA
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
expectedConfig.Clusters["another-cluster"] = *clusterInfoWithCAData expectedConfig.Clusters["another-cluster"] = clusterInfoWithCAData
test := configCommandTest{ test := configCommandTest{
args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=" + fakeCAFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"}, args: []string{"set-cluster", "another-cluster", "--" + clientcmd.FlagCAFile + "=" + fakeCAFile.Name(), "--" + clientcmd.FlagEmbedCerts + "=true"},
@ -566,7 +560,7 @@ func TestMergeExistingAuth(t *testing.T) {
func TestNewEmptyCluster(t *testing.T) { func TestNewEmptyCluster(t *testing.T) {
expectedConfig := *clientcmdapi.NewConfig() expectedConfig := *clientcmdapi.NewConfig()
expectedConfig.Clusters["new-cluster"] = *clientcmdapi.NewCluster() expectedConfig.Clusters["new-cluster"] = clientcmdapi.NewCluster()
test := configCommandTest{ test := configCommandTest{
args: []string{"set-cluster", "new-cluster"}, args: []string{"set-cluster", "new-cluster"},
startingConfig: *clientcmdapi.NewConfig(), startingConfig: *clientcmdapi.NewConfig(),
@ -578,7 +572,7 @@ func TestNewEmptyCluster(t *testing.T) {
func TestAdditionalCluster(t *testing.T) { func TestAdditionalCluster(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
cluster := *clientcmdapi.NewCluster() cluster := clientcmdapi.NewCluster()
cluster.APIVersion = testapi.Version() cluster.APIVersion = testapi.Version()
cluster.CertificateAuthority = "ca-location" cluster.CertificateAuthority = "ca-location"
cluster.InsecureSkipTLSVerify = false cluster.InsecureSkipTLSVerify = false
@ -595,7 +589,7 @@ func TestAdditionalCluster(t *testing.T) {
func TestOverwriteExistingCluster(t *testing.T) { func TestOverwriteExistingCluster(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
cluster := *clientcmdapi.NewCluster() cluster := clientcmdapi.NewCluster()
cluster.Server = "serverlocation" cluster.Server = "serverlocation"
expectedConfig.Clusters["cow-cluster"] = cluster expectedConfig.Clusters["cow-cluster"] = cluster
@ -610,7 +604,7 @@ func TestOverwriteExistingCluster(t *testing.T) {
func TestNewEmptyContext(t *testing.T) { func TestNewEmptyContext(t *testing.T) {
expectedConfig := *clientcmdapi.NewConfig() expectedConfig := *clientcmdapi.NewConfig()
expectedConfig.Contexts["new-context"] = *clientcmdapi.NewContext() expectedConfig.Contexts["new-context"] = clientcmdapi.NewContext()
test := configCommandTest{ test := configCommandTest{
args: []string{"set-context", "new-context"}, args: []string{"set-context", "new-context"},
startingConfig: *clientcmdapi.NewConfig(), startingConfig: *clientcmdapi.NewConfig(),
@ -622,7 +616,7 @@ func TestNewEmptyContext(t *testing.T) {
func TestAdditionalContext(t *testing.T) { func TestAdditionalContext(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig() expectedConfig := newRedFederalCowHammerConfig()
context := *clientcmdapi.NewContext() context := clientcmdapi.NewContext()
context.Cluster = "some-cluster" context.Cluster = "some-cluster"
context.AuthInfo = "some-user" context.AuthInfo = "some-user"
context.Namespace = "different-namespace" context.Namespace = "different-namespace"
@ -683,10 +677,13 @@ func TestToBool(t *testing.T) {
} }
func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (string, clientcmdapi.Config) { func testConfigCommand(args []string, startingConfig clientcmdapi.Config, t *testing.T) (string, clientcmdapi.Config) {
fakeKubeFile, _ := ioutil.TempFile("", "") fakeKubeFile, _ := ioutil.TempFile("", "")
defer os.Remove(fakeKubeFile.Name()) defer os.Remove(fakeKubeFile.Name())
clientcmd.WriteToFile(startingConfig, fakeKubeFile.Name()) err := clientcmd.WriteToFile(startingConfig, fakeKubeFile.Name())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
argsToUse := make([]string, 0, 2+len(args)) argsToUse := make([]string, 0, 2+len(args))
argsToUse = append(argsToUse, "--kubeconfig="+fakeKubeFile.Name()) argsToUse = append(argsToUse, "--kubeconfig="+fakeKubeFile.Name())
@ -712,7 +709,7 @@ type configCommandTest struct {
} }
func (test configCommandTest) run(t *testing.T) string { func (test configCommandTest) run(t *testing.T) string {
out, actualConfig := testConfigCommand(test.args, test.startingConfig) out, actualConfig := testConfigCommand(test.args, test.startingConfig, t)
testSetNilMapsToEmpties(reflect.ValueOf(&test.expectedConfig)) testSetNilMapsToEmpties(reflect.ValueOf(&test.expectedConfig))
testSetNilMapsToEmpties(reflect.ValueOf(&actualConfig)) testSetNilMapsToEmpties(reflect.ValueOf(&actualConfig))
@ -755,20 +752,7 @@ func testSetNilMapsToEmpties(curr reflect.Value) {
case reflect.Map: case reflect.Map:
for _, mapKey := range actualCurrValue.MapKeys() { for _, mapKey := range actualCurrValue.MapKeys() {
currMapValue := actualCurrValue.MapIndex(mapKey) currMapValue := actualCurrValue.MapIndex(mapKey)
testSetNilMapsToEmpties(currMapValue)
// our maps do not hold pointers to structs, they hold the structs themselves. This means that MapIndex returns the struct itself
// That in turn means that they have kinds of type.Struct, which is not a settable type. Because of this, we need to make new struct of that type
// copy all the data from the old value into the new value, then take the .addr of the new value to modify it in the next recursion.
// clear as mud
modifiableMapValue := reflect.New(currMapValue.Type()).Elem()
modifiableMapValue.Set(currMapValue)
if modifiableMapValue.Kind() == reflect.Struct {
modifiableMapValue = modifiableMapValue.Addr()
}
testSetNilMapsToEmpties(modifiableMapValue)
actualCurrValue.SetMapIndex(mapKey, reflect.Indirect(modifiableMapValue))
} }
case reflect.Struct: case reflect.Struct:

View File

@ -108,8 +108,12 @@ func (o createAuthInfoOptions) run() error {
return err return err
} }
authInfo := o.modifyAuthInfo(config.AuthInfos[o.name]) startingStanza, exists := config.AuthInfos[o.name]
config.AuthInfos[o.name] = authInfo if !exists {
startingStanza = clientcmdapi.NewAuthInfo()
}
authInfo := o.modifyAuthInfo(*startingStanza)
config.AuthInfos[o.name] = &authInfo
if err := ModifyConfig(o.configAccess, *config); err != nil { if err := ModifyConfig(o.configAccess, *config); err != nil {
return err return err

View File

@ -94,8 +94,12 @@ func (o createClusterOptions) run() error {
return err return err
} }
cluster := o.modifyCluster(config.Clusters[o.name]) startingStanza, exists := config.Clusters[o.name]
config.Clusters[o.name] = cluster if !exists {
startingStanza = clientcmdapi.NewCluster()
}
cluster := o.modifyCluster(*startingStanza)
config.Clusters[o.name] = &cluster
if err := ModifyConfig(o.configAccess, *config); err != nil { if err := ModifyConfig(o.configAccess, *config); err != nil {
return err return err

View File

@ -81,8 +81,12 @@ func (o createContextOptions) run() error {
return err return err
} }
context := o.modifyContext(config.Contexts[o.name]) startingStanza, exists := config.Contexts[o.name]
config.Contexts[o.name] = context if !exists {
startingStanza = clientcmdapi.NewContext()
}
context := o.modifyContext(*startingStanza)
config.Contexts[o.name] = &context
if err := ModifyConfig(o.configAccess, *config); err != nil { if err := ModifyConfig(o.configAccess, *config); err != nil {
return err return err

View File

@ -50,7 +50,7 @@ func newNavigationSteps(path string) (*navigationSteps, error) {
// store them as a single step. In order to do that, we need to determine what set of tokens is a legal step AFTER the name of the map key // store them as a single step. In order to do that, we need to determine what set of tokens is a legal step AFTER the name of the map key
// This set of reflective code pulls the type of the map values, uses that type to look up the set of legal tags. Those legal tags are used to // This set of reflective code pulls the type of the map values, uses that type to look up the set of legal tags. Those legal tags are used to
// walk the list of remaining parts until we find a match to a legal tag or the end of the string. That name is used to burn all the used parts. // walk the list of remaining parts until we find a match to a legal tag or the end of the string. That name is used to burn all the used parts.
mapValueType := currType.Elem() mapValueType := currType.Elem().Elem()
mapValueOptions, err := getPotentialTypeValues(mapValueType) mapValueOptions, err := getPotentialTypeValues(mapValueType)
if err != nil { if err != nil {
return nil, err return nil, err
@ -120,6 +120,10 @@ func findNameStep(parts []string, typeOptions util.StringSet) string {
// getPotentialTypeValues takes a type and looks up the tags used to represent its fields when serialized. // getPotentialTypeValues takes a type and looks up the tags used to represent its fields when serialized.
func getPotentialTypeValues(typeValue reflect.Type) (map[string]reflect.Type, error) { func getPotentialTypeValues(typeValue reflect.Type) (map[string]reflect.Type, error) {
if typeValue.Kind() == reflect.Ptr {
typeValue = typeValue.Elem()
}
if typeValue.Kind() != reflect.Struct { if typeValue.Kind() != reflect.Struct {
return nil, fmt.Errorf("%v is not of type struct", typeValue) return nil, fmt.Errorf("%v is not of type struct", typeValue)
} }

View File

@ -36,7 +36,7 @@ func TestParseWithDots(t *testing.T) {
path: "clusters.my.dot.delimited.name.server", path: "clusters.my.dot.delimited.name.server",
expectedNavigationSteps: navigationSteps{ expectedNavigationSteps: navigationSteps{
steps: []navigationStep{ steps: []navigationStep{
{"clusters", reflect.TypeOf(make(map[string]clientcmdapi.Cluster))}, {"clusters", reflect.TypeOf(make(map[string]*clientcmdapi.Cluster))},
{"my.dot.delimited.name", reflect.TypeOf(clientcmdapi.Cluster{})}, {"my.dot.delimited.name", reflect.TypeOf(clientcmdapi.Cluster{})},
{"server", reflect.TypeOf("")}, {"server", reflect.TypeOf("")},
}, },
@ -51,7 +51,7 @@ func TestParseWithDotsEndingWithName(t *testing.T) {
path: "contexts.10.12.12.12", path: "contexts.10.12.12.12",
expectedNavigationSteps: navigationSteps{ expectedNavigationSteps: navigationSteps{
steps: []navigationStep{ steps: []navigationStep{
{"contexts", reflect.TypeOf(make(map[string]clientcmdapi.Context))}, {"contexts", reflect.TypeOf(make(map[string]*clientcmdapi.Context))},
{"10.12.12.12", reflect.TypeOf(clientcmdapi.Context{})}, {"10.12.12.12", reflect.TypeOf(clientcmdapi.Context{})},
}, },
}, },
@ -91,5 +91,6 @@ func (test stepParserTest) run(t *testing.T) {
if !reflect.DeepEqual(test.expectedNavigationSteps, *actualSteps) { if !reflect.DeepEqual(test.expectedNavigationSteps, *actualSteps) {
t.Errorf("diff: %v", util.ObjectDiff(test.expectedNavigationSteps, *actualSteps)) t.Errorf("diff: %v", util.ObjectDiff(test.expectedNavigationSteps, *actualSteps))
t.Errorf("expected: %#v\n actual: %#v", test.expectedNavigationSteps, *actualSteps)
} }
} }

View File

@ -139,26 +139,15 @@ func modifyConfig(curr reflect.Value, steps *navigationSteps, propertyValue stri
needToSetNewMapValue := currMapValue.Kind() == reflect.Invalid needToSetNewMapValue := currMapValue.Kind() == reflect.Invalid
if needToSetNewMapValue { if needToSetNewMapValue {
currMapValue = reflect.New(mapValueType).Elem() currMapValue = reflect.New(mapValueType.Elem()).Elem().Addr()
actualCurrValue.SetMapIndex(mapKey, currMapValue) actualCurrValue.SetMapIndex(mapKey, currMapValue)
} }
// our maps do not hold pointers to structs, they hold the structs themselves. This means that MapIndex returns the struct itself err := modifyConfig(currMapValue, steps, propertyValue, unset)
// That in turn means that they have kinds of type.Struct, which is not a settable type. Because of this, we need to make new struct of that type
// copy all the data from the old value into the new value, then take the .addr of the new value to modify it in the next recursion.
// clear as mud
modifiableMapValue := reflect.New(currMapValue.Type()).Elem()
modifiableMapValue.Set(currMapValue)
if modifiableMapValue.Kind() == reflect.Struct {
modifiableMapValue = modifiableMapValue.Addr()
}
err := modifyConfig(modifiableMapValue, steps, propertyValue, unset)
if err != nil { if err != nil {
return err return err
} }
actualCurrValue.SetMapIndex(mapKey, reflect.Indirect(modifiableMapValue))
return nil return nil
case reflect.String: case reflect.String:
@ -213,5 +202,6 @@ func modifyConfig(curr reflect.Value, steps *navigationSteps, propertyValue stri
} }
return fmt.Errorf("Unrecognized type: %v", actualCurrValue) panic(fmt.Errorf("Unrecognized type: %v", actualCurrValue))
return nil
} }