conditions.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2014 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 conditions
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/apimachinery/pkg/watch"
  20. )
  21. // ErrPodCompleted is returned by PodRunning or PodContainerRunning to indicate that
  22. // the pod has already reached completed state.
  23. var ErrPodCompleted = fmt.Errorf("pod ran to completion")
  24. // PodRunning returns true if the pod is running, false if the pod has not yet reached running state,
  25. // returns ErrPodCompleted if the pod has run to completion, or an error in any other case.
  26. func PodRunning(event watch.Event) (bool, error) {
  27. switch event.Type {
  28. case watch.Deleted:
  29. return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
  30. }
  31. switch t := event.Object.(type) {
  32. case *v1.Pod:
  33. switch t.Status.Phase {
  34. case v1.PodRunning:
  35. return true, nil
  36. case v1.PodFailed, v1.PodSucceeded:
  37. return false, ErrPodCompleted
  38. }
  39. }
  40. return false, nil
  41. }
  42. // PodCompleted returns true if the pod has run to completion, false if the pod has not yet
  43. // reached running state, or an error in any other case.
  44. func PodCompleted(event watch.Event) (bool, error) {
  45. switch event.Type {
  46. case watch.Deleted:
  47. return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
  48. }
  49. switch t := event.Object.(type) {
  50. case *v1.Pod:
  51. switch t.Status.Phase {
  52. case v1.PodFailed, v1.PodSucceeded:
  53. return true, nil
  54. }
  55. }
  56. return false, nil
  57. }
  58. // ServiceAccountHasSecrets returns true if the service account has at least one secret,
  59. // false if it does not, or an error.
  60. func ServiceAccountHasSecrets(event watch.Event) (bool, error) {
  61. switch event.Type {
  62. case watch.Deleted:
  63. return false, errors.NewNotFound(schema.GroupResource{Resource: "serviceaccounts"}, "")
  64. }
  65. switch t := event.Object.(type) {
  66. case *v1.ServiceAccount:
  67. return len(t.Secrets) > 0, nil
  68. }
  69. return false, nil
  70. }