helper.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2018 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 app
  14. import (
  15. "context"
  16. "fmt"
  17. "net/http"
  18. "time"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/klog"
  23. )
  24. // WaitForAPIServer waits for the API Server's /healthz endpoint to report "ok" with timeout.
  25. func WaitForAPIServer(client clientset.Interface, timeout time.Duration) error {
  26. var lastErr error
  27. err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
  28. healthStatus := 0
  29. result := client.Discovery().RESTClient().Get().AbsPath("/healthz").Do(context.TODO()).StatusCode(&healthStatus)
  30. if result.Error() != nil {
  31. lastErr = fmt.Errorf("failed to get apiserver /healthz status: %v", result.Error())
  32. return false, nil
  33. }
  34. if healthStatus != http.StatusOK {
  35. content, _ := result.Raw()
  36. lastErr = fmt.Errorf("APIServer isn't healthy: %v", string(content))
  37. klog.Warningf("APIServer isn't healthy yet: %v. Waiting a little while.", string(content))
  38. return false, nil
  39. }
  40. return true, nil
  41. })
  42. if err != nil {
  43. return fmt.Errorf("%v: %v", err, lastErr)
  44. }
  45. return nil
  46. }
  47. // IsControllerEnabled check if a specified controller enabled or not.
  48. func IsControllerEnabled(name string, disabledByDefaultControllers sets.String, controllers []string) bool {
  49. hasStar := false
  50. for _, ctrl := range controllers {
  51. if ctrl == name {
  52. return true
  53. }
  54. if ctrl == "-"+name {
  55. return false
  56. }
  57. if ctrl == "*" {
  58. hasStar = true
  59. }
  60. }
  61. // if we get here, there was no explicit choice
  62. if !hasStar {
  63. // nothing on by default
  64. return false
  65. }
  66. return !disabledByDefaultControllers.Has(name)
  67. }