etcd_cross_group_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2019 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 etcd
  14. import (
  15. "context"
  16. "encoding/json"
  17. "testing"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/meta"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/apimachinery/pkg/util/sets"
  25. "k8s.io/apimachinery/pkg/watch"
  26. "k8s.io/client-go/dynamic"
  27. "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
  28. )
  29. // TestCrossGroupStorage tests to make sure that all objects stored in an expected location in etcd can be converted/read.
  30. func TestCrossGroupStorage(t *testing.T) {
  31. master := StartRealMasterOrDie(t, func(opts *options.ServerRunOptions) {
  32. // force enable all resources so we can check storage.
  33. })
  34. defer master.Cleanup()
  35. etcdStorageData := GetEtcdStorageData()
  36. crossGroupResources := map[schema.GroupVersionKind][]Resource{}
  37. master.Client.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}, metav1.CreateOptions{})
  38. // Group by persisted GVK
  39. for _, resourceToPersist := range master.Resources {
  40. gvk := resourceToPersist.Mapping.GroupVersionKind
  41. data, exists := etcdStorageData[resourceToPersist.Mapping.Resource]
  42. if !exists {
  43. continue
  44. }
  45. storageGVK := gvk
  46. if data.ExpectedGVK != nil {
  47. storageGVK = *data.ExpectedGVK
  48. }
  49. crossGroupResources[storageGVK] = append(crossGroupResources[storageGVK], resourceToPersist)
  50. }
  51. // Clear any without cross-group sources
  52. for gvk, resources := range crossGroupResources {
  53. groups := sets.NewString()
  54. for _, resource := range resources {
  55. groups.Insert(resource.Mapping.GroupVersionKind.Group)
  56. }
  57. if len(groups) < 2 {
  58. delete(crossGroupResources, gvk)
  59. }
  60. }
  61. if len(crossGroupResources) == 0 {
  62. // Sanity check
  63. t.Fatal("no cross-group resources found")
  64. }
  65. // Test all potential cross-group sources can be watched and fetched from all other sources
  66. for gvk, resources := range crossGroupResources {
  67. t.Run(gvk.String(), func(t *testing.T) {
  68. // use the first one to create the initial object
  69. resource := resources[0]
  70. // compute namespace
  71. ns := ""
  72. if resource.Mapping.Scope.Name() == meta.RESTScopeNameNamespace {
  73. ns = testNamespace
  74. }
  75. data := etcdStorageData[resource.Mapping.Resource]
  76. // create object
  77. resourceClient, obj, err := JSONToUnstructured(data.Stub, ns, resource.Mapping, master.Dynamic)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. actual, err := resourceClient.Create(obj, metav1.CreateOptions{})
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. name := actual.GetName()
  86. // Set up clients, versioned data, and watches for all versions
  87. var (
  88. clients = map[schema.GroupVersionResource]dynamic.ResourceInterface{}
  89. versionedData = map[schema.GroupVersionResource]*unstructured.Unstructured{}
  90. watches = map[schema.GroupVersionResource]watch.Interface{}
  91. )
  92. for _, resource := range resources {
  93. clients[resource.Mapping.Resource] = master.Dynamic.Resource(resource.Mapping.Resource).Namespace(ns)
  94. versionedData[resource.Mapping.Resource], err = clients[resource.Mapping.Resource].Get(name, metav1.GetOptions{})
  95. if err != nil {
  96. t.Fatalf("error finding resource via %s: %v", resource.Mapping.Resource.GroupVersion().String(), err)
  97. }
  98. watches[resource.Mapping.Resource], err = clients[resource.Mapping.Resource].Watch(metav1.ListOptions{ResourceVersion: actual.GetResourceVersion()})
  99. if err != nil {
  100. t.Fatalf("error opening watch via %s: %v", resource.Mapping.Resource.GroupVersion().String(), err)
  101. }
  102. }
  103. for _, resource := range resources {
  104. // clear out the things cleared in etcd
  105. versioned := versionedData[resource.Mapping.Resource]
  106. versioned.SetResourceVersion("")
  107. versioned.SetSelfLink("")
  108. versionedJSON, err := versioned.MarshalJSON()
  109. if err != nil {
  110. t.Error(err)
  111. continue
  112. }
  113. // Update in etcd
  114. if _, err := master.KV.Put(context.Background(), data.ExpectedEtcdPath, string(versionedJSON)); err != nil {
  115. t.Error(err)
  116. continue
  117. }
  118. t.Logf("wrote %s to etcd", resource.Mapping.Resource.GroupVersion().String())
  119. // Ensure everyone gets a watch event with the right version
  120. for watchResource, watcher := range watches {
  121. select {
  122. case event, ok := <-watcher.ResultChan():
  123. if !ok {
  124. t.Fatalf("watch of %s closed in response to persisting %s", watchResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String())
  125. }
  126. if event.Type != watch.Modified {
  127. eventJSON, _ := json.Marshal(event)
  128. t.Errorf("unexpected watch event sent to watch of %s in response to persisting %s: %s", watchResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String(), string(eventJSON))
  129. continue
  130. }
  131. if event.Object.GetObjectKind().GroupVersionKind().GroupVersion() != watchResource.GroupVersion() {
  132. t.Errorf("unexpected group version object sent to watch of %s in response to persisting %s: %#v", watchResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String(), event.Object)
  133. continue
  134. }
  135. t.Logf(" received event for %s", watchResource.GroupVersion().String())
  136. case <-time.After(30 * time.Second):
  137. t.Errorf("timed out waiting for watch event for %s in response to persisting %s", watchResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String())
  138. continue
  139. }
  140. }
  141. // Ensure everyone can do a direct get and gets the right version
  142. for clientResource, client := range clients {
  143. obj, err := client.Get(name, metav1.GetOptions{})
  144. if err != nil {
  145. t.Errorf("error looking up %s after persisting %s", clientResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String())
  146. continue
  147. }
  148. if obj.GetObjectKind().GroupVersionKind().GroupVersion() != clientResource.GroupVersion() {
  149. t.Errorf("unexpected group version retrieved from %s after persisting %s: %#v", clientResource.GroupVersion().String(), resource.Mapping.Resource.GroupVersion().String(), obj)
  150. continue
  151. }
  152. t.Logf(" fetched object for %s", clientResource.GroupVersion().String())
  153. }
  154. }
  155. })
  156. }
  157. }