Dashboards: Make conversion data optional (#109965)
This commit is contained in:
@@ -4,14 +4,14 @@ import (
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
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/pkg/infra/log"
|
||||
)
|
||||
|
||||
var logger = log.New("dashboard.conversion")
|
||||
var logger = logging.DefaultLogger.With("logger", "dashboard.conversion")
|
||||
|
||||
func RegisterConversions(s *runtime.Scheme) error {
|
||||
// v0 conversions
|
||||
|
||||
@@ -9,8 +9,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/apis"
|
||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
@@ -27,7 +28,7 @@ func TestConversionMatrixExist(t *testing.T) {
|
||||
// Initialize the migrator with a test data source provider
|
||||
migration.Initialize(testutil.GetTestDataSourceProvider(), testutil.GetTestPanelProvider())
|
||||
|
||||
versions := []v1.Object{
|
||||
versions := []metav1.Object{
|
||||
&dashv0.Dashboard{Spec: common.Unstructured{Object: map[string]any{"title": "dashboardV0"}}},
|
||||
&dashv1.Dashboard{Spec: common.Unstructured{Object: map[string]any{"title": "dashboardV1"}}},
|
||||
&dashv2alpha1.Dashboard{Spec: dashv2alpha1.DashboardSpec{Title: "dashboardV2alpha1"}},
|
||||
@@ -110,13 +111,13 @@ func TestDashboardConversionToAllVersions(t *testing.T) {
|
||||
require.True(t, ok, "apiVersion not found or not a string")
|
||||
|
||||
// Parse group and version from apiVersion (format: "group/version")
|
||||
parts := strings.Split(apiVersion, "/")
|
||||
require.Equal(t, 2, len(parts), "apiVersion should be in format 'group/version'")
|
||||
sourceVersion := parts[1]
|
||||
gv, err := schema.ParseGroupVersion(apiVersion)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dashv0.GROUP, gv.Group)
|
||||
|
||||
// Create source object based on version
|
||||
var sourceDash v1.Object
|
||||
switch sourceVersion {
|
||||
var sourceDash metav1.Object
|
||||
switch gv.Version {
|
||||
case "v0alpha1":
|
||||
var dash dashv0.Dashboard
|
||||
err = json.Unmarshal(inputData, &dash)
|
||||
@@ -134,7 +135,7 @@ func TestDashboardConversionToAllVersions(t *testing.T) {
|
||||
err = json.Unmarshal(inputData, &dash)
|
||||
sourceDash = &dash
|
||||
default:
|
||||
t.Fatalf("Unsupported source version: %s", sourceVersion)
|
||||
t.Fatalf("Unsupported source version: %s", gv.Version)
|
||||
}
|
||||
require.NoError(t, err, "Failed to unmarshal dashboard into typed object")
|
||||
|
||||
@@ -157,22 +158,26 @@ func TestDashboardConversionToAllVersions(t *testing.T) {
|
||||
if kind.Kind == "Dashboard" {
|
||||
for _, version := range kind.Versions {
|
||||
// Skip converting to the same version
|
||||
if version.VersionName == sourceVersion {
|
||||
if version.VersionName == gv.Version {
|
||||
continue
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s.%s.json", originalName, version.VersionName)
|
||||
typeMeta := metav1.TypeMeta{
|
||||
APIVersion: fmt.Sprintf("%s/%s", dashv0.APIGroup, version.VersionName),
|
||||
Kind: kind.Kind, // Dashboard
|
||||
}
|
||||
|
||||
// Create target object based on version
|
||||
switch version.VersionName {
|
||||
case "v0alpha1":
|
||||
targetVersions[filename] = &dashv0.Dashboard{}
|
||||
targetVersions[filename] = &dashv0.Dashboard{TypeMeta: typeMeta}
|
||||
case "v1beta1":
|
||||
targetVersions[filename] = &dashv1.Dashboard{}
|
||||
targetVersions[filename] = &dashv1.Dashboard{TypeMeta: typeMeta}
|
||||
case "v2alpha1":
|
||||
targetVersions[filename] = &dashv2alpha1.Dashboard{}
|
||||
targetVersions[filename] = &dashv2alpha1.Dashboard{TypeMeta: typeMeta}
|
||||
case "v2beta1":
|
||||
targetVersions[filename] = &dashv2beta1.Dashboard{}
|
||||
targetVersions[filename] = &dashv2beta1.Dashboard{TypeMeta: typeMeta}
|
||||
default:
|
||||
t.Logf("Unknown version %s, skipping", version.VersionName)
|
||||
}
|
||||
@@ -192,14 +197,14 @@ func TestDashboardConversionToAllVersions(t *testing.T) {
|
||||
require.NoError(t, err, "Conversion failed for %s", filename)
|
||||
|
||||
// Test the changes in the conversion result
|
||||
testConversion(t, target.(v1.Object), filename, outDir)
|
||||
testConversion(t, target.(metav1.Object), filename, outDir)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testConversion(t *testing.T, convertedDash v1.Object, filename, outputDir string) {
|
||||
func testConversion(t *testing.T, convertedDash metav1.Object, filename, outputDir string) {
|
||||
t.Helper()
|
||||
|
||||
outPath := filepath.Join(outputDir, filename)
|
||||
|
||||
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v0alpha1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-complete",
|
||||
"creationTimestamp": null,
|
||||
@@ -13,8 +15,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v1beta1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-complete",
|
||||
"creationTimestamp": null,
|
||||
@@ -13,8 +15,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -504,8 +504,7 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": false,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": ""
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v0alpha1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-annotations",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v1beta1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-annotations",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+1
-2
@@ -1087,8 +1087,7 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": false,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": ""
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v0alpha1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-groupby-adhoc-vars",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v1beta1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-groupby-adhoc-vars",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+1
-2
@@ -93,8 +93,7 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": false,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": ""
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v0alpha1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-viz-config",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"kind": "Dashboard",
|
||||
"apiVersion": "dashboard.grafana.app/v1beta1",
|
||||
"metadata": {
|
||||
"name": "test-v2alpha1-viz-config",
|
||||
"creationTimestamp": null
|
||||
@@ -7,8 +9,8 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": true,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": "backend conversion not yet implemented"
|
||||
"error": "backend conversion not yet implemented",
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -215,8 +215,7 @@
|
||||
"status": {
|
||||
"conversion": {
|
||||
"failed": false,
|
||||
"storedVersion": "v2alpha1",
|
||||
"error": ""
|
||||
"storedVersion": "v2alpha1"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
|
||||
@@ -21,13 +22,13 @@ func Convert_V0_to_V1(in *dashv0.Dashboard, out *dashv1.Dashboard, scope convers
|
||||
|
||||
out.Status = dashv1.DashboardStatus{
|
||||
Conversion: &dashv1.DashboardConversionStatus{
|
||||
StoredVersion: dashv0.VERSION,
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
},
|
||||
}
|
||||
|
||||
if err := migration.Migrate(out.Spec.Object, schemaversion.LATEST_VERSION); err != nil {
|
||||
out.Status.Conversion.Failed = true
|
||||
out.Status.Conversion.Error = err.Error()
|
||||
out.Status.Conversion.Error = ptr.To(err.Error())
|
||||
|
||||
// Classify error type for metrics
|
||||
errorType := "conversion_error"
|
||||
@@ -92,9 +93,9 @@ func Convert_V0_to_V2alpha1(in *dashv0.Dashboard, out *dashv2alpha1.Dashboard, s
|
||||
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: dashv0.VERSION,
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -108,9 +109,9 @@ func Convert_V0_to_V2beta1(in *dashv0.Dashboard, out *dashv2beta1.Dashboard, sco
|
||||
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: dashv0.VERSION,
|
||||
StoredVersion: ptr.To(dashv0.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package conversion
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
|
||||
@@ -16,7 +17,7 @@ func Convert_V1_to_V0(in *dashv1.Dashboard, out *dashv0.Dashboard, scope convers
|
||||
|
||||
out.Status = dashv0.DashboardStatus{
|
||||
Conversion: &dashv0.DashboardConversionStatus{
|
||||
StoredVersion: dashv1.VERSION,
|
||||
StoredVersion: ptr.To(dashv1.VERSION),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -45,9 +46,9 @@ func Convert_V1_to_V2alpha1(in *dashv1.Dashboard, out *dashv2alpha1.Dashboard, s
|
||||
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: dashv1.VERSION,
|
||||
StoredVersion: ptr.To(dashv1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -61,9 +62,9 @@ func Convert_V1_to_V2beta1(in *dashv1.Dashboard, out *dashv2beta1.Dashboard, sco
|
||||
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: dashv1.VERSION,
|
||||
StoredVersion: ptr.To(dashv1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package conversion
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
|
||||
@@ -16,9 +17,9 @@ func Convert_V2alpha1_to_V0(in *dashv2alpha1.Dashboard, out *dashv0.Dashboard, s
|
||||
|
||||
out.Status = dashv0.DashboardStatus{
|
||||
Conversion: &dashv0.DashboardConversionStatus{
|
||||
StoredVersion: dashv2alpha1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2alpha1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -32,9 +33,9 @@ func Convert_V2alpha1_to_V1(in *dashv2alpha1.Dashboard, out *dashv1.Dashboard, s
|
||||
|
||||
out.Status = dashv1.DashboardStatus{
|
||||
Conversion: &dashv1.DashboardConversionStatus{
|
||||
StoredVersion: dashv2alpha1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2alpha1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -48,9 +49,9 @@ func Convert_V2alpha1_to_V2beta1(in *dashv2alpha1.Dashboard, out *dashv2beta1.Da
|
||||
if err := ConvertDashboard_V2alpha1_to_V2beta1(in, out, scope); err != nil {
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: dashv2alpha1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2alpha1.VERSION),
|
||||
Failed: true,
|
||||
Error: err.Error(),
|
||||
Error: ptr.To(err.Error()),
|
||||
},
|
||||
}
|
||||
return err
|
||||
@@ -59,7 +60,7 @@ func Convert_V2alpha1_to_V2beta1(in *dashv2alpha1.Dashboard, out *dashv2beta1.Da
|
||||
// Set successful conversion status
|
||||
out.Status = dashv2beta1.DashboardStatus{
|
||||
Conversion: &dashv2beta1.DashboardConversionStatus{
|
||||
StoredVersion: dashv2alpha1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2alpha1.VERSION),
|
||||
Failed: false,
|
||||
},
|
||||
}
|
||||
@@ -74,9 +75,9 @@ func Convert_V2beta1_to_V0(in *dashv2beta1.Dashboard, out *dashv0.Dashboard, sco
|
||||
|
||||
out.Status = dashv0.DashboardStatus{
|
||||
Conversion: &dashv0.DashboardConversionStatus{
|
||||
StoredVersion: dashv2beta1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2beta1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -90,9 +91,9 @@ func Convert_V2beta1_to_V1(in *dashv2beta1.Dashboard, out *dashv1.Dashboard, sco
|
||||
|
||||
out.Status = dashv1.DashboardStatus{
|
||||
Conversion: &dashv1.DashboardConversionStatus{
|
||||
StoredVersion: dashv2beta1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2beta1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -106,9 +107,9 @@ func Convert_V2beta1_to_V2alpha1(in *dashv2beta1.Dashboard, out *dashv2alpha1.Da
|
||||
|
||||
out.Status = dashv2alpha1.DashboardStatus{
|
||||
Conversion: &dashv2alpha1.DashboardConversionStatus{
|
||||
StoredVersion: dashv2beta1.VERSION,
|
||||
StoredVersion: ptr.To(dashv2beta1.VERSION),
|
||||
Failed: true,
|
||||
Error: "backend conversion not yet implemented",
|
||||
Error: ptr.To("backend conversion not yet implemented"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user