mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
Cleanup apiserver storage selflink references where possible
This commit is contained in:
parent
d63b79ec47
commit
9b2908ea3b
@ -60,8 +60,7 @@ type serializationsCache map[runtime.Identifier]*serializationResult
|
|||||||
// so that each of those is computed exactly once.
|
// so that each of those is computed exactly once.
|
||||||
//
|
//
|
||||||
// cachingObject implements the metav1.Object interface (accessors for
|
// cachingObject implements the metav1.Object interface (accessors for
|
||||||
// all metadata fields). However, setters for all fields except from
|
// all metadata fields).
|
||||||
// SelfLink (which is set lately in the path) are ignored.
|
|
||||||
type cachingObject struct {
|
type cachingObject struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
|
|
||||||
|
@ -83,36 +83,39 @@ func TestCachingObject(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSelfLink(t *testing.T) {
|
func TestCachingObjectFieldAccessors(t *testing.T) {
|
||||||
object, err := newCachingObject(&v1.Pod{})
|
object, err := newCachingObject(&v1.Pod{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("couldn't create cachingObject: %v", err)
|
t.Fatalf("couldn't create cachingObject: %v", err)
|
||||||
}
|
}
|
||||||
selfLink := "selfLink"
|
|
||||||
object.SetSelfLink(selfLink)
|
|
||||||
|
|
||||||
encodeSelfLink := func(obj runtime.Object, w io.Writer) error {
|
// Given accessors for all fields implement the same logic,
|
||||||
|
// we are choosing an arbitrary one to test.
|
||||||
|
clusterName := "clusterName"
|
||||||
|
object.SetClusterName(clusterName)
|
||||||
|
|
||||||
|
encodeClusterName := func(obj runtime.Object, w io.Writer) error {
|
||||||
accessor, err := meta.Accessor(obj)
|
accessor, err := meta.Accessor(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to get accessor for %#v: %v", obj, err)
|
t.Fatalf("failed to get accessor for %#v: %v", obj, err)
|
||||||
}
|
}
|
||||||
_, err = w.Write([]byte(accessor.GetSelfLink()))
|
_, err = w.Write([]byte(accessor.GetClusterName()))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
if err := object.CacheEncode("", encodeSelfLink, buffer); err != nil {
|
if err := object.CacheEncode("", encodeClusterName, buffer); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
if a, e := buffer.String(), selfLink; a != e {
|
if a, e := buffer.String(), clusterName; a != e {
|
||||||
t.Errorf("unexpected serialization: %s, expected: %s", a, e)
|
t.Errorf("unexpected serialization: %s, expected: %s", a, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetObject should also set selfLink.
|
// GetObject should also set clusterName.
|
||||||
buffer.Reset()
|
buffer.Reset()
|
||||||
if err := encodeSelfLink(object.GetObject(), buffer); err != nil {
|
if err := encodeClusterName(object.GetObject(), buffer); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
if a, e := buffer.String(), selfLink; a != e {
|
if a, e := buffer.String(), clusterName; a != e {
|
||||||
t.Errorf("unexpected serialization: %s, expected: %s", a, e)
|
t.Errorf("unexpected serialization: %s, expected: %s", a, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,7 +139,7 @@ func TestCachingObjectRaces(t *testing.T) {
|
|||||||
for i := 0; i < numWorkers; i++ {
|
for i := 0; i < numWorkers; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
object.SetSelfLink("selfLink")
|
object.SetClusterName("clusterName")
|
||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
for _, encoder := range encoders {
|
for _, encoder := range encoders {
|
||||||
buffer.Reset()
|
buffer.Reset()
|
||||||
@ -152,8 +155,8 @@ func TestCachingObjectRaces(t *testing.T) {
|
|||||||
t.Errorf("failed to get accessor: %v", err)
|
t.Errorf("failed to get accessor: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if selfLink := accessor.GetSelfLink(); selfLink != "selfLink" {
|
if clusterName := accessor.GetClusterName(); clusterName != "clusterName" {
|
||||||
t.Errorf("unexpected selfLink: %s", selfLink)
|
t.Errorf("unexpected clusterName: %s", clusterName)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ func (a APIObjectVersioner) UpdateList(obj runtime.Object, resourceVersion uint6
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareObjectForStorage clears resource version and self link prior to writing to etcd.
|
// PrepareObjectForStorage clears resourceVersion and selfLink prior to writing to etcd.
|
||||||
func (a APIObjectVersioner) PrepareObjectForStorage(obj runtime.Object) error {
|
func (a APIObjectVersioner) PrepareObjectForStorage(obj runtime.Object) error {
|
||||||
accessor, err := meta.Accessor(obj)
|
accessor, err := meta.Accessor(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -135,7 +135,7 @@ func TestCreate(t *testing.T) {
|
|||||||
t.Errorf("output should have non-empty resource version")
|
t.Errorf("output should have non-empty resource version")
|
||||||
}
|
}
|
||||||
if out.SelfLink != "" {
|
if out.SelfLink != "" {
|
||||||
t.Errorf("output should have empty self link")
|
t.Errorf("output should have empty selfLink")
|
||||||
}
|
}
|
||||||
|
|
||||||
checkStorageInvariants(ctx, t, etcdClient, store, key)
|
checkStorageInvariants(ctx, t, etcdClient, store, key)
|
||||||
@ -158,7 +158,7 @@ func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clien
|
|||||||
t.Errorf("stored object should have empty resource version")
|
t.Errorf("stored object should have empty resource version")
|
||||||
}
|
}
|
||||||
if obj.SelfLink != "" {
|
if obj.SelfLink != "" {
|
||||||
t.Errorf("stored output should have empty self link")
|
t.Errorf("stored output should have empty selfLink")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -686,7 +686,7 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
expectNotFoundErr: false,
|
expectNotFoundErr: false,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: true,
|
expectNoUpdate: true,
|
||||||
}, { // GuaranteedUpdate with same data AND a self link
|
}, { // GuaranteedUpdate with same data AND a selfLink
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
@ -768,7 +768,7 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
t.Errorf("#%d: pod name want=%s, get=%s", i, name, out.ObjectMeta.Name)
|
t.Errorf("#%d: pod name want=%s, get=%s", i, name, out.ObjectMeta.Name)
|
||||||
}
|
}
|
||||||
if out.SelfLink != "" {
|
if out.SelfLink != "" {
|
||||||
t.Errorf("#%d: selflink should not be set", i)
|
t.Errorf("#%d: selfLink should not be set", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify that kv pair is not empty after set and that the underlying data matches expectations
|
// verify that kv pair is not empty after set and that the underlying data matches expectations
|
||||||
@ -2359,7 +2359,7 @@ func TestLeaseMaxObjectCount(t *testing.T) {
|
|||||||
})
|
})
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", SelfLink: "testlink"}}
|
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
|
||||||
out := &example.Pod{}
|
out := &example.Pod{}
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user