K8s/Dashboards: Pass the legacy internal ID into labels (#98311)

---------

Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
This commit is contained in:
Ryan McKinley
2024-12-20 21:33:49 +02:00
committed by GitHub
co-authored by Stephanie Hingtgen Todd Treece
parent 54333473f7
commit 1a46039037
8 changed files with 240 additions and 9 deletions
+47
View File
@@ -34,6 +34,9 @@ const AnnoKeyRepoPath = "grafana.app/repoPath"
const AnnoKeyRepoHash = "grafana.app/repoHash"
const AnnoKeyRepoTimestamp = "grafana.app/repoTimestamp"
// Deprecated: will be removed in grafana 13
const labelKeyDeprecatedInternalID = "grafana.app/deprecatedInternalID"
// These can be removed once we verify that non of the dual-write sources
// (for dashboards/playlists/etc) depend on the saved internal ID in SQL
const oldAnnoKeyOriginName = "grafana.app/originName"
@@ -102,6 +105,12 @@ type GrafanaMetaAccessor interface {
SetBlob(v *BlobInfo)
GetBlob() *BlobInfo
// Deprecated: This will be removed in Grafana 13
GetDeprecatedInternalID() int64
// Deprecated: This will be removed in Grafana 13
SetDeprecatedInternalID(id int64)
GetRepositoryInfo() (*ResourceRepositoryInfo, error)
SetRepositoryInfo(info *ResourceRepositoryInfo)
GetRepositoryName() string
@@ -283,6 +292,44 @@ func (m *grafanaMetaAccessor) SetSlug(v string) {
m.SetAnnotation(AnnoKeySlug, v)
}
// This will be removed in Grafana 13. Do not add any new usage of it.
func (m *grafanaMetaAccessor) GetDeprecatedInternalID() int64 {
labels := m.obj.GetLabels()
if labels == nil {
return 0
}
if internalID, ok := labels[labelKeyDeprecatedInternalID]; ok {
id, err := strconv.ParseInt(internalID, 10, 64)
if err == nil {
return id
}
}
return 0
}
// This will be removed in Grafana 13. Do not add any new usage of it.
func (m *grafanaMetaAccessor) SetDeprecatedInternalID(id int64) {
labels := m.obj.GetLabels()
// disallow setting it to 0
if id == 0 {
if labels != nil {
delete(labels, labelKeyDeprecatedInternalID)
m.obj.SetLabels(labels)
}
return
}
if labels == nil {
labels = make(map[string]string)
}
labels[labelKeyDeprecatedInternalID] = strconv.FormatInt(id, 10)
m.obj.SetLabels(labels)
}
// This allows looking up a primary and secondary key -- if either exist the value will be returned
func (m *grafanaMetaAccessor) getAnnoValue(primary, secondary string) (string, bool) {
v, ok := m.obj.GetAnnotations()[primary]
+22
View File
@@ -154,6 +154,28 @@ func TestMetaAccessor(t *testing.T) {
require.NoError(t, err) // Must be a pointer
})
t.Run("get and set grafana labels (unstructured)", func(t *testing.T) {
res := &unstructured.Unstructured{
Object: map[string]any{},
}
meta, err := utils.MetaAccessor(res)
require.NoError(t, err)
// should return 0 when not set
require.Equal(t, meta.GetDeprecatedInternalID(), int64(0))
// 0 is not allowed
meta.SetDeprecatedInternalID(0)
require.Equal(t, map[string]string(nil), res.GetLabels())
// should be able to set and get
meta.SetDeprecatedInternalID(1)
require.Equal(t, map[string]string{
"grafana.app/deprecatedInternalID": "1",
}, res.GetLabels())
require.Equal(t, meta.GetDeprecatedInternalID(), int64(1))
})
t.Run("get and set grafana metadata (unstructured)", func(t *testing.T) {
// Error reading spec+status when missing
res := &unstructured.Unstructured{