Schema: convert dashboards from v1beta1 to v2beta1 (#109037)
- Implement full conversion pipeline from v1beta1 → v2beta1 - Ensure frontend–backend parity for all dashboard serialization paths - Add automatic data loss detection for conversions (panels, queries, annotations, links, variables) - Extract atomic conversion functions for v0 → v1beta1 → v2alpha1 → v2beta1 - Introduce conversion metrics and detailed logging for loss tracking - Normalize datasource resolution, defaults, and annotation processing - Improve panel layout serialization and y-coordinate normalization - Fix inconsistencies in nested panels and collapsed row behavior - Refine variable handling: - Filter refId from variable query specs - Default variable refresh to 'never' (matches frontend) - Fix constant and interval variable handling for missing queries - Unify schema defaults (enable, hide, iconColor, editable, liveNow) - Fix pluginId usage (UID vs type) and datasource references - Fix metrics.go bug swallowing errors (return nil → return err) - Add tests for version-specific conversion error handling - Add data loss detection tests using source/target version comparison - Clean up lint issues, legacy code, and redundant files - Update OpenAPI snapshots and migrated dashboards - Improve backend migrator to reuse datasource provider and match frontend logic Co-authored-by: Haris Rozajac <haris.rozajac12@gmail.com> Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com> Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
This commit is contained in:
co-authored by
Haris Rozajac
Oscar Kilhed
Stephanie Hingtgen
parent
09942c08db
commit
e463781077
@@ -1,10 +1,6 @@
|
||||
package conversion
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/authlib/types"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
@@ -12,91 +8,104 @@ import (
|
||||
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
|
||||
dashv2alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
|
||||
dashv2beta1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2beta1"
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration"
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
)
|
||||
|
||||
func Convert_V0_to_V1(in *dashv0.Dashboard, out *dashv1.Dashboard, scope conversion.Scope) error {
|
||||
out.ObjectMeta = in.ObjectMeta
|
||||
|
||||
out.Spec.Object = in.Spec.Object
|
||||
|
||||
out.Status = dashv1.DashboardStatus{
|
||||
Conversion: &dashv1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
},
|
||||
}
|
||||
|
||||
// the scope passed into this function is used in k8s apimachinery for migrations, but we also need the context
|
||||
// to have what grafana expects in the request context, so that we can retrieve datasources for migrating
|
||||
// some of the old dashboard schemas (these migrations used to be run in the frontend)
|
||||
ctx := request.WithNamespace(context.Background(), in.GetNamespace())
|
||||
nsInfo, err := types.ParseNamespace(in.GetNamespace())
|
||||
if err != nil {
|
||||
out.Status.Conversion.Failed = true
|
||||
out.Status.Conversion.Error = ptr.To(err.Error())
|
||||
return schemaversion.NewMigrationError(err.Error(), schemaversion.GetSchemaVersion(in.Spec.Object), schemaversion.LATEST_VERSION, "Convert_V0_to_V1")
|
||||
}
|
||||
|
||||
// a background service identity is used here because the user who is reading the specific dashboard
|
||||
// may not have access to all the datasources in the dashboard, but the migration still needs to take place
|
||||
// in order to be able to convert between k8s versions (so that we have a guaranteed structure to convert between)
|
||||
ctx, _ = identity.WithServiceIdentity(ctx, nsInfo.OrgID)
|
||||
|
||||
if err := migration.Migrate(ctx, out.Spec.Object, schemaversion.LATEST_VERSION); err != nil {
|
||||
out.Status.Conversion.Failed = true
|
||||
out.Status.Conversion.Error = ptr.To(err.Error())
|
||||
return schemaversion.NewMigrationError(err.Error(), schemaversion.GetSchemaVersion(in.Spec.Object), schemaversion.LATEST_VERSION, "Convert_V0_to_V1")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Convert_V0_to_V2alpha1(in *dashv0.Dashboard, out *dashv2alpha1.Dashboard, scope conversion.Scope) error {
|
||||
out.ObjectMeta = in.ObjectMeta
|
||||
|
||||
// TODO (@radiohead): implement V0 to V2 conversion
|
||||
// This is the bare minimum conversion that is needed to make the dashboard servable.
|
||||
|
||||
if v, ok := in.Spec.Object["title"]; ok {
|
||||
if title, ok := v.(string); ok {
|
||||
out.Spec.Title = title
|
||||
func Convert_V0_to_V1beta1(in *dashv0.Dashboard, out *dashv1.Dashboard, scope conversion.Scope) error {
|
||||
if err := ConvertDashboard_V0_to_V1beta1(in, out, scope); err != nil {
|
||||
out.Status = dashv1.DashboardStatus{
|
||||
Conversion: &dashv1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Convert_V0_to_V2alpha1(in *dashv0.Dashboard, out *dashv2alpha1.Dashboard, scope conversion.Scope, dsInfoProvider schemaversion.DataSourceInfoProvider) error {
|
||||
v1beta1 := &dashv1.Dashboard{}
|
||||
if err := ConvertDashboard_V0_to_V1beta1(in, v1beta1, scope); err != nil {
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
// Don't return error - just set status (matches test expectations)
|
||||
// Ensure layout is set even on error to prevent JSON marshaling issues
|
||||
if out.Spec.Layout.GridLayoutKind == nil && out.Spec.Layout.RowsLayoutKind == nil {
|
||||
out.Spec.Layout = dashv2alpha1.DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTabsLayoutKind{
|
||||
GridLayoutKind: &dashv2alpha1.DashboardGridLayoutKind{
|
||||
Kind: "GridLayout",
|
||||
Spec: dashv2alpha1.DashboardGridLayoutSpec{},
|
||||
},
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// We need to make sure the layout is set to some value, otherwise the JSON marshaling will fail.
|
||||
out.Spec.Layout = dashv2alpha1.DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTabsLayoutKind{
|
||||
GridLayoutKind: &dashv2alpha1.DashboardGridLayoutKind{
|
||||
Kind: "GridLayout",
|
||||
Spec: dashv2alpha1.DashboardGridLayoutSpec{},
|
||||
},
|
||||
}
|
||||
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
Source: in,
|
||||
},
|
||||
if err := ConvertDashboard_V1beta1_to_V2alpha1(v1beta1, out, scope, dsInfoProvider); err != nil {
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
// Don't return error - just set status (matches test expectations)
|
||||
// Ensure layout is set even on error to prevent JSON marshaling issues
|
||||
if out.Spec.Layout.GridLayoutKind == nil && out.Spec.Layout.RowsLayoutKind == nil {
|
||||
out.Spec.Layout = dashv2alpha1.DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTabsLayoutKind{
|
||||
GridLayoutKind: &dashv2alpha1.DashboardGridLayoutKind{
|
||||
Kind: "GridLayout",
|
||||
Spec: dashv2alpha1.DashboardGridLayoutSpec{},
|
||||
},
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Convert_V0_to_V2beta1(in *dashv0.Dashboard, out *dashv2beta1.Dashboard, scope conversion.Scope) error {
|
||||
out.ObjectMeta = in.ObjectMeta
|
||||
func Convert_V0_to_V2beta1(in *dashv0.Dashboard, out *dashv2beta1.Dashboard, scope conversion.Scope, dsInfoProvider schemaversion.DataSourceInfoProvider) error {
|
||||
v1beta1 := &dashv1.Dashboard{}
|
||||
if err := ConvertDashboard_V0_to_V1beta1(in, v1beta1, scope); err != nil {
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: implement V0 to v2beta1 conversion
|
||||
v2alpha1 := &dashv2alpha1.Dashboard{}
|
||||
if err := ConvertDashboard_V1beta1_to_V2alpha1(v1beta1, v2alpha1, scope, dsInfoProvider); err != nil {
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
Source: in,
|
||||
},
|
||||
if err := ConvertDashboard_V2alpha1_to_V2beta1(v2alpha1, out, scope); err != nil {
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user