Files
Igor Suleymanov ea89a68028 K8s/Dashboards: Generate Dashboards k8s APIs using Grafana App SDK (#99966)
* Generate Dashboard kinds with `grafana-app-sdk`

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Hack together a fix for invalid TS codegen for v0 & v1

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Address Go linter issues

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Address TS linter issues

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Add new app to CODEOWNERS

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Fix a couple of issues detected by tests

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Update OpenAPI definitions and test files

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Remove title from Dashboard v1alpha1 spec

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Remove unused CUE schemas

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* remove unrelated files

* allow any in the generated betterer

* Add a comment explaining why we don't use deepcopy-gen

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

* Default to v2alpha1 if dashboards v2 FF is enabled

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>

---------

Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com>
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2025-03-11 13:00:37 +02:00

78 lines
2.2 KiB
Go

package dashboard
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
commonV0 "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
"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"
"github.com/grafana/grafana/pkg/storage/unified/apistore"
)
func NewDashboardLargeObjectSupport(scheme *runtime.Scheme) *apistore.BasicLargeObjectSupport {
return &apistore.BasicLargeObjectSupport{
TheGroupResource: dashboardV0.DashboardResourceInfo.GroupResource(),
// 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 {
meta, err := utils.MetaAccessor(obj)
if err != nil {
return err
}
switch dash := obj.(type) {
case *dashboardV0.Dashboard:
reduceUnstructredSpec(&dash.Spec)
case *dashboardV1.Dashboard:
reduceUnstructredSpec(&dash.Spec)
case *dashboardV2.Dashboard:
dash.Spec = dashboardV2.DashboardSpec{
Title: dash.Spec.Title,
Description: dash.Spec.Description,
Tags: dash.Spec.Tags,
}
default:
return fmt.Errorf("unsupported dashboard type %T", obj)
}
meta.SetManagedFields(nil) // this could be bigger than the object!
return nil
},
RebuildSpec: func(obj runtime.Object, blob []byte) error {
switch dash := obj.(type) {
case *dashboardV0.Dashboard:
return dash.Spec.UnmarshalJSON(blob)
case *dashboardV1.Dashboard:
return dash.Spec.UnmarshalJSON(blob)
case *dashboardV2.Dashboard:
return json.Unmarshal(blob, &dash.Spec)
default:
return fmt.Errorf("unsupported dashboard type %T", obj)
}
},
}
}
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
}