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.
This commit is contained in:
Chao Xu 2019-05-24 17:35:00 -07:00
parent 01a5ec3d3d
commit e28a1072d9
9 changed files with 46 additions and 23 deletions

View File

@ -112,7 +112,7 @@ func (h *HumanReadablePrinter) GenerateTable(obj runtime.Object, options PrintOp
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink() table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue() table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount() table.SetRemainingItemCount(m.GetRemainingItemCount())
} else { } else {
if m, err := meta.CommonAccessor(obj); err == nil { if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()

View File

@ -87,7 +87,7 @@ func (c *convertor) ConvertToTable(ctx context.Context, obj runtime.Object, tabl
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink() table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue() table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount() table.SetRemainingItemCount(m.GetRemainingItemCount())
} else { } else {
if m, err := meta.CommonAccessor(obj); err == nil { if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()

View File

@ -115,8 +115,20 @@ func (meta *ListMeta) GetSelfLink() string { return meta.SelfLink
func (meta *ListMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink } func (meta *ListMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink }
func (meta *ListMeta) GetContinue() string { return meta.Continue } func (meta *ListMeta) GetContinue() string { return meta.Continue }
func (meta *ListMeta) SetContinue(c string) { meta.Continue = c } 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 } func (obj *TypeMeta) GetObjectKind() schema.ObjectKind { return obj }

View File

@ -88,7 +88,7 @@ type ListMeta struct {
// because it is unpaginated or because this is the last page), then there are no more remaining // 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. // items and this field will also be unset. Servers older than v1.15 do not set this field.
// +optional // +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 // These are internal finalizer values for Kubernetes-like APIs, must be qualified name unless defined here

View File

@ -20,8 +20,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/google/gofuzz" fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer" metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -202,7 +201,7 @@ type InternalTypeMeta struct {
SelfLink string `json:"selfLink,omitempty"` SelfLink string `json:"selfLink,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty"` ResourceVersion string `json:"resourceVersion,omitempty"`
Continue string `json:"next,omitempty"` Continue string `json:"next,omitempty"`
RemainingItemCount int64 `json:"remainingItemCount,omitempty"` RemainingItemCount *int64 `json:"remainingItemCount,omitempty"`
APIVersion string `json:"apiVersion,omitempty"` APIVersion string `json:"apiVersion,omitempty"`
Labels map[string]string `json:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"` Annotations map[string]string `json:"annotations,omitempty"`
@ -210,14 +209,26 @@ type InternalTypeMeta struct {
OwnerReferences []metav1.OwnerReference `json:"ownerReferences,omitempty"` OwnerReferences []metav1.OwnerReference `json:"ownerReferences,omitempty"`
} }
func (m *InternalTypeMeta) GetResourceVersion() string { return m.ResourceVersion } func (m *InternalTypeMeta) GetResourceVersion() string { return m.ResourceVersion }
func (m *InternalTypeMeta) SetResourceVersion(rv string) { m.ResourceVersion = rv } func (m *InternalTypeMeta) SetResourceVersion(rv string) { m.ResourceVersion = rv }
func (m *InternalTypeMeta) GetSelfLink() string { return m.SelfLink } func (m *InternalTypeMeta) GetSelfLink() string { return m.SelfLink }
func (m *InternalTypeMeta) SetSelfLink(link string) { m.SelfLink = link } func (m *InternalTypeMeta) SetSelfLink(link string) { m.SelfLink = link }
func (m *InternalTypeMeta) GetContinue() string { return m.Continue } func (m *InternalTypeMeta) GetContinue() string { return m.Continue }
func (m *InternalTypeMeta) SetContinue(c string) { m.Continue = c } 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) 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 { type MyAPIObject struct {
TypeMeta InternalTypeMeta `json:",inline"` TypeMeta InternalTypeMeta `json:",inline"`

View File

@ -67,7 +67,7 @@ func (c defaultTableConvertor) ConvertToTable(ctx context.Context, object runtim
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink() table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue() table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount() table.SetRemainingItemCount(m.GetRemainingItemCount())
} else { } else {
if m, err := meta.CommonAccessor(object); err == nil { if m, err := meta.CommonAccessor(object); err == nil {
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()

View File

@ -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)) t.Errorf("(%s): length of list want=%d, got=%d", tt.name, len(tt.expectedOut), len(out.Items))
continue 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) t.Errorf("(%s): remainingItemCount want=%d, got=%d", tt.name, e, a)
} }
for j, wantPod := range tt.expectedOut { for j, wantPod := range tt.expectedOut {

View File

@ -73,7 +73,7 @@ func (c *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOpti
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink() table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue() table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount() table.SetRemainingItemCount(m.GetRemainingItemCount())
} else { } else {
if m, err := meta.CommonAccessor(obj); err == nil { if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()

View File

@ -89,7 +89,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() {
lastRV = list.ResourceVersion lastRV = list.ResourceVersion
} }
gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV)) 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 { for _, item := range list.Items {
gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found))) gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found)))
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) gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
firstToken := list.Continue firstToken := list.Continue
firstRV := list.ResourceVersion 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) 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") 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(list.ResourceVersion).ToNot(gomega.Equal(firstRV))
gomega.Expect(len(list.Items)).To(gomega.BeNumerically("==", opts.Limit)) gomega.Expect(len(list.Items)).To(gomega.BeNumerically("==", opts.Limit))
found := int(oneTenth) 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 { for _, item := range list.Items {
gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found))) gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found)))
found++ found++
@ -169,7 +169,7 @@ var _ = SIGDescribe("Servers with support for API chunking", func() {
for { for {
list, err := client.List(opts) 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(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) 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(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit))
gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV)) gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV))