diff --git a/cmd/kubeadm/app/cmd/alpha/kubeconfig.go b/cmd/kubeadm/app/cmd/alpha/kubeconfig.go index 6a3cc2faa07..389f3a72d70 100644 --- a/cmd/kubeadm/app/cmd/alpha/kubeconfig.go +++ b/cmd/kubeadm/app/cmd/alpha/kubeconfig.go @@ -99,6 +99,7 @@ func newCmdUserKubeConfig(out io.Writer) *cobra.Command { // Add ClusterConfiguration backed flags to the command cmd.Flags().StringVar(&clusterCfg.CertificatesDir, options.CertificatesDir, clusterCfg.CertificatesDir, "The path where certificates are stored") + cmd.Flags().StringVar(&clusterCfg.ClusterName, "cluster-name", clusterCfg.ClusterName, "Cluster name to be used in kubeconfig") // Add InitConfiguration backed flags to the command cmd.Flags().StringVar(&initCfg.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, initCfg.LocalAPIEndpoint.AdvertiseAddress, "The IP address the API server is accessible on") diff --git a/cmd/kubeadm/app/cmd/alpha/kubeconfig_test.go b/cmd/kubeadm/app/cmd/alpha/kubeconfig_test.go index e80e9a652c5..5477e72ec82 100644 --- a/cmd/kubeadm/app/cmd/alpha/kubeconfig_test.go +++ b/cmd/kubeadm/app/cmd/alpha/kubeconfig_test.go @@ -56,6 +56,7 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) { command string withClientCert bool withToken bool + withClusterName bool additionalFlags []string }{ { @@ -63,12 +64,26 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) { command: "user", withClientCert: true, }, + { + name: "user subCommand withClientCert", + command: "user", + withClientCert: true, + withClusterName: true, + additionalFlags: []string{"--cluster-name=my-cluster"}, + }, { name: "user subCommand withToken", withToken: true, command: "user", additionalFlags: []string{"--token=123456"}, }, + { + name: "user subCommand withToken", + withToken: true, + command: "user", + withClusterName: true, + additionalFlags: []string{"--token=123456", "--cluster-name=my-cluster"}, + }, } for _, test := range tests { @@ -104,6 +119,11 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) { // checks that kubeconfig files have expected token kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456") } + + if test.withClusterName { + // checks that kubeconfig files have expected cluster name + kubeconfigtestutil.AssertKubeConfigCurrentContextWithClusterName(t, config, "my-cluster") + } }) } } diff --git a/cmd/kubeadm/test/kubeconfig/util.go b/cmd/kubeadm/test/kubeconfig/util.go index 51092a79d7e..46774cd02ab 100644 --- a/cmd/kubeadm/test/kubeconfig/util.go +++ b/cmd/kubeadm/test/kubeconfig/util.go @@ -98,3 +98,15 @@ func AssertKubeConfigCurrentAuthInfoWithToken(t *testing.T, config *clientcmdapi return } } + +// AssertKubeConfigCurrentContextWithClusterName is a utility function for kubeadm testing that asserts if the Current Cluster config in +// the given KubeConfig object refers to expected cluster name +func AssertKubeConfigCurrentContextWithClusterName(t *testing.T, config *clientcmdapi.Config, expectedClusterName string) { + currentContext := config.Contexts[config.CurrentContext] + + // assert cluster name + if currentContext.Cluster != expectedClusterName { + t.Errorf("kubeconfig.currentContext.clusterName [%s], expected [%s]", currentContext.Cluster, expectedClusterName) + return + } +}