K8s: Refactor config/options for aggregation (#81739)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
clientrest "k8s.io/client-go/rest"
|
||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
func FormatKubeConfig(restConfig *clientrest.Config) clientcmdapi.Config {
|
||||
clusters := make(map[string]*clientcmdapi.Cluster)
|
||||
clusters["default-cluster"] = &clientcmdapi.Cluster{
|
||||
Server: restConfig.Host,
|
||||
InsecureSkipTLSVerify: true,
|
||||
}
|
||||
|
||||
contexts := make(map[string]*clientcmdapi.Context)
|
||||
contexts["default-context"] = &clientcmdapi.Context{
|
||||
Cluster: "default-cluster",
|
||||
Namespace: "default",
|
||||
AuthInfo: "default",
|
||||
}
|
||||
|
||||
authinfos := make(map[string]*clientcmdapi.AuthInfo)
|
||||
authinfos["default"] = &clientcmdapi.AuthInfo{
|
||||
Token: restConfig.BearerToken,
|
||||
}
|
||||
|
||||
return clientcmdapi.Config{
|
||||
Kind: "Config",
|
||||
APIVersion: "v1",
|
||||
Clusters: clusters,
|
||||
Contexts: contexts,
|
||||
CurrentContext: "default-context",
|
||||
AuthInfos: authinfos,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// Annotation keys
|
||||
|
||||
const AnnoKeyCreatedBy = "grafana.app/createdBy"
|
||||
const AnnoKeyUpdatedTimestamp = "grafana.app/updatedTimestamp"
|
||||
const AnnoKeyUpdatedBy = "grafana.app/updatedBy"
|
||||
const AnnoKeyFolder = "grafana.app/folder"
|
||||
const AnnoKeySlug = "grafana.app/slug"
|
||||
|
||||
// Identify where values came from
|
||||
|
||||
const AnnoKeyOriginName = "grafana.app/originName"
|
||||
const AnnoKeyOriginPath = "grafana.app/originPath"
|
||||
const AnnoKeyOriginKey = "grafana.app/originKey"
|
||||
const AnnoKeyOriginTimestamp = "grafana.app/originTimestamp"
|
||||
|
||||
// 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
|
||||
_ any `json:"-"`
|
||||
}
|
||||
|
||||
// Accessor functions for k8s objects
|
||||
type GrafanaResourceMetaAccessor interface {
|
||||
GetUpdatedTimestamp() (*time.Time, error)
|
||||
SetUpdatedTimestamp(v *time.Time)
|
||||
SetUpdatedTimestampMillis(unix int64)
|
||||
GetCreatedBy() string
|
||||
SetCreatedBy(user string)
|
||||
GetUpdatedBy() string
|
||||
SetUpdatedBy(user string)
|
||||
GetFolder() string
|
||||
SetFolder(uid string)
|
||||
GetSlug() string
|
||||
SetSlug(v string)
|
||||
GetOriginInfo() (*ResourceOriginInfo, error)
|
||||
SetOriginInfo(info *ResourceOriginInfo)
|
||||
GetOriginName() string
|
||||
GetOriginPath() string
|
||||
GetOriginKey() string
|
||||
GetOriginTimestamp() (*time.Time, error)
|
||||
|
||||
// Find a title in the object
|
||||
// This will reflect the object and try to get:
|
||||
// * spec.title
|
||||
// * spec.name
|
||||
// * title
|
||||
// and return an empty string if nothing was found
|
||||
FindTitle(defaultTitle string) string
|
||||
}
|
||||
|
||||
var _ GrafanaResourceMetaAccessor = (*grafanaResourceMetaAccessor)(nil)
|
||||
|
||||
type grafanaResourceMetaAccessor struct {
|
||||
raw interface{} // the original object (it implements metav1.Object)
|
||||
obj metav1.Object
|
||||
}
|
||||
|
||||
// Accessor takes an arbitrary object pointer and returns meta.Interface.
|
||||
// obj must be a pointer to an API type. An error is returned if the minimum
|
||||
// required fields are missing. Fields that are not required return the default
|
||||
// value and are a no-op if set.
|
||||
func MetaAccessor(raw interface{}) (GrafanaResourceMetaAccessor, error) {
|
||||
obj, err := meta.Accessor(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &grafanaResourceMetaAccessor{raw, obj}, nil
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) set(key string, val string) {
|
||||
anno := m.obj.GetAnnotations()
|
||||
if val == "" {
|
||||
if anno != nil {
|
||||
delete(anno, key)
|
||||
}
|
||||
} else {
|
||||
if anno == nil {
|
||||
anno = make(map[string]string)
|
||||
}
|
||||
anno[key] = val
|
||||
}
|
||||
m.obj.SetAnnotations(anno)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) get(key string) string {
|
||||
return m.obj.GetAnnotations()[key]
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetUpdatedTimestamp() (*time.Time, error) {
|
||||
v, ok := m.obj.GetAnnotations()[AnnoKeyUpdatedTimestamp]
|
||||
if !ok || v == "" {
|
||||
return nil, nil
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339, v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid updated timestamp: %s", err.Error())
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetUpdatedTimestampMillis(v int64) {
|
||||
if v > 0 {
|
||||
t := time.UnixMilli(v)
|
||||
m.SetUpdatedTimestamp(&t)
|
||||
} else {
|
||||
m.set(AnnoKeyUpdatedTimestamp, "") // will clear the annotation
|
||||
}
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetUpdatedTimestamp(v *time.Time) {
|
||||
txt := ""
|
||||
if v != nil && v.Unix() != 0 {
|
||||
txt = v.UTC().Format(time.RFC3339)
|
||||
}
|
||||
m.set(AnnoKeyUpdatedTimestamp, txt)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetCreatedBy() string {
|
||||
return m.get(AnnoKeyCreatedBy)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetCreatedBy(user string) {
|
||||
m.set(AnnoKeyCreatedBy, user)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetUpdatedBy() string {
|
||||
return m.get(AnnoKeyUpdatedBy)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetUpdatedBy(user string) {
|
||||
m.set(AnnoKeyUpdatedBy, user)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetFolder() string {
|
||||
return m.get(AnnoKeyFolder)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetFolder(uid string) {
|
||||
m.set(AnnoKeyFolder, uid)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetSlug() string {
|
||||
return m.get(AnnoKeySlug)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetSlug(v string) {
|
||||
m.set(AnnoKeySlug, v)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) SetOriginInfo(info *ResourceOriginInfo) {
|
||||
anno := m.obj.GetAnnotations()
|
||||
if anno == nil {
|
||||
if info == nil {
|
||||
return
|
||||
}
|
||||
anno = make(map[string]string, 0)
|
||||
}
|
||||
|
||||
delete(anno, AnnoKeyOriginName)
|
||||
delete(anno, AnnoKeyOriginPath)
|
||||
delete(anno, AnnoKeyOriginKey)
|
||||
delete(anno, AnnoKeyOriginTimestamp)
|
||||
if info != nil && info.Name != "" {
|
||||
anno[AnnoKeyOriginName] = info.Name
|
||||
if info.Path != "" {
|
||||
anno[AnnoKeyOriginPath] = info.Path
|
||||
}
|
||||
if info.Key != "" {
|
||||
anno[AnnoKeyOriginKey] = info.Key
|
||||
}
|
||||
if info.Timestamp != nil {
|
||||
anno[AnnoKeyOriginTimestamp] = info.Timestamp.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
m.obj.SetAnnotations(anno)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetOriginInfo() (*ResourceOriginInfo, error) {
|
||||
v, ok := m.obj.GetAnnotations()[AnnoKeyOriginName]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
t, err := m.GetOriginTimestamp()
|
||||
return &ResourceOriginInfo{
|
||||
Name: v,
|
||||
Path: m.GetOriginPath(),
|
||||
Key: m.GetOriginKey(),
|
||||
Timestamp: t,
|
||||
}, err
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetOriginName() string {
|
||||
return m.get(AnnoKeyOriginName)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetOriginPath() string {
|
||||
return m.get(AnnoKeyOriginPath)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetOriginKey() string {
|
||||
return m.get(AnnoKeyOriginKey)
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) GetOriginTimestamp() (*time.Time, error) {
|
||||
v, ok := m.obj.GetAnnotations()[AnnoKeyOriginTimestamp]
|
||||
if !ok || v == "" {
|
||||
return nil, nil
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339, v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid origin timestamp: %s", err.Error())
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (m *grafanaResourceMetaAccessor) FindTitle(defaultTitle string) string {
|
||||
// look for Spec.Title or Spec.Name
|
||||
r := reflect.ValueOf(m.raw)
|
||||
if r.Kind() == reflect.Ptr || r.Kind() == reflect.Interface {
|
||||
r = r.Elem()
|
||||
}
|
||||
if r.Kind() == reflect.Struct {
|
||||
spec := r.FieldByName("Spec")
|
||||
if spec.Kind() == reflect.Struct {
|
||||
title := spec.FieldByName("Title")
|
||||
if title.IsValid() && title.Kind() == reflect.String {
|
||||
return title.String()
|
||||
}
|
||||
name := spec.FieldByName("Name")
|
||||
if name.IsValid() && name.Kind() == reflect.String {
|
||||
return name.String()
|
||||
}
|
||||
}
|
||||
|
||||
title := r.FieldByName("Title")
|
||||
if title.IsValid() && title.Kind() == reflect.String {
|
||||
return title.String()
|
||||
}
|
||||
}
|
||||
return defaultTitle
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/utils"
|
||||
)
|
||||
|
||||
type TestResource struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
// Standard object's metadata
|
||||
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
|
||||
// +optional
|
||||
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||||
|
||||
Spec Spec `json:"spec,omitempty"`
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *TestResource) DeepCopyInto(out *TestResource) {
|
||||
*out = *in
|
||||
out.TypeMeta = in.TypeMeta
|
||||
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
|
||||
in.Spec.DeepCopyInto(&out.Spec)
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Playlist.
|
||||
func (in *TestResource) DeepCopy() *TestResource {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(TestResource)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *TestResource) DeepCopyObject() runtime.Object {
|
||||
if c := in.DeepCopy(); c != nil {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Spec defines model for Spec.
|
||||
type Spec struct {
|
||||
// Name of the object.
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Spec) DeepCopyInto(out *Spec) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Spec.
|
||||
func (in *Spec) DeepCopy() *Spec {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Spec)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
type TestResource2 struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
// Standard object's metadata
|
||||
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
|
||||
// +optional
|
||||
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||||
|
||||
Spec Spec2 `json:"spec,omitempty"`
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *TestResource2) DeepCopyInto(out *TestResource2) {
|
||||
*out = *in
|
||||
out.TypeMeta = in.TypeMeta
|
||||
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
|
||||
in.Spec.DeepCopyInto(&out.Spec)
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Playlist.
|
||||
func (in *TestResource2) DeepCopy() *TestResource2 {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(TestResource2)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *TestResource2) DeepCopyObject() runtime.Object {
|
||||
if c := in.DeepCopy(); c != nil {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Spec defines model for Spec.
|
||||
type Spec2 struct{}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Spec2) DeepCopyInto(out *Spec2) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Spec.
|
||||
func (in *Spec2) DeepCopy() *Spec2 {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Spec2)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func TestMetaAccessor(t *testing.T) {
|
||||
originInfo := &utils.ResourceOriginInfo{
|
||||
Name: "test",
|
||||
Path: "a/b/c",
|
||||
Key: "kkk",
|
||||
}
|
||||
|
||||
t.Run("fails for non resource objects", func(t *testing.T) {
|
||||
_, err := utils.MetaAccessor("hello")
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = utils.MetaAccessor(unstructured.Unstructured{})
|
||||
require.Error(t, err) // Not a pointer!
|
||||
|
||||
_, err = utils.MetaAccessor(&unstructured.Unstructured{})
|
||||
require.NoError(t, err) // Must be a pointer
|
||||
|
||||
_, err = utils.MetaAccessor(&TestResource{
|
||||
Spec: Spec{
|
||||
Title: "HELLO",
|
||||
},
|
||||
})
|
||||
require.NoError(t, err) // Must be a pointer
|
||||
})
|
||||
|
||||
t.Run("get and set grafana metadata", func(t *testing.T) {
|
||||
res := &unstructured.Unstructured{}
|
||||
meta, err := utils.MetaAccessor(res)
|
||||
require.NoError(t, err)
|
||||
|
||||
meta.SetOriginInfo(originInfo)
|
||||
meta.SetFolder("folderUID")
|
||||
|
||||
require.Equal(t, map[string]string{
|
||||
"grafana.app/originName": "test",
|
||||
"grafana.app/originPath": "a/b/c",
|
||||
"grafana.app/originKey": "kkk",
|
||||
"grafana.app/folder": "folderUID",
|
||||
}, res.GetAnnotations())
|
||||
})
|
||||
|
||||
t.Run("find titles", func(t *testing.T) {
|
||||
// with a k8s object that has Spec.Title
|
||||
obj := &TestResource{
|
||||
Spec: Spec{
|
||||
Title: "HELLO",
|
||||
},
|
||||
}
|
||||
|
||||
meta, err := utils.MetaAccessor(obj)
|
||||
require.NoError(t, err)
|
||||
meta.SetOriginInfo(originInfo)
|
||||
meta.SetFolder("folderUID")
|
||||
|
||||
require.Equal(t, map[string]string{
|
||||
"grafana.app/originName": "test",
|
||||
"grafana.app/originPath": "a/b/c",
|
||||
"grafana.app/originKey": "kkk",
|
||||
"grafana.app/folder": "folderUID",
|
||||
}, obj.GetAnnotations())
|
||||
|
||||
require.Equal(t, "HELLO", obj.Spec.Title)
|
||||
require.Equal(t, "HELLO", meta.FindTitle(""))
|
||||
obj.Spec.Title = ""
|
||||
require.Equal(t, "", meta.FindTitle("xxx"))
|
||||
|
||||
// with a k8s object without Spec.Title
|
||||
obj2 := &TestResource2{}
|
||||
|
||||
meta, err = utils.MetaAccessor(obj2)
|
||||
require.NoError(t, err)
|
||||
meta.SetOriginInfo(originInfo)
|
||||
meta.SetFolder("folderUID")
|
||||
|
||||
require.Equal(t, map[string]string{
|
||||
"grafana.app/originName": "test",
|
||||
"grafana.app/originPath": "a/b/c",
|
||||
"grafana.app/originKey": "kkk",
|
||||
"grafana.app/folder": "folderUID",
|
||||
}, obj2.GetAnnotations())
|
||||
|
||||
require.Equal(t, "xxx", meta.FindTitle("xxx"))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
)
|
||||
|
||||
// Based on https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apiserver/pkg/registry/rest/table.go
|
||||
type customTableConvertor struct {
|
||||
gr schema.GroupResource
|
||||
columns []metav1.TableColumnDefinition
|
||||
reader func(obj any) ([]interface{}, error)
|
||||
}
|
||||
|
||||
func NewTableConverter(gr schema.GroupResource, columns []metav1.TableColumnDefinition, reader func(obj any) ([]interface{}, error)) rest.TableConvertor {
|
||||
converter := customTableConvertor{
|
||||
gr: gr,
|
||||
columns: columns,
|
||||
reader: reader,
|
||||
}
|
||||
// Replace the description on standard columns with the global values
|
||||
for idx, column := range converter.columns {
|
||||
if column.Description == "" {
|
||||
switch column.Name {
|
||||
case "Name":
|
||||
converter.columns[idx].Description = swaggerMetadataDescriptions["name"]
|
||||
case "Created At":
|
||||
converter.columns[idx].Description = swaggerMetadataDescriptions["creationTimestamp"]
|
||||
}
|
||||
}
|
||||
}
|
||||
return converter
|
||||
}
|
||||
|
||||
func NewDefaultTableConverter(gr schema.GroupResource) rest.TableConvertor {
|
||||
return NewTableConverter(gr,
|
||||
[]metav1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name"},
|
||||
{Name: "Created At", Type: "date"},
|
||||
},
|
||||
func(obj any) ([]interface{}, error) {
|
||||
v, err := meta.Accessor(obj)
|
||||
if err == nil && v != nil {
|
||||
return []interface{}{
|
||||
v.GetName(),
|
||||
v.GetCreationTimestamp().UTC().Format(time.RFC3339),
|
||||
}, nil
|
||||
}
|
||||
|
||||
r := reflect.ValueOf(obj).Elem()
|
||||
n := r.FieldByName("Name").String()
|
||||
if n != "" {
|
||||
return []interface{}{
|
||||
n,
|
||||
"",
|
||||
}, nil
|
||||
}
|
||||
|
||||
return []interface{}{
|
||||
fmt.Sprintf("%v", obj),
|
||||
"",
|
||||
}, nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
var _ rest.TableConvertor = &customTableConvertor{}
|
||||
var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()
|
||||
|
||||
func (c customTableConvertor) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
table, ok := object.(*metav1.Table)
|
||||
if ok {
|
||||
return table, nil
|
||||
} else {
|
||||
table = &metav1.Table{}
|
||||
}
|
||||
fn := func(obj runtime.Object) error {
|
||||
cells, err := c.reader(obj)
|
||||
if err != nil {
|
||||
resource := c.gr
|
||||
if info, ok := request.RequestInfoFrom(ctx); ok {
|
||||
resource = schema.GroupResource{Group: info.APIGroup, Resource: info.Resource}
|
||||
}
|
||||
return errNotAcceptable{resource: resource}
|
||||
}
|
||||
table.Rows = append(table.Rows, metav1.TableRow{
|
||||
Cells: cells,
|
||||
Object: runtime.RawExtension{Object: obj},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case meta.IsListType(object):
|
||||
if err := meta.EachListItem(object, fn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
if err := fn(object); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if m, err := meta.ListAccessor(object); err == nil {
|
||||
table.ResourceVersion = m.GetResourceVersion()
|
||||
table.Continue = m.GetContinue()
|
||||
table.RemainingItemCount = m.GetRemainingItemCount()
|
||||
} else {
|
||||
if m, err := meta.CommonAccessor(object); err == nil {
|
||||
table.ResourceVersion = m.GetResourceVersion()
|
||||
}
|
||||
}
|
||||
if opt, ok := tableOptions.(*metav1.TableOptions); !ok || !opt.NoHeaders {
|
||||
table.ColumnDefinitions = c.columns
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
||||
// errNotAcceptable indicates the resource doesn't support Table conversion
|
||||
type errNotAcceptable struct {
|
||||
resource schema.GroupResource
|
||||
}
|
||||
|
||||
func (e errNotAcceptable) Error() string {
|
||||
return fmt.Sprintf("the resource %s does not support being converted to a Table", e.resource)
|
||||
}
|
||||
|
||||
func (e errNotAcceptable) Status() metav1.Status {
|
||||
return metav1.Status{
|
||||
Status: metav1.StatusFailure,
|
||||
Code: http.StatusNotAcceptable,
|
||||
Reason: metav1.StatusReason("NotAcceptable"),
|
||||
Message: e.Error(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/utils"
|
||||
)
|
||||
|
||||
func TestTableConverter(t *testing.T) {
|
||||
// dummy converter
|
||||
converter := utils.NewTableConverter(
|
||||
schema.GroupResource{Group: "x", Resource: "y"},
|
||||
[]metav1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name"},
|
||||
{Name: "Dummy", Type: "string", Format: "string", Description: "Something here"},
|
||||
{Name: "Created At", Type: "date"},
|
||||
},
|
||||
func(obj any) ([]interface{}, error) {
|
||||
m, ok := obj.(*metav1.APIGroup)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected status")
|
||||
}
|
||||
ts := metav1.NewTime(time.UnixMilli(10000000))
|
||||
return []interface{}{
|
||||
m.Name,
|
||||
"dummy",
|
||||
ts.Time.UTC().Format(time.RFC3339),
|
||||
}, nil
|
||||
},
|
||||
)
|
||||
|
||||
// Convert a single table
|
||||
table, err := converter.ConvertToTable(context.Background(), &metav1.APIGroup{
|
||||
Name: "hello",
|
||||
}, nil)
|
||||
require.NoError(t, err)
|
||||
out, err := json.MarshalIndent(table, "", " ")
|
||||
require.NoError(t, err)
|
||||
//fmt.Printf("%s", string(out))
|
||||
require.JSONEq(t, `{
|
||||
"metadata": {},
|
||||
"columnDefinitions": [
|
||||
{
|
||||
"name": "Name",
|
||||
"type": "string",
|
||||
"format": "name",
|
||||
"description": "Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names",
|
||||
"priority": 0
|
||||
},
|
||||
{
|
||||
"name": "Dummy",
|
||||
"type": "string",
|
||||
"format": "string",
|
||||
"description": "Something here",
|
||||
"priority": 0
|
||||
},
|
||||
{
|
||||
"name": "Created At",
|
||||
"type": "date",
|
||||
"format": "",
|
||||
"description": "CreationTimestamp is a timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC.\n\nPopulated by the system. Read-only. Null for lists. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata",
|
||||
"priority": 0
|
||||
}
|
||||
],
|
||||
"rows": [
|
||||
{
|
||||
"cells": [
|
||||
"hello",
|
||||
"dummy",
|
||||
"1970-01-01T02:46:40Z"
|
||||
],
|
||||
"object": {
|
||||
"name": "hello",
|
||||
"versions": null,
|
||||
"preferredVersion": {
|
||||
"groupVersion": "",
|
||||
"version": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}`, string(out))
|
||||
|
||||
// Convert something else
|
||||
table, err = converter.ConvertToTable(context.Background(), &metav1.Status{}, nil)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, table)
|
||||
require.Equal(t, "the resource y.x does not support being converted to a Table", err.Error())
|
||||
|
||||
// Default table converter
|
||||
// Convert a single table
|
||||
converter = utils.NewDefaultTableConverter(schema.GroupResource{Group: "x", Resource: "y"})
|
||||
table, err = converter.ConvertToTable(context.Background(), &metav1.APIGroup{
|
||||
Name: "hello",
|
||||
}, nil)
|
||||
require.NoError(t, err)
|
||||
out, err = json.MarshalIndent(table.Rows, "", " ")
|
||||
require.NoError(t, err)
|
||||
//fmt.Printf("%s", string(out))
|
||||
require.JSONEq(t, `[
|
||||
{
|
||||
"cells": [
|
||||
"hello",
|
||||
""
|
||||
],
|
||||
"object": {
|
||||
"name": "hello",
|
||||
"versions": null,
|
||||
"preferredVersion": {
|
||||
"groupVersion": "",
|
||||
"version": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
]`, string(out))
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
// Create a stable UID that will be unique across a multi-tenant cluster
|
||||
// This is useful while we migrate from SQL storage to something where the UID (GUID)
|
||||
// is actually baked into the storage engine itself.
|
||||
func CalculateClusterWideUID(obj runtime.Object) types.UID {
|
||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||
hasher := sha256.New()
|
||||
hasher.Write([]byte(gvk.Group))
|
||||
hasher.Write([]byte("|"))
|
||||
hasher.Write([]byte(gvk.Kind))
|
||||
hasher.Write([]byte("|"))
|
||||
meta, err := meta.Accessor(obj)
|
||||
if err == nil {
|
||||
hasher.Write([]byte(meta.GetNamespace()))
|
||||
hasher.Write([]byte("|"))
|
||||
hasher.Write([]byte(meta.GetName()))
|
||||
}
|
||||
v := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
|
||||
return types.UID(strings.Map(func(r rune) rune {
|
||||
if !(unicode.IsLetter(r) || unicode.IsDigit(r)) {
|
||||
return 'X'
|
||||
}
|
||||
return r
|
||||
}, v))
|
||||
}
|
||||
Reference in New Issue
Block a user