[v3.2] Add grafana dashboard importing API (#11)

* Add API to import grafana templates to kubesphere dashboard
* Merge and fix the latest codes from kubesphere #2501

Signed-off-by: zhu733756 <talonzhu@yunify.com>
This commit is contained in:
zhu733756
2021-08-16 11:41:29 +08:00
committed by zhu733756
parent 9df6df5544
commit 242ceb54f6
217 changed files with 119028 additions and 96 deletions

View File

@@ -0,0 +1,15 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
func (_ *Dashboard) Hub() {}

View File

@@ -18,7 +18,6 @@ package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/json"
"kubesphere.io/monitoring-dashboard/api/v1alpha1/panels"
)
@@ -48,9 +47,14 @@ type Time struct {
To string `json:"to,omitempty"`
}
// Supported panel type
// Supported panel
type Panel struct {
// It can only be one of the following three types
// panel metadata
PanelMeta `json:",inline"`
// A collection of queries
// Only for panels with `graph` or `singlestat` type
Targets []panels.Target `json:"targets,omitempty"`
// The panel row
Row *panels.Row `json:",inline"`
@@ -68,42 +72,13 @@ const (
PanelSingleStat PanelType = "singlestat"
)
func (p *Panel) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var t struct{ Type PanelType }
err := json.Unmarshal(data, &t)
if err != nil {
return err
}
switch t.Type {
case PanelRow:
p.Row = &panels.Row{}
return json.Unmarshal(data, p.Row)
case PanelGraph:
p.Graph = &panels.Graph{}
return json.Unmarshal(data, p.Graph)
case PanelSingleStat:
p.SingleStat = &panels.SingleStat{}
return json.Unmarshal(data, p.SingleStat)
}
return json.Unmarshal(data, p)
}
func (p *Panel) MarshalJSON() (data []byte, err error) {
switch {
case p.Row != nil:
return json.Marshal(p.Row)
case p.Graph != nil:
return json.Marshal(p.Graph)
case p.SingleStat != nil:
return json.Marshal(p.SingleStat)
}
return json.Marshal(p)
type PanelMeta struct {
// Name of the panel
Title string `json:"title,omitempty"`
// Panel ID
Id int64 `json:"id,omitempty"`
// Panel Type, one of `row`, `graph`, `singlestat`
Type PanelType `json:"type"`
}
// Templating defines a variable, which can be used as a placeholder in query
@@ -115,6 +90,7 @@ type Templating struct {
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// Dashboard is the Schema for the dashboards API
type Dashboard struct {

View File

@@ -5,15 +5,15 @@ package panels
// Graph visualizes range query results into a linear graph
type Graph struct {
// Name of the graph panel
Title string `json:"title,omitempty"`
//Title string `json:"title,omitempty"`
// Must be `graph`
Type string `json:"type"`
//Type string `json:"type"`
// Panel ID
Id int64 `json:"id,omitempty"`
//Id int64 `json:"id,omitempty"`
// Panel description
Description string `json:"description,omitempty"`
// A collection of queries
Targets []Target `json:"targets,omitempty"`
//Targets []Target `json:"targets,omitempty"`
// Display as a bar chart
Bars bool `json:"bars,omitempty"`
// Set series color

View File

@@ -5,9 +5,9 @@ package panels
// Row groups relevant charts
type Row struct {
// Name of the row panel
Title string `json:"title,omitempty"`
//Title string `json:"title,omitempty"`
// Must be `row`
Type string `json:"type"`
//Type string `json:"type"`
// Panel ID
Id int64 `json:"id,omitempty"`
//Id int64 `json:"id,omitempty"`
}

View File

@@ -5,15 +5,15 @@ package panels
// SingleStat shows instant query result
type SingleStat struct {
// Name of the signlestat panel
Title string `json:"title,omitempty"`
//Title string `json:"title,omitempty"`
// Must be `singlestat`
Type string `json:"type"`
//Type string `json:"type"`
// Panel ID
Id int64 `json:"id,omitempty"`
//Id int64 `json:"id,omitempty"`
// A collection of queries
Targets []Target `json:"targets,omitempty"`
//Targets []Target `json:"targets,omitempty"`
// Limit the decimal numbers
Decimals int64 `json:"decimals,omitempty"`
Decimals *int64 `json:"decimals,omitempty"`
// Display unit
Format string `json:"format,omitempty"`
}

View File

@@ -25,11 +25,6 @@ import ()
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Graph) DeepCopyInto(out *Graph) {
*out = *in
if in.Targets != nil {
in, out := &in.Targets, &out.Targets
*out = make([]Target, len(*in))
copy(*out, *in)
}
if in.Colors != nil {
in, out := &in.Colors, &out.Colors
*out = make([]string, len(*in))
@@ -70,10 +65,10 @@ func (in *Row) DeepCopy() *Row {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *SingleStat) DeepCopyInto(out *SingleStat) {
*out = *in
if in.Targets != nil {
in, out := &in.Targets, &out.Targets
*out = make([]Target, len(*in))
copy(*out, *in)
if in.Decimals != nil {
in, out := &in.Decimals, &out.Decimals
*out = new(int64)
**out = **in
}
}

View File

@@ -172,6 +172,12 @@ func (in *DashboardSpec) DeepCopy() *DashboardSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Panel) DeepCopyInto(out *Panel) {
*out = *in
out.PanelMeta = in.PanelMeta
if in.Targets != nil {
in, out := &in.Targets, &out.Targets
*out = make([]panels.Target, len(*in))
copy(*out, *in)
}
if in.Row != nil {
in, out := &in.Row, &out.Row
*out = new(panels.Row)
@@ -199,6 +205,21 @@ func (in *Panel) DeepCopy() *Panel {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *PanelMeta) DeepCopyInto(out *PanelMeta) {
*out = *in
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PanelMeta.
func (in *PanelMeta) DeepCopy() *PanelMeta {
if in == nil {
return nil
}
out := new(PanelMeta)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Templating) DeepCopyInto(out *Templating) {
*out = *in