diff --git a/pkg/apis/scope/v0alpha1/register.go b/pkg/apis/scope/v0alpha1/register.go index 1503e9d9294..c7819d667ce 100644 --- a/pkg/apis/scope/v0alpha1/register.go +++ b/pkg/apis/scope/v0alpha1/register.go @@ -25,6 +25,12 @@ var ScopeDashboardBindingResourceInfo = common.NewResourceInfo(GROUP, VERSION, func() runtime.Object { return &ScopeDashboardBindingList{} }, ) +var ScopeNodeResourceInfo = common.NewResourceInfo(GROUP, VERSION, + "scopenodes", "scopenode", "ScopeNode", + func() runtime.Object { return &ScopeNode{} }, + func() runtime.Object { return &ScopeNodeList{} }, +) + var ( // SchemeGroupVersion is group version used to register these objects SchemeGroupVersion = schema.GroupVersion{Group: GROUP, Version: VERSION} @@ -49,6 +55,8 @@ func AddKnownTypes(gv schema.GroupVersion, scheme *runtime.Scheme) error { &ScopeList{}, &ScopeDashboardBinding{}, &ScopeDashboardBindingList{}, + &ScopeNode{}, + &ScopeNodeList{}, ) //metav1.AddToGroupVersion(scheme, gv) return nil diff --git a/pkg/apis/scope/v0alpha1/types.go b/pkg/apis/scope/v0alpha1/types.go index 0bab9e922c8..f7bf0781d5e 100644 --- a/pkg/apis/scope/v0alpha1/types.go +++ b/pkg/apis/scope/v0alpha1/types.go @@ -73,3 +73,30 @@ type ScopeDashboardBindingList struct { Items []ScopeDashboardBinding `json:"items,omitempty"` } + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type ScopeNode struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ScopeNodeSpec `json:"spec,omitempty"` +} + +type ScopeNodeSpec struct { + //+optional + ParentName *string `json:"parentName"` + Title string `json:"title"` + Description string `json:"description"` + IsLeaf bool `json:"isLeaf"` + IsSelectable bool `json:"isSelectable"` + LeafType string `json:"leafType"` + LeafName string `json:"leafName"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type ScopeNodeList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + + Items []ScopeNode `json:"items,omitempty"` +} diff --git a/pkg/apis/scope/v0alpha1/zz_generated.deepcopy.go b/pkg/apis/scope/v0alpha1/zz_generated.deepcopy.go index a7840e969a6..195b2c45660 100644 --- a/pkg/apis/scope/v0alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/scope/v0alpha1/zz_generated.deepcopy.go @@ -163,6 +163,87 @@ func (in *ScopeList) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ScopeNode) DeepCopyInto(out *ScopeNode) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopeNode. +func (in *ScopeNode) DeepCopy() *ScopeNode { + if in == nil { + return nil + } + out := new(ScopeNode) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ScopeNode) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ScopeNodeList) DeepCopyInto(out *ScopeNodeList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ScopeNode, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopeNodeList. +func (in *ScopeNodeList) DeepCopy() *ScopeNodeList { + if in == nil { + return nil + } + out := new(ScopeNodeList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ScopeNodeList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ScopeNodeSpec) DeepCopyInto(out *ScopeNodeSpec) { + *out = *in + if in.ParentName != nil { + in, out := &in.ParentName, &out.ParentName + *out = new(string) + **out = **in + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopeNodeSpec. +func (in *ScopeNodeSpec) DeepCopy() *ScopeNodeSpec { + if in == nil { + return nil + } + out := new(ScopeNodeSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ScopeSpec) DeepCopyInto(out *ScopeSpec) { *out = *in diff --git a/pkg/apis/scope/v0alpha1/zz_generated.openapi.go b/pkg/apis/scope/v0alpha1/zz_generated.openapi.go index b1857d6c49a..e55d02962e5 100644 --- a/pkg/apis/scope/v0alpha1/zz_generated.openapi.go +++ b/pkg/apis/scope/v0alpha1/zz_generated.openapi.go @@ -22,6 +22,9 @@ func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenA "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeDashboardBindingSpec": schema_pkg_apis_scope_v0alpha1_ScopeDashboardBindingSpec(ref), "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeFilter": schema_pkg_apis_scope_v0alpha1_ScopeFilter(ref), "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeList": schema_pkg_apis_scope_v0alpha1_ScopeList(ref), + "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNode": schema_pkg_apis_scope_v0alpha1_ScopeNode(ref), + "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNodeList": schema_pkg_apis_scope_v0alpha1_ScopeNodeList(ref), + "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNodeSpec": schema_pkg_apis_scope_v0alpha1_ScopeNodeSpec(ref), "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeSpec": schema_pkg_apis_scope_v0alpha1_ScopeSpec(ref), } } @@ -263,6 +266,154 @@ func schema_pkg_apis_scope_v0alpha1_ScopeList(ref common.ReferenceCallback) comm } } +func schema_pkg_apis_scope_v0alpha1_ScopeNode(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"object"}, + Properties: map[string]spec.Schema{ + "kind": { + SchemaProps: spec.SchemaProps{ + Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", + Type: []string{"string"}, + Format: "", + }, + }, + "apiVersion": { + SchemaProps: spec.SchemaProps{ + Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", + Type: []string{"string"}, + Format: "", + }, + }, + "metadata": { + SchemaProps: spec.SchemaProps{ + Default: map[string]interface{}{}, + Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"), + }, + }, + "spec": { + SchemaProps: spec.SchemaProps{ + Default: map[string]interface{}{}, + Ref: ref("github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNodeSpec"), + }, + }, + }, + }, + }, + Dependencies: []string{ + "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNodeSpec", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"}, + } +} + +func schema_pkg_apis_scope_v0alpha1_ScopeNodeList(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"object"}, + Properties: map[string]spec.Schema{ + "kind": { + SchemaProps: spec.SchemaProps{ + Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", + Type: []string{"string"}, + Format: "", + }, + }, + "apiVersion": { + SchemaProps: spec.SchemaProps{ + Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", + Type: []string{"string"}, + Format: "", + }, + }, + "metadata": { + SchemaProps: spec.SchemaProps{ + Default: map[string]interface{}{}, + Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"), + }, + }, + "items": { + SchemaProps: spec.SchemaProps{ + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Default: map[string]interface{}{}, + Ref: ref("github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNode"), + }, + }, + }, + }, + }, + }, + }, + }, + Dependencies: []string{ + "github.com/grafana/grafana/pkg/apis/scope/v0alpha1.ScopeNode", "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"}, + } +} + +func schema_pkg_apis_scope_v0alpha1_ScopeNodeSpec(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"object"}, + Properties: map[string]spec.Schema{ + "parentName": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "title": { + SchemaProps: spec.SchemaProps{ + Default: "", + Type: []string{"string"}, + Format: "", + }, + }, + "description": { + SchemaProps: spec.SchemaProps{ + Default: "", + Type: []string{"string"}, + Format: "", + }, + }, + "isLeaf": { + SchemaProps: spec.SchemaProps{ + Default: false, + Type: []string{"boolean"}, + Format: "", + }, + }, + "isSelectable": { + SchemaProps: spec.SchemaProps{ + Default: false, + Type: []string{"boolean"}, + Format: "", + }, + }, + "leafType": { + SchemaProps: spec.SchemaProps{ + Default: "", + Type: []string{"string"}, + Format: "", + }, + }, + "leafName": { + SchemaProps: spec.SchemaProps{ + Default: "", + Type: []string{"string"}, + Format: "", + }, + }, + }, + Required: []string{"title", "description", "isLeaf", "isSelectable", "leafType", "leafName"}, + }, + }, + } +} + func schema_pkg_apis_scope_v0alpha1_ScopeSpec(ref common.ReferenceCallback) common.OpenAPIDefinition { return common.OpenAPIDefinition{ Schema: spec.Schema{ diff --git a/pkg/registry/apis/scope/register.go b/pkg/registry/apis/scope/register.go index 71699b5f9ef..89c1ff249e7 100644 --- a/pkg/registry/apis/scope/register.go +++ b/pkg/registry/apis/scope/register.go @@ -82,6 +82,22 @@ func (b *ScopeAPIBuilder) InstallSchema(scheme *runtime.Scheme) error { return err } + err = scheme.AddFieldLabelConversionFunc( + scope.ScopeNodeResourceInfo.GroupVersionKind(), + func(label, value string) (string, string, error) { + fieldSet := SelectableScopeNodeFields(&scope.ScopeNode{}) + for key := range fieldSet { + if label == key { + return label, value, nil + } + } + return "", "", fmt.Errorf("field label not supported for %s: %s", scope.ScopeNodeResourceInfo.GroupVersionKind(), label) + }, + ) + if err != nil { + return err + } + // This is required for --server-side apply err = scope.AddKnownTypes(scope.InternalGroupVersion, scheme) if err != nil { @@ -102,6 +118,7 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo( scopeResourceInfo := scope.ScopeResourceInfo scopeDashboardResourceInfo := scope.ScopeDashboardBindingResourceInfo + scopeNodeResourceInfo := scope.ScopeNodeResourceInfo storage := map[string]rest.Storage{} @@ -117,6 +134,12 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo( } storage[scopeDashboardResourceInfo.StoragePath()] = scopeDashboardStorage + scopeNodeStorage, err := newScopeNodeStorage(scheme, optsGetter) + if err != nil { + return nil, err + } + storage[scopeNodeResourceInfo.StoragePath()] = scopeNodeStorage + apiGroupInfo.VersionedResourcesStorageMap[scope.VERSION] = storage return &apiGroupInfo, nil } diff --git a/pkg/registry/apis/scope/storage.go b/pkg/registry/apis/scope/storage.go index 1a2a7bdeb46..941e2d2b053 100644 --- a/pkg/registry/apis/scope/storage.go +++ b/pkg/registry/apis/scope/storage.go @@ -100,6 +100,44 @@ func newScopeDashboardBindingStorage(scheme *runtime.Scheme, optsGetter generic. return &storage{Store: store}, nil } +func newScopeNodeStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) { + strategy := grafanaregistry.NewStrategy(scheme) + + resourceInfo := scope.ScopeNodeResourceInfo + store := &genericregistry.Store{ + NewFunc: resourceInfo.NewFunc, + NewListFunc: resourceInfo.NewListFunc, + PredicateFunc: Matcher, + DefaultQualifiedResource: resourceInfo.GroupResource(), + SingularQualifiedResource: resourceInfo.SingularGroupResource(), + TableConvertor: utils.NewTableConverter( + resourceInfo.GroupResource(), + []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string", Format: "name"}, + {Name: "Created At", Type: "date"}, + }, + func(obj any) ([]interface{}, error) { + m, ok := obj.(*scope.ScopeNode) + if !ok { + return nil, fmt.Errorf("expected scope node") + } + return []interface{}{ + m.Name, + m.CreationTimestamp.UTC().Format(time.RFC3339), + }, nil + }, + ), + CreateStrategy: strategy, + UpdateStrategy: strategy, + DeleteStrategy: strategy, + } + options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} + if err := store.CompleteWithOptions(options); err != nil { + return nil, err + } + return &storage{Store: store}, nil +} + func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { if s, ok := obj.(*scope.Scope); ok { return labels.Set(s.Labels), SelectableScopeFields(s), nil @@ -107,6 +145,9 @@ func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { if s, ok := obj.(*scope.ScopeDashboardBinding); ok { return labels.Set(s.Labels), SelectableScopeDashboardBindingFields(s), nil } + if s, ok := obj.(*scope.ScopeNode); ok { + return labels.Set(s.Labels), SelectableScopeNodeFields(s), nil + } return nil, nil, fmt.Errorf("not a scope or ScopeDashboardBinding object") } @@ -131,3 +172,15 @@ func SelectableScopeDashboardBindingFields(obj *scope.ScopeDashboardBinding) fie "spec.scope": obj.Spec.Scope, }) } + +func SelectableScopeNodeFields(obj *scope.ScopeNode) fields.Set { + parentName := "" + + if obj.Spec.ParentName != nil { + parentName = *obj.Spec.ParentName + } + + return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{ + "spec.parentName": parentName, + }) +} diff --git a/pkg/tests/apis/scopes/scopes_test.go b/pkg/tests/apis/scopes/scopes_test.go index caaa02f89a3..2706717635a 100644 --- a/pkg/tests/apis/scopes/scopes_test.go +++ b/pkg/tests/apis/scopes/scopes_test.go @@ -46,22 +46,38 @@ func TestIntegrationScopes(t *testing.T) { "apiVersion": "v1", "groupVersion": "scope.grafana.app/v0alpha1", "resources": [ - { - "name": "scopedashboardbindings", - "singularName": "scopedashboardbinding", + { + "name": "scopedashboardbindings", + "singularName": "scopedashboardbinding", + "namespaced": true, + "kind": "ScopeDashboardBinding", + "verbs": [ + "create", + "delete", + "deletecollection", + "get", + "list", + "patch", + "update", + "watch" + ] + },{ + "name": "scopenodes", + "singularName": "scopenode", "namespaced": true, - "kind": "ScopeDashboardBinding", + "kind": "ScopeNode", "verbs": [ - "create", - "delete", - "deletecollection", - "get", - "list", - "patch", - "update", - "watch" + "create", + "delete", + "deletecollection", + "get", + "list", + "patch", + "update", + "watch" ] }, + { "name": "scopes", "singularName": "scope", @@ -78,6 +94,7 @@ func TestIntegrationScopes(t *testing.T) { "watch" ] } + ] }`, string(v1Disco)) })