Skip to content

Commit

Permalink
Add --context support (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Dibowitz <[email protected]>
  • Loading branch information
jaymzh authored Mar 6, 2024
1 parent c4c2cd3 commit 546a558
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func newDshDeleteCommand(
out io.Writer, namespace *string, nodeName *string,
out io.Writer, context *string, namespace *string, nodeName *string,
) *cobra.Command {
dshDelete := &dshCmd{
out: out,
Expand All @@ -21,17 +21,17 @@ func newDshDeleteCommand(
Short: "delete pods for <ds>",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
return dshDelete.deletePods(*namespace, args[0], *nodeName)
return dshDelete.deletePods(*context, *namespace, args[0], *nodeName)
},
}

return cmd
}

func (sv *dshCmd) deletePods(
namespace string, ds string, nodeName string,
ccontext string, namespace string, ds string, nodeName string,
) error {
clientset, err := getClientSet()
clientset, err := getClientSet(ccontext)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func newDshDescribeCommand(
out io.Writer, namespace *string, nodeName *string,
out io.Writer, context *string, namespace *string, nodeName *string,
) *cobra.Command {
dshDescribe := &dshCmd{
out: out,
Expand All @@ -29,17 +29,17 @@ func newDshDescribeCommand(
if len(args) == 1 {
ds = args[0]
}
return dshDescribe.describePods(*namespace, ds, *nodeName)
return dshDescribe.describePods(*context, *namespace, ds, *nodeName)
},
}

return cmd
}

func (sv *dshCmd) describePods(
namespace string, ds string, nodeName string,
ccontext string, namespace string, ds string, nodeName string,
) error {
clientset, err := getClientSet()
clientset, err := getClientSet(ccontext)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/dsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type dshCmd struct {
}

func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
var context string
var namespace string
var nodeName string

Expand All @@ -24,6 +25,9 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
},
}

dshCmd.PersistentFlags().StringVarP(
&context, "context", "", "", "Context",
)
dshCmd.PersistentFlags().StringVarP(
&namespace, "namespace", "n", "default", "Namespace to look in",
)
Expand All @@ -32,9 +36,9 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
)

dshCmd.AddCommand(newVersionCommand(streams.Out))
dshCmd.AddCommand(newDshGetCommand(streams.Out, &namespace, &nodeName))
dshCmd.AddCommand(newDshDeleteCommand(streams.Out, &namespace, &nodeName))
dshCmd.AddCommand(newDshDescribeCommand(streams.Out, &namespace, &nodeName))
dshCmd.AddCommand(newDshLogCommand(streams.Out, &namespace, &nodeName))
dshCmd.AddCommand(newDshGetCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshDeleteCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshDescribeCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshLogCommand(streams.Out, &context, &namespace, &nodeName))
return dshCmd
}
8 changes: 4 additions & 4 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (


func newDshGetCommand(
out io.Writer, namespace *string, nodeName *string,
out io.Writer, context *string, namespace *string, nodeName *string,
) *cobra.Command {
var output string

Expand All @@ -34,7 +34,7 @@ func newDshGetCommand(
if len(args) == 1 {
ds = args[0]
}
return dshGet.getPods(*namespace, ds, *nodeName, output)
return dshGet.getPods(*context, *namespace, ds, *nodeName, output)
},
}

Expand All @@ -46,9 +46,9 @@ func newDshGetCommand(
}

func (sv *dshCmd) getPods(
namespace string, ds string, nodeName string, output string,
context string, namespace string, ds string, nodeName string, output string,
) error {
clientset, err := getClientSet()
clientset, err := getClientSet(context)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func newDshLogCommand(
out io.Writer, namespace *string, nodeName *string,
out io.Writer, context *string, namespace *string, nodeName *string,
) *cobra.Command {
var container string
var tail int
Expand All @@ -28,7 +28,7 @@ func newDshLogCommand(
Args: cobra.MatchAll(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
return dshLog.getLogs(
*namespace, args[0], *nodeName, container, follow, &tail,
*context, *namespace, args[0], *nodeName, container, follow, &tail,
)
},
}
Expand All @@ -48,10 +48,10 @@ func newDshLogCommand(
}

func (sv *dshCmd) getLogs(
namespace string, ds string, nodeName string, container string, follow bool,
lines *int,
ccontext string, namespace string, ds string, nodeName string, container string,
follow bool, lines *int,
) error {
clientset, err := getClientSet()
clientset, err := getClientSet(ccontext)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getClientSet() (*kubernetes.Clientset, error) {
func getClientSet(context string) (*kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
configOverrides := &clientcmd.ConfigOverrides{
CurrentContext: context,
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, configOverrides,
)
Expand Down

0 comments on commit 546a558

Please sign in to comment.