From 078e77fe97c8623a58bb62d60a7fca7940ab10d5 Mon Sep 17 00:00:00 2001 From: David Collom Date: Mon, 19 Jul 2021 04:10:52 +0100 Subject: [PATCH] Adding PodDisruptionBudget support (#5) * Adding PodDisruptionBudget support * Update plugin description * Switch to print string as we can have Ints or Percentiles --- cmd/plugin/cli/root.go | 6 ++--- pkg/plugin/plugin.go | 47 ++++++++++++++++++++++++++++++++++++-- pkg/select-pod/template.go | 3 ++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/cmd/plugin/cli/root.go b/cmd/plugin/cli/root.go index 9c95766..7fd4494 100644 --- a/cmd/plugin/cli/root.go +++ b/cmd/plugin/cli/root.go @@ -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, @@ -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 `) } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index f5f7e37..4a4558f 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -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" @@ -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" @@ -42,6 +45,7 @@ type AllInfo struct { ConfigMapList *v1.ConfigMapList SecretList *v1.SecretList Hpa *autov1.HorizontalPodAutoscaler + Pdbs []*policyv1beta1.PodDisruptionBudget Workload Workload } @@ -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 @@ -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) @@ -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 } diff --git a/pkg/select-pod/template.go b/pkg/select-pod/template.go index f0fd160..ef6ddcd 100644 --- a/pkg/select-pod/template.go +++ b/pkg/select-pod/template.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/manifoldco/promptui" ) + var podTemplate = &promptui.SelectTemplates{ Label: "{{ . }}", Active: fmt.Sprintf("%s {{ .Name | cyan }}", promptui.IconSelect), @@ -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 }}`, -} \ No newline at end of file +}