Skip to content

Commit

Permalink
add labelselector
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Mar 23, 2021
1 parent 6390565 commit 20bb61e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 3 additions & 1 deletion cmd/plugin/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var (
KubernetesConfigFlags *genericclioptions.ConfigFlags
allNamespacesFlag bool
labelFlag string
)

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

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

Expand All @@ -60,6 +61,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")
cmd.Flags().StringVarP(&labelFlag, "selector", "l", "", "Selector (label query) to filter on, only supports '=' and a parameter (e.g. -l key1=value1)")

klog.InitFlags(nil)
cmd.Flags().AddGoFlagSet(flag.CommandLine)
Expand Down
10 changes: 8 additions & 2 deletions doc/en/more-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ title: More Info
kubectl pod-lens
```

## Show pod-related resources
## Show pod-related resources

```shell
kubectl pod-lens <pod-name>
```
```

## Assign LabelSelector

```shell
kubectl pod-lens <pod-name> -l app=demo
```
12 changes: 9 additions & 3 deletions doc/zh/pod-lens.md → doc/zh/more-info.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Pod Lens
title: 更多信息
---

[![asciicast](https://asciinema.org/a/400180.svg)](https://asciinema.org/a/400180)
Expand All @@ -10,8 +10,14 @@ title: Pod Lens
kubectl pod-lens
```

## 展示 Pod 相关 K8S 资源
## 展示 Pod 相关 K8S 资源

```shell
kubectl pod-lens <pod-name>
```
```

## 指定 LabelSelector

```shell
kubectl pod-lens <pod-name> -l app=demo
```
31 changes: 22 additions & 9 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
select_pod "github.com/sunny0826/kubectl-pod-lens/pkg/select-pod"
"k8s.io/klog"
"os"
"regexp"
"strings"

mapset "github.com/deckarep/golang-set"
Expand Down Expand Up @@ -103,13 +104,25 @@ func (sf *SnifferPlugin) findNodeByName() error {
return nil
}

func (sf *SnifferPlugin) getLabelByPod() error {
func (sf *SnifferPlugin) getLabelByPod(labelFlag string) error {
if labelFlag != "" {
match, err := regexp.MatchString("[a-z0-9/-]+=([a-z0-9/-]+)( |$)", labelFlag)
if err != nil {
return err
}
if match {
sf.LabelSelector = labelFlag
return nil
} else {
return errors.New(labelFlag + " is incorrectly formatted.")
}
}
var labelSelector string
labels := sf.PodObject.Labels
if _, ok := labels["release"]; ok {
labelSelector = "release=" + labels["release"]
} else if _, ok = labels["app"]; ok {
if _, ok := labels["app"]; ok {
labelSelector = "app=" + labels["app"]
} else if _, ok = labels["release"]; ok {
labelSelector = "release=" + labels["release"]
} else if _, ok = labels["k8s-app"]; ok {
labelSelector = "k8s-app=" + labels["k8s-app"]
} else if _, ok = labels["app.kubernetes.io/name"]; ok {
Expand Down Expand Up @@ -557,7 +570,7 @@ func (sf *SnifferPlugin) printResource() error {
return nil
}

func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string, allNamespacesFlag bool) error {
func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string, allNamespacesFlag bool, labelFlag string) error {
klog.V(1).Info("start run plugins")
sf, err := NewSnifferPlugin(configFlags)
if err != nil {
Expand All @@ -570,23 +583,23 @@ func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string,
namespace = getNamespace(configFlags)
}

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

if err := sf.findNodeByName(); err != nil {
if err = sf.findNodeByName(); err != nil {
return err
}

if err := sf.getOwnerByPod(); err != nil {
if err = sf.getOwnerByPod(); err != nil {
return err
}

if err = sf.printPodLeveledList(); err != nil {
return err
}

if err = sf.getLabelByPod(); err != nil {
if err = sf.getLabelByPod(labelFlag); err != nil {
return err
}

Expand Down

0 comments on commit 20bb61e

Please sign in to comment.