mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Storage: Support grafana.app/folder field selector & label selectors (#79816)
* support grafana.app/folder field selector & label selectors * lint fix
This commit is contained in:
parent
539bc6d31b
commit
b2b4d89501
@ -49,6 +49,14 @@ func (info *ResourceInfo) GroupResource() schema.GroupResource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (info *ResourceInfo) GroupVersionKind() schema.GroupVersionKind {
|
||||||
|
return schema.GroupVersionKind{
|
||||||
|
Group: info.group,
|
||||||
|
Version: info.version,
|
||||||
|
Kind: info.kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (info *ResourceInfo) SingularGroupResource() schema.GroupResource {
|
func (info *ResourceInfo) SingularGroupResource() schema.GroupResource {
|
||||||
return schema.GroupResource{
|
return schema.GroupResource{
|
||||||
Group: info.group,
|
Group: info.group,
|
||||||
|
@ -2,6 +2,7 @@ package playlist
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -67,6 +68,28 @@ func (b *PlaylistAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
|||||||
Version: runtime.APIVersionInternal,
|
Version: runtime.APIVersionInternal,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
gvk := playlist.PlaylistResourceInfo.GroupVersionKind()
|
||||||
|
|
||||||
|
// Add playlist thing
|
||||||
|
_ = scheme.AddFieldLabelConversionFunc(gvk,
|
||||||
|
runtime.FieldLabelConversionFunc(
|
||||||
|
func(label, value string) (string, string, error) {
|
||||||
|
if strings.HasPrefix(label, "grafana.app/") {
|
||||||
|
return label, value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch label {
|
||||||
|
case "metadata.name":
|
||||||
|
return label, value, nil
|
||||||
|
case "metadata.namespace":
|
||||||
|
return label, value, nil
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q", label, "metadata.name", "metadata.namespace")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
// If multiple versions exist, then register conversions from zz_generated.conversion.go
|
// If multiple versions exist, then register conversions from zz_generated.conversion.go
|
||||||
// if err := playlist.RegisterConversions(scheme); err != nil {
|
// if err := playlist.RegisterConversions(scheme); err != nil {
|
||||||
// return err
|
// return err
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/conversion"
|
"k8s.io/apimachinery/pkg/conversion"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/selection"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/apiserver/pkg/storage"
|
"k8s.io/apiserver/pkg/storage"
|
||||||
@ -218,14 +219,53 @@ func (s *Storage) GetList(ctx context.Context, key string, opts storage.ListOpti
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp, err := s.store.List(ctx, &entityStore.EntityListRequest{
|
req := &entityStore.EntityListRequest{
|
||||||
Key: []string{key},
|
Key: []string{key},
|
||||||
WithBody: true,
|
WithBody: true,
|
||||||
WithStatus: true,
|
WithStatus: true,
|
||||||
NextPageToken: opts.Predicate.Continue,
|
NextPageToken: opts.Predicate.Continue,
|
||||||
Limit: opts.Predicate.Limit,
|
Limit: opts.Predicate.Limit,
|
||||||
|
Labels: map[string]string{},
|
||||||
// TODO push label/field matching down to storage
|
// TODO push label/field matching down to storage
|
||||||
|
}
|
||||||
|
|
||||||
|
// translate "equals" label selectors to storage label conditions
|
||||||
|
requirements, selectable := opts.Predicate.Label.Requirements()
|
||||||
|
if !selectable {
|
||||||
|
return apierrors.NewBadRequest("label selector is not selectable")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range requirements {
|
||||||
|
if r.Operator() == selection.Equals {
|
||||||
|
req.Labels[r.Key()] = r.Values().List()[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// translate grafana.app/folder field selector to folder condition
|
||||||
|
fields := opts.Predicate.Field.Requirements()
|
||||||
|
for _, f := range fields {
|
||||||
|
if f.Field == "grafana.app/folder" {
|
||||||
|
if f.Operator != selection.Equals {
|
||||||
|
return apierrors.NewBadRequest("grafana.app/folder field selector only supports equality")
|
||||||
|
}
|
||||||
|
|
||||||
|
// select items in the spcified folder
|
||||||
|
req.Folder = f.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// use Transform function to remove grafana.app/folder field selector
|
||||||
|
opts.Predicate.Field, err = opts.Predicate.Field.Transform(func(field, value string) (string, string, error) {
|
||||||
|
if field == "grafana.app/folder" {
|
||||||
|
return "", "", nil
|
||||||
|
}
|
||||||
|
return field, value, nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rsp, err := s.store.List(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apierrors.NewInternalError(err)
|
return apierrors.NewInternalError(err)
|
||||||
}
|
}
|
||||||
@ -314,6 +354,15 @@ func (s *Storage) guaranteedUpdate(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accessor, err := meta.Accessor(destination)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
previousVersion, _ := strconv.ParseInt(accessor.GetResourceVersion(), 10, 64)
|
||||||
|
if preconditions != nil && preconditions.ResourceVersion != nil {
|
||||||
|
previousVersion, _ = strconv.ParseInt(*preconditions.ResourceVersion, 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
res := &storage.ResponseMeta{}
|
res := &storage.ResponseMeta{}
|
||||||
updatedObj, _, err := tryUpdate(destination, *res)
|
updatedObj, _, err := tryUpdate(destination, *res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -333,11 +382,6 @@ func (s *Storage) guaranteedUpdate(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
previousVersion := int64(0)
|
|
||||||
if preconditions != nil && preconditions.ResourceVersion != nil {
|
|
||||||
previousVersion, _ = strconv.ParseInt(*preconditions.ResourceVersion, 10, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
req := &entityStore.UpdateEntityRequest{
|
req := &entityStore.UpdateEntityRequest{
|
||||||
Entity: e,
|
Entity: e,
|
||||||
PreviousVersion: previousVersion,
|
PreviousVersion: previousVersion,
|
||||||
|
@ -42,7 +42,7 @@ func ParseKey(key string) (*Key, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *Key) String() string {
|
func (k *Key) String() string {
|
||||||
s := k.Group + "/" + k.Resource + "/" + k.Namespace
|
s := "/" + k.Group + "/" + k.Resource + "/" + k.Namespace
|
||||||
if len(k.Name) > 0 {
|
if len(k.Name) > 0 {
|
||||||
s += "/" + k.Name
|
s += "/" + k.Name
|
||||||
if len(k.Subresource) > 0 {
|
if len(k.Subresource) > 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user