crd_watch.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright 2018 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. "fmt"
  16. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  17. "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  18. "k8s.io/apiextensions-apiserver/test/integration/fixtures"
  19. "k8s.io/apimachinery/pkg/api/meta"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/watch"
  25. "k8s.io/client-go/dynamic"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. "github.com/onsi/ginkgo"
  28. )
  29. var _ = SIGDescribe("CustomResourceDefinition Watch [Privileged:ClusterAdmin]", func() {
  30. f := framework.NewDefaultFramework("crd-watch")
  31. ginkgo.Context("CustomResourceDefinition Watch", func() {
  32. /*
  33. Release: v1.16
  34. Testname: Custom Resource Definition, watch
  35. Description: Create a Custom Resource Definition. Attempt to watch it; the watch MUST observe create,
  36. modify and delete events.
  37. */
  38. framework.ConformanceIt("watch on custom resource definition objects", func() {
  39. const (
  40. watchCRNameA = "name1"
  41. watchCRNameB = "name2"
  42. )
  43. config, err := framework.LoadConfig()
  44. if err != nil {
  45. framework.Failf("failed to load config: %v", err)
  46. }
  47. apiExtensionClient, err := clientset.NewForConfig(config)
  48. if err != nil {
  49. framework.Failf("failed to initialize apiExtensionClient: %v", err)
  50. }
  51. noxuDefinition := fixtures.NewNoxuV1CustomResourceDefinition(apiextensionsv1.ClusterScoped)
  52. noxuDefinition, err = fixtures.CreateNewV1CustomResourceDefinition(noxuDefinition, apiExtensionClient, f.DynamicClient)
  53. if err != nil {
  54. framework.Failf("failed to create CustomResourceDefinition: %v", err)
  55. }
  56. defer func() {
  57. err = fixtures.DeleteV1CustomResourceDefinition(noxuDefinition, apiExtensionClient)
  58. if err != nil {
  59. framework.Failf("failed to delete CustomResourceDefinition: %v", err)
  60. }
  61. }()
  62. ns := ""
  63. noxuResourceClient, err := newNamespacedCustomResourceClient(ns, f.DynamicClient, noxuDefinition)
  64. framework.ExpectNoError(err, "creating custom resource client")
  65. watchA, err := watchCRWithName(noxuResourceClient, watchCRNameA)
  66. framework.ExpectNoError(err, "failed to watch custom resource: %s", watchCRNameA)
  67. watchB, err := watchCRWithName(noxuResourceClient, watchCRNameB)
  68. framework.ExpectNoError(err, "failed to watch custom resource: %s", watchCRNameB)
  69. testCrA := fixtures.NewNoxuInstance(ns, watchCRNameA)
  70. testCrB := fixtures.NewNoxuInstance(ns, watchCRNameB)
  71. ginkgo.By("Creating first CR ")
  72. testCrA, err = instantiateCustomResource(testCrA, noxuResourceClient, noxuDefinition)
  73. framework.ExpectNoError(err, "failed to instantiate custom resource: %+v", testCrA)
  74. expectEvent(watchA, watch.Added, testCrA)
  75. expectNoEvent(watchB, watch.Added, testCrA)
  76. ginkgo.By("Creating second CR")
  77. testCrB, err = instantiateCustomResource(testCrB, noxuResourceClient, noxuDefinition)
  78. framework.ExpectNoError(err, "failed to instantiate custom resource: %+v", testCrB)
  79. expectEvent(watchB, watch.Added, testCrB)
  80. expectNoEvent(watchA, watch.Added, testCrB)
  81. ginkgo.By("Modifying first CR")
  82. err = patchCustomResource(noxuResourceClient, watchCRNameA)
  83. framework.ExpectNoError(err, "failed to patch custom resource: %s", watchCRNameA)
  84. expectEvent(watchA, watch.Modified, nil)
  85. expectNoEvent(watchB, watch.Modified, nil)
  86. ginkgo.By("Modifying second CR")
  87. err = patchCustomResource(noxuResourceClient, watchCRNameB)
  88. framework.ExpectNoError(err, "failed to patch custom resource: %s", watchCRNameB)
  89. expectEvent(watchB, watch.Modified, nil)
  90. expectNoEvent(watchA, watch.Modified, nil)
  91. ginkgo.By("Deleting first CR")
  92. err = deleteCustomResource(noxuResourceClient, watchCRNameA)
  93. framework.ExpectNoError(err, "failed to delete custom resource: %s", watchCRNameA)
  94. expectEvent(watchA, watch.Deleted, nil)
  95. expectNoEvent(watchB, watch.Deleted, nil)
  96. ginkgo.By("Deleting second CR")
  97. err = deleteCustomResource(noxuResourceClient, watchCRNameB)
  98. framework.ExpectNoError(err, "failed to delete custom resource: %s", watchCRNameB)
  99. expectEvent(watchB, watch.Deleted, nil)
  100. expectNoEvent(watchA, watch.Deleted, nil)
  101. })
  102. })
  103. })
  104. func watchCRWithName(crdResourceClient dynamic.ResourceInterface, name string) (watch.Interface, error) {
  105. return crdResourceClient.Watch(
  106. metav1.ListOptions{
  107. FieldSelector: "metadata.name=" + name,
  108. TimeoutSeconds: int64ptr(600),
  109. },
  110. )
  111. }
  112. func instantiateCustomResource(instanceToCreate *unstructured.Unstructured, client dynamic.ResourceInterface, definition *apiextensionsv1.CustomResourceDefinition) (*unstructured.Unstructured, error) {
  113. createdInstance, err := client.Create(instanceToCreate, metav1.CreateOptions{})
  114. if err != nil {
  115. return nil, err
  116. }
  117. createdObjectMeta, err := meta.Accessor(createdInstance)
  118. if err != nil {
  119. return nil, err
  120. }
  121. // it should have a UUID
  122. if len(createdObjectMeta.GetUID()) == 0 {
  123. return nil, fmt.Errorf("missing uuid: %#v", createdInstance)
  124. }
  125. createdTypeMeta, err := meta.TypeAccessor(createdInstance)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if len(definition.Spec.Versions) != 1 {
  130. return nil, fmt.Errorf("expected exactly one version, got %v", definition.Spec.Versions)
  131. }
  132. if e, a := definition.Spec.Group+"/"+definition.Spec.Versions[0].Name, createdTypeMeta.GetAPIVersion(); e != a {
  133. return nil, fmt.Errorf("expected %v, got %v", e, a)
  134. }
  135. if e, a := definition.Spec.Names.Kind, createdTypeMeta.GetKind(); e != a {
  136. return nil, fmt.Errorf("expected %v, got %v", e, a)
  137. }
  138. return createdInstance, nil
  139. }
  140. func patchCustomResource(client dynamic.ResourceInterface, name string) error {
  141. _, err := client.Patch(
  142. name,
  143. types.JSONPatchType,
  144. []byte(`[{ "op": "add", "path": "/dummy", "value": "test" }]`),
  145. metav1.PatchOptions{})
  146. return err
  147. }
  148. func deleteCustomResource(client dynamic.ResourceInterface, name string) error {
  149. return client.Delete(name, &metav1.DeleteOptions{})
  150. }
  151. func newNamespacedCustomResourceClient(ns string, client dynamic.Interface, crd *apiextensionsv1.CustomResourceDefinition) (dynamic.ResourceInterface, error) {
  152. if len(crd.Spec.Versions) != 1 {
  153. return nil, fmt.Errorf("expected exactly one version, got %v", crd.Spec.Versions)
  154. }
  155. gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: crd.Spec.Versions[0].Name, Resource: crd.Spec.Names.Plural}
  156. if crd.Spec.Scope != apiextensionsv1.ClusterScoped {
  157. return client.Resource(gvr).Namespace(ns), nil
  158. }
  159. return client.Resource(gvr), nil
  160. }