2024-11-09 13:09:46 +08:00
|
|
|
package dashboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
|
|
|
commonV0 "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
2025-03-04 12:47:45 +08:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
|
|
|
dashboardV0 "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1"
|
|
|
|
dashboardV1 "github.com/grafana/grafana/pkg/apis/dashboard/v1alpha1"
|
|
|
|
dashboardV2 "github.com/grafana/grafana/pkg/apis/dashboard/v2alpha1"
|
2024-11-09 13:09:46 +08:00
|
|
|
"github.com/grafana/grafana/pkg/storage/unified/apistore"
|
|
|
|
)
|
|
|
|
|
2024-11-16 04:49:45 +08:00
|
|
|
func NewDashboardLargeObjectSupport(scheme *runtime.Scheme) *apistore.BasicLargeObjectSupport {
|
2024-11-09 13:09:46 +08:00
|
|
|
return &apistore.BasicLargeObjectSupport{
|
2025-03-04 12:47:45 +08:00
|
|
|
TheGroupResource: dashboardV0.DashboardResourceInfo.GroupResource(),
|
2024-11-09 13:09:46 +08:00
|
|
|
|
|
|
|
// byte size, while testing lets do almost everything (10bytes)
|
|
|
|
ThresholdSize: 10,
|
|
|
|
|
|
|
|
// 10mb -- we should check what the largest ones are... might be bigger
|
|
|
|
MaxByteSize: 10 * 1024 * 1024,
|
|
|
|
|
|
|
|
ReduceSpec: func(obj runtime.Object) error {
|
2025-03-04 12:47:45 +08:00
|
|
|
meta, err := utils.MetaAccessor(obj)
|
2024-11-16 04:49:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-11-09 13:09:46 +08:00
|
|
|
}
|
|
|
|
|
2025-03-04 12:47:45 +08:00
|
|
|
switch dash := obj.(type) {
|
|
|
|
case *dashboardV0.Dashboard:
|
|
|
|
reduceUnstructredSpec(&dash.Spec)
|
|
|
|
case *dashboardV1.Dashboard:
|
|
|
|
reduceUnstructredSpec(&dash.Spec)
|
|
|
|
case *dashboardV2.Dashboard:
|
|
|
|
reduceUnstructredSpec(&dash.Spec)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported dashboard type %T", obj)
|
2024-11-16 04:49:45 +08:00
|
|
|
}
|
|
|
|
|
2025-03-04 12:47:45 +08:00
|
|
|
meta.SetManagedFields(nil) // this could be bigger than the object!
|
2024-11-09 13:09:46 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
|
|
|
|
RebuildSpec: func(obj runtime.Object, blob []byte) error {
|
2025-03-04 12:47:45 +08:00
|
|
|
body := commonV0.Unstructured{}
|
|
|
|
err := body.UnmarshalJSON(blob)
|
2024-11-16 04:49:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-11-09 13:09:46 +08:00
|
|
|
}
|
2025-02-07 16:29:25 +08:00
|
|
|
|
2025-03-04 12:47:45 +08:00
|
|
|
switch dash := obj.(type) {
|
|
|
|
case *dashboardV0.Dashboard:
|
|
|
|
dash.Spec = body
|
|
|
|
case *dashboardV1.Dashboard:
|
|
|
|
dash.Spec = body
|
|
|
|
case *dashboardV2.Dashboard:
|
|
|
|
dash.Spec = body
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported dashboard type %T", obj)
|
2025-02-07 16:29:25 +08:00
|
|
|
}
|
|
|
|
return nil
|
2024-11-09 13:09:46 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2025-03-04 12:47:45 +08:00
|
|
|
|
|
|
|
func reduceUnstructredSpec(spec *commonV0.Unstructured) {
|
|
|
|
vals := make(map[string]any, 5)
|
|
|
|
keep := []string{"title", "description", "tags", "schemaVersion"}
|
|
|
|
for _, k := range keep {
|
|
|
|
v, ok := spec.Object[k]
|
|
|
|
if ok {
|
|
|
|
vals[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec.Object = vals
|
|
|
|
}
|