install_eso_crds.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "fmt"
  17. "os/exec"
  18. "path/filepath"
  19. "time"
  20. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. . "github.com/onsi/ginkgo/v2"
  25. )
  26. var requiredRuntimeCRDNames = []string{
  27. "clusterproviderclasses.external-secrets.io",
  28. "providerclasses.external-secrets.io",
  29. }
  30. var (
  31. externalSecretsCRDInstallPollInterval = time.Second
  32. externalSecretsCRDInstallTimeout = 5 * time.Minute
  33. )
  34. func installCRDs(cfg *Config) error {
  35. bundlePath := filepath.Join(AssetDir(), "deploy/crds/bundle.yaml")
  36. cmd := exec.Command("kubectl", "apply", "--server-side", "--force-conflicts", "-f", bundlePath)
  37. output, err := cmd.CombinedOutput()
  38. if err != nil {
  39. return fmt.Errorf("unable to install eso CRDs from %s: %w: %s", bundlePath, err, string(output))
  40. }
  41. return wait.PollUntilContextTimeout(GinkgoT().Context(), externalSecretsCRDInstallPollInterval, externalSecretsCRDInstallTimeout, true, func(ctx context.Context) (bool, error) {
  42. for _, crdName := range requiredRuntimeCRDNames {
  43. var crd apiextensionsv1.CustomResourceDefinition
  44. err := cfg.CRClient.Get(ctx, types.NamespacedName{Name: crdName}, &crd)
  45. if apierrors.IsNotFound(err) {
  46. return false, nil
  47. }
  48. if err != nil {
  49. return false, err
  50. }
  51. established := false
  52. for _, condition := range crd.Status.Conditions {
  53. if condition.Type == apiextensionsv1.Established && condition.Status == apiextensionsv1.ConditionTrue {
  54. established = true
  55. break
  56. }
  57. }
  58. if !established {
  59. return false, nil
  60. }
  61. }
  62. return true, nil
  63. })
  64. }