Skip to content

Commit be1d43a

Browse files
authored
Merge pull request kubernetes#93992 from prabhu43/kubeadm-kubeconfig-clustername
Make clustername configurable in generation of kubeconfig using kubeadm command
2 parents 5b3b205 + bdd0cca commit be1d43a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

cmd/kubeadm/app/cmd/alpha/kubeconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func newCmdUserKubeConfig(out io.Writer) *cobra.Command {
9999

100100
// Add ClusterConfiguration backed flags to the command
101101
cmd.Flags().StringVar(&clusterCfg.CertificatesDir, options.CertificatesDir, clusterCfg.CertificatesDir, "The path where certificates are stored")
102+
cmd.Flags().StringVar(&clusterCfg.ClusterName, "cluster-name", clusterCfg.ClusterName, "Cluster name to be used in kubeconfig")
102103

103104
// Add InitConfiguration backed flags to the command
104105
cmd.Flags().StringVar(&initCfg.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, initCfg.LocalAPIEndpoint.AdvertiseAddress, "The IP address the API server is accessible on")

cmd/kubeadm/app/cmd/alpha/kubeconfig_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,34 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
5656
command string
5757
withClientCert bool
5858
withToken bool
59+
withClusterName bool
5960
additionalFlags []string
6061
}{
6162
{
6263
name: "user subCommand withClientCert",
6364
command: "user",
6465
withClientCert: true,
6566
},
67+
{
68+
name: "user subCommand withClientCert",
69+
command: "user",
70+
withClientCert: true,
71+
withClusterName: true,
72+
additionalFlags: []string{"--cluster-name=my-cluster"},
73+
},
6674
{
6775
name: "user subCommand withToken",
6876
withToken: true,
6977
command: "user",
7078
additionalFlags: []string{"--token=123456"},
7179
},
80+
{
81+
name: "user subCommand withToken",
82+
withToken: true,
83+
command: "user",
84+
withClusterName: true,
85+
additionalFlags: []string{"--token=123456", "--cluster-name=my-cluster"},
86+
},
7287
}
7388

7489
for _, test := range tests {
@@ -104,6 +119,11 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
104119
// checks that kubeconfig files have expected token
105120
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
106121
}
122+
123+
if test.withClusterName {
124+
// checks that kubeconfig files have expected cluster name
125+
kubeconfigtestutil.AssertKubeConfigCurrentContextWithClusterName(t, config, "my-cluster")
126+
}
107127
})
108128
}
109129
}

cmd/kubeadm/test/kubeconfig/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ func AssertKubeConfigCurrentAuthInfoWithToken(t *testing.T, config *clientcmdapi
9898
return
9999
}
100100
}
101+
102+
// AssertKubeConfigCurrentContextWithClusterName is a utility function for kubeadm testing that asserts if the Current Cluster config in
103+
// the given KubeConfig object refers to expected cluster name
104+
func AssertKubeConfigCurrentContextWithClusterName(t *testing.T, config *clientcmdapi.Config, expectedClusterName string) {
105+
currentContext := config.Contexts[config.CurrentContext]
106+
107+
// assert cluster name
108+
if currentContext.Cluster != expectedClusterName {
109+
t.Errorf("kubeconfig.currentContext.clusterName [%s], expected [%s]", currentContext.Cluster, expectedClusterName)
110+
return
111+
}
112+
}

0 commit comments

Comments
 (0)