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. "time"
  16. . "github.com/onsi/ginkgo/v2"
  17. . "github.com/onsi/gomega"
  18. corev1 "k8s.io/api/core/v1"
  19. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  25. )
  26. type testCase struct {
  27. crd *apiextensions.CustomResourceDefinition
  28. crd2 *apiextensions.CustomResourceDefinition
  29. service corev1.Service
  30. secret corev1.Secret
  31. assert func()
  32. }
  33. var _ = Describe("CRD reconcile", func() {
  34. var test *testCase
  35. BeforeEach(func() {
  36. test = makeDefaultTestcase()
  37. })
  38. AfterEach(func() {
  39. ctx := context.Background()
  40. k8sClient.Delete(ctx, &test.secret)
  41. k8sClient.Delete(ctx, &test.service)
  42. deleteCRD(test.crd)
  43. deleteCRD(test.crd2)
  44. })
  45. // a invalid provider config should be reflected
  46. // in the store status condition
  47. PatchesCRD := func(tc *testCase) {
  48. tc.assert = func() {
  49. Eventually(func() bool {
  50. crd := &apiextensions.CustomResourceDefinition{}
  51. err := k8sClient.Get(context.Background(), types.NamespacedName{
  52. Name: "secretstores.test.io",
  53. }, crd)
  54. if err != nil {
  55. return false
  56. }
  57. return crd.Spec.Conversion.Webhook.ClientConfig.Service.Name !=
  58. tc.crd.Spec.Conversion.Webhook.ClientConfig.Service.Name
  59. }).
  60. WithTimeout(time.Second * 10).
  61. WithPolling(time.Second).
  62. Should(BeTrue())
  63. }
  64. }
  65. // if controllerClass does not match the controller
  66. // should not touch this store
  67. ignoreNonTargetCRDs := func(tc *testCase) {
  68. tc.assert = func() {
  69. Consistently(func() bool {
  70. crd := &apiextensions.CustomResourceDefinition{}
  71. err := k8sClient.Get(context.Background(), types.NamespacedName{
  72. Name: "some-other.test.io",
  73. }, crd)
  74. if err != nil {
  75. return false
  76. }
  77. return crd.Spec.Conversion.Webhook.ClientConfig.Service.Name ==
  78. tc.crd2.Spec.Conversion.Webhook.ClientConfig.Service.Name
  79. }).
  80. WithTimeout(time.Second * 3).
  81. WithPolling(time.Millisecond * 500).
  82. Should(BeTrue())
  83. }
  84. }
  85. DescribeTable("Controller Reconcile logic", func(muts ...func(tc *testCase)) {
  86. for _, mut := range muts {
  87. mut(test)
  88. }
  89. ctx := context.Background()
  90. err := k8sClient.Create(ctx, &test.secret)
  91. Expect(err).ToNot(HaveOccurred())
  92. err = k8sClient.Create(ctx, &test.service)
  93. Expect(err).ToNot(HaveOccurred())
  94. err = k8sClient.Create(ctx, test.crd)
  95. Expect(err).ToNot(HaveOccurred())
  96. err = k8sClient.Create(ctx, test.crd2)
  97. Expect(err).ToNot(HaveOccurred())
  98. test.assert()
  99. },
  100. Entry("[namespace] Ignore non Target CRDs", ignoreNonTargetCRDs),
  101. Entry("[namespace] Patch target CRDs", PatchesCRD),
  102. )
  103. })
  104. func deleteCRD(crd *apiextensions.CustomResourceDefinition) {
  105. err := k8sClient.Delete(context.Background(), crd, client.GracePeriodSeconds(0))
  106. if err != nil && !apierrors.IsNotFound(err) {
  107. Fail("unable to delete crd " + crd.Name)
  108. return
  109. }
  110. Eventually(func() bool {
  111. err := k8sClient.Get(context.Background(), types.NamespacedName{
  112. Name: crd.Name,
  113. }, crd)
  114. if err == nil {
  115. // force delete by removing finalizers
  116. // note: we can not delete a CRD with an invalid caBundle field
  117. cpy := crd.DeepCopy()
  118. controllerutil.RemoveFinalizer(cpy, "customresourcecleanup.apiextensions.k8s.io")
  119. p := client.MergeFrom(crd)
  120. k8sClient.Patch(context.Background(), cpy, p)
  121. return false
  122. }
  123. return apierrors.IsNotFound(err)
  124. }).WithTimeout(time.Second * 5).WithPolling(time.Second).Should(BeTrue())
  125. }
  126. func makeCRD(plural, group string) *apiextensions.CustomResourceDefinition {
  127. return &apiextensions.CustomResourceDefinition{
  128. ObjectMeta: metav1.ObjectMeta{
  129. Name: plural + "." + group,
  130. },
  131. Spec: apiextensions.CustomResourceDefinitionSpec{
  132. Versions: []apiextensions.CustomResourceDefinitionVersion{
  133. {
  134. Name: "v1",
  135. Served: true,
  136. Storage: true,
  137. Schema: &apiextensions.CustomResourceValidation{
  138. OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
  139. Type: "object",
  140. },
  141. },
  142. },
  143. },
  144. Group: group,
  145. Scope: apiextensions.NamespaceScoped,
  146. Names: apiextensions.CustomResourceDefinitionNames{
  147. Plural: plural,
  148. Singular: "idc",
  149. Kind: "IDC",
  150. ListKind: "IDCList",
  151. },
  152. Conversion: &apiextensions.CustomResourceConversion{
  153. Strategy: "Webhook",
  154. Webhook: &apiextensions.WebhookConversion{
  155. ConversionReviewVersions: []string{"v1"},
  156. ClientConfig: &apiextensions.WebhookClientConfig{
  157. CABundle: []byte(`Cg==`),
  158. Service: &apiextensions.ServiceReference{
  159. Name: "webhook",
  160. Namespace: "default",
  161. },
  162. },
  163. },
  164. },
  165. },
  166. }
  167. }
  168. func makeSecret() corev1.Secret {
  169. return corev1.Secret{
  170. ObjectMeta: metav1.ObjectMeta{
  171. Name: "foo",
  172. Namespace: "default",
  173. Labels: map[string]string{
  174. "foo": "bar",
  175. },
  176. },
  177. }
  178. }
  179. func makeService() corev1.Service {
  180. return corev1.Service{
  181. ObjectMeta: metav1.ObjectMeta{
  182. Name: "foo",
  183. Namespace: "default",
  184. Labels: map[string]string{
  185. "foo": "bar",
  186. },
  187. },
  188. Spec: corev1.ServiceSpec{
  189. Ports: []corev1.ServicePort{
  190. {
  191. Name: "http",
  192. Port: 80,
  193. },
  194. },
  195. },
  196. }
  197. }
  198. func makeDefaultTestcase() *testCase {
  199. return &testCase{
  200. assert: func() {
  201. // this is a noop by default
  202. },
  203. crd: makeCRD("secretstores", "test.io"),
  204. crd2: makeCRD("some-other", "test.io"),
  205. secret: makeSecret(),
  206. service: makeService(),
  207. }
  208. }