chunking.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. apierrors "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. )
  32. func shouldCheckRemainingItem() bool {
  33. return utilfeature.DefaultFeatureGate.Enabled(features.RemainingItemCount)
  34. }
  35. const numberOfTotalResources = 400
  36. var _ = SIGDescribe("Servers with support for API chunking", func() {
  37. f := framework.NewDefaultFramework("chunking")
  38. ginkgo.BeforeEach(func() {
  39. ns := f.Namespace.Name
  40. c := f.ClientSet
  41. client := c.CoreV1().PodTemplates(ns)
  42. ginkgo.By("creating a large number of resources")
  43. workqueue.ParallelizeUntil(context.TODO(), 20, numberOfTotalResources, func(i int) {
  44. for tries := 3; tries >= 0; tries-- {
  45. _, err := client.Create(context.TODO(), &v1.PodTemplate{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: fmt.Sprintf("template-%04d", i),
  48. },
  49. Template: v1.PodTemplateSpec{
  50. Spec: v1.PodSpec{
  51. Containers: []v1.Container{
  52. {Name: "test", Image: "test2"},
  53. },
  54. },
  55. },
  56. }, metav1.CreateOptions{})
  57. if err == nil {
  58. return
  59. }
  60. framework.Logf("Got an error creating template %d: %v", i, err)
  61. }
  62. framework.Failf("Unable to create template %d, exiting", i)
  63. })
  64. })
  65. ginkgo.It("should return chunks of results for list calls", func() {
  66. ns := f.Namespace.Name
  67. c := f.ClientSet
  68. client := c.CoreV1().PodTemplates(ns)
  69. ginkgo.By("retrieving those results in paged fashion several times")
  70. for i := 0; i < 3; i++ {
  71. opts := metav1.ListOptions{}
  72. found := 0
  73. var lastRV string
  74. for {
  75. opts.Limit = int64(rand.Int31n(numberOfTotalResources/10) + 1)
  76. list, err := client.List(context.TODO(), opts)
  77. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  78. framework.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, list.Continue)
  79. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit))
  80. if len(lastRV) == 0 {
  81. lastRV = list.ResourceVersion
  82. }
  83. framework.ExpectEqual(list.ResourceVersion, lastRV)
  84. if shouldCheckRemainingItem() {
  85. if list.GetContinue() == "" {
  86. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  87. } else {
  88. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  89. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  90. }
  91. }
  92. for _, item := range list.Items {
  93. framework.ExpectEqual(item.Name, fmt.Sprintf("template-%04d", found))
  94. found++
  95. }
  96. if len(list.Continue) == 0 {
  97. break
  98. }
  99. opts.Continue = list.Continue
  100. }
  101. gomega.Expect(found).To(gomega.BeNumerically("==", numberOfTotalResources))
  102. }
  103. ginkgo.By("retrieving those results all at once")
  104. opts := metav1.ListOptions{Limit: numberOfTotalResources + 1}
  105. list, err := client.List(context.TODO(), opts)
  106. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  107. gomega.Expect(list.Items).To(gomega.HaveLen(numberOfTotalResources))
  108. })
  109. ginkgo.It("should support continue listing from the last key if the original version has been compacted away, though the list is inconsistent [Slow]", func() {
  110. ns := f.Namespace.Name
  111. c := f.ClientSet
  112. client := c.CoreV1().PodTemplates(ns)
  113. ginkgo.By("retrieving the first page")
  114. oneTenth := int64(numberOfTotalResources / 10)
  115. opts := metav1.ListOptions{}
  116. opts.Limit = oneTenth
  117. list, err := client.List(context.TODO(), opts)
  118. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  119. firstToken := list.Continue
  120. firstRV := list.ResourceVersion
  121. if shouldCheckRemainingItem() {
  122. if list.GetContinue() == "" {
  123. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  124. } else {
  125. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  126. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items)).To(gomega.BeNumerically("==", numberOfTotalResources))
  127. }
  128. }
  129. framework.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, firstToken)
  130. ginkgo.By("retrieving the second page until the token expires")
  131. opts.Continue = firstToken
  132. var inconsistentToken string
  133. wait.Poll(20*time.Second, 2*storagebackend.DefaultCompactInterval, func() (bool, error) {
  134. _, err := client.List(context.TODO(), opts)
  135. if err == nil {
  136. framework.Logf("Token %s has not expired yet", firstToken)
  137. return false, nil
  138. }
  139. if err != nil && !apierrors.IsResourceExpired(err) {
  140. return false, err
  141. }
  142. framework.Logf("got error %s", err)
  143. status, ok := err.(apierrors.APIStatus)
  144. if !ok {
  145. return false, fmt.Errorf("expect error to implement the APIStatus interface, got %v", reflect.TypeOf(err))
  146. }
  147. inconsistentToken = status.Status().ListMeta.Continue
  148. if len(inconsistentToken) == 0 {
  149. return false, fmt.Errorf("expect non empty continue token")
  150. }
  151. framework.Logf("Retrieved inconsistent continue %s", inconsistentToken)
  152. return true, nil
  153. })
  154. ginkgo.By("retrieving the second page again with the token received with the error message")
  155. opts.Continue = inconsistentToken
  156. list, err = client.List(context.TODO(), opts)
  157. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given inconsistent continue token %s and limit: %d", ns, opts.Continue, opts.Limit)
  158. framework.ExpectNotEqual(list.ResourceVersion, firstRV)
  159. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("==", opts.Limit))
  160. found := int(oneTenth)
  161. if shouldCheckRemainingItem() {
  162. if list.GetContinue() == "" {
  163. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  164. } else {
  165. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  166. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  167. }
  168. }
  169. for _, item := range list.Items {
  170. framework.ExpectEqual(item.Name, fmt.Sprintf("template-%04d", found))
  171. found++
  172. }
  173. ginkgo.By("retrieving all remaining pages")
  174. opts.Continue = list.Continue
  175. lastRV := list.ResourceVersion
  176. for {
  177. list, err := client.List(context.TODO(), opts)
  178. framework.ExpectNoError(err, "failed to list pod templates in namespace: %s, given limit: %d", ns, opts.Limit)
  179. if shouldCheckRemainingItem() {
  180. if list.GetContinue() == "" {
  181. gomega.Expect(list.GetRemainingItemCount()).To(gomega.BeNil())
  182. } else {
  183. gomega.Expect(list.GetRemainingItemCount()).ToNot(gomega.BeNil())
  184. gomega.Expect(int(*list.GetRemainingItemCount()) + len(list.Items) + found).To(gomega.BeNumerically("==", numberOfTotalResources))
  185. }
  186. }
  187. framework.Logf("Retrieved %d/%d results with rv %s and continue %s", len(list.Items), opts.Limit, list.ResourceVersion, list.Continue)
  188. gomega.Expect(len(list.Items)).To(gomega.BeNumerically("<=", opts.Limit))
  189. framework.ExpectEqual(list.ResourceVersion, lastRV)
  190. for _, item := range list.Items {
  191. framework.ExpectEqual(item.Name, fmt.Sprintf("template-%04d", found))
  192. found++
  193. }
  194. if len(list.Continue) == 0 {
  195. break
  196. }
  197. opts.Continue = list.Continue
  198. }
  199. gomega.Expect(found).To(gomega.BeNumerically("==", numberOfTotalResources))
  200. })
  201. })