Dashboards: Apply schemaVersion migration in v2 conversion (#99973)
This commit is contained in:
@@ -12,25 +12,23 @@ import (
|
||||
)
|
||||
|
||||
func Convert_v0alpha1_Unstructured_To_v1alpha1_DashboardSpec(in *common.Unstructured, out *DashboardSpec, s conversion.Scope) error {
|
||||
err := migration.Migrate(in.Object, schemaversion.LATEST_VERSION)
|
||||
out.Unstructured = *in
|
||||
err := migration.Migrate(out.Unstructured.Object, schemaversion.LATEST_VERSION)
|
||||
if err != nil {
|
||||
minErr := &schemaversion.MinimumVersionError{}
|
||||
if errors.As(err, &minErr) {
|
||||
in.Object["__migrationError"] = err.Error()
|
||||
out.Unstructured.Object["__migrationError"] = err.Error()
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
out.Unstructured = *in
|
||||
|
||||
t, ok := in.Object["title"].(string)
|
||||
t, ok := out.Unstructured.Object["title"].(string)
|
||||
if !ok {
|
||||
klog.V(5).Infof("unstructured dashboard title field is not a string %v", t)
|
||||
return nil // skip setting the title if it's not a string in the unstructured object
|
||||
}
|
||||
out.Title = t
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
}
|
||||
]
|
||||
},
|
||||
"refresh": true,
|
||||
"description": "",
|
||||
"editable": true,
|
||||
"fiscalYearStartMonth": 0,
|
||||
@@ -35,7 +36,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
"links": [],
|
||||
"panels": [],
|
||||
"preload": false,
|
||||
"schemaVersion": 40,
|
||||
"schemaVersion": 39,
|
||||
"tags": [],
|
||||
"templating": {
|
||||
"list": []
|
||||
@@ -56,6 +57,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, result.Title, "New dashboard")
|
||||
require.Equal(t, result.Unstructured, object)
|
||||
require.Equal(t, result.Unstructured.Object["refresh"], "", "schemaVersion migration not applied. refresh should be an empty string")
|
||||
|
||||
// now convert back & ensure it is the same
|
||||
object2 := common.Unstructured{}
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
package v2alpha1
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
conversion "k8s.io/apimachinery/pkg/conversion"
|
||||
klog "k8s.io/klog/v2"
|
||||
|
||||
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/apis/dashboard/migration"
|
||||
"github.com/grafana/grafana/pkg/apis/dashboard/migration/schemaversion"
|
||||
)
|
||||
|
||||
func Convert_v0alpha1_Unstructured_To_v2alpha1_DashboardSpec(in *common.Unstructured, out *DashboardSpec, s conversion.Scope) error {
|
||||
out.Unstructured = *in
|
||||
|
||||
t, ok := in.Object["title"]
|
||||
if !ok {
|
||||
return nil // skip setting the title if it's not in the unstructured object
|
||||
err := migration.Migrate(out.Unstructured.Object, schemaversion.LATEST_VERSION)
|
||||
if err != nil {
|
||||
minErr := &schemaversion.MinimumVersionError{}
|
||||
if errors.As(err, &minErr) {
|
||||
out.Unstructured.Object["__migrationError"] = err.Error()
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
title, ok := t.(string)
|
||||
t, ok := out.Unstructured.Object["title"].(string)
|
||||
if !ok {
|
||||
klog.V(5).Infof("unstructured dashboard title field is not a string %v", t)
|
||||
return nil // skip setting the title if it's not a string in the unstructured object
|
||||
}
|
||||
out.Title = title
|
||||
|
||||
out.Title = t
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
}
|
||||
]
|
||||
},
|
||||
"refresh": true,
|
||||
"description": "",
|
||||
"editable": true,
|
||||
"fiscalYearStartMonth": 0,
|
||||
@@ -35,7 +36,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
"links": [],
|
||||
"panels": [],
|
||||
"preload": false,
|
||||
"schemaVersion": 40,
|
||||
"schemaVersion": 39,
|
||||
"tags": [],
|
||||
"templating": {
|
||||
"list": []
|
||||
@@ -56,6 +57,7 @@ func TestConvertDashboardVersions(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, result.Title, "New dashboard")
|
||||
require.Equal(t, result.Unstructured, object)
|
||||
require.Equal(t, result.Unstructured.Object["refresh"], "", "schemaVersion migration not applied. refresh should be an empty string")
|
||||
|
||||
// now convert back & ensure it is the same
|
||||
object2 := common.Unstructured{}
|
||||
|
||||
Reference in New Issue
Block a user