Dashboards: Apply schemaVersion migration in v2 conversion (#99973)

pull/100084/head
Todd Treece 6 months ago committed by GitHub
parent e74cf72d99
commit 3fd1b67381
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      pkg/apis/dashboard/v1alpha1/conversion.go
  2. 4
      pkg/apis/dashboard/v1alpha1/conversion_test.go
  3. 21
      pkg/apis/dashboard/v2alpha1/conversion.go
  4. 4
      pkg/apis/dashboard/v2alpha1/conversion_test.go

@ -12,25 +12,23 @@ import (
) )
func Convert_v0alpha1_Unstructured_To_v1alpha1_DashboardSpec(in *common.Unstructured, out *DashboardSpec, s conversion.Scope) error { 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 { if err != nil {
minErr := &schemaversion.MinimumVersionError{} minErr := &schemaversion.MinimumVersionError{}
if errors.As(err, &minErr) { if errors.As(err, &minErr) {
in.Object["__migrationError"] = err.Error() out.Unstructured.Object["__migrationError"] = err.Error()
} else { } else {
return err return err
} }
} }
out.Unstructured = *in t, ok := out.Unstructured.Object["title"].(string)
t, ok := in.Object["title"].(string)
if !ok { if !ok {
klog.V(5).Infof("unstructured dashboard title field is not a string %v", t) 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 return nil // skip setting the title if it's not a string in the unstructured object
} }
out.Title = t out.Title = t
return nil return nil
} }

@ -27,6 +27,7 @@ func TestConvertDashboardVersions(t *testing.T) {
} }
] ]
}, },
"refresh": true,
"description": "", "description": "",
"editable": true, "editable": true,
"fiscalYearStartMonth": 0, "fiscalYearStartMonth": 0,
@ -35,7 +36,7 @@ func TestConvertDashboardVersions(t *testing.T) {
"links": [], "links": [],
"panels": [], "panels": [],
"preload": false, "preload": false,
"schemaVersion": 40, "schemaVersion": 39,
"tags": [], "tags": [],
"templating": { "templating": {
"list": [] "list": []
@ -56,6 +57,7 @@ func TestConvertDashboardVersions(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, result.Title, "New dashboard") require.Equal(t, result.Title, "New dashboard")
require.Equal(t, result.Unstructured, object) 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 // now convert back & ensure it is the same
object2 := common.Unstructured{} object2 := common.Unstructured{}

@ -1,27 +1,34 @@
package v2alpha1 package v2alpha1
import ( import (
"errors"
conversion "k8s.io/apimachinery/pkg/conversion" conversion "k8s.io/apimachinery/pkg/conversion"
klog "k8s.io/klog/v2" klog "k8s.io/klog/v2"
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1" 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 { func Convert_v0alpha1_Unstructured_To_v2alpha1_DashboardSpec(in *common.Unstructured, out *DashboardSpec, s conversion.Scope) error {
out.Unstructured = *in out.Unstructured = *in
err := migration.Migrate(out.Unstructured.Object, schemaversion.LATEST_VERSION)
t, ok := in.Object["title"] if err != nil {
if !ok { minErr := &schemaversion.MinimumVersionError{}
return nil // skip setting the title if it's not in the unstructured object 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 { if !ok {
klog.V(5).Infof("unstructured dashboard title field is not a string %v", t) 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 return nil // skip setting the title if it's not a string in the unstructured object
} }
out.Title = title out.Title = t
return nil return nil
} }

@ -27,6 +27,7 @@ func TestConvertDashboardVersions(t *testing.T) {
} }
] ]
}, },
"refresh": true,
"description": "", "description": "",
"editable": true, "editable": true,
"fiscalYearStartMonth": 0, "fiscalYearStartMonth": 0,
@ -35,7 +36,7 @@ func TestConvertDashboardVersions(t *testing.T) {
"links": [], "links": [],
"panels": [], "panels": [],
"preload": false, "preload": false,
"schemaVersion": 40, "schemaVersion": 39,
"tags": [], "tags": [],
"templating": { "templating": {
"list": [] "list": []
@ -56,6 +57,7 @@ func TestConvertDashboardVersions(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, result.Title, "New dashboard") require.Equal(t, result.Title, "New dashboard")
require.Equal(t, result.Unstructured, object) 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 // now convert back & ensure it is the same
object2 := common.Unstructured{} object2 := common.Unstructured{}

Loading…
Cancel
Save