K8s/Dashboard: Always set an internl id (even in mode 4+) (#98320)

This commit is contained in:
Ryan McKinley
2024-12-20 22:17:59 +02:00
committed by GitHub
parent cd7dcdb9ea
commit bc535181cf
5 changed files with 32 additions and 5 deletions
@@ -113,6 +113,7 @@ func (b *DashboardsAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver
return err
}
storageOpts := apistore.StorageOptions{
RequireDeprecatedInternalID: true,
InternalConversion: (func(b []byte, desiredObj runtime.Object) (runtime.Object, error) {
internal := &dashboardinternal.Dashboard{}
obj, _, err := defaultOpts.StorageConfig.Config.Codec.Decode(b, nil, internal)
@@ -108,6 +108,7 @@ func (b *DashboardsAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver
return err
}
storageOpts := apistore.StorageOptions{
RequireDeprecatedInternalID: true,
InternalConversion: (func(b []byte, desiredObj runtime.Object) (runtime.Object, error) {
internal := &dashboardinternal.Dashboard{}
obj, _, err := defaultOpts.StorageConfig.Config.Codec.Decode(b, nil, internal)
@@ -108,6 +108,7 @@ func (b *DashboardsAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver
return err
}
storageOpts := apistore.StorageOptions{
RequireDeprecatedInternalID: true,
InternalConversion: (func(b []byte, desiredObj runtime.Object) (runtime.Object, error) {
internal := &dashboardinternal.Dashboard{}
obj, _, err := defaultOpts.StorageConfig.Config.Codec.Decode(b, nil, internal)
+9
View File
@@ -56,6 +56,15 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime
obj.SetUID(types.UID(uuid.NewString()))
}
if s.opts.RequireDeprecatedInternalID {
// nolint:staticcheck
id := obj.GetDeprecatedInternalID()
if id < 1 {
// nolint:staticcheck
obj.SetDeprecatedInternalID(s.snowflake.Generate().Int64())
}
}
obj.SetGenerateName("") // Clear the random name field
obj.SetResourceVersion("")
obj.SetSelfLink("")
+20 -5
View File
@@ -15,6 +15,7 @@ import (
"strconv"
"time"
"golang.org/x/exp/rand"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -27,6 +28,8 @@ import (
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
"k8s.io/client-go/tools/cache"
"github.com/bwmarrin/snowflake"
"github.com/grafana/grafana/pkg/apimachinery/utils"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
"github.com/grafana/grafana/pkg/apiserver/rest"
@@ -45,6 +48,8 @@ var _ storage.Interface = (*Storage)(nil)
type StorageOptions struct {
LargeObjectSupport LargeObjectSupport
InternalConversion func([]byte, runtime.Object) (runtime.Object, error)
RequireDeprecatedInternalID bool
}
// Storage implements storage.Interface and storage resources as JSON files on disk.
@@ -58,8 +63,9 @@ type Storage struct {
trigger storage.IndexerFuncs
indexers *cache.Indexers
store resource.ResourceClient
getKey func(string) (*resource.ResourceKey, error)
store resource.ResourceClient
getKey func(string) (*resource.ResourceKey, error)
snowflake *snowflake.Node // used to enforce internal ids
versioner storage.Versioner
@@ -104,6 +110,14 @@ func NewStorage(
opts: opts,
}
if opts.RequireDeprecatedInternalID {
node, err := snowflake.NewNode(rand.Int63n(1024))
if err != nil {
return nil, nil, err
}
s.snowflake = node
}
// The key parsing callback allows us to support the hardcoded paths from upstream tests
if s.getKey == nil {
s.getKey = func(key string) (*resource.ResourceKey, error) {
@@ -227,9 +241,10 @@ func (s *Storage) Delete(
}
}
// ?? this was after delete before
if err := validateDeletion(ctx, out); err != nil {
return err
if validateDeletion != nil {
if err := validateDeletion(ctx, out); err != nil {
return err
}
}
rsp, err := s.store.Delete(ctx, cmd)
if err != nil {