Skip to content

Commit

Permalink
Adding PodDisruptionBudget support (#5)
Browse files Browse the repository at this point in the history
* Adding PodDisruptionBudget support

* Update plugin description

* Switch to print string as we can have Ints or Percentiles
  • Loading branch information
davidcollom authored Jul 19, 2021
1 parent c66b6e7 commit 078e77f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/plugin/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RootCmd() *cobra.Command {
# Interactive operation
$ kubectl pod-lens
# Show pod-related resources
$ kubectl pod-lens prometheus-prometheus-operator-prometheus-0
$ kubectl pod-lens prometheus-prometheus-operator-prometheus-0
`,
SilenceErrors: true,
SilenceUsage: true,
Expand Down Expand Up @@ -115,8 +115,8 @@ func printLogo() string {
{{| $$ }}::white
{{|__/ }}::white
Find related {{workloads}}::green|underline, {{namespace}}::green|underline, {{node}}::green|underline, {{service}}::green|underline, {{configmap}}::green|underline, {{secret}}::green|underline,
{{ingress}}::green|underline {{PVC}}::green|underline and {{HPA}}::green|underline by {{pod name}}::lightRed and display them in a {{tree}}::lightBlue and {{table}}::lightBlue.
Find related {{workloads}}::green|underline, {{namespace}}::green|underline, {{node}}::green|underline, {{service}}::green|underline, {{configmap}}::green|underline, {{secret}}::green|underline,
{{ingress}}::green|underline, {{PVC}}::green|underline, {{HPA}}::green|underline and {{PDB}}::green|underline by {{pod name}}::lightRed and display them in a {{tree}}::lightBlue and {{table}}::lightBlue.
Find more information at: {{https://pod-lens.guoxudong.io/}}::lightMagenta|underline
`)
}
47 changes: 45 additions & 2 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package plugin

import (
"fmt"
select_pod "github.com/sunny0826/kubectl-pod-lens/pkg/select-pod"
"k8s.io/klog"
"os"
"regexp"
"strings"

select_pod "github.com/sunny0826/kubectl-pod-lens/pkg/select-pod"
"k8s.io/klog"

mapset "github.com/deckarep/golang-set"
"github.com/gosuri/uitable"

Expand All @@ -18,7 +19,9 @@ import (
autov1 "k8s.io/api/autoscaling/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -42,6 +45,7 @@ type AllInfo struct {
ConfigMapList *v1.ConfigMapList
SecretList *v1.SecretList
Hpa *autov1.HorizontalPodAutoscaler
Pdbs []*policyv1beta1.PodDisruptionBudget
Workload Workload
}

Expand Down Expand Up @@ -303,6 +307,25 @@ func (sf *SnifferPlugin) findHpaByName(namespace string) error {
return nil
}

func (sf *SnifferPlugin) findPdbByName(namespace string) error {

pdbFind, err := sf.Clientset.PolicyV1beta1().PodDisruptionBudgets(namespace).List(
metav1.ListOptions{})
if err != nil {
return err
}
for _, pdb := range pdbFind.Items {
selector, err := metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
if err != nil {
return err
}
if selector.Empty() || selector.Matches(labels.Set(sf.PodObject.Labels)) {
sf.AllInfo.Pdbs = append(sf.AllInfo.Pdbs, &pdb)
}
}
return nil
}

func (sf *SnifferPlugin) printPodLeveledList() error {
var leveledList pterm.LeveledList
var stateList string
Expand Down Expand Up @@ -563,6 +586,22 @@ func (sf *SnifferPlugin) printResource() error {
table.AddRow("---", "---")
}

for _, pdb := range sf.AllInfo.Pdbs {
table.AddRow("Kind:", cfmt.Sprintf("{{PDB}}::cyan"))
table.AddRow("Name:", pdb.Name)
if pdb.Spec.MinAvailable != nil {
table.AddRow("MinAvailable:", cfmt.Sprintf("{{%s}}::lightGreen",
pdb.Spec.MinAvailable))
}
if pdb.Spec.MaxUnavailable != nil {
table.AddRow("MaxAvailable:", cfmt.Sprintf("{{%s}}::lightGreen",
pdb.Spec.MaxUnavailable))
}
table.AddRow("Disruptions:", cfmt.Sprintf("{{%d}}::lightGreen",
pdb.Status.PodDisruptionsAllowed))
table.AddRow("---", "---")
}

if len(table.Rows) > 1 {
_, _ = cfmt.Println("{{ Related Resources }}::bgCyan|#ffffff")
fmt.Println(table)
Expand Down Expand Up @@ -639,6 +678,10 @@ func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string,
return err
}

if err = sf.findPdbByName(sf.PodObject.Namespace); err != nil {
return err
}

if err = sf.printResource(); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/select-pod/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/manifoldco/promptui"
)

var podTemplate = &promptui.SelectTemplates{
Label: "{{ . }}",
Active: fmt.Sprintf("%s {{ .Name | cyan }}", promptui.IconSelect),
Expand All @@ -15,4 +16,4 @@ var podTemplate = &promptui.SelectTemplates{
{{ "Node:" | faint }} {{ .Spec.NodeName | yellow }}
{{if ne .Status.Phase "Running"}}{{ "Status:" | faint }} {{ .Status.Phase | red }}{{else}}{{ "Status:" | faint }} {{ .Status.Phase | green }}{{end}}
{{ "Pod IP:" | faint }} {{ .Status.PodIP | yellow }}`,
}
}

0 comments on commit 078e77f

Please sign in to comment.