Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b5258a53
Commit
b5258a53
authored
Jul 31, 2018
by
Antoine Pelisse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
storage: Move precondition check as a method of preconditions
parent
68937c49
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
53 deletions
+31
-53
dryrun.go
.../k8s.io/apiserver/pkg/registry/generic/registry/dryrun.go
+2
-19
etcd_helper.go
staging/src/k8s.io/apiserver/pkg/storage/etcd/etcd_helper.go
+2
-17
store.go
staging/src/k8s.io/apiserver/pkg/storage/etcd3/store.go
+2
-17
interfaces.go
staging/src/k8s.io/apiserver/pkg/storage/interfaces.go
+25
-0
No files found.
staging/src/k8s.io/apiserver/pkg/registry/generic/registry/dryrun.go
View file @
b5258a53
...
@@ -18,9 +18,7 @@ package registry
...
@@ -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
...
@@ -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(
...
@@ -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) {
...
@@ -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
...
...
staging/src/k8s.io/apiserver/pkg/storage/etcd/etcd_helper.go
View file @
b5258a53
...
@@ -153,21 +153,6 @@ func (h *etcdHelper) Create(ctx context.Context, key string, obj, out runtime.Ob
...
@@ -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,
...
@@ -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(
...
@@ -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
{}
...
...
staging/src/k8s.io/apiserver/pkg/storage/etcd3/store.go
View file @
b5258a53
...
@@ -233,7 +233,7 @@ func (s *store) conditionalDelete(ctx context.Context, key string, out runtime.O
...
@@ -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(
...
@@ -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
...
@@ -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
)
}
}
staging/src/k8s.io/apiserver/pkg/storage/interfaces.go
View file @
b5258a53
...
@@ -18,7 +18,9 @@ package storage
...
@@ -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 {
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment