common_test.go 6.1 KB

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