From 7d1b2c5f6f3829f748389ebd198f289c7293c5b4 Mon Sep 17 00:00:00 2001 From: Phil Dibowitz Date: Tue, 5 Mar 2024 18:25:50 -0800 Subject: [PATCH] Add --context support Signed-off-by: Phil Dibowitz --- cmd/delete.go | 8 ++++---- cmd/describe.go | 8 ++++---- cmd/dsh.go | 12 ++++++++---- cmd/get.go | 8 ++++---- cmd/log.go | 10 +++++----- cmd/util.go | 6 ++++-- 6 files changed, 29 insertions(+), 23 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index d2ee515..e13d099 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -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, @@ -21,7 +21,7 @@ func newDshDeleteCommand( Short: "delete pods for ", 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) }, } @@ -29,9 +29,9 @@ func newDshDeleteCommand( } 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 } diff --git a/cmd/describe.go b/cmd/describe.go index 61a1ee2..3ea365d 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -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, @@ -29,7 +29,7 @@ func newDshDescribeCommand( if len(args) == 1 { ds = args[0] } - return dshDescribe.describePods(*namespace, ds, *nodeName) + return dshDescribe.describePods(*context, *namespace, ds, *nodeName) }, } @@ -37,9 +37,9 @@ func newDshDescribeCommand( } 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 } diff --git a/cmd/dsh.go b/cmd/dsh.go index 0c22542..2a02424 100644 --- a/cmd/dsh.go +++ b/cmd/dsh.go @@ -12,6 +12,7 @@ type dshCmd struct { } func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command { + var context string var namespace string var nodeName string @@ -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", ) @@ -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 } diff --git a/cmd/get.go b/cmd/get.go index fa0f218..b8db163 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -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 @@ -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) }, } @@ -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 } diff --git a/cmd/log.go b/cmd/log.go index 8822d12..20299c4 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -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 @@ -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, ) }, } @@ -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 } diff --git a/cmd/util.go b/cmd/util.go index b57f478..c77707f 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -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, )