uninstall_eso_crds.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 addon
  14. import (
  15. "context"
  16. "strings"
  17. "time"
  18. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. . "github.com/onsi/ginkgo/v2"
  23. )
  24. var (
  25. externalSecretsCRDDeletePollInterval = time.Second
  26. externalSecretsCRDDeleteTimeout = 5 * time.Minute
  27. )
  28. func uninstallCRDs(cfg *Config) error {
  29. By("Uninstalling eso CRDs")
  30. crdList, err := listExternalSecretsCRDs(GinkgoT().Context(), cfg)
  31. if err != nil {
  32. return err
  33. }
  34. for _, crd := range crdList {
  35. err := cfg.CRClient.Delete(GinkgoT().Context(), &crd, &client.DeleteOptions{})
  36. if err != nil && !apierrors.IsNotFound(err) {
  37. return err
  38. }
  39. }
  40. if len(crdList) == 0 {
  41. return nil
  42. }
  43. return wait.PollUntilContextTimeout(GinkgoT().Context(), externalSecretsCRDDeletePollInterval, externalSecretsCRDDeleteTimeout, true, func(ctx context.Context) (bool, error) {
  44. crds, err := listExternalSecretsCRDs(ctx, cfg)
  45. if err != nil {
  46. return false, err
  47. }
  48. return len(crds) == 0, nil
  49. })
  50. }
  51. func listExternalSecretsCRDs(ctx context.Context, cfg *Config) ([]apiextensionsv1.CustomResourceDefinition, error) {
  52. var crdList apiextensionsv1.CustomResourceDefinitionList
  53. if err := cfg.CRClient.List(ctx, &crdList); err != nil {
  54. return nil, err
  55. }
  56. crds := make([]apiextensionsv1.CustomResourceDefinition, 0, len(crdList.Items))
  57. for _, crd := range crdList.Items {
  58. if !isExternalSecretsCRDGroup(crd.Spec.Group) {
  59. continue
  60. }
  61. crds = append(crds, crd)
  62. }
  63. return crds, nil
  64. }
  65. func isExternalSecretsCRDGroup(group string) bool {
  66. return strings.Contains(group, "external-secrets.io")
  67. }