DataSource: Support config CRUD from apiservers (#106996)

This commit is contained in:
Ryan McKinley
2025-08-28 22:28:26 +03:00
committed by GitHub
parent 15fab1cb99
commit eda94a6434
61 changed files with 2041 additions and 468 deletions
@@ -0,0 +1,73 @@
package v0alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
)
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DataSource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
// DataSource configuration -- these properties are all visible
// to anyone able to query the data source from their browser
Spec UnstructuredSpec `json:"spec"`
// Secure values allows setting values that are never shown to users
// The returned properties are only the names of the configured values
Secure common.InlineSecureValues `json:"secure,omitzero,omitempty"`
}
// DsAccess represents how the datasource connects to the remote service
// +k8s:openapi-gen=true
// +enum
type DsAccess string
const (
// The frontend can connect directly to the remote URL
// This method is discouraged
DsAccessDirect DsAccess = "direct"
// Connect to the remote datasource through the grafana backend
DsAccessProxy DsAccess = "proxy"
)
func (dsa DsAccess) String() string {
return string(dsa)
}
// +k8s:openapi-gen=true
type GenericDataSourceSpec struct {
// The display name (previously saved as the "name" property)
Title string `json:"title"`
Access DsAccess `json:"access,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
IsDefault bool `json:"isDefault,omitempty"`
// Server URL
URL string `json:"url,omitempty"`
User string `json:"user,omitempty"`
Database string `json:"database,omitempty"`
BasicAuth bool `json:"basicAuth,omitempty"`
BasicAuthUser string `json:"basicAuthUser,omitempty"`
WithCredentials bool `json:"withCredentials,omitempty"`
// Generic unstructured configuration settings
JsonData common.Unstructured `json:"jsonData,omitzero"`
}
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DataSourceList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []DataSource `json:"items"`
}
-3
View File
@@ -1,6 +1,3 @@
// +k8s:deepcopy-gen=package
// +k8s:openapi-gen=true
// +k8s:defaulter-gen=TypeMeta
// +groupName=datasource.grafana.com
package v0alpha1
+11 -12
View File
@@ -4,9 +4,10 @@ import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/apimachinery/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/grafana/grafana/pkg/apimachinery/utils"
)
const (
@@ -14,26 +15,24 @@ const (
VERSION = "v0alpha1"
)
var GenericConnectionResourceInfo = utils.NewResourceInfo(GROUP, VERSION,
"connections", "connection", "DataSourceConnection",
func() runtime.Object { return &DataSourceConnection{} },
func() runtime.Object { return &DataSourceConnectionList{} },
var DataSourceResourceInfo = utils.NewResourceInfo(GROUP, VERSION,
"datasources", "datasource", "DataSource",
func() runtime.Object { return &DataSource{} },
func() runtime.Object { return &DataSourceList{} },
utils.TableColumns{
Definition: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name"},
{Name: "Title", Type: "string", Format: "string", Description: "The datasource title"},
{Name: "APIVersion", Type: "string", Format: "string", Description: "API Version"},
{Name: "Title", Type: "string", Format: "string", Description: "Title"},
{Name: "Created At", Type: "date"},
},
Reader: func(obj any) ([]interface{}, error) {
m, ok := obj.(*DataSourceConnection)
Reader: func(obj any) ([]any, error) {
m, ok := obj.(*DataSource)
if !ok {
return nil, fmt.Errorf("expected connection")
}
return []interface{}{
return []any{
m.Name,
m.Title,
m.APIVersion,
m.Spec.Object["title"],
m.CreationTimestamp.UTC().Format(time.RFC3339),
}, nil
},
+2 -20
View File
@@ -6,26 +6,8 @@ import (
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DataSourceConnection struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
// The display name
Title string `json:"title"`
// Optional description for the data source (does not exist yet)
Description string `json:"description,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DataSourceConnectionList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DataSourceConnection `json:"items"`
}
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type HealthCheckResult struct {
metav1.TypeMeta `json:",inline"`
@@ -0,0 +1,173 @@
package v0alpha1
import (
"encoding/json"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
openapi "k8s.io/kube-openapi/pkg/common"
spec "k8s.io/kube-openapi/pkg/validation/spec"
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
)
// UnstructuredSpec allows any property to be saved into the spec
// Validation will happen from the dynamically loaded schemas for each datasource
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type UnstructuredSpec common.Unstructured
func (u *UnstructuredSpec) GetString(key string) string {
if u.Object == nil {
return ""
}
v := u.Object[key]
str, _ := v.(string)
return str
}
func (u *UnstructuredSpec) Set(key string, val any) *UnstructuredSpec {
if u.Object == nil {
u.Object = make(map[string]any)
}
if val == nil || val == "" || val == false {
delete(u.Object, key)
} else {
u.Object[key] = val
}
return u
}
func (u *UnstructuredSpec) Title() string {
return u.GetString("title")
}
func (u *UnstructuredSpec) SetTitle(v string) *UnstructuredSpec {
return u.Set("title", v)
}
func (u *UnstructuredSpec) URL() string {
return u.GetString("url")
}
func (u *UnstructuredSpec) SetURL(v string) *UnstructuredSpec {
return u.Set("url", v)
}
func (u *UnstructuredSpec) Database() string {
return u.GetString("database")
}
func (u *UnstructuredSpec) SetDatabase(v string) *UnstructuredSpec {
return u.Set("database", v)
}
func (u *UnstructuredSpec) Access() DsAccess {
return DsAccess(u.GetString("access"))
}
func (u *UnstructuredSpec) SetAccess(v string) *UnstructuredSpec {
return u.Set("access", v)
}
func (u *UnstructuredSpec) User() string {
return u.GetString("user")
}
func (u *UnstructuredSpec) SetUser(v string) *UnstructuredSpec {
return u.Set("user", v)
}
func (u *UnstructuredSpec) BasicAuth() bool {
v, _, _ := unstructured.NestedBool(u.Object, "basicAuth")
return v
}
func (u *UnstructuredSpec) SetBasicAuth(v bool) *UnstructuredSpec {
return u.Set("basicAuth", v)
}
func (u *UnstructuredSpec) BasicAuthUser() string {
return u.GetString("basicAuthUser")
}
func (u *UnstructuredSpec) SetBasicAuthUser(v string) *UnstructuredSpec {
return u.Set("basicAuthUser", v)
}
func (u *UnstructuredSpec) WithCredentials() bool {
v, _, _ := unstructured.NestedBool(u.Object, "withCredentials")
return v
}
func (u *UnstructuredSpec) SetWithCredentials(v bool) *UnstructuredSpec {
return u.Set("withCredentials", v)
}
func (u *UnstructuredSpec) IsDefault() bool {
v, _, _ := unstructured.NestedBool(u.Object, "isDefault")
return v
}
func (u *UnstructuredSpec) SetIsDefault(v bool) *UnstructuredSpec {
return u.Set("isDefault", v)
}
func (u *UnstructuredSpec) ReadOnly() bool {
v, _, _ := unstructured.NestedBool(u.Object, "readOnly")
return v
}
func (u *UnstructuredSpec) SetReadOnly(v bool) *UnstructuredSpec {
return u.Set("readOnly", v)
}
func (u *UnstructuredSpec) JSONData() any {
return u.Object["jsonData"]
}
func (u *UnstructuredSpec) SetJSONData(v any) *UnstructuredSpec {
return u.Set("jsonData", v)
}
// The OpenAPI spec uses the generated values from GenericDataSourceSpec, except that it:
// 1. Allows additional properties at the root
// 2. The jsonData field *may* be an raw value OR a map
func (UnstructuredSpec) OpenAPIDefinition() openapi.OpenAPIDefinition {
s := schema_pkg_apis_datasource_v0alpha1_GenericDataSourceSpec(func(path string) spec.Ref {
return spec.MustCreateRef(path)
})
s.Schema.AdditionalProperties = &spec.SchemaOrBool{
Allows: true,
}
return s
}
// MarshalJSON ensures that the unstructured object produces proper
// JSON when passed to Go's standard JSON library.
func (u *UnstructuredSpec) MarshalJSON() ([]byte, error) {
return json.Marshal(u.Object)
}
// UnmarshalJSON ensures that the unstructured object properly decodes
// JSON when passed to Go's standard JSON library.
func (u *UnstructuredSpec) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &u.Object)
}
func (u *UnstructuredSpec) DeepCopy() *UnstructuredSpec {
if u == nil {
return nil
}
out := new(UnstructuredSpec)
*out = *u
tmp := common.Unstructured{Object: u.Object}
copy := tmp.DeepCopy()
out.Object = copy.Object
return out
}
func (u *UnstructuredSpec) DeepCopyInto(out *UnstructuredSpec) {
clone := u.DeepCopy()
*out = *clone
}
@@ -8,29 +8,38 @@
package v0alpha1
import (
commonv0alpha1 "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
runtime "k8s.io/apimachinery/pkg/runtime"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *DataSourceConnection) DeepCopyInto(out *DataSourceConnection) {
func (in *DataSource) DeepCopyInto(out *DataSource) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
if in.Secure != nil {
in, out := &in.Secure, &out.Secure
*out = make(map[string]commonv0alpha1.InlineSecureValue, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DataSourceConnection.
func (in *DataSourceConnection) DeepCopy() *DataSourceConnection {
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DataSource.
func (in *DataSource) DeepCopy() *DataSource {
if in == nil {
return nil
}
out := new(DataSourceConnection)
out := new(DataSource)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *DataSourceConnection) DeepCopyObject() runtime.Object {
func (in *DataSource) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
@@ -38,13 +47,13 @@ func (in *DataSourceConnection) DeepCopyObject() runtime.Object {
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *DataSourceConnectionList) DeepCopyInto(out *DataSourceConnectionList) {
func (in *DataSourceList) DeepCopyInto(out *DataSourceList) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ListMeta.DeepCopyInto(&out.ListMeta)
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]DataSourceConnection, len(*in))
*out = make([]DataSource, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -52,18 +61,18 @@ func (in *DataSourceConnectionList) DeepCopyInto(out *DataSourceConnectionList)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DataSourceConnectionList.
func (in *DataSourceConnectionList) DeepCopy() *DataSourceConnectionList {
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DataSourceList.
func (in *DataSourceList) DeepCopy() *DataSourceList {
if in == nil {
return nil
}
out := new(DataSourceConnectionList)
out := new(DataSourceList)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *DataSourceConnectionList) DeepCopyObject() runtime.Object {
func (in *DataSourceList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
@@ -1,19 +0,0 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
// SPDX-License-Identifier: AGPL-3.0-only
// Code generated by defaulter-gen. DO NOT EDIT.
package v0alpha1
import (
runtime "k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
return nil
}
@@ -14,13 +14,15 @@ import (
func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
return map[string]common.OpenAPIDefinition{
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSourceConnection": schema_pkg_apis_datasource_v0alpha1_DataSourceConnection(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSourceConnectionList": schema_pkg_apis_datasource_v0alpha1_DataSourceConnectionList(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.HealthCheckResult": schema_pkg_apis_datasource_v0alpha1_HealthCheckResult(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSource": schema_pkg_apis_datasource_v0alpha1_DataSource(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSourceList": schema_pkg_apis_datasource_v0alpha1_DataSourceList(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.GenericDataSourceSpec": schema_pkg_apis_datasource_v0alpha1_GenericDataSourceSpec(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.HealthCheckResult": schema_pkg_apis_datasource_v0alpha1_HealthCheckResult(ref),
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.UnstructuredSpec": UnstructuredSpec{}.OpenAPIDefinition(),
}
}
func schema_pkg_apis_datasource_v0alpha1_DataSourceConnection(ref common.ReferenceCallback) common.OpenAPIDefinition {
func schema_pkg_apis_datasource_v0alpha1_DataSource(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
@@ -46,31 +48,37 @@ func schema_pkg_apis_datasource_v0alpha1_DataSourceConnection(ref common.Referen
Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"),
},
},
"title": {
"spec": {
SchemaProps: spec.SchemaProps{
Description: "The display name",
Default: "",
Type: []string{"string"},
Format: "",
Description: "DataSource configuration -- these properties are all visible to anyone able to query the data source from their browser",
Ref: ref("github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.UnstructuredSpec"),
},
},
"description": {
"secure": {
SchemaProps: spec.SchemaProps{
Description: "Optional description for the data source (does not exist yet)",
Type: []string{"string"},
Format: "",
Description: "Secure values allows setting values that are never shown to users The returned properties are only the names of the configured values",
Type: []string{"object"},
AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: map[string]interface{}{},
Ref: ref("github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1.InlineSecureValue"),
},
},
},
},
},
},
Required: []string{"title"},
Required: []string{"metadata", "spec"},
},
},
Dependencies: []string{
"k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
"github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1.InlineSecureValue", "github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.UnstructuredSpec", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
}
}
func schema_pkg_apis_datasource_v0alpha1_DataSourceConnectionList(ref common.ReferenceCallback) common.OpenAPIDefinition {
func schema_pkg_apis_datasource_v0alpha1_DataSourceList(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
@@ -103,18 +111,104 @@ func schema_pkg_apis_datasource_v0alpha1_DataSourceConnectionList(ref common.Ref
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: map[string]interface{}{},
Ref: ref("github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSourceConnection"),
Ref: ref("github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSource"),
},
},
},
},
},
},
Required: []string{"items"},
Required: []string{"metadata", "items"},
},
},
Dependencies: []string{
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSourceConnection", "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"},
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1.DataSource", "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"},
}
}
func schema_pkg_apis_datasource_v0alpha1_GenericDataSourceSpec(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"title": {
SchemaProps: spec.SchemaProps{
Description: "The display name (previously saved as the \"name\" property)",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"access": {
SchemaProps: spec.SchemaProps{
Description: "Possible enum values:\n - `\"direct\"` The frontend can connect directly to the remote URL This method is discouraged\n - `\"proxy\"` Connect to the remote datasource through the grafana backend",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"direct", "proxy"},
},
},
"readOnly": {
SchemaProps: spec.SchemaProps{
Type: []string{"boolean"},
Format: "",
},
},
"isDefault": {
SchemaProps: spec.SchemaProps{
Type: []string{"boolean"},
Format: "",
},
},
"url": {
SchemaProps: spec.SchemaProps{
Description: "Server URL",
Type: []string{"string"},
Format: "",
},
},
"user": {
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
},
},
"database": {
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
},
},
"basicAuth": {
SchemaProps: spec.SchemaProps{
Type: []string{"boolean"},
Format: "",
},
},
"basicAuthUser": {
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
},
},
"withCredentials": {
SchemaProps: spec.SchemaProps{
Type: []string{"boolean"},
Format: "",
},
},
"jsonData": {
SchemaProps: spec.SchemaProps{
Description: "Generic unstructured configuration settings",
Ref: ref("github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1.Unstructured"),
},
},
},
Required: []string{"title", "jsonData"},
},
},
Dependencies: []string{
"github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1.Unstructured"},
}
}
@@ -0,0 +1,2 @@
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/datasource/v0alpha1,UnstructuredSpec,Object
API rule violation: streaming_list_type_json_tags,github.com/grafana/grafana/pkg/apis/datasource/v0alpha1,DataSourceList,ListMeta