provider_namespace.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 common
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. . "github.com/onsi/ginkgo/v2"
  19. . "github.com/onsi/gomega"
  20. corev1 "k8s.io/api/core/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. "github.com/external-secrets/external-secrets-e2e/framework"
  25. )
  26. func CreateProviderCaseNamespace(f *framework.Framework, prefix string, pollInterval time.Duration) string {
  27. if pollInterval <= 0 {
  28. pollInterval = 5 * time.Second
  29. }
  30. namespace := &corev1.Namespace{
  31. ObjectMeta: metav1.ObjectMeta{
  32. GenerateName: fmt.Sprintf("e2e-tests-%s-", prefix),
  33. },
  34. }
  35. Expect(f.CRClient.Create(context.Background(), namespace)).To(Succeed())
  36. DeferCleanup(func() {
  37. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
  38. defer cancel()
  39. err := f.CRClient.Delete(ctx, namespace)
  40. if err != nil && !apierrors.IsNotFound(err) {
  41. Expect(err).ToNot(HaveOccurred())
  42. }
  43. err = wait.PollUntilContextTimeout(ctx, pollInterval, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
  44. _, err := f.KubeClientSet.CoreV1().Namespaces().Get(ctx, namespace.Name, metav1.GetOptions{})
  45. if apierrors.IsNotFound(err) {
  46. return true, nil
  47. }
  48. if err != nil {
  49. return false, err
  50. }
  51. return false, nil
  52. })
  53. Expect(err).To(Succeed())
  54. })
  55. return namespace.Name
  56. }