From e28a1072d94d947f38db7abc4c66426b8f057b17 Mon Sep 17 00:00:00 2001 From: Chao Xu Date: Fri, 24 May 2019 17:35:00 -0700 Subject: [PATCH] Make ListMeta.RemainingItemCount a pointer (*int64) to make sure it's omitted when serialized to proto. The SetRemainingItemCount() and GetRemainingItemCount() still takes and returns an int64 to make developers life easier. --- pkg/printers/tablegenerator.go | 2 +- .../tableconvertor/tableconvertor.go | 2 +- .../apimachinery/pkg/apis/meta/v1/meta.go | 16 +++++++-- .../apimachinery/pkg/apis/meta/v1/types.go | 2 +- .../pkg/test/api_meta_meta_test.go | 33 ++++++++++++------- .../apiserver/pkg/registry/rest/table.go | 2 +- .../apiserver/pkg/storage/etcd3/store_test.go | 2 +- .../pkg/registry/apiservice/etcd/etcd.go | 2 +- test/e2e/apimachinery/chunking.go | 8 ++--- 9 files changed, 46 insertions(+), 23 deletions(-) diff --git a/pkg/printers/tablegenerator.go b/pkg/printers/tablegenerator.go index a9d62c45a61..7f4da18f1ef 100644 --- a/pkg/printers/tablegenerator.go +++ b/pkg/printers/tablegenerator.go @@ -112,7 +112,7 @@ func (h *HumanReadablePrinter) GenerateTable(obj runtime.Object, options PrintOp table.ResourceVersion = m.GetResourceVersion() table.SelfLink = m.GetSelfLink() table.Continue = m.GetContinue() - table.RemainingItemCount = m.GetRemainingItemCount() + table.SetRemainingItemCount(m.GetRemainingItemCount()) } else { if m, err := meta.CommonAccessor(obj); err == nil { table.ResourceVersion = m.GetResourceVersion() diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/tableconvertor.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/tableconvertor.go index 7e0d64e7abf..587ec8284e6 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/tableconvertor.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/tableconvertor.go @@ -87,7 +87,7 @@ func (c *convertor) ConvertToTable(ctx context.Context, obj runtime.Object, tabl table.ResourceVersion = m.GetResourceVersion() table.SelfLink = m.GetSelfLink() table.Continue = m.GetContinue() - table.RemainingItemCount = m.GetRemainingItemCount() + table.SetRemainingItemCount(m.GetRemainingItemCount()) } else { if m, err := meta.CommonAccessor(obj); err == nil { table.ResourceVersion = m.GetResourceVersion() diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/meta.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/meta.go index 7b179c1d4aa..8de14872b85 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/meta.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/meta.go @@ -115,8 +115,20 @@ func (meta *ListMeta) GetSelfLink() string { return meta.SelfLink func (meta *ListMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink } func (meta *ListMeta) GetContinue() string { return meta.Continue } func (meta *ListMeta) SetContinue(c string) { meta.Continue = c } -func (meta *ListMeta) GetRemainingItemCount() int64 { return meta.RemainingItemCount } -func (meta *ListMeta) SetRemainingItemCount(c int64) { meta.RemainingItemCount = c } + +func (meta *ListMeta) GetRemainingItemCount() int64 { + if meta.RemainingItemCount != nil { + return *meta.RemainingItemCount + } + return 0 +} + +func (meta *ListMeta) SetRemainingItemCount(c int64) { + if meta.RemainingItemCount == nil { + meta.RemainingItemCount = new(int64) + } + *meta.RemainingItemCount = c +} func (obj *TypeMeta) GetObjectKind() schema.ObjectKind { return obj } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go index 9e7497a0620..10ae143c20b 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go @@ -88,7 +88,7 @@ type ListMeta struct { // because it is unpaginated or because this is the last page), then there are no more remaining // items and this field will also be unset. Servers older than v1.15 do not set this field. // +optional - RemainingItemCount int64 `json:"remainingItemCount,omitempty" protobuf:"bytes,4,opt,name=remainingItemCount"` + RemainingItemCount *int64 `json:"remainingItemCount,omitempty" protobuf:"bytes,4,opt,name=remainingItemCount"` } // These are internal finalizer values for Kubernetes-like APIs, must be qualified name unless defined here diff --git a/staging/src/k8s.io/apimachinery/pkg/test/api_meta_meta_test.go b/staging/src/k8s.io/apimachinery/pkg/test/api_meta_meta_test.go index a8582c51bba..74496f264b6 100644 --- a/staging/src/k8s.io/apimachinery/pkg/test/api_meta_meta_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/test/api_meta_meta_test.go @@ -20,8 +20,7 @@ import ( "reflect" "testing" - "github.com/google/gofuzz" - + fuzz "github.com/google/gofuzz" "k8s.io/apimachinery/pkg/api/meta" metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -202,7 +201,7 @@ type InternalTypeMeta struct { SelfLink string `json:"selfLink,omitempty"` ResourceVersion string `json:"resourceVersion,omitempty"` Continue string `json:"next,omitempty"` - RemainingItemCount int64 `json:"remainingItemCount,omitempty"` + RemainingItemCount *int64 `json:"remainingItemCount,omitempty"` APIVersion string `json:"apiVersion,omitempty"` Labels map[string]string `json:"labels,omitempty"` Annotations map[string]string `json:"annotations,omitempty"` @@ -210,14 +209,26 @@ type InternalTypeMeta struct { OwnerReferences []metav1.OwnerReference `json:"ownerReferences,omitempty"` } -func (m *InternalTypeMeta) GetResourceVersion() string { return m.ResourceVersion } -func (m *InternalTypeMeta) SetResourceVersion(rv string) { m.ResourceVersion = rv } -func (m *InternalTypeMeta) GetSelfLink() string { return m.SelfLink } -func (m *InternalTypeMeta) SetSelfLink(link string) { m.SelfLink = link } -func (m *InternalTypeMeta) GetContinue() string { return m.Continue } -func (m *InternalTypeMeta) SetContinue(c string) { m.Continue = c } -func (m *InternalTypeMeta) GetRemainingItemCount() int64 { return m.RemainingItemCount } -func (m *InternalTypeMeta) SetRemainingItemCount(c int64) { m.RemainingItemCount = c } +func (m *InternalTypeMeta) GetResourceVersion() string { return m.ResourceVersion } +func (m *InternalTypeMeta) SetResourceVersion(rv string) { m.ResourceVersion = rv } +func (m *InternalTypeMeta) GetSelfLink() string { return m.SelfLink } +func (m *InternalTypeMeta) SetSelfLink(link string) { m.SelfLink = link } +func (m *InternalTypeMeta) GetContinue() string { return m.Continue } +func (m *InternalTypeMeta) SetContinue(c string) { m.Continue = c } + +func (m *InternalTypeMeta) GetRemainingItemCount() int64 { + if m.RemainingItemCount != nil { + return *m.RemainingItemCount + } + return 0 +} + +func (m *InternalTypeMeta) SetRemainingItemCount(c int64) { + if m.RemainingItemCount == nil { + m.RemainingItemCount = new(int64) + } + *m.RemainingItemCount = c +} type MyAPIObject struct { TypeMeta InternalTypeMeta `json:",inline"` diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/table.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/table.go index 684f4acdcba..f6a8fdd1e22 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/rest/table.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/table.go @@ -67,7 +67,7 @@ func (c defaultTableConvertor) ConvertToTable(ctx context.Context, object runtim table.ResourceVersion = m.GetResourceVersion() table.SelfLink = m.GetSelfLink() table.Continue = m.GetContinue() - table.RemainingItemCount = m.GetRemainingItemCount() + table.SetRemainingItemCount(m.GetRemainingItemCount()) } else { if m, err := meta.CommonAccessor(object); err == nil { table.ResourceVersion = m.GetResourceVersion() diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go index 65627fbbe41..0dec9f27d64 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go @@ -1062,7 +1062,7 @@ func TestList(t *testing.T) { t.Errorf("(%s): length of list want=%d, got=%d", tt.name, len(tt.expectedOut), len(out.Items)) continue } - if e, a := tt.expectedRemainingItemCount, out.ListMeta.RemainingItemCount; e != a { + if e, a := tt.expectedRemainingItemCount, out.ListMeta.GetRemainingItemCount(); e != a { t.Errorf("(%s): remainingItemCount want=%d, got=%d", tt.name, e, a) } for j, wantPod := range tt.expectedOut { diff --git a/staging/src/k8s.io/kube-aggregator/pkg/registry/apiservice/etcd/etcd.go b/staging/src/k8s.io/kube-aggregator/pkg/registry/apiservice/etcd/etcd.go index 1179f69d6fb..804976a2820 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/registry/apiservice/etcd/etcd.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/registry/apiservice/etcd/etcd.go @@ -73,7 +73,7 @@ func (c *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOpti table.ResourceVersion = m.GetResourceVersion() table.SelfLink = m.GetSelfLink() table.Continue = m.GetContinue() - table.RemainingItemCount = m.GetRemainingItemCount() + table.SetRemainingItemCount(m.GetRemainingItemCount()) } else { if m, err := meta.CommonAccessor(obj); err == nil { table.ResourceVersion = m.GetResourceVersion() diff --git a/test/e2e/apimachinery/chunking.go b/test/e2e/apimachinery/chunking.go index 4a5b817abea..1f773c9a6f3 100644 --- a/test/e2e/apimachinery/chunking.go +++ b/test/e2e/apimachinery/chunking.go @@ -89,7 +89,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() { lastRV = list.ResourceVersion } gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV)) - gomega.Expect(int(list.RemainingItemCount) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) + gomega.Expect(int(list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) for _, item := range list.Items { gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found))) found++ @@ -122,7 +122,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() { gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit) firstToken := list.Continue firstRV := list.ResourceVersion - gomega.Expect(int(list.RemainingItemCount) + len(list.Items)).To(gomega.BeNumerically("==", numberOfTotalResources)) + gomega.Expect(int(list.GetRemainingItemCount()) + len(list.Items)).To(gomega.BeNumerically("==", numberOfTotalResources)) e2elog.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, firstToken) ginkgo.By("retrieving the second page until the token expires") @@ -157,7 +157,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() { gomega.Expect(list.ResourceVersion).ToNot(gomega.Equal(firstRV)) gomega.Expect(len(list.Items)).To(gomega.BeNumerically("==", opts.Limit)) found := int(oneTenth) - gomega.Expect(int(list.RemainingItemCount) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) + gomega.Expect(int(list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) for _, item := range list.Items { gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found))) found++ @@ -169,7 +169,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() { for { list, err := client.List(opts) gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit) - gomega.Expect(int(list.RemainingItemCount) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) + gomega.Expect(int(list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources)) e2elog.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, list.Continue) gomega.Expect(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit)) gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV))