mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
storage: Move precondition check as a method of preconditions
This commit is contained in:
parent
68937c4934
commit
b5258a5380
@ -18,9 +18,7 @@ package registry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/apiserver/pkg/storage"
|
"k8s.io/apiserver/pkg/storage"
|
||||||
@ -51,7 +49,7 @@ func (s *DryRunnableStorage) Delete(ctx context.Context, key string, out runtime
|
|||||||
if err := s.Storage.Get(ctx, key, "", out, false); err != nil {
|
if err := s.Storage.Get(ctx, key, "", out, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return checkPreconditions(key, preconditions, out)
|
return preconditions.Check(key, out)
|
||||||
}
|
}
|
||||||
return s.Storage.Delete(ctx, key, out, preconditions)
|
return s.Storage.Delete(ctx, key, out, preconditions)
|
||||||
}
|
}
|
||||||
@ -84,7 +82,7 @@ func (s *DryRunnableStorage) GuaranteedUpdate(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = checkPreconditions(key, preconditions, ptrToType)
|
err = preconditions.Check(key, ptrToType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -103,21 +101,6 @@ func (s *DryRunnableStorage) Count(key string) (int64, error) {
|
|||||||
return s.Storage.Count(key)
|
return s.Storage.Count(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPreconditions(key string, preconditions *storage.Preconditions, obj runtime.Object) error {
|
|
||||||
if preconditions == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
objMeta, err := meta.Accessor(obj)
|
|
||||||
if err != nil {
|
|
||||||
return storage.NewInternalErrorf("can't enforce preconditions %v on un-introspectable object %v, got error: %v", *preconditions, obj, err)
|
|
||||||
}
|
|
||||||
if preconditions.UID != nil && *preconditions.UID != objMeta.GetUID() {
|
|
||||||
errMsg := fmt.Sprintf("Precondition failed: UID in precondition: %v, UID in object meta: %v", *preconditions.UID, objMeta.GetUID())
|
|
||||||
return storage.NewInvalidObjError(key, errMsg)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *DryRunnableStorage) copyInto(in, out runtime.Object) error {
|
func (s *DryRunnableStorage) copyInto(in, out runtime.Object) error {
|
||||||
var data []byte
|
var data []byte
|
||||||
|
|
||||||
|
@ -153,21 +153,6 @@ func (h *etcdHelper) Create(ctx context.Context, key string, obj, out runtime.Ob
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPreconditions(key string, preconditions *storage.Preconditions, out runtime.Object) error {
|
|
||||||
if preconditions == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
objMeta, err := meta.Accessor(out)
|
|
||||||
if err != nil {
|
|
||||||
return storage.NewInternalErrorf("can't enforce preconditions %v on un-introspectable object %v, got error: %v", *preconditions, out, err)
|
|
||||||
}
|
|
||||||
if preconditions.UID != nil && *preconditions.UID != objMeta.GetUID() {
|
|
||||||
errMsg := fmt.Sprintf("Precondition failed: UID in precondition: %v, UID in object meta: %v", preconditions.UID, objMeta.GetUID())
|
|
||||||
return storage.NewInvalidObjError(key, errMsg)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements storage.Interface.
|
// Implements storage.Interface.
|
||||||
func (h *etcdHelper) Delete(ctx context.Context, key string, out runtime.Object, preconditions *storage.Preconditions) error {
|
func (h *etcdHelper) Delete(ctx context.Context, key string, out runtime.Object, preconditions *storage.Preconditions) error {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
@ -199,7 +184,7 @@ func (h *etcdHelper) Delete(ctx context.Context, key string, out runtime.Object,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return toStorageErr(err, key, 0)
|
return toStorageErr(err, key, 0)
|
||||||
}
|
}
|
||||||
if err := checkPreconditions(key, preconditions, obj); err != nil {
|
if err := preconditions.Check(key, obj); err != nil {
|
||||||
return toStorageErr(err, key, 0)
|
return toStorageErr(err, key, 0)
|
||||||
}
|
}
|
||||||
index := uint64(0)
|
index := uint64(0)
|
||||||
@ -493,7 +478,7 @@ func (h *etcdHelper) GuaranteedUpdate(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return toStorageErr(err, key, 0)
|
return toStorageErr(err, key, 0)
|
||||||
}
|
}
|
||||||
if err := checkPreconditions(key, preconditions, obj); err != nil {
|
if err := preconditions.Check(key, obj); err != nil {
|
||||||
return toStorageErr(err, key, 0)
|
return toStorageErr(err, key, 0)
|
||||||
}
|
}
|
||||||
meta := storage.ResponseMeta{}
|
meta := storage.ResponseMeta{}
|
||||||
|
@ -233,7 +233,7 @@ func (s *store) conditionalDelete(ctx context.Context, key string, out runtime.O
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkPreconditions(key, preconditions, origState.obj); err != nil {
|
if err := preconditions.Check(key, origState.obj); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
txnResp, err := s.client.KV.Txn(ctx).If(
|
txnResp, err := s.client.KV.Txn(ctx).If(
|
||||||
@ -294,7 +294,7 @@ func (s *store) GuaranteedUpdate(
|
|||||||
|
|
||||||
transformContext := authenticatedDataString(key)
|
transformContext := authenticatedDataString(key)
|
||||||
for {
|
for {
|
||||||
if err := checkPreconditions(key, preconditions, origState.obj); err != nil {
|
if err := preconditions.Check(key, origState.obj); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -791,21 +791,6 @@ func appendListItem(v reflect.Value, data []byte, rev uint64, pred storage.Selec
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPreconditions(key string, preconditions *storage.Preconditions, out runtime.Object) error {
|
|
||||||
if preconditions == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
objMeta, err := meta.Accessor(out)
|
|
||||||
if err != nil {
|
|
||||||
return storage.NewInternalErrorf("can't enforce preconditions %v on un-introspectable object %v, got error: %v", *preconditions, out, err)
|
|
||||||
}
|
|
||||||
if preconditions.UID != nil && *preconditions.UID != objMeta.GetUID() {
|
|
||||||
errMsg := fmt.Sprintf("Precondition failed: UID in precondition: %v, UID in object meta: %v", *preconditions.UID, objMeta.GetUID())
|
|
||||||
return storage.NewInvalidObjError(key, errMsg)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func notFound(key string) clientv3.Cmp {
|
func notFound(key string) clientv3.Cmp {
|
||||||
return clientv3.Compare(clientv3.ModRevision(key), "=", 0)
|
return clientv3.Compare(clientv3.ModRevision(key), "=", 0)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,9 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/apimachinery/pkg/fields"
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@ -106,6 +108,29 @@ func NewUIDPreconditions(uid string) *Preconditions {
|
|||||||
return &Preconditions{UID: &u}
|
return &Preconditions{UID: &u}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Preconditions) Check(key string, obj runtime.Object) error {
|
||||||
|
if p == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
objMeta, err := meta.Accessor(obj)
|
||||||
|
if err != nil {
|
||||||
|
return NewInternalErrorf(
|
||||||
|
"can't enforce preconditions %v on un-introspectable object %v, got error: %v",
|
||||||
|
*p,
|
||||||
|
obj,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
if p.UID != nil && *p.UID != objMeta.GetUID() {
|
||||||
|
err := fmt.Sprintf(
|
||||||
|
"Precondition failed: UID in precondition: %v, UID in object meta: %v",
|
||||||
|
*p.UID,
|
||||||
|
objMeta.GetUID())
|
||||||
|
return NewInvalidObjError(key, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Interface offers a common interface for object marshaling/unmarshaling operations and
|
// Interface offers a common interface for object marshaling/unmarshaling operations and
|
||||||
// hides all the storage-related operations behind it.
|
// hides all the storage-related operations behind it.
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user