From 3c56e32b0c87ef303b115cc632df88d89b6bec43 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 13 Feb 2025 14:04:03 +0300 Subject: [PATCH] K8s/Utils: Find title in unstructured content (#100576) --- pkg/apimachinery/utils/meta.go | 12 ++++++++++++ pkg/apimachinery/utils/meta_test.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/pkg/apimachinery/utils/meta.go b/pkg/apimachinery/utils/meta.go index 4d39d4fcd57..5b2978533bb 100644 --- a/pkg/apimachinery/utils/meta.go +++ b/pkg/apimachinery/utils/meta.go @@ -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() diff --git a/pkg/apimachinery/utils/meta_test.go b/pkg/apimachinery/utils/meta_test.go index 451deaa47d5..12d12cc06b5 100644 --- a/pkg/apimachinery/utils/meta_test.go +++ b/pkg/apimachinery/utils/meta_test.go @@ -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()