Skip to content

Commit

Permalink
Merge pull request #11 from jlandowner/support-context
Browse files Browse the repository at this point in the history
Support context
  • Loading branch information
jlandowner authored Apr 27, 2021
2 parents 6d73a5a + 80f3ee2 commit 5405c75
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (

RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
k8sclient, err := client.NewClient(&kubeconfigPath)
k8sclient, err := client.NewClient(&kubeconfigPath, &kubecontext)
if err != nil {
return fmt.Errorf("Failed to load kubeconfig %v: %v", kubeconfigPath, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
PersistentPreRunE: c.PreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
k8sclient, err := client.NewClient(&kubeconfigPath)
k8sclient, err := client.NewClient(&kubeconfigPath, &kubecontext)
if err != nil {
return fmt.Errorf("Failed to load kubeconfig %v: %v", kubeconfigPath, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (

RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
k8sclient, err := client.NewClient(&kubeconfigPath)
k8sclient, err := client.NewClient(&kubeconfigPath, &kubecontext)
if err != nil {
return fmt.Errorf("Failed to load kubeconfig %v: %v", kubeconfigPath, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
PersistentPreRunE: l.PreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
k8sclient, err := client.NewClient(&kubeconfigPath)
k8sclient, err := client.NewClient(&kubeconfigPath, &kubecontext)
if err != nil {
return fmt.Errorf("Failed to load kubeconfig %v: %v", kubeconfigPath, err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var (
kubeconfigPath string
kubecontext string

rootCmd = &cobra.Command{
Use: "psp-util",
Expand All @@ -41,7 +42,8 @@ Apache License Version 2.0 Copyright 2020 jlandowner
)

func init() {
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig", "", "kube config file (default is $HOME/.kube/config)")
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig", "", "kubeconfig file path (default: $HOME/.kube/config)")
rootCmd.PersistentFlags().StringVar(&kubecontext, "context", "", "kube-context (default: current context)")
}

func Execute() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var treeCmd = &cobra.Command{
Short: "View a relational tree between PSP and Subjects in cluster",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
k8sclient, err := client.NewClient(&kubeconfigPath)
k8sclient, err := client.NewClient(&kubeconfigPath, &kubecontext)
if err != nil {
return fmt.Errorf("Failed to load kubeconfig %v: %v", kubeconfigPath, err.Error())
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ import (
)

// NewClient returns kubernetes Clientset
func NewClient(kubeconfigPath *string) (*kubernetes.Clientset, error) {
func NewClient(kubeconfigPath *string, kubecontext *string) (*kubernetes.Clientset, error) {
if *kubeconfigPath == "" {
*kubeconfigPath = filepath.Join(homeDir(), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfigPath)
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfigPath},
&clientcmd.ConfigOverrides{CurrentContext: *kubecontext}).ClientConfig()

if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}

Expand Down

0 comments on commit 5405c75

Please sign in to comment.