K8s/Dashboards: Fix title extraction (#101990)
This commit is contained in:
@@ -607,6 +607,22 @@ func (m *grafanaMetaAccessor) FindTitle(defaultTitle string) string {
|
||||
if name.IsValid() && name.Kind() == reflect.String {
|
||||
return name.String()
|
||||
}
|
||||
|
||||
// Unstructured uses Object subtype
|
||||
object := spec.FieldByName("Object")
|
||||
if object.IsValid() && object.Kind() == reflect.Map {
|
||||
key := reflect.ValueOf("title")
|
||||
value := object.MapIndex(key)
|
||||
if value.IsValid() {
|
||||
if value.CanInterface() {
|
||||
v := value.Interface()
|
||||
t, ok := v.(string)
|
||||
if ok {
|
||||
return t
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
obj, ok := m.obj.(*unstructured.Unstructured)
|
||||
|
||||
@@ -2,12 +2,15 @@ package conversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"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"
|
||||
@@ -15,9 +18,9 @@ import (
|
||||
|
||||
func TestConversionMatrixExist(t *testing.T) {
|
||||
versions := []v1.Object{
|
||||
&dashboardV0.Dashboard{},
|
||||
&dashboardV1.Dashboard{},
|
||||
&dashboardV2.Dashboard{},
|
||||
&dashboardV0.Dashboard{Spec: v0alpha1.Unstructured{Object: map[string]any{"title": "dashboardV0"}}},
|
||||
&dashboardV1.Dashboard{Spec: v0alpha1.Unstructured{Object: map[string]any{"title": "dashboardV1"}}},
|
||||
&dashboardV2.Dashboard{Spec: dashboardV2.DashboardSpec{Title: "dashboardV2"}},
|
||||
}
|
||||
|
||||
scheme := runtime.NewScheme()
|
||||
@@ -34,6 +37,11 @@ func TestConversionMatrixExist(t *testing.T) {
|
||||
err = scheme.Convert(in, out, nil)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Make sure we get the right title for each value
|
||||
meta, err := utils.MetaAccessor(in)
|
||||
require.NoError(t, err)
|
||||
require.True(t, strings.HasPrefix(meta.FindTitle(""), "dashboard"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user