helper.go 2.1 KB

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