Skip to content

Commit

Permalink
Merge pull request #215 from grafana/support-k8s-form
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu authored Dec 3, 2024
2 parents 1e79993 + 6c64556 commit 9cec106
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ type Dashboard struct {
Rows []Row `json:"rows,omitempty"`
Panels []Panel `json:"panels,omitempty"`
Editable bool `json:"editable,omitempty"`

// Kubernetes shaped dashboards will include an APIVersion and Kind
APIVersion string `json:"apiVersion,omitempty"`

// When reading a kubernetes encoded dashboard, the Dashboard will be
Spec json.RawMessage `json:"spec,omitempty"`
}

// GetPanels returns the all panels whether they are nested in the (now deprecated) "rows" property or
Expand Down Expand Up @@ -308,5 +314,19 @@ func NewDashboard(buf []byte) (Dashboard, error) {
if err := json.Unmarshal(buf, &dash); err != nil {
return dash, err
}

// Support kubernetes flavored dashboards
if dash.Spec != nil {
apiVersion := dash.APIVersion
if apiVersion != "" {
if !(strings.HasPrefix(apiVersion, "v0") || strings.HasPrefix(apiVersion, "v1")) {
return dash, fmt.Errorf("unsupported apiVersion")
}
}
if err := json.Unmarshal(dash.Spec, &dash); err != nil {
return dash, err
}
dash.APIVersion = apiVersion // preserve the original APIVersion
}
return dash, nil
}
13 changes: 13 additions & 0 deletions lint/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ func TestParseDashboard(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, dashboard.Annotations.List, 1)
})

t.Run("v0alpha1 dashboard", func(t *testing.T) {
wrap := `{
"apiVersion": "v0alpha1",
"kind": "Dashboard",
"spec": ` + string(sampleDashboard) + `
}`

dashboard, err := NewDashboard([]byte(wrap))
assert.NoError(t, err)
assert.Len(t, dashboard.Annotations.List, 1)
assert.Equal(t, "v0alpha1", dashboard.APIVersion)
})
}

func TestParseTemplateValue(t *testing.T) {
Expand Down

0 comments on commit 9cec106

Please sign in to comment.