From 2b78acf3e5e7d03bb67738c901a28e6f935769ef Mon Sep 17 00:00:00 2001 From: Bryce Palmer Date: Tue, 14 Nov 2023 11:46:46 -0500 Subject: [PATCH] (feature): Add namespace and label selector filtering to table panel (#23) Signed-off-by: Bryce Palmer --- pkg/paneler/table.go | 16 +++++++- pkg/types/panels.go | 4 +- test.json | 87 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/pkg/paneler/table.go b/pkg/paneler/table.go index b1bc4ee..e354d73 100644 --- a/pkg/paneler/table.go +++ b/pkg/paneler/table.go @@ -11,7 +11,9 @@ import ( "github.com/everettraven/buoy/pkg/charm/styles" buoytypes "github.com/everettraven/buoy/pkg/types" "k8s.io/apimachinery/pkg/api/meta" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" @@ -78,7 +80,6 @@ func (t *Table) modelWrapperForTablePanel(tablePanel buoytypes.Table) *panels.Ta func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table) error { // create informer and event handler - infFact := dynamicinformer.NewDynamicSharedInformerFactory(t.dynamicClient, 1*time.Minute) gvk := schema.GroupVersionKind{ Group: tablePanel.Group, Version: tablePanel.Version, @@ -88,6 +89,19 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table if err != nil { return fmt.Errorf("error creating resource mapping: %w", err) } + ns := tablePanel.Namespace + if mapping.Scope.Name() == meta.RESTScopeNameRoot { + ns = "" + } + infFact := dynamicinformer.NewFilteredDynamicSharedInformerFactory( + t.dynamicClient, + 1*time.Minute, + ns, + dynamicinformer.TweakListOptionsFunc(func(options *v1.ListOptions) { + ls := labels.SelectorFromSet(tablePanel.LabelSelector) + options.LabelSelector = ls.String() + }), + ) inf := infFact.ForResource(mapping.Resource) _, err = inf.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ diff --git a/pkg/types/panels.go b/pkg/types/panels.go index 49804f9..03e33d5 100644 --- a/pkg/types/panels.go +++ b/pkg/types/panels.go @@ -37,7 +37,9 @@ func (p *Panel) UnmarshalJSON(data []byte) error { type Table struct { PanelBase - Columns []Column `json:"columns"` + Columns []Column `json:"columns"` + Namespace string `json:"namespace"` + LabelSelector map[string]string `json:"labelSelector"` } type Column struct { diff --git a/test.json b/test.json index c99bacd..0910727 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { "panels": [ { - "name": "Pods", + "name": "All Pods", "group": "", "version": "v1", "kind": "Pod", @@ -33,6 +33,91 @@ } ] }, + { + "name": "Kube-System Pods", + "group": "", + "version": "v1", + "kind": "Pod", + "type": "table", + "namespace": "kube-system", + "columns": [ + { + "header": "Namespace", + "path": "metadata.namespace" + }, + { + "header": "Name", + "path": "metadata.name" + }, + { + "header": "Phase", + "path": "status.phase" + }, + { + "header": "PodIP", + "path": "status.podIP" + }, + { + "header": "Start Time", + "path": "status.startTime" + }, + { + "header": "UID", + "path": "metadata.uid" + } + ] + }, + { + "name": "Kube-System Pods with label tier=control-plane", + "group": "", + "version": "v1", + "kind": "Pod", + "type": "table", + "namespace": "kube-system", + "labelSelector": { + "tier": "control-plane" + }, + "columns": [ + { + "header": "Namespace", + "path": "metadata.namespace" + }, + { + "header": "Name", + "path": "metadata.name" + }, + { + "header": "Phase", + "path": "status.phase" + }, + { + "header": "PodIP", + "path": "status.podIP" + }, + { + "header": "Start Time", + "path": "status.startTime" + }, + { + "header": "UID", + "path": "metadata.uid" + } + ] + }, + { + "name": "ClusterRoles", + "group": "rbac.authorization.k8s.io", + "version": "v1", + "kind": "ClusterRole", + "type": "table", + "namespace": "kube-system", + "columns": [ + { + "header": "Name", + "path": "metadata.name" + } + ] + }, { "name": "Deployments", "group": "apps",