Kinds: Use apimachinery ObjectMeta for metadata (#68668)

This commit is contained in:
Ryan McKinley
2023-05-24 09:13:44 -07:00
committed by GitHub
parent f91c1b9897
commit c66d5721f7
22 changed files with 695 additions and 11 deletions

View File

@@ -9,7 +9,30 @@
package dashboard
// Resource is the wire representation of Dashboard. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of Dashboard. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "Dashboard",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of Dashboard.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package folder
// Resource is the wire representation of Folder. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of Folder. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "Folder",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of Folder.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

150
pkg/kinds/general.go Normal file
View File

@@ -0,0 +1,150 @@
package kinds
import (
"time"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ResourceOriginInfo is saved in annotations. This is used to identify where the resource came from
// This object can model the same data as our existing provisioning table or a more general git sync
type ResourceOriginInfo struct {
// Name of the origin/provisioning source
Name string `json:"name,omitempty"`
// The path within the named origin above (external_id in the existing dashboard provisioing)
Path string `json:"path,omitempty"`
// Verification/identification key (check_sum in existing dashboard provisioning)
Key string `json:"key,omitempty"`
// Origin modification timestamp when the resource was saved
// This will be before the resource updated time
Timestamp *time.Time `json:"time,omitempty"`
// Avoid extending
_ interface{}
}
// GrafanaResourceMetadata is standard k8s object metadata with helper functions
type GrafanaResourceMetadata v1.ObjectMeta
// GrafanaResource is a generic kubernetes resource with a helper for the common grafana metadata
// This is a temporary solution until this object (or similar) can be moved to the app-sdk or kindsys
type GrafanaResource[Spec interface{}, Status interface{}] struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata GrafanaResourceMetadata `json:"metadata"`
Spec *Spec `json:"spec,omitempty"`
Status *Status `json:"status,omitempty"`
// Avoid extending
_ interface{}
}
// Annotation keys
const annoKeyCreatedBy = "grafana.com/createdBy"
const annoKeyUpdatedTimestamp = "grafana.com/updatedTimestamp"
const annoKeyUpdatedBy = "grafana.com/updatedBy"
// The folder identifier
const annoKeyFolder = "grafana.com/folder"
const annoKeySlug = "grafana.com/slug"
// Identify where values came from
const annoKeyOriginName = "grafana.com/origin/name"
const annoKeyOriginPath = "grafana.com/origin/path"
const annoKeyOriginKey = "grafana.com/origin/key"
const annoKeyOriginTime = "grafana.com/origin/time"
func (m *GrafanaResourceMetadata) GetUpdatedTimestamp() *time.Time {
v, ok := m.Annotations[annoKeyUpdatedTimestamp]
if ok {
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return &t
}
}
return nil
}
func (m *GrafanaResourceMetadata) SetUpdatedTimestamp(v *time.Time) {
if v == nil {
delete(m.Annotations, annoKeyUpdatedTimestamp)
} else {
m.Annotations[annoKeyUpdatedTimestamp] = v.Format(time.RFC3339)
}
}
func (m *GrafanaResourceMetadata) GetCreatedBy() string {
return m.Annotations[annoKeyCreatedBy]
}
func (m *GrafanaResourceMetadata) SetCreatedBy(user string) {
m.Annotations[annoKeyCreatedBy] = user // user GRN
}
func (m *GrafanaResourceMetadata) GetUpdatedBy() string {
return m.Annotations[annoKeyUpdatedBy]
}
func (m *GrafanaResourceMetadata) SetUpdatedBy(user string) {
m.Annotations[annoKeyUpdatedBy] = user // user GRN
}
func (m *GrafanaResourceMetadata) GetFolder() string {
return m.Annotations[annoKeyFolder]
}
func (m *GrafanaResourceMetadata) SetFolder(uid string) {
m.Annotations[annoKeyFolder] = uid
}
func (m *GrafanaResourceMetadata) GetSlug() string {
return m.Annotations[annoKeySlug]
}
func (m *GrafanaResourceMetadata) SetSlug(v string) {
m.Annotations[annoKeySlug] = v
}
func (m *GrafanaResourceMetadata) SetOriginInfo(info *ResourceOriginInfo) {
delete(m.Annotations, annoKeyOriginName)
delete(m.Annotations, annoKeyOriginPath)
delete(m.Annotations, annoKeyOriginKey)
delete(m.Annotations, annoKeyOriginTime)
if info != nil || info.Name != "" {
m.Annotations[annoKeyOriginName] = info.Name
if info.Path != "" {
m.Annotations[annoKeyOriginPath] = info.Path
}
if info.Key != "" {
m.Annotations[annoKeyOriginKey] = info.Key
}
if info.Timestamp != nil {
m.Annotations[annoKeyOriginTime] = info.Timestamp.Format(time.RFC3339)
}
}
}
// GetOriginInfo returns the origin info stored in k8s metadata annotations
func (m *GrafanaResourceMetadata) GetOriginInfo() *ResourceOriginInfo {
v, ok := m.Annotations[annoKeyOriginName]
if !ok {
return nil
}
info := &ResourceOriginInfo{
Name: v,
Path: m.Annotations[annoKeyOriginPath],
Key: m.Annotations[annoKeyOriginKey],
}
v, ok = m.Annotations[annoKeyOriginTime]
if ok {
t, err := time.Parse(time.RFC3339, v)
if err != nil {
info.Timestamp = &t
}
}
return info
}

View File

@@ -9,7 +9,30 @@
package librarypanel
// Resource is the wire representation of LibraryPanel. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of LibraryPanel. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "LibraryPanel",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of LibraryPanel.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package playlist
// Resource is the wire representation of Playlist. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of Playlist. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "Playlist",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of Playlist.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package preferences
// Resource is the wire representation of Preferences. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of Preferences. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "Preferences",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of Preferences.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package publicdashboard
// Resource is the wire representation of PublicDashboard. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of PublicDashboard. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "PublicDashboard",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of PublicDashboard.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package serviceaccount
// Resource is the wire representation of ServiceAccount. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of ServiceAccount. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "ServiceAccount",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of ServiceAccount.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`

View File

@@ -9,7 +9,30 @@
package team
// Resource is the wire representation of Team. (TODO be better)
import (
"github.com/grafana/grafana/pkg/kinds"
)
// Resource is the kubernetes style representation of Team. (TODO be better)
type K8sResource = kinds.GrafanaResource[Spec, Status]
// NewResource creates a new instance of the resource with a given name (UID)
func NewK8sResource(name string, s *Spec) K8sResource {
return K8sResource{
Kind: "Team",
APIVersion: "v0.0-alpha",
Metadata: kinds.GrafanaResourceMetadata{
Name: name,
Annotations: make(map[string]string),
Labels: make(map[string]string),
},
Spec: s,
}
}
// Resource is the wire representation of Team.
// It currently will soon be merged into the k8s flavor (TODO be better)
type Resource struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`