common_test.go 6.0 KB

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