Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feature): Add namespace and label selector filtering to table panel #23

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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