generate.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 status
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. // GeneratePodReadyCondition returns ready condition if all containers in a pod are ready, else it
  20. // returns an unready condition.
  21. func GeneratePodReadyCondition(spec *api.PodSpec, containerStatuses []api.ContainerStatus, podPhase api.PodPhase) api.PodCondition {
  22. // Find if all containers are ready or not.
  23. if containerStatuses == nil {
  24. return api.PodCondition{
  25. Type: api.PodReady,
  26. Status: api.ConditionFalse,
  27. Reason: "UnknownContainerStatuses",
  28. }
  29. }
  30. unknownContainers := []string{}
  31. unreadyContainers := []string{}
  32. for _, container := range spec.Containers {
  33. if containerStatus, ok := api.GetContainerStatus(containerStatuses, container.Name); ok {
  34. if !containerStatus.Ready {
  35. unreadyContainers = append(unreadyContainers, container.Name)
  36. }
  37. } else {
  38. unknownContainers = append(unknownContainers, container.Name)
  39. }
  40. }
  41. // If all containers are known and succeeded, just return PodCompleted.
  42. if podPhase == api.PodSucceeded && len(unknownContainers) == 0 {
  43. return api.PodCondition{
  44. Type: api.PodReady,
  45. Status: api.ConditionFalse,
  46. Reason: "PodCompleted",
  47. }
  48. }
  49. unreadyMessages := []string{}
  50. if len(unknownContainers) > 0 {
  51. unreadyMessages = append(unreadyMessages, fmt.Sprintf("containers with unknown status: %s", unknownContainers))
  52. }
  53. if len(unreadyContainers) > 0 {
  54. unreadyMessages = append(unreadyMessages, fmt.Sprintf("containers with unready status: %s", unreadyContainers))
  55. }
  56. unreadyMessage := strings.Join(unreadyMessages, ", ")
  57. if unreadyMessage != "" {
  58. return api.PodCondition{
  59. Type: api.PodReady,
  60. Status: api.ConditionFalse,
  61. Reason: "ContainersNotReady",
  62. Message: unreadyMessage,
  63. }
  64. }
  65. return api.PodCondition{
  66. Type: api.PodReady,
  67. Status: api.ConditionTrue,
  68. }
  69. }
  70. // GeneratePodInitializedCondition returns initialized condition if all init containers in a pod are ready, else it
  71. // returns an uninitialized condition.
  72. func GeneratePodInitializedCondition(spec *api.PodSpec, containerStatuses []api.ContainerStatus, podPhase api.PodPhase) api.PodCondition {
  73. // Find if all containers are ready or not.
  74. if containerStatuses == nil && len(spec.InitContainers) > 0 {
  75. return api.PodCondition{
  76. Type: api.PodInitialized,
  77. Status: api.ConditionFalse,
  78. Reason: "UnknownContainerStatuses",
  79. }
  80. }
  81. unknownContainers := []string{}
  82. unreadyContainers := []string{}
  83. for _, container := range spec.InitContainers {
  84. if containerStatus, ok := api.GetContainerStatus(containerStatuses, container.Name); ok {
  85. if !containerStatus.Ready {
  86. unreadyContainers = append(unreadyContainers, container.Name)
  87. }
  88. } else {
  89. unknownContainers = append(unknownContainers, container.Name)
  90. }
  91. }
  92. // If all init containers are known and succeeded, just return PodCompleted.
  93. if podPhase == api.PodSucceeded && len(unknownContainers) == 0 {
  94. return api.PodCondition{
  95. Type: api.PodInitialized,
  96. Status: api.ConditionTrue,
  97. Reason: "PodCompleted",
  98. }
  99. }
  100. unreadyMessages := []string{}
  101. if len(unknownContainers) > 0 {
  102. unreadyMessages = append(unreadyMessages, fmt.Sprintf("containers with unknown status: %s", unknownContainers))
  103. }
  104. if len(unreadyContainers) > 0 {
  105. unreadyMessages = append(unreadyMessages, fmt.Sprintf("containers with incomplete status: %s", unreadyContainers))
  106. }
  107. unreadyMessage := strings.Join(unreadyMessages, ", ")
  108. if unreadyMessage != "" {
  109. return api.PodCondition{
  110. Type: api.PodInitialized,
  111. Status: api.ConditionFalse,
  112. Reason: "ContainersNotInitialized",
  113. Message: unreadyMessage,
  114. }
  115. }
  116. return api.PodCondition{
  117. Type: api.PodInitialized,
  118. Status: api.ConditionTrue,
  119. }
  120. }