helpers.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package runtime
  15. import (
  16. "fmt"
  17. "strconv"
  18. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. )
  22. func (m *runtimeManager) sandboxToContainer(s *runtimeapi.PodSandbox) (*Container, error) {
  23. if s == nil || s.Id == "" {
  24. return nil, errors.Errorf("unable to convert a nil pointer to a runtime container")
  25. }
  26. return &Container{
  27. ID: ContainerID{Type: m.runtimeName, ID: s.Id},
  28. State: SandboxToContainerState(s.State),
  29. }, nil
  30. }
  31. func SandboxToContainerState(state runtimeapi.PodSandboxState) State {
  32. switch state {
  33. case runtimeapi.PodSandboxState_SANDBOX_READY:
  34. return ContainerStateRunning
  35. case runtimeapi.PodSandboxState_SANDBOX_NOTREADY:
  36. return ContainerStateExited
  37. }
  38. return ContainerStateUnknown
  39. }
  40. func (m *runtimeManager) toContainer(c *runtimeapi.Container) (*Container, error) {
  41. if c == nil || c.Id == "" || c.Image == nil {
  42. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime container")
  43. }
  44. return &Container{
  45. ID: ContainerID{Type: m.runtimeName, ID: c.Id},
  46. Name: c.GetMetadata().GetName(),
  47. Image: c.ImageRef,
  48. ImageID: c.Image.Image,
  49. State: toContainerState(c.State),
  50. }, nil
  51. }
  52. // toContainerState converts runtime.ContainerState to State.
  53. func toContainerState(state runtimeapi.ContainerState) State {
  54. switch state {
  55. case runtimeapi.ContainerState_CONTAINER_CREATED:
  56. return ContainerStateCreated
  57. case runtimeapi.ContainerState_CONTAINER_RUNNING:
  58. return ContainerStateRunning
  59. case runtimeapi.ContainerState_CONTAINER_EXITED:
  60. return ContainerStateExited
  61. case runtimeapi.ContainerState_CONTAINER_UNKNOWN:
  62. return ContainerStateUnknown
  63. }
  64. return ContainerStateUnknown
  65. }
  66. type labeledContainerInfo struct {
  67. ContainerName string
  68. PodName string
  69. PodNamespace string
  70. PodUid string
  71. }
  72. func getStringValueFromLabel(labels map[string]string, label string) string {
  73. if labels == nil {
  74. return ""
  75. }
  76. if value, found := labels[label]; found {
  77. return value
  78. }
  79. // Do not report error, because there should be many old containers without label now.
  80. // Return empty string "" for these containers, the caller will get value by other ways.
  81. return ""
  82. }
  83. func getIntValueFromLabel(labels map[string]string, label string) (int, error) {
  84. if strValue, found := labels[label]; found {
  85. intValue, err := strconv.Atoi(strValue)
  86. if err != nil {
  87. // This really should not happen. Just set value to 0 to handle this abnormal case
  88. return 0, err
  89. }
  90. return intValue, nil
  91. }
  92. // Do not report error, because there should be many old containers without label now.
  93. log.Infof("Container doesn't have label %s, it may be an old or invalid container", label)
  94. // Just set the value to 0
  95. return 0, nil
  96. }
  97. func getContainerInfoFromLabels(labels, annotations map[string]string) *labeledContainerInfo {
  98. podName := getStringValueFromLabel(labels, PodNameLabel)
  99. if podName == "" {
  100. podName = getStringValueFromLabel(annotations, SandboxNameAnnotation)
  101. }
  102. podNamespace := getStringValueFromLabel(labels, PodNamespaceLabel)
  103. if podNamespace == "" {
  104. podNamespace = getStringValueFromLabel(annotations, SandboxNamespaceAnnotation)
  105. }
  106. podUid := getStringValueFromLabel(labels, PodUIDLabel)
  107. if podUid == "" {
  108. podUid = getStringValueFromLabel(annotations, SandboxUidAnnotation)
  109. }
  110. containerName := getStringValueFromLabel(labels, ContainerNameLabel)
  111. if containerName == "" {
  112. containerName = getStringValueFromLabel(annotations, ContainerNameAnnotation)
  113. }
  114. return &labeledContainerInfo{
  115. PodName: podName,
  116. PodNamespace: podNamespace,
  117. PodUid: podUid,
  118. ContainerName: containerName,
  119. }
  120. }
  121. type annotatedContainerInfo struct {
  122. Hash uint64
  123. RestartCount int
  124. PodDeletionGracePeriod *int64
  125. PodTerminationGracePeriod *int64
  126. TerminationMessagePath string
  127. }
  128. // getContainerInfoFromAnnotations gets annotatedContainerInfo from annotations.
  129. func getContainerInfoFromAnnotations(annotations map[string]string) *annotatedContainerInfo {
  130. if annotations == nil {
  131. return nil
  132. }
  133. var err error
  134. containerInfo := &annotatedContainerInfo{}
  135. if containerInfo.RestartCount, err = getIntValueFromLabel(annotations, ContainerRestartCountLabel); err != nil {
  136. log.Errorf("Unable to get %q from annotations %v: %v", ContainerRestartCountLabel, annotations, err)
  137. }
  138. return containerInfo
  139. }
  140. type containerStatusByCreated []*Status
  141. func (c containerStatusByCreated) Len() int { return len(c) }
  142. func (c containerStatusByCreated) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
  143. func (c containerStatusByCreated) Less(i, j int) bool { return c[i].CreatedAt.After(c[j].CreatedAt) }