chunking.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apimachinery
  14. import (
  15. "context"
  16. "fmt"
  17. "math/rand"
  18. "reflect"
  19. "time"
  20. "github.com/onsi/ginkgo"
  21. "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/api/errors"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. "k8s.io/apiserver/pkg/features"
  27. "k8s.io/apiserver/pkg/storage/storagebackend"
  28. utilfeature "k8s.io/apiserver/pkg/util/feature"
  29. "k8s.io/client-go/util/workqueue"
  30. "k8s.io/kubernetes/test/e2e/framework"
  31. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  32. )
  33. func shouldCheckRemainingItem() bool {
  34. return utilfeature.DefaultFeatureGate.Enabled(features.RemainingItemCount)
  35. }
  36. const numberOfTotalResources = 400
  37. var _ = SIGDescribe("Servers with support for API chunking", func() {
  38. f := framework.NewDefaultFramework("chunking")
  39. ginkgo.BeforeEach(func() {
  40. ns := f.Namespace.Name
  41. c := f.ClientSet
  42. client := c.CoreV1().PodTemplates(ns)
  43. ginkgo.By("creating a large number of resources")
  44. workqueue.ParallelizeUntil(context.TODO(), 20, numberOfTotalResources, func(i int) {
  45. for tries := 3; tries >= 0; tries-- {
  46. _, err := client.Create(&v1.PodTemplate{
  47. ObjectMeta: metav1.ObjectMeta{
  48. Name: fmt.Sprintf("template-%04d", i),
  49. },
  50. Template: v1.PodTemplateSpec{
  51. Spec: v1.PodSpec{
  52. Containers: []v1.Container{
  53. {Name: "test", Image: "test2"},
  54. },
  55. },
  56. },
  57. })
  58. if err == nil {
  59. return
  60. }
  61. e2elog.Logf("Got an error creating template %d: %v", i, err)
  62. }
  63. ginkgo.Fail("Unable to create template %d, exiting", i)
  64. })
  65. })
  66. ginkgo.It("should return chunks of results for list calls", func() {
  67. ns := f.Namespace.Name
  68. c := f.ClientSet
  69. client := c.CoreV1().PodTemplates(ns)
  70. ginkgo.By("retrieving those results in paged fashion several times")
  71. for i := 0; i < 3; i++ {
  72. opts := metav1.ListOptions{}
  73. found := 0
  74. var lastRV string
  75. for {
  76. opts.Limit = int64(rand.Int31n(numberOfTotalResources/10) + 1)
  77. list, err := client.List(opts)
  78. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  79. e2elog.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, list.Continue)
  80. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit))
  81. if len(lastRV) == 0 {
  82. lastRV = list.ResourceVersion
  83. }
  84. gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV))
  85. if shouldCheckRemainingItem() {
  86. if list.GetContinue() == "" {
  87. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  88. } else {
  89. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  90. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  91. }
  92. }
  93. for _, item := range list.Items {
  94. gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found)))
  95. found++
  96. }
  97. if len(list.Continue) == 0 {
  98. break
  99. }
  100. opts.Continue = list.Continue
  101. }
  102. gomega.Expect(found).To(gomega.BeNumerically("==", numberOfTotalResources))
  103. }
  104. ginkgo.By("retrieving those results all at once")
  105. opts := metav1.ListOptions{Limit: numberOfTotalResources + 1}
  106. list, err := client.List(opts)
  107. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  108. gomega.Expect(list.Items).To(gomega.HaveLen(numberOfTotalResources))
  109. })
  110. ginkgo.It("should support continue listing from the last key if the original version has been compacted away, though the list is inconsistent", func() {
  111. ns := f.Namespace.Name
  112. c := f.ClientSet
  113. client := c.CoreV1().PodTemplates(ns)
  114. ginkgo.By("retrieving the first page")
  115. oneTenth := int64(numberOfTotalResources / 10)
  116. opts := metav1.ListOptions{}
  117. opts.Limit = oneTenth
  118. list, err := client.List(opts)
  119. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  120. firstToken := list.Continue
  121. firstRV := list.ResourceVersion
  122. if shouldCheckRemainingItem() {
  123. if list.GetContinue() == "" {
  124. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  125. } else {
  126. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  127. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items)).To(gomega.BeNumerically("==", numberOfTotalResources))
  128. }
  129. }
  130. e2elog.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, firstToken)
  131. ginkgo.By("retrieving the second page until the token expires")
  132. opts.Continue = firstToken
  133. var inconsistentToken string
  134. wait.Poll(20*time.Second, 2*storagebackend.DefaultCompactInterval, func() (bool, error) {
  135. _, err := client.List(opts)
  136. if err == nil {
  137. e2elog.Logf("Token %s has not expired yet", firstToken)
  138. return false, nil
  139. }
  140. if err != nil && !errors.IsResourceExpired(err) {
  141. return false, err
  142. }
  143. e2elog.Logf("got error %s", err)
  144. status, ok := err.(errors.APIStatus)
  145. if !ok {
  146. return false, fmt.Errorf("expect error to implement the APIStatus interface, got %v", reflect.TypeOf(err))
  147. }
  148. inconsistentToken = status.Status().ListMeta.Continue
  149. if len(inconsistentToken) == 0 {
  150. return false, fmt.Errorf("expect non empty continue token")
  151. }
  152. e2elog.Logf("Retrieved inconsistent continue %s", inconsistentToken)
  153. return true, nil
  154. })
  155. ginkgo.By("retrieving the second page again with the token received with the error message")
  156. opts.Continue = inconsistentToken
  157. list, err = client.List(opts)
  158. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given inconsistent continue token %s and limit: %d", ns, opts.Continue, opts.Limit)
  159. gomega.Expect(list.ResourceVersion).ToNot(gomega.Equal(firstRV))
  160. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("==", opts.Limit))
  161. found := int(oneTenth)
  162. if shouldCheckRemainingItem() {
  163. if list.GetContinue() == "" {
  164. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  165. } else {
  166. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  167. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  168. }
  169. }
  170. for _, item := range list.Items {
  171. gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found)))
  172. found++
  173. }
  174. ginkgo.By("retrieving all remaining pages")
  175. opts.Continue = list.Continue
  176. lastRV := list.ResourceVersion
  177. for {
  178. list, err := client.List(opts)
  179. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  180. if shouldCheckRemainingItem() {
  181. if list.GetContinue() == "" {
  182. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  183. } else {
  184. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  185. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  186. }
  187. }
  188. e2elog.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, list.Continue)
  189. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit))
  190. gomega.Expect(list.ResourceVersion).To(gomega.Equal(lastRV))
  191. for _, item := range list.Items {
  192. gomega.Expect(item.Name).To(gomega.Equal(fmt.Sprintf("template-%04d", found)))
  193. found++
  194. }
  195. if len(list.Continue) == 0 {
  196. break
  197. }
  198. opts.Continue = list.Continue
  199. }
  200. gomega.Expect(found).To(gomega.BeNumerically("==", numberOfTotalResources))
  201. })
  202. })