Storage: Always set a UID in the apiserver (#96086)

This commit is contained in:
Ryan McKinley 2024-11-12 19:58:32 +03:00 committed by GitHub
parent 7f99e943a6
commit e9956f2345
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 4 deletions

View File

@ -7,7 +7,9 @@ import (
"math"
"time"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/storage"
"k8s.io/klog/v2"
@ -50,6 +52,9 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime
if obj.GetResourceVersion() != "" {
return nil, storage.ErrResourceVersionSetOnCreate
}
if obj.GetUID() == "" {
obj.SetUID(types.UID(uuid.NewString()))
}
obj.SetGenerateName("") // Clear the random name field
obj.SetResourceVersion("")
@ -95,7 +100,9 @@ func (s *Storage) prepareObjectForUpdate(ctx context.Context, updateObject runti
if previous.GetUID() == "" {
klog.Errorf("object is missing UID: %s, %s", obj.GetGroupVersionKind().String(), obj.GetName())
} else if obj.GetUID() != previous.GetUID() {
klog.Errorf("object UID mismatch: %s, was:%s, now: %s", obj.GetGroupVersionKind().String(), previous.GetName(), obj.GetUID())
if obj.GetUID() != "" {
klog.Errorf("object UID mismatch: %s, was:%s, now: %s", obj.GetGroupVersionKind().String(), previous.GetName(), obj.GetUID())
}
obj.SetUID(previous.GetUID())
}

View File

@ -303,6 +303,8 @@ func (s *server) newEvent(ctx context.Context, user claims.AuthInfo, key *Resour
}
if obj.GetUID() == "" {
// TODO! once https://github.com/grafana/grafana/pull/96086 is deployed everywhere
// return nil, NewBadRequestError("object is missing UID")
s.log.Error("object is missing UID", "key", key)
}

View File

@ -8,13 +8,14 @@ import (
"testing"
"time"
"github.com/grafana/authlib/claims"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/apimachinery/utils"
"github.com/stretchr/testify/require"
"gocloud.dev/blob/fileblob"
"gocloud.dev/blob/memblob"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/grafana/authlib/claims"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/apimachinery/utils"
)
func TestSimpleServer(t *testing.T) {
@ -56,6 +57,7 @@ func TestSimpleServer(t *testing.T) {
"kind": "Playlist",
"metadata": {
"name": "fdgsv37qslr0ga",
"uid": "xyz",
"namespace": "default",
"annotations": {
"grafana.app/originName": "elsewhere",
@ -172,6 +174,7 @@ func TestSimpleServer(t *testing.T) {
"metadata": {
"name": "fdgsv37qslr0ga",
"namespace": "default",
"uid": "xyz",
"annotations": {
"grafana.app/originName": "elsewhere",
"grafana.app/originPath": "path/to/item",