merge main

This commit is contained in:
Ryan McKinley
2024-06-26 08:48:10 +03:00
3231 changed files with 10512 additions and 8100 deletions
@@ -23,12 +23,12 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/storagebackend"
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
"k8s.io/klog/v2"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
entityStore "github.com/grafana/grafana/pkg/services/store/entity"
)
@@ -74,16 +74,16 @@ func NewStorage(
// in seconds (0 means forever). If no error is returned and out is not nil, out will be
// set to the read value from database.
func (s *Storage) Create(ctx context.Context, key string, obj runtime.Object, out runtime.Object, ttl uint64) error {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return apierrors.NewInternalError(fmt.Errorf("could not get request info"))
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return err
}
if err := s.Versioner().PrepareObjectForStorage(obj); err != nil {
return err
}
e, err := resourceToEntity(obj, requestInfo, s.codec)
e, err := resourceToEntity(obj, *k, s.codec)
if err != nil {
return err
}
@@ -114,17 +114,9 @@ func (s *Storage) Create(ctx context.Context, key string, obj runtime.Object, ou
// current version of the object to avoid read operation from storage to get it.
// However, the implementations have to retry in case suggestion is stale.
func (s *Storage) Delete(ctx context.Context, key string, out runtime.Object, preconditions *storage.Preconditions, validateDeletion storage.ValidateObjectFunc, cachedExistingObject runtime.Object) error {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return apierrors.NewInternalError(fmt.Errorf("could not get request info"))
}
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: requestInfo.Name,
Subresource: requestInfo.Subresource,
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return err
}
previousVersion := int64(0)
@@ -156,17 +148,9 @@ func (s *Storage) Delete(ctx context.Context, key string, out runtime.Object, pr
// If resource version is "0", this interface will get current object at given key
// and send it in an "ADDED" event, before watch starts.
func (s *Storage) Watch(ctx context.Context, key string, opts storage.ListOptions) (watch.Interface, error) {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return nil, apierrors.NewInternalError(fmt.Errorf("could not get request info"))
}
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: requestInfo.Name,
Subresource: requestInfo.Subresource,
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return nil, err
}
if opts.Predicate.Field != nil {
@@ -288,21 +272,12 @@ func (s *Storage) Watch(ctx context.Context, key string, opts storage.ListOption
// The returned contents may be delayed, but it is guaranteed that they will
// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
func (s *Storage) Get(ctx context.Context, key string, opts storage.GetOptions, objPtr runtime.Object) error {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return apierrors.NewInternalError(fmt.Errorf("could not get request info"))
}
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: requestInfo.Name,
Subresource: requestInfo.Subresource,
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return err
}
resourceVersion := int64(0)
var err error
if opts.ResourceVersion != "" {
resourceVersion, err = strconv.ParseInt(opts.ResourceVersion, 10, 64)
if err != nil {
@@ -343,17 +318,9 @@ func (s *Storage) Get(ctx context.Context, key string, opts storage.GetOptions,
// The returned contents may be delayed, but it is guaranteed that they will
// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
func (s *Storage) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return apierrors.NewInternalError(fmt.Errorf("could not get request info"))
}
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: requestInfo.Name,
Subresource: requestInfo.Subresource,
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return err
}
listPtr, err := meta.GetItemsPtr(listObj)
@@ -519,17 +486,9 @@ func (s *Storage) GuaranteedUpdate(
tryUpdate storage.UpdateFunc,
cachedExistingObject runtime.Object,
) error {
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return apierrors.NewInternalError(fmt.Errorf("could not get request info"))
}
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: requestInfo.Name,
Subresource: requestInfo.Subresource,
k, err := grafanaregistry.ParseKey(key)
if err != nil {
return err
}
getErr := s.Get(ctx, k.String(), storage.GetOptions{}, destination)
@@ -565,7 +524,7 @@ func (s *Storage) GuaranteedUpdate(
return apierrors.NewInternalError(fmt.Errorf("could not successfully update object. key=%s, err=%s", k.String(), err.Error()))
}
e, err := resourceToEntity(updatedObj, requestInfo, s.codec)
e, err := resourceToEntity(updatedObj, *k, s.codec)
if err != nil {
return err
}
+5 -11
View File
@@ -13,9 +13,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/endpoints/request"
"github.com/grafana/grafana/pkg/apimachinery/utils"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
entityStore "github.com/grafana/grafana/pkg/services/store/entity"
)
@@ -98,7 +98,7 @@ func EntityToRuntimeObject(rsp *entityStore.Entity, res runtime.Object, codec ru
return nil
}
func resourceToEntity(res runtime.Object, requestInfo *request.RequestInfo, codec runtime.Codec) (*entityStore.Entity, error) {
func resourceToEntity(res runtime.Object, k grafanaregistry.Key, codec runtime.Codec) (*entityStore.Entity, error) {
metaAccessor, err := meta.Accessor(res)
if err != nil {
return nil, err
@@ -110,19 +110,13 @@ func resourceToEntity(res runtime.Object, requestInfo *request.RequestInfo, code
}
rv, _ := strconv.ParseInt(metaAccessor.GetResourceVersion(), 10, 64)
k := &entityStore.Key{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
Namespace: requestInfo.Namespace,
Name: metaAccessor.GetName(),
Subresource: requestInfo.Subresource,
}
// add the object's name to the provided key
k.Name = metaAccessor.GetName()
rsp := &entityStore.Entity{
Group: k.Group,
GroupVersion: requestInfo.APIVersion,
GroupVersion: res.GetObjectKind().GroupVersionKind().Version,
Resource: k.Resource,
Subresource: k.Subresource,
Namespace: k.Namespace,
Key: k.String(),
Name: k.Name,
@@ -10,9 +10,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/endpoints/request"
"github.com/grafana/grafana/pkg/apis/playlist/v0alpha1"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
entityStore "github.com/grafana/grafana/pkg/services/store/entity"
)
@@ -30,7 +30,7 @@ func TestResourceToEntity(t *testing.T) {
Codecs := serializer.NewCodecFactory(Scheme)
testCases := []struct {
requestInfo *request.RequestInfo
key grafanaregistry.Key
resource runtime.Object
codec runtime.Codec
expectedKey string
@@ -52,14 +52,17 @@ func TestResourceToEntity(t *testing.T) {
expectedBody []byte
}{
{
requestInfo: &request.RequestInfo{
APIGroup: "playlist.grafana.app",
APIVersion: "v0alpha1",
Resource: "playlists",
Namespace: "default",
Name: "test-name",
key: grafanaregistry.Key{
Group: "playlist.grafana.app",
Resource: "playlists",
Namespace: "default",
Name: "test-name",
},
resource: &v0alpha1.Playlist{
TypeMeta: metav1.TypeMeta{
APIVersion: "playlist.grafana.app/v0alpha1",
Kind: "Playlist",
},
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: createdAt,
Labels: map[string]string{"label1": "value1", "label2": "value2"},
@@ -105,7 +108,7 @@ func TestResourceToEntity(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.resource.GetObjectKind().GroupVersionKind().Kind+" to entity conversion should succeed", func(t *testing.T) {
entity, err := resourceToEntity(tc.resource, tc.requestInfo, Codecs.LegacyCodec(v0alpha1.PlaylistResourceInfo.GroupVersion()))
entity, err := resourceToEntity(tc.resource, tc.key, Codecs.LegacyCodec(v0alpha1.PlaylistResourceInfo.GroupVersion()))
require.NoError(t, err)
assert.Equal(t, tc.expectedKey, entity.Key)
assert.Equal(t, tc.expectedName, entity.Name)