pinniped whoami: print correct cluster info when --kubeconfig-context is used
Signed-off-by: Monis Khan <mok@vmware.com>
This commit is contained in:
parent
50e4531215
commit
964d4889c4
@ -78,7 +78,7 @@ func runWhoami(output io.Writer, getClientset getConciergeClientsetFunc, flags *
|
|||||||
return fmt.Errorf("could not configure Kubernetes client: %w", err)
|
return fmt.Errorf("could not configure Kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
clusterInfo, err := getCurrentCluster(clientConfig)
|
clusterInfo, err := getCurrentCluster(clientConfig, flags.kubeconfigContextOverride)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get current cluster info: %w", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentCluster(clientConfig clientcmd.ClientConfig) (*clusterInfo, error) {
|
func getCurrentCluster(clientConfig clientcmd.ClientConfig, currentContextNameOverride string) (*clusterInfo, error) {
|
||||||
currentKubeconfig, err := clientConfig.RawConfig()
|
currentKubeConfig, err := clientConfig.RawConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contextName := currentKubeConfig.CurrentContext
|
||||||
|
if len(currentContextNameOverride) > 0 {
|
||||||
|
contextName = currentContextNameOverride
|
||||||
|
}
|
||||||
|
|
||||||
unknownClusterInfo := &clusterInfo{name: "???", url: "???"}
|
unknownClusterInfo := &clusterInfo{name: "???", url: "???"}
|
||||||
context, ok := currentKubeconfig.Contexts[currentKubeconfig.CurrentContext]
|
ctx, ok := currentKubeConfig.Contexts[contextName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return unknownClusterInfo, nil
|
return unknownClusterInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster, ok := currentKubeconfig.Clusters[context.Cluster]
|
cluster, ok := currentKubeConfig.Clusters[ctx.Cluster]
|
||||||
if !ok {
|
if !ok {
|
||||||
return unknownClusterInfo, nil
|
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 {
|
func writeWhoamiOutput(output io.Writer, flags *whoamiFlags, cInfo *clusterInfo, whoAmI *identityv1alpha1.WhoAmIRequest) error {
|
||||||
|
@ -205,6 +205,60 @@ func TestWhoami(t *testing.T) {
|
|||||||
wantError: true,
|
wantError: true,
|
||||||
wantStderr: "Error: could not get current cluster info: stat this-file-does-not-exist: no such file or directory\n",
|
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",
|
name: "getting clientset fails",
|
||||||
gettingClientsetErr: constable.Error("some get clientset error"),
|
gettingClientsetErr: constable.Error("some get clientset error"),
|
||||||
|
Loading…
Reference in New Issue
Block a user