K8s/Utils: Find title in unstructured content (#100576)

This commit is contained in:
Ryan McKinley
2025-02-13 14:04:03 +03:00
committed by GitHub
parent 1b1954de28
commit 3c56e32b0c
2 changed files with 14 additions and 0 deletions

View File

@@ -699,6 +699,18 @@ func (m *grafanaMetaAccessor) FindTitle(defaultTitle string) string {
}
}
obj, ok := m.obj.(*unstructured.Unstructured)
if ok {
title, ok, _ := unstructured.NestedString(obj.Object, "spec", "title")
if ok && title != "" {
return title
}
title, ok, _ = unstructured.NestedString(obj.Object, "spec", "name")
if ok && title != "" {
return title
}
}
title := m.r.FieldByName("Title")
if title.IsValid() && title.Kind() == reflect.String {
return title.String()

View File

@@ -194,6 +194,7 @@ func TestMetaAccessor(t *testing.T) {
res.Object = map[string]any{
"spec": map[string]any{
"hello": "world",
"title": "Title",
},
"status": map[string]any{
"sloth": "🦥",
@@ -218,6 +219,7 @@ func TestMetaAccessor(t *testing.T) {
rv, err := meta.GetResourceVersionInt64()
require.NoError(t, err)
require.Equal(t, int64(12345), rv)
require.Equal(t, "Title", meta.FindTitle(""))
// Make sure access to spec works for Unstructured
spec, err = meta.GetSpec()