Fernand Galiana 2023-12-25 18:25:53 -07:00 committed by GitHub
parent 490b6d5359
commit 00d16beedb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -96,7 +96,16 @@ func (c *Config) CurrentClusterName() (string, error) {
return "", err
}
ct := cfg.Contexts[cfg.CurrentContext]
ct, ok := cfg.Contexts[cfg.CurrentContext]
if !ok {
return "", fmt.Errorf("invalid current context specified: %q", cfg.CurrentContext)
}
if isSet(c.flags.Context) {
ct, ok = cfg.Contexts[*c.flags.Context]
if !ok {
return "", fmt.Errorf("invalid context specified: %q", *c.flags.Context)
}
}
return ct.Cluster, nil

View File

@ -54,15 +54,20 @@ func TestConfigCurrentCluster(t *testing.T) {
name, kubeConfig := "blee", "./testdata/config"
uu := map[string]struct {
flags *genericclioptions.ConfigFlags
context string
cluster string
}{
"default": {
flags: &genericclioptions.ConfigFlags{KubeConfig: &kubeConfig},
context: "fred",
flags: &genericclioptions.ConfigFlags{
KubeConfig: &kubeConfig,
},
cluster: "zorg",
},
"custom": {
flags: &genericclioptions.ConfigFlags{KubeConfig: &kubeConfig, Context: &name},
context: "blee",
flags: &genericclioptions.ConfigFlags{
KubeConfig: &kubeConfig,
Context: &name,
},
cluster: "blee",
},
}
@ -70,9 +75,9 @@ func TestConfigCurrentCluster(t *testing.T) {
u := uu[k]
t.Run(k, func(t *testing.T) {
cfg := client.NewConfig(u.flags)
ct, err := cfg.CurrentContextName()
ct, err := cfg.CurrentClusterName()
assert.Nil(t, err)
assert.Equal(t, u.context, ct)
assert.Equal(t, u.cluster, ct)
})
}
}
@ -252,7 +257,7 @@ func TestConfigRestConfig(t *testing.T) {
cfg := client.NewConfig(&flags)
rc, err := cfg.RESTConfig()
assert.Nil(t, err)
assert.Equal(t, "https://localhost:3000", rc.Host)
assert.Equal(t, "https://localhost:3002", rc.Host)
}
func TestConfigBadConfig(t *testing.T) {

View File

@ -13,10 +13,10 @@ clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://localhost:3002
name: duh
name: zorg
contexts:
- context:
cluster: fred
cluster: zorg
user: fred
name: fred
- context: