Skip to content

Commit

Permalink
add flag -A
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Mar 16, 2021
1 parent 062392b commit 4ed9796
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/plugin/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var (
KubernetesConfigFlags *genericclioptions.ConfigFlags
allNamespacesFlag bool
)

func RootCmd() *cobra.Command {
Expand All @@ -43,7 +44,7 @@ $ kubectl pod-lens prometheus-prometheus-operator-prometheus-0
argsChannel := make(chan string, 1)
argsChannel <- podName

if err := plugin.RunPlugin(KubernetesConfigFlags, argsChannel); err != nil {
if err := plugin.RunPlugin(KubernetesConfigFlags, argsChannel, allNamespacesFlag); err != nil {
return errors.Cause(err)
}

Expand All @@ -55,6 +56,7 @@ $ kubectl pod-lens prometheus-prometheus-operator-prometheus-0

KubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
KubernetesConfigFlags.AddFlags(cmd.Flags())
cmd.Flags().BoolVarP(&allNamespacesFlag, "all-namespaces", "A", false, "query all objects in all API groups, both namespaced and non-namespaced")

klog.InitFlags(nil)
_ = cmd.Flags().MarkHidden("as-group")
Expand Down
22 changes: 19 additions & 3 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewSnifferPlugin(configFlags *genericclioptions.ConfigFlags) (*SnifferPlugi
}, nil
}

func (sf *SnifferPlugin) findPodByName(name string, namespace string) error {
func (sf *SnifferPlugin) findPodByName(name, namespace string) error {
pods, err := sf.Clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil || len(pods.Items) == 0 {
return errors.New("Failed to get pod: [" +
Expand Down Expand Up @@ -556,15 +556,19 @@ func (sf *SnifferPlugin) printResource() error {
return nil
}

func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string) error {
func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string, allNamespacesFlag bool) error {
sf, err := NewSnifferPlugin(configFlags)
if err != nil {
return err
}

podName := <-outputCh
var namespace string
if !allNamespacesFlag {
namespace = getNamespace(configFlags)
}

if err := sf.findPodByName(podName, *configFlags.Namespace); err != nil {
if err := sf.findPodByName(podName, namespace); err != nil {
return err
}

Expand Down Expand Up @@ -626,3 +630,15 @@ func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string)

return nil
}

func getNamespace(configFlags *genericclioptions.ConfigFlags) string {
if v := *configFlags.Namespace; v != "" {
return v
}
clientConfig := configFlags.ToRawKubeConfigLoader()
defaultNamespace, _, err := clientConfig.Namespace()
if err != nil {
defaultNamespace = "default"
}
return defaultNamespace
}

0 comments on commit 4ed9796

Please sign in to comment.