common_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package crds
  13. import (
  14. "context"
  15. "encoding/json"
  16. "time"
  17. . "github.com/onsi/ginkgo/v2"
  18. . "github.com/onsi/gomega"
  19. corev1 "k8s.io/api/core/v1"
  20. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  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/types"
  25. )
  26. const (
  27. crdGroup = "apiextensions.k8s.io"
  28. crdKind = "CustomResourceDefinition"
  29. crdVersion = "v1"
  30. )
  31. type testCase struct {
  32. crd unstructured.Unstructured
  33. crd2 unstructured.Unstructured
  34. service corev1.Service
  35. secret corev1.Secret
  36. assert func()
  37. }
  38. var _ = Describe("CRD reconcile", func() {
  39. var test *testCase
  40. BeforeEach(func() {
  41. test = makeDefaultTestcase()
  42. })
  43. AfterEach(func() {
  44. // To improve later on with proper clean up.
  45. })
  46. // a invalid provider config should be reflected
  47. // in the store status condition
  48. PatchesCRD := func(tc *testCase) {
  49. tc.assert = func() {
  50. Consistently(func() bool {
  51. ss := unstructured.Unstructured{}
  52. ss.SetGroupVersionKind(schema.GroupVersionKind{Kind: crdKind, Version: crdVersion, Group: crdGroup})
  53. err := k8sClient.Get(context.Background(), types.NamespacedName{
  54. Name: "secretstores.test.io",
  55. }, &ss)
  56. if err != nil {
  57. return false
  58. }
  59. val, ok, err := unstructured.NestedString(ss.Object, "spec", "conversion", "webhook", "clientConfig", "service", "name")
  60. if err != nil || !ok {
  61. return false
  62. }
  63. want, ok, err := unstructured.NestedString(tc.crd.Object, "spec", "conversion", "webhook", "clientConfig", "service", "name")
  64. if err != nil || !ok {
  65. return false
  66. }
  67. return want != val
  68. }).
  69. WithTimeout(time.Second * 10).
  70. WithPolling(time.Second).
  71. Should(BeTrue())
  72. }
  73. }
  74. // if controllerClass does not match the controller
  75. // should not touch this store
  76. ignoreNonTargetCRDs := func(tc *testCase) {
  77. tc.assert = func() {
  78. Consistently(func() bool {
  79. ss := unstructured.Unstructured{}
  80. ss.SetGroupVersionKind(schema.GroupVersionKind{Kind: crdKind, Version: crdVersion, Group: crdGroup})
  81. err := k8sClient.Get(context.Background(), types.NamespacedName{
  82. Name: "some-other.test.io",
  83. }, &ss)
  84. if err != nil {
  85. return false
  86. }
  87. got, ok, err := unstructured.NestedString(ss.Object, "spec", "conversion", "webhook", "clientConfig", "service", "name")
  88. if !ok || err != nil {
  89. return false
  90. }
  91. want, ok, err := unstructured.NestedString(tc.crd2.Object, "spec", "conversion", "webhook", "clientConfig", "service", "name")
  92. if !ok || err != nil {
  93. return false
  94. }
  95. return got == want
  96. }).
  97. WithTimeout(time.Second * 3).
  98. WithPolling(time.Millisecond * 500).
  99. Should(BeTrue())
  100. }
  101. }
  102. DescribeTable("Controller Reconcile logic", func(muts ...func(tc *testCase)) {
  103. for _, mut := range muts {
  104. mut(test)
  105. }
  106. ctx := context.Background()
  107. k8sClient.Create(ctx, &test.secret)
  108. k8sClient.Create(ctx, &test.service)
  109. k8sClient.Create(ctx, &test.crd)
  110. k8sClient.Create(ctx, &test.crd2)
  111. test.assert()
  112. },
  113. Entry("[namespace] Ignore non Target CRDs", ignoreNonTargetCRDs),
  114. Entry("[namespace] Patch target CRDs", PatchesCRD),
  115. )
  116. })
  117. func makeUnstructuredCRD(plural, group string) unstructured.Unstructured {
  118. crd := apiextensions.CustomResourceDefinition{
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: plural + "." + group,
  121. },
  122. Spec: apiextensions.CustomResourceDefinitionSpec{
  123. Versions: []apiextensions.CustomResourceDefinitionVersion{
  124. {
  125. Name: "v1",
  126. Served: true,
  127. Storage: true,
  128. Schema: &apiextensions.CustomResourceValidation{
  129. OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
  130. Type: "object",
  131. },
  132. },
  133. },
  134. },
  135. Group: group,
  136. Scope: apiextensions.NamespaceScoped,
  137. Names: apiextensions.CustomResourceDefinitionNames{
  138. Plural: plural,
  139. Singular: "idc",
  140. Kind: "IDC",
  141. ListKind: "IDCList",
  142. },
  143. Conversion: &apiextensions.CustomResourceConversion{
  144. Strategy: "Webhook",
  145. Webhook: &apiextensions.WebhookConversion{
  146. ConversionReviewVersions: []string{"v1"},
  147. ClientConfig: &apiextensions.WebhookClientConfig{
  148. CABundle: []byte("foobar"),
  149. Service: &apiextensions.ServiceReference{
  150. Name: "webhook",
  151. Namespace: "default",
  152. },
  153. },
  154. },
  155. },
  156. },
  157. }
  158. marshal, _ := json.Marshal(crd)
  159. unmarshal := make(map[string]interface{})
  160. json.Unmarshal(marshal, &unmarshal)
  161. u := unstructured.Unstructured{
  162. Object: unmarshal,
  163. }
  164. u.SetGroupVersionKind(schema.GroupVersionKind{Kind: crdKind, Version: "v1", Group: crdGroup})
  165. return u
  166. }
  167. func makeSecret() corev1.Secret {
  168. return corev1.Secret{
  169. ObjectMeta: metav1.ObjectMeta{
  170. Name: "foo",
  171. Namespace: "default",
  172. Labels: map[string]string{
  173. "foo": "bar",
  174. },
  175. },
  176. }
  177. }
  178. func makeService() corev1.Service {
  179. return corev1.Service{
  180. ObjectMeta: metav1.ObjectMeta{
  181. Name: "foo",
  182. Namespace: "default",
  183. Labels: map[string]string{
  184. "foo": "bar",
  185. },
  186. },
  187. Spec: corev1.ServiceSpec{
  188. Ports: []corev1.ServicePort{
  189. {
  190. Name: "http",
  191. Port: 80,
  192. },
  193. },
  194. },
  195. }
  196. }
  197. func makeDefaultTestcase() *testCase {
  198. return &testCase{
  199. assert: func() {
  200. // this is a noop by default
  201. },
  202. crd: makeUnstructuredCRD("secretstores", "test.io"),
  203. crd2: makeUnstructuredCRD("some-other", "test.io"),
  204. secret: makeSecret(),
  205. service: makeService(),
  206. }
  207. }