Skip to content

Commit

Permalink
(feature): Add namespace and label selector filtering to table panel (#…
Browse files Browse the repository at this point in the history
…23)

Signed-off-by: Bryce Palmer <everettraven@gmail.com>
  • Loading branch information
everettraven committed Nov 14, 2023
1 parent 46f80c7 commit 2b78acf
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
16 changes: 15 additions & 1 deletion pkg/paneler/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down
4 changes: 3 additions & 1 deletion pkg/types/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
87 changes: 86 additions & 1 deletion test.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"panels": [
{
"name": "Pods",
"name": "All Pods",
"group": "",
"version": "v1",
"kind": "Pod",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 2b78acf

Please sign in to comment.