wait.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2019 The Kubernetes 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. http://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 service
  14. import (
  15. "context"
  16. "fmt"
  17. apierrors "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/wait"
  20. clientset "k8s.io/client-go/kubernetes"
  21. servicehelper "k8s.io/cloud-provider/service/helpers"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. "github.com/onsi/ginkgo"
  24. )
  25. // WaitForServiceDeletedWithFinalizer waits for the service with finalizer to be deleted.
  26. func WaitForServiceDeletedWithFinalizer(cs clientset.Interface, namespace, name string) {
  27. ginkgo.By("Delete service with finalizer")
  28. if err := cs.CoreV1().Services(namespace).Delete(context.TODO(), name, nil); err != nil {
  29. framework.Failf("Failed to delete service %s/%s", namespace, name)
  30. }
  31. ginkgo.By("Wait for service to disappear")
  32. if pollErr := wait.PollImmediate(LoadBalancerPollInterval, GetServiceLoadBalancerCreationTimeout(cs), func() (bool, error) {
  33. svc, err := cs.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{})
  34. if err != nil {
  35. if apierrors.IsNotFound(err) {
  36. framework.Logf("Service %s/%s is gone.", namespace, name)
  37. return true, nil
  38. }
  39. return false, err
  40. }
  41. framework.Logf("Service %s/%s still exists with finalizers: %v", namespace, name, svc.Finalizers)
  42. return false, nil
  43. }); pollErr != nil {
  44. framework.Failf("Failed to wait for service to disappear: %v", pollErr)
  45. }
  46. }
  47. // WaitForServiceUpdatedWithFinalizer waits for the service to be updated to have or
  48. // don't have a finalizer.
  49. func WaitForServiceUpdatedWithFinalizer(cs clientset.Interface, namespace, name string, hasFinalizer bool) {
  50. ginkgo.By(fmt.Sprintf("Wait for service to hasFinalizer=%t", hasFinalizer))
  51. if pollErr := wait.PollImmediate(LoadBalancerPollInterval, GetServiceLoadBalancerCreationTimeout(cs), func() (bool, error) {
  52. svc, err := cs.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{})
  53. if err != nil {
  54. return false, err
  55. }
  56. foundFinalizer := false
  57. for _, finalizer := range svc.Finalizers {
  58. if finalizer == servicehelper.LoadBalancerCleanupFinalizer {
  59. foundFinalizer = true
  60. break
  61. }
  62. }
  63. if foundFinalizer != hasFinalizer {
  64. framework.Logf("Service %s/%s hasFinalizer=%t, want %t", namespace, name, foundFinalizer, hasFinalizer)
  65. return false, nil
  66. }
  67. return true, nil
  68. }); pollErr != nil {
  69. framework.Failf("Failed to wait for service to hasFinalizer=%t: %v", hasFinalizer, pollErr)
  70. }
  71. }