From 89a37a5907334af2e618abe5655a049bc3382810 Mon Sep 17 00:00:00 2001 From: Bryce Palmer Date: Wed, 15 Nov 2023 13:53:15 -0500 Subject: [PATCH] (feature): add support for reading dashboard configuration from YAML (#26) * (feature): add support for reading dashboard configuration from YAML Signed-off-by: Bryce Palmer * add test.yaml Signed-off-by: Bryce Palmer --------- Signed-off-by: Bryce Palmer --- README.md | 4 ++- internal/cli/root.go | 12 +++++-- pkg/types/panels.go | 30 ++++++++-------- pkg/types/types.go | 2 +- test.yaml | 84 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 test.yaml diff --git a/README.md b/README.md index 8611ab3..a8d7118 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,9 @@ kind create cluster The `test.json` file contains samples for each of the different panel types that `buoy` currently supports. As this is a hobby project very early in the development cycle there are some limitations and things are bound to not work as expected. -`buoy` uses https://github.com/tidwall/gjson for the path evaluation and extracting of values from resources. Please consult their documentation for valid path syntax +`test.yaml` results in the exact same dashboard as `test.json` and exists to show YAML support. + +`buoy` uses https://github.com/tidwall/gjson for the path evaluation and extracting of values from resources. Please consult their documentation for valid path syntax. ## Controls - `ctrl+c`, `q`, `esc` will quit the program and exit the tui diff --git a/internal/cli/root.go b/internal/cli/root.go index 872c723..df6ea95 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "path/filepath" tea "github.com/charmbracelet/bubbletea" "github.com/everettraven/buoy/pkg/charm/models" @@ -12,6 +13,7 @@ import ( "github.com/everettraven/buoy/pkg/types" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/yaml" ) var rootCommand = &cobra.Command{ @@ -28,13 +30,19 @@ func init() { } func run(file string) error { - rawJson, err := os.ReadFile(file) + ext := filepath.Ext(file) + raw, err := os.ReadFile(file) if err != nil { log.Fatalf("reading file: %s", err) } + fmt.Println(ext) dash := &types.Dashboard{} - err = json.Unmarshal(rawJson, dash) + if ext == ".yaml" { + err = yaml.Unmarshal(raw, dash) + } else { + err = json.Unmarshal(raw, dash) + } if err != nil { log.Fatalf("unmarshalling dashboard: %s", err) } diff --git a/pkg/types/panels.go b/pkg/types/panels.go index 03e33d5..73c6227 100644 --- a/pkg/types/panels.go +++ b/pkg/types/panels.go @@ -13,16 +13,16 @@ const ( ) type PanelBase struct { - Name string `json:"name"` - Group string `json:"group"` - Version string `json:"version"` - Kind string `json:"kind"` - Type string `json:"type"` + Name string `json:"name" yaml:"name"` + Group string `json:"group" yaml:"group"` + Version string `json:"version" yaml:"version"` + Kind string `json:"kind" yaml:"kind"` + Type string `json:"type" yaml:"type"` } type Panel struct { PanelBase - Blob json.RawMessage `json:"blob"` + Blob json.RawMessage `json:"blob" yaml:"blob"` } func (p *Panel) UnmarshalJSON(data []byte) error { @@ -37,24 +37,24 @@ func (p *Panel) UnmarshalJSON(data []byte) error { type Table struct { PanelBase - Columns []Column `json:"columns"` - Namespace string `json:"namespace"` - LabelSelector map[string]string `json:"labelSelector"` + Columns []Column `json:"columns" yaml:"columns"` + Namespace string `json:"namespace" yaml:"namespace"` + LabelSelector map[string]string `json:"labelSelector" yaml:"labelSelector"` } type Column struct { - Header string `json:"header"` - Width int `json:"width"` - Path string `json:"path"` + Header string `json:"header" yaml:"header"` + Width int `json:"width" yaml:"width"` + Path string `json:"path" yaml:"path"` } type Item struct { PanelBase - Key types.NamespacedName + Key types.NamespacedName `json:"key" yaml:"key"` } type Logs struct { PanelBase - Key types.NamespacedName - Container string `json:"container"` + Key types.NamespacedName `json:"key" yaml:"key"` + Container string `json:"container" yaml:"container"` } diff --git a/pkg/types/types.go b/pkg/types/types.go index 2cedb4c..a25e1d5 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -1,5 +1,5 @@ package types type Dashboard struct { - Panels []Panel `json:"panels"` + Panels []Panel `json:"panels" yaml:"panels"` } diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..99c1180 --- /dev/null +++ b/test.yaml @@ -0,0 +1,84 @@ +panels: + - name: All Pods + group: "" + version: v1 + kind: Pod + type: table + columns: + - header: Namespace + path: metadata.namespace + - header: Name + path: metadata.name + - header: Labels + path: metadata.labels + - header: Ready Condition + path: status.conditions.#(type==Ready) + - header: Phase + path: status.phase + - header: Containers + path: spec.containers.#.name + - 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: Deployments + group: apps + version: v1 + kind: Deployment + type: table + columns: + - header: Namespace + path: metadata.namespace + - header: Name + path: metadata.name + - header: Replicas + path: status.replicas + - name: Kube API Server + group: "" + version: v1 + kind: Pod + type: item + key: + namespace: kube-system + name: kube-apiserver-kind-control-plane + - name: Kube API Server Logs + group: "" + version: v1 + kind: Pod + type: logs + key: + namespace: kube-system + name: kube-apiserver-kind-control-plane + - name: Namespaces + group: "" + version: v1 + kind: Namespace + type: table + columns: + - header: Name + path: metadata.name + - name: CoreDNS Deployment Logs + group: apps + version: v1 + kind: Deployment + type: logs + key: + namespace: kube-system + name: coredns \ No newline at end of file