Skip to content

Commit 9dcbc0b

Browse files
committed
update override behavior for kubectl --tls-server-name
1 parent 37c81ed commit 9dcbc0b

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

staging/src/k8s.io/client-go/tools/clientcmd/api/v1/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/client-go/tools/clientcmd/client_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,11 @@ func (config *DirectClientConfig) getCluster() (clientcmdapi.Cluster, error) {
461461
mergedClusterInfo.CertificateAuthorityData = config.overrides.ClusterInfo.CertificateAuthorityData
462462
}
463463

464-
if config.overrides.ClusterInfo.TLSServerName != "" {
464+
// if the --tls-server-name has been set in overrides, use that value.
465+
// if the --server has been set in overrides, then use the value of --tls-server-name specified on the CLI too. This gives the property
466+
// that setting a --server will effectively clear the KUBECONFIG value of tls-server-name if it is specified on the command line which is
467+
// usually correct.
468+
if config.overrides.ClusterInfo.TLSServerName != "" || config.overrides.ClusterInfo.Server != "" {
465469
mergedClusterInfo.TLSServerName = config.overrides.ClusterInfo.TLSServerName
466470
}
467471

staging/src/k8s.io/client-go/tools/clientcmd/client_config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ func TestTLSServerName(t *testing.T) {
199199
matchByteArg(nil, actualCfg.TLSClientConfig.CAData, t)
200200
}
201201

202+
func TestTLSServerNameClearsWhenServerNameSet(t *testing.T) {
203+
config := createValidTestConfig()
204+
205+
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{
206+
ClusterInfo: clientcmdapi.Cluster{
207+
Server: "http://something",
208+
},
209+
}, nil)
210+
211+
actualCfg, err := clientBuilder.ClientConfig()
212+
if err != nil {
213+
t.Errorf("Unexpected error: %v", err)
214+
}
215+
216+
matchStringArg("", actualCfg.ServerName, t)
217+
}
218+
202219
func TestMergeContext(t *testing.T) {
203220
const namespace = "overridden-namespace"
204221

staging/src/k8s.io/kubectl/pkg/cmd/config/create_cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluste
124124

125125
if o.server.Provided() {
126126
modifiedCluster.Server = o.server.Value()
127+
// specifying a --server on the command line, overrides the TLSServerName that was specified in the kubeconfig file.
128+
// if both are specified, then the next if block will write the new TLSServerName.
129+
modifiedCluster.TLSServerName = ""
127130
}
128131
if o.tlsServerName.Provided() {
129132
modifiedCluster.TLSServerName = o.tlsServerName.Value()

staging/src/k8s.io/kubectl/pkg/cmd/config/create_cluster_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestCreateCluster(t *testing.T) {
5858
func TestModifyCluster(t *testing.T) {
5959
conf := clientcmdapi.Config{
6060
Clusters: map[string]*clientcmdapi.Cluster{
61-
"my-cluster": {Server: "https://192.168.0.1"},
61+
"my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"},
6262
},
6363
}
6464
test := createClusterTest{
@@ -78,6 +78,30 @@ func TestModifyCluster(t *testing.T) {
7878
test.run(t)
7979
}
8080

81+
func TestModifyClusterServerAndTLS(t *testing.T) {
82+
conf := clientcmdapi.Config{
83+
Clusters: map[string]*clientcmdapi.Cluster{
84+
"my-cluster": {Server: "https://192.168.0.1"},
85+
},
86+
}
87+
test := createClusterTest{
88+
description: "Testing 'kubectl config set-cluster' with an existing cluster",
89+
config: conf,
90+
args: []string{"my-cluster"},
91+
flags: []string{
92+
"--server=https://192.168.0.99",
93+
"--tls-server-name=my-cluster-name",
94+
},
95+
expected: `Cluster "my-cluster" set.` + "\n",
96+
expectedConfig: clientcmdapi.Config{
97+
Clusters: map[string]*clientcmdapi.Cluster{
98+
"my-cluster": {Server: "https://192.168.0.99", TLSServerName: "my-cluster-name"},
99+
},
100+
},
101+
}
102+
test.run(t)
103+
}
104+
81105
func (test createClusterTest) run(t *testing.T) {
82106
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
83107
if err != nil {
@@ -117,7 +141,7 @@ func (test createClusterTest) run(t *testing.T) {
117141
t.Errorf("Fail in %q\n expected cluster server %v\n but got %v\n ", test.description, test.expectedConfig.Clusters[test.args[0]].Server, cluster.Server)
118142
}
119143
if cluster.TLSServerName != test.expectedConfig.Clusters[test.args[0]].TLSServerName {
120-
t.Errorf("Fail in %q\n expected cluster TLS server name %v\n but got %v\n ", test.description, test.expectedConfig.Clusters[test.args[0]].TLSServerName, cluster.TLSServerName)
144+
t.Errorf("Fail in %q\n expected cluster TLS server name %q\n but got %q\n ", test.description, test.expectedConfig.Clusters[test.args[0]].TLSServerName, cluster.TLSServerName)
121145
}
122146
}
123147
}

0 commit comments

Comments
 (0)