Skip to content

Commit

Permalink
✨ Add --context flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 13, 2022
1 parent 1e8b7f6 commit 0adb671
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func init() {
cobra.OnInitialize(initLog)

flags.Kubeconfig(Command)
flags.Context(Command)
flags.Namespace(Command)
flags.Grammar(Command)
flags.Pod(Command)
Expand Down
24 changes: 24 additions & 0 deletions internal/config/flags/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ func Kubeconfig(cmd *cobra.Command) {
cmd.PersistentFlags().String("kubeconfig", kubeconfigDefault, "absolute path to the kubeconfig file")
}

func Context(cmd *cobra.Command) {
cmd.PersistentFlags().String("context", "", "name of the kubeconfig context to use")
err := cmd.RegisterFlagCompletionFunc(
"context",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
panic(err)
}
conf, err := kubernetes.RawConfig(kubeconfig)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
names := make([]string, 0, len(conf.Contexts))
for name, _ := range conf.Contexts {
names = append(names, name)
}
return names, cobra.ShellCompDirectiveNoFileComp
})
if err != nil {
panic(err)
}
}

func Namespace(cmd *cobra.Command) {
cmd.PersistentFlags().StringP("namespace", "n", "", "the namespace scope for this CLI request")
err := cmd.RegisterFlagCompletionFunc(
Expand Down
26 changes: 23 additions & 3 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)

type KubeClient struct {
Expand All @@ -26,9 +27,16 @@ func (client KubeClient) Secrets() v1.SecretInterface {
return client.ClientSet.CoreV1().Secrets(client.Namespace)
}

func NewClient(kubeconfigPath string, namespace string) (config KubeClient, err error) {
func NewClient(kubeconfigPath, context, namespace string) (config KubeClient, err error) {
var overrides *clientcmd.ConfigOverrides
if context != "" {
overrides = &clientcmd.ConfigOverrides{CurrentContext: context}
}

configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, nil)
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
overrides,
)

config.ClientConfig, err = configLoader.ClientConfig()
if err != nil {
Expand Down Expand Up @@ -58,10 +66,22 @@ func NewClientFromCmd(cmd *cobra.Command) (KubeClient, error) {
panic(err)
}

context, err := cmd.Flags().GetString("context")
if err != nil {
panic(err)
}

namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
panic(err)
}

return NewClient(kubeconfig, namespace)
return NewClient(kubeconfig, context, namespace)
}

func RawConfig(kubeconfigPath string) (api.Config, error) {
configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, nil,
)
return configLoader.RawConfig()
}

0 comments on commit 0adb671

Please sign in to comment.