Allow to configure clustername in creation of kubeconfig using kubeadm command

Signed-off-by: Prabhu Jayakumar <j.prabhu91@gmail.com>
This commit is contained in:
Prabhu Jayakumar 2020-08-14 01:52:59 +05:30
parent b497fa1d34
commit bdd0ccaddb
No known key found for this signature in database
GPG Key ID: 1901435BB9AA9A6C
3 changed files with 33 additions and 0 deletions

View File

@ -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")

View File

@ -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")
}
})
}
}

View File

@ -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
}
}