conditions.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2016 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 utils
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  18. )
  19. type ContainerFailures struct {
  20. status *v1.ContainerStateTerminated
  21. Restarts int
  22. }
  23. // PodRunningReady checks whether pod p's phase is running and it has a ready
  24. // condition of status true.
  25. func PodRunningReady(p *v1.Pod) (bool, error) {
  26. // Check the phase is running.
  27. if p.Status.Phase != v1.PodRunning {
  28. return false, fmt.Errorf("want pod '%s' on '%s' to be '%v' but was '%v'",
  29. p.ObjectMeta.Name, p.Spec.NodeName, v1.PodRunning, p.Status.Phase)
  30. }
  31. // Check the ready condition is true.
  32. if !podutil.IsPodReady(p) {
  33. return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
  34. p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionTrue, p.Status.Conditions)
  35. }
  36. return true, nil
  37. }
  38. func PodRunningReadyOrSucceeded(p *v1.Pod) (bool, error) {
  39. // Check if the phase is succeeded.
  40. if p.Status.Phase == v1.PodSucceeded {
  41. return true, nil
  42. }
  43. return PodRunningReady(p)
  44. }
  45. // FailedContainers inspects all containers in a pod and returns failure
  46. // information for containers that have failed or been restarted.
  47. // A map is returned where the key is the containerID and the value is a
  48. // struct containing the restart and failure information
  49. func FailedContainers(pod *v1.Pod) map[string]ContainerFailures {
  50. var state ContainerFailures
  51. states := make(map[string]ContainerFailures)
  52. statuses := pod.Status.ContainerStatuses
  53. if len(statuses) == 0 {
  54. return nil
  55. }
  56. for _, status := range statuses {
  57. if status.State.Terminated != nil {
  58. states[status.ContainerID] = ContainerFailures{status: status.State.Terminated}
  59. } else if status.LastTerminationState.Terminated != nil {
  60. states[status.ContainerID] = ContainerFailures{status: status.LastTerminationState.Terminated}
  61. }
  62. if status.RestartCount > 0 {
  63. var ok bool
  64. if state, ok = states[status.ContainerID]; !ok {
  65. state = ContainerFailures{}
  66. }
  67. state.Restarts = int(status.RestartCount)
  68. states[status.ContainerID] = state
  69. }
  70. }
  71. return states
  72. }
  73. // TerminatedContainers inspects all containers in a pod and returns a map
  74. // of "container name: termination reason", for all currently terminated
  75. // containers.
  76. func TerminatedContainers(pod *v1.Pod) map[string]string {
  77. states := make(map[string]string)
  78. statuses := pod.Status.ContainerStatuses
  79. if len(statuses) == 0 {
  80. return states
  81. }
  82. for _, status := range statuses {
  83. if status.State.Terminated != nil {
  84. states[status.Name] = status.State.Terminated.Reason
  85. }
  86. }
  87. return states
  88. }
  89. // PodNotReady checks whether pod p's has a ready condition of status false.
  90. func PodNotReady(p *v1.Pod) (bool, error) {
  91. // Check the ready condition is false.
  92. if podutil.IsPodReady(p) {
  93. return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
  94. p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionFalse, p.Status.Conditions)
  95. }
  96. return true, nil
  97. }