pinniped whoami: print correct cluster info when --kubeconfig-context is used

Signed-off-by: Monis Khan <mok@vmware.com>
This commit is contained in:
Monis Khan 2021-03-19 18:40:17 -04:00
parent 50e4531215
commit 964d4889c4
No known key found for this signature in database
GPG Key ID: 52C90ADA01B269B8
2 changed files with 65 additions and 6 deletions

View File

@ -78,7 +78,7 @@ func runWhoami(output io.Writer, getClientset getConciergeClientsetFunc, flags *
return fmt.Errorf("could not configure Kubernetes client: %w", err)
}
clusterInfo, err := getCurrentCluster(clientConfig)
clusterInfo, err := getCurrentCluster(clientConfig, flags.kubeconfigContextOverride)
if err != nil {
return fmt.Errorf("could not get current cluster info: %w", err)
}
@ -101,24 +101,29 @@ func runWhoami(output io.Writer, getClientset getConciergeClientsetFunc, flags *
return nil
}
func getCurrentCluster(clientConfig clientcmd.ClientConfig) (*clusterInfo, error) {
currentKubeconfig, err := clientConfig.RawConfig()
func getCurrentCluster(clientConfig clientcmd.ClientConfig, currentContextNameOverride string) (*clusterInfo, error) {
currentKubeConfig, err := clientConfig.RawConfig()
if err != nil {
return nil, err
}
contextName := currentKubeConfig.CurrentContext
if len(currentContextNameOverride) > 0 {
contextName = currentContextNameOverride
}
unknownClusterInfo := &clusterInfo{name: "???", url: "???"}
context, ok := currentKubeconfig.Contexts[currentKubeconfig.CurrentContext]
ctx, ok := currentKubeConfig.Contexts[contextName]
if !ok {
return unknownClusterInfo, nil
}
cluster, ok := currentKubeconfig.Clusters[context.Cluster]
cluster, ok := currentKubeConfig.Clusters[ctx.Cluster]
if !ok {
return unknownClusterInfo, nil
}
return &clusterInfo{name: context.Cluster, url: cluster.Server}, nil
return &clusterInfo{name: ctx.Cluster, url: cluster.Server}, nil
}
func writeWhoamiOutput(output io.Writer, flags *whoamiFlags, cInfo *clusterInfo, whoAmI *identityv1alpha1.WhoAmIRequest) error {

View File

@ -205,6 +205,60 @@ func TestWhoami(t *testing.T) {
wantError: true,
wantStderr: "Error: could not get current cluster info: stat this-file-does-not-exist: no such file or directory\n",
},
{
name: "different kubeconfig context, but same as current",
args: []string{
"--kubeconfig", "./testdata/kubeconfig.yaml",
"--kubeconfig-context", "kind-kind",
},
wantStdout: here.Doc(`
Current cluster info:
Name: kind-kind
URL: https://fake-server-url-value
Current user info:
Username: some-username
Groups: some-group-0, some-group-1
`),
},
{
name: "different kubeconfig context, not current",
args: []string{
"--kubeconfig", "./testdata/kubeconfig.yaml",
"--kubeconfig-context", "some-other-context",
},
wantStdout: here.Doc(`
Current cluster info:
Name: some-other-cluster
URL: https://some-other-fake-server-url-value
Current user info:
Username: some-username
Groups: some-group-0, some-group-1
`),
},
{
name: "invalid kubeconfig context prints ???",
args: []string{
"--kubeconfig", "./testdata/kubeconfig.yaml",
"--kubeconfig-context", "invalid",
},
wantStdout: here.Doc(`
Current cluster info:
Name: ???
URL: ???
Current user info:
Username: some-username
Groups: some-group-0, some-group-1
`),
},
{
name: "getting clientset fails",
gettingClientsetErr: constable.Error("some get clientset error"),