Skip to content

Commit

Permalink
Merge pull request #1520 from sttts/sttts-cli-short
Browse files Browse the repository at this point in the history
cli: promote '--short' option to be usable also in 'kubectl ws --short' syntax
  • Loading branch information
openshift-ci[bot] authored Jul 14, 2022
2 parents b42b037 + d642357 commit 7de8978
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
4 changes: 1 addition & 3 deletions pkg/cliplugins/workspace/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
},
}

var shortWorkspaceOutput bool
currentCmd := &cobra.Command{
Use: "current [--short]",
Short: "Print the current workspace. Same as 'kubectl ws .'.",
Expand All @@ -129,10 +128,9 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
if len(args) != 0 {
return cmd.Help()
}
return kubeconfig.CurrentWorkspace(c.Context(), shortWorkspaceOutput)
return kubeconfig.CurrentWorkspace(c.Context())
},
}
currentCmd.Flags().BoolVar(&shortWorkspaceOutput, "short", shortWorkspaceOutput, "Print only the name of the workspace, e.g. for integration into the shell prompt")

listCmd := &cobra.Command{
Use: "list",
Expand Down
30 changes: 16 additions & 14 deletions pkg/cliplugins/workspace/plugin/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ const (
// and allows modifications on it through workspace-related
// actions
type KubeConfig struct {
startingConfig *clientcmdapi.Config
overrides *clientcmd.ConfigOverrides
currentContext string // including override
startingConfig *clientcmdapi.Config
overrides *clientcmd.ConfigOverrides
currentContext string // including override
shortWorkspaceOutput bool

clusterClient tenancyclient.ClusterInterface
personalClient tenancyclient.ClusterInterface
Expand Down Expand Up @@ -97,9 +98,10 @@ func NewKubeConfig(opts *Options) (*KubeConfig, error) {
}

return &KubeConfig{
startingConfig: startingConfig,
overrides: opts.KubectlOverrides,
currentContext: currentContext,
startingConfig: startingConfig,
overrides: opts.KubectlOverrides,
currentContext: currentContext,
shortWorkspaceOutput: opts.ShortWorkspaceOutput,

clusterClient: clusterClient,
personalClient: &personalClusterClient{clusterConfig},
Expand Down Expand Up @@ -160,7 +162,7 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
return err
}

return kc.currentWorkspace(ctx, newKubeConfig.Clusters[newKubeConfig.Contexts[kcpCurrentWorkspaceContextKey].Cluster].Server, nil, false)
return kc.currentWorkspace(ctx, newKubeConfig.Clusters[newKubeConfig.Contexts[kcpCurrentWorkspaceContextKey].Cluster].Server, nil)

case "..":
config, err := clientcmd.NewDefaultClientConfig(*kc.startingConfig, kc.overrides).ClientConfig()
Expand Down Expand Up @@ -197,7 +199,7 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
newServerHost = homeWorkspace.Status.URL

case ".":
return kc.CurrentWorkspace(ctx, false)
return kc.CurrentWorkspace(ctx)

default:
cluster := logicalcluster.New(name)
Expand Down Expand Up @@ -268,30 +270,30 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
return err
}

return kc.currentWorkspace(ctx, newServerHost, workspaceType, false)
return kc.currentWorkspace(ctx, newServerHost, workspaceType)
}

// CurrentWorkspace outputs the current workspace.
func (kc *KubeConfig) CurrentWorkspace(ctx context.Context, shortWorkspaceOutput bool) error {
func (kc *KubeConfig) CurrentWorkspace(ctx context.Context) error {
config, err := clientcmd.NewDefaultClientConfig(*kc.startingConfig, kc.overrides).ClientConfig()
if err != nil {
return err
}

return kc.currentWorkspace(ctx, config.Host, nil, shortWorkspaceOutput)
return kc.currentWorkspace(ctx, config.Host, nil)
}

func (kc *KubeConfig) currentWorkspace(ctx context.Context, host string, workspaceType *tenancyv1alpha1.ClusterWorkspaceTypeReference, shortWorkspaceOutput bool) error {
func (kc *KubeConfig) currentWorkspace(ctx context.Context, host string, workspaceType *tenancyv1alpha1.ClusterWorkspaceTypeReference) error {
_, clusterName, err := pluginhelpers.ParseClusterURL(host)
if err != nil {
if shortWorkspaceOutput {
if kc.shortWorkspaceOutput {
return nil
}
_, err = fmt.Fprintf(kc.Out, "Current workspace is the URL %q.\n", host)
return err
}

if shortWorkspaceOutput {
if kc.shortWorkspaceOutput {
fmt.Fprintf(kc.Out, "%s\n", clusterName) // nolint: errcheck
return nil
}
Expand Down
31 changes: 29 additions & 2 deletions pkg/cliplugins/workspace/plugin/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func TestUse(t *testing.T) {
existingObjects map[logicalcluster.Name][]string
prettyNames map[logicalcluster.Name]map[string]string
unready map[logicalcluster.Name]map[string]bool // unready workspaces
short bool

param string

Expand Down Expand Up @@ -324,6 +325,31 @@ func TestUse(t *testing.T) {
},
wantStdout: []string{"Current workspace is \"root:foo:bar\""},
},
{
name: "workspace name, short output",
config: clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
Contexts: map[string]*clientcmdapi.Context{"workspace.kcp.dev/current": {Cluster: "workspace.kcp.dev/current", AuthInfo: "test"}},
Clusters: map[string]*clientcmdapi.Cluster{"workspace.kcp.dev/current": {Server: "https://test/clusters/root:foo"}},
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
},
existingObjects: map[logicalcluster.Name][]string{
logicalcluster.New("root:foo"): {"bar"},
},
param: "bar",
short: true,
expected: &clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
Contexts: map[string]*clientcmdapi.Context{
"workspace.kcp.dev/current": {Cluster: "workspace.kcp.dev/current", AuthInfo: "test"},
"workspace.kcp.dev/previous": {Cluster: "workspace.kcp.dev/previous", AuthInfo: "test"},
},
Clusters: map[string]*clientcmdapi.Cluster{
"workspace.kcp.dev/current": {Server: "https://test/clusters/root:foo:bar"},
"workspace.kcp.dev/previous": {Server: "https://test/clusters/root:foo"},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
},
wantStdout: []string{"root:foo:bar"},
},
{
name: "workspace name, no cluster URL",
config: clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
Expand Down Expand Up @@ -765,8 +791,9 @@ func TestUse(t *testing.T) {
streams, _, stdout, stderr := genericclioptions.NewTestIOStreams()

kc := &KubeConfig{
startingConfig: tt.config.DeepCopy(),
currentContext: tt.config.CurrentContext,
startingConfig: tt.config.DeepCopy(),
currentContext: tt.config.CurrentContext,
shortWorkspaceOutput: tt.short,

clusterClient: fakeTenancyClient{
t: t,
Expand Down
7 changes: 5 additions & 2 deletions pkg/cliplugins/workspace/plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
// Options provides options that will drive the update of the current context
// on a user's KUBECONFIG based on actions done on KCP workspaces
type Options struct {
KubectlOverrides *clientcmd.ConfigOverrides
Scope string
KubectlOverrides *clientcmd.ConfigOverrides
Scope string
ShortWorkspaceOutput bool

genericclioptions.IOStreams
}
Expand Down Expand Up @@ -63,6 +64,8 @@ func (o *Options) BindFlags(cmd *cobra.Command) {

cmd.PersistentFlags().StringVar(&o.Scope, "scope", o.Scope, `The 'personal' scope shows only the workspaces you personally own, with the name you gave them at creation.
The 'all' scope returns all the workspaces you are allowed to see in the organization, with the disambiguated names they have inside the whole organization.`)

cmd.PersistentFlags().BoolVar(&o.ShortWorkspaceOutput, "short", o.ShortWorkspaceOutput, "Print only the name of the workspace, e.g. for integration into the shell prompt")
}

func (o *Options) Validate() error {
Expand Down

0 comments on commit 7de8978

Please sign in to comment.