K8s: Add numeric revisionVersion helpers to grafanaMetaAccessor (#89191)

This commit is contained in:
Ryan McKinley 2024-06-14 02:35:23 +03:00 committed by GitHub
parent e807f4201b
commit f2b6a9aa3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package utils
import (
"fmt"
"reflect"
"strconv"
"time"
"k8s.io/apimachinery/pkg/api/meta"
@ -53,6 +54,11 @@ type GrafanaMetaAccessor interface {
GetGroupVersionKind() schema.GroupVersionKind
// Helper to get resource versions as int64, however this is not required
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions
GetResourceVersionInt64() (int64, error)
SetResourceVersionInt64(int64)
GetUpdatedTimestamp() (*time.Time, error)
SetUpdatedTimestamp(v *time.Time)
SetUpdatedTimestampMillis(unix int64)
@ -62,6 +68,8 @@ type GrafanaMetaAccessor interface {
SetUpdatedBy(user string)
GetFolder() string
SetFolder(uid string)
SetAnnotation(key string, val string)
GetSlug() string
SetSlug(v string)
@ -99,7 +107,7 @@ func MetaAccessor(raw interface{}) (GrafanaMetaAccessor, error) {
return nil, err
}
// look for Spec.Title or Spec.Name
// reflection to find title and other non object properties
r := reflect.ValueOf(raw)
if r.Kind() == reflect.Ptr || r.Kind() == reflect.Interface {
r = r.Elem()
@ -107,11 +115,19 @@ func MetaAccessor(raw interface{}) (GrafanaMetaAccessor, error) {
return &grafanaMetaAccessor{raw, obj, r}, nil
}
func (m *grafanaMetaAccessor) Object() metav1.Object {
return m.obj
func (m *grafanaMetaAccessor) GetResourceVersionInt64() (int64, error) {
v := m.obj.GetResourceVersion()
if v == "" {
return 0, nil
}
return strconv.ParseInt(v, 10, 64)
}
func (m *grafanaMetaAccessor) set(key string, val string) {
func (m *grafanaMetaAccessor) SetResourceVersionInt64(rv int64) {
m.obj.SetResourceVersion(strconv.FormatInt(rv, 10))
}
func (m *grafanaMetaAccessor) SetAnnotation(key string, val string) {
anno := m.obj.GetAnnotations()
if val == "" {
if anno != nil {
@ -148,7 +164,7 @@ func (m *grafanaMetaAccessor) SetUpdatedTimestampMillis(v int64) {
t := time.UnixMilli(v)
m.SetUpdatedTimestamp(&t)
} else {
m.set(AnnoKeyUpdatedTimestamp, "") // will clear the annotation
m.SetAnnotation(AnnoKeyUpdatedTimestamp, "") // will clear the annotation
}
}
@ -157,7 +173,7 @@ func (m *grafanaMetaAccessor) SetUpdatedTimestamp(v *time.Time) {
if v != nil && v.Unix() != 0 {
txt = v.UTC().Format(time.RFC3339)
}
m.set(AnnoKeyUpdatedTimestamp, txt)
m.SetAnnotation(AnnoKeyUpdatedTimestamp, txt)
}
func (m *grafanaMetaAccessor) GetCreatedBy() string {
@ -165,7 +181,7 @@ func (m *grafanaMetaAccessor) GetCreatedBy() string {
}
func (m *grafanaMetaAccessor) SetCreatedBy(user string) {
m.set(AnnoKeyCreatedBy, user)
m.SetAnnotation(AnnoKeyCreatedBy, user)
}
func (m *grafanaMetaAccessor) GetUpdatedBy() string {
@ -173,7 +189,7 @@ func (m *grafanaMetaAccessor) GetUpdatedBy() string {
}
func (m *grafanaMetaAccessor) SetUpdatedBy(user string) {
m.set(AnnoKeyUpdatedBy, user)
m.SetAnnotation(AnnoKeyUpdatedBy, user)
}
func (m *grafanaMetaAccessor) GetFolder() string {
@ -181,7 +197,7 @@ func (m *grafanaMetaAccessor) GetFolder() string {
}
func (m *grafanaMetaAccessor) SetFolder(uid string) {
m.set(AnnoKeyFolder, uid)
m.SetAnnotation(AnnoKeyFolder, uid)
}
func (m *grafanaMetaAccessor) GetSlug() string {
@ -189,7 +205,7 @@ func (m *grafanaMetaAccessor) GetSlug() string {
}
func (m *grafanaMetaAccessor) SetSlug(v string) {
m.set(AnnoKeySlug, v)
m.SetAnnotation(AnnoKeySlug, v)
}
func (m *grafanaMetaAccessor) SetOriginInfo(info *ResourceOriginInfo) {

View File

@ -163,8 +163,13 @@ func TestMetaAccessor(t *testing.T) {
}, res.GetAnnotations())
meta.SetNamespace("aaa")
meta.SetResourceVersionInt64(12345)
require.Equal(t, "aaa", res.GetNamespace())
require.Equal(t, "aaa", meta.GetNamespace())
rv, err := meta.GetResourceVersionInt64()
require.NoError(t, err)
require.Equal(t, int64(12345), rv)
})
t.Run("find titles", func(t *testing.T) {