util.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package util
  13. import (
  14. "context"
  15. "fmt"
  16. "net/http"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. "k8s.io/client-go/kubernetes"
  23. )
  24. const (
  25. // How often to poll for conditions.
  26. Poll = 2 * time.Second
  27. )
  28. // CreateKubeNamespace creates a new Kubernetes Namespace for a test.
  29. func CreateKubeNamespace(baseName string, kubeClientSet kubernetes.Interface) (*v1.Namespace, error) {
  30. ns := &v1.Namespace{
  31. ObjectMeta: metav1.ObjectMeta{
  32. GenerateName: fmt.Sprintf("e2e-tests-%v-", baseName),
  33. },
  34. }
  35. return kubeClientSet.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
  36. }
  37. // DeleteKubeNamespace will delete a namespace resource.
  38. func DeleteKubeNamespace(namespace string, kubeClientSet kubernetes.Interface) error {
  39. return kubeClientSet.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
  40. }
  41. // WaitForKubeNamespaceNotExist will wait for the namespace with the given name
  42. // to not exist for up to 2 minutes.
  43. func WaitForKubeNamespaceNotExist(namespace string, kubeClientSet kubernetes.Interface) error {
  44. return wait.PollImmediate(Poll, time.Minute*2, namespaceNotExist(kubeClientSet, namespace))
  45. }
  46. func namespaceNotExist(c kubernetes.Interface, namespace string) wait.ConditionFunc {
  47. return func() (bool, error) {
  48. _, err := c.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
  49. if apierrors.IsNotFound(err) {
  50. return true, nil
  51. }
  52. if err != nil {
  53. return false, err
  54. }
  55. return false, nil
  56. }
  57. }
  58. // WaitForURL tests the provided url. Once a http 200 is returned the func returns with no error.
  59. // Timeout is 5min.
  60. func WaitForURL(url string) error {
  61. return wait.PollImmediate(2*time.Second, time.Minute*5, func() (bool, error) {
  62. req, err := http.NewRequest(http.MethodGet, url, nil)
  63. if err != nil {
  64. return false, nil
  65. }
  66. res, err := http.DefaultClient.Do(req)
  67. if err != nil {
  68. return false, nil
  69. }
  70. defer res.Body.Close()
  71. if res.StatusCode == http.StatusOK {
  72. return true, nil
  73. }
  74. return false, err
  75. })
  76. }