mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 01:23:32 -06:00
more utils
This commit is contained in:
parent
197a837e1e
commit
cfc192a2b5
@ -58,6 +58,7 @@ type GrafanaMetaAccessor interface {
|
|||||||
metav1.Object
|
metav1.Object
|
||||||
|
|
||||||
GetGroupVersionKind() schema.GroupVersionKind
|
GetGroupVersionKind() schema.GroupVersionKind
|
||||||
|
GetRuntimeObject() (runtime.Object, bool)
|
||||||
|
|
||||||
// Helper to get resource versions as int64, however this is not required
|
// Helper to get resource versions as int64, however this is not required
|
||||||
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions
|
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions
|
||||||
@ -90,6 +91,8 @@ type GrafanaMetaAccessor interface {
|
|||||||
GetOriginHash() string
|
GetOriginHash() string
|
||||||
GetOriginTimestamp() (*time.Time, error)
|
GetOriginTimestamp() (*time.Time, error)
|
||||||
|
|
||||||
|
GetSpec() (any, error)
|
||||||
|
|
||||||
// Find a title in the object
|
// Find a title in the object
|
||||||
// This will reflect the object and try to get:
|
// This will reflect the object and try to get:
|
||||||
// * spec.title
|
// * spec.title
|
||||||
@ -133,6 +136,11 @@ func (m *grafanaMetaAccessor) GetResourceVersionInt64() (int64, error) {
|
|||||||
return strconv.ParseInt(v, 10, 64)
|
return strconv.ParseInt(v, 10, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *grafanaMetaAccessor) GetRuntimeObject() (runtime.Object, bool) {
|
||||||
|
obj, ok := m.raw.(runtime.Object)
|
||||||
|
return obj, ok
|
||||||
|
}
|
||||||
|
|
||||||
func (m *grafanaMetaAccessor) SetResourceVersionInt64(rv int64) {
|
func (m *grafanaMetaAccessor) SetResourceVersionInt64(rv int64) {
|
||||||
m.obj.SetResourceVersion(strconv.FormatInt(rv, 10))
|
m.obj.SetResourceVersion(strconv.FormatInt(rv, 10))
|
||||||
}
|
}
|
||||||
@ -486,6 +494,16 @@ func (m *grafanaMetaAccessor) GetGroupVersionKind() schema.GroupVersionKind {
|
|||||||
return gvk
|
return gvk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *grafanaMetaAccessor) GetSpec() (spec any, err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
err = fmt.Errorf("error reading spec")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
spec = m.r.FieldByName("Spec").Interface()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (m *grafanaMetaAccessor) FindTitle(defaultTitle string) string {
|
func (m *grafanaMetaAccessor) FindTitle(defaultTitle string) string {
|
||||||
// look for Spec.Title or Spec.Name
|
// look for Spec.Title or Spec.Name
|
||||||
spec := m.r.FieldByName("Spec")
|
spec := m.r.FieldByName("Spec")
|
||||||
|
@ -228,5 +228,13 @@ func TestMetaAccessor(t *testing.T) {
|
|||||||
}, obj2.GetAnnotations())
|
}, obj2.GetAnnotations())
|
||||||
|
|
||||||
require.Equal(t, "xxx", meta.FindTitle("xxx"))
|
require.Equal(t, "xxx", meta.FindTitle("xxx"))
|
||||||
|
|
||||||
|
rt, ok := meta.GetRuntimeObject()
|
||||||
|
require.Equal(t, obj2, rt)
|
||||||
|
require.True(t, ok)
|
||||||
|
|
||||||
|
spec, err := meta.GetSpec()
|
||||||
|
require.Equal(t, obj2.Spec, spec)
|
||||||
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user