convert.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 dockershim
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. dockertypes "github.com/docker/docker/api/types"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. "k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
  21. )
  22. // This file contains helper functions to convert docker API types to runtime
  23. // API types, or vice versa.
  24. func imageToRuntimeAPIImage(image *dockertypes.ImageSummary) (*runtimeapi.Image, error) {
  25. if image == nil {
  26. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime API image")
  27. }
  28. size := uint64(image.VirtualSize)
  29. return &runtimeapi.Image{
  30. Id: image.ID,
  31. RepoTags: image.RepoTags,
  32. RepoDigests: image.RepoDigests,
  33. Size_: size,
  34. }, nil
  35. }
  36. func imageInspectToRuntimeAPIImage(image *dockertypes.ImageInspect) (*runtimeapi.Image, error) {
  37. if image == nil || image.Config == nil {
  38. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime API image")
  39. }
  40. size := uint64(image.VirtualSize)
  41. runtimeImage := &runtimeapi.Image{
  42. Id: image.ID,
  43. RepoTags: image.RepoTags,
  44. RepoDigests: image.RepoDigests,
  45. Size_: size,
  46. }
  47. uid, username := getUserFromImageUser(image.Config.User)
  48. if uid != nil {
  49. runtimeImage.Uid = &runtimeapi.Int64Value{Value: *uid}
  50. }
  51. runtimeImage.Username = username
  52. return runtimeImage, nil
  53. }
  54. func toPullableImageID(id string, image *dockertypes.ImageInspect) string {
  55. // Default to the image ID, but if RepoDigests is not empty, use
  56. // the first digest instead.
  57. imageID := DockerImageIDPrefix + id
  58. if image != nil && len(image.RepoDigests) > 0 {
  59. imageID = DockerPullableImageIDPrefix + image.RepoDigests[0]
  60. }
  61. return imageID
  62. }
  63. func toRuntimeAPIContainer(c *dockertypes.Container) (*runtimeapi.Container, error) {
  64. state := toRuntimeAPIContainerState(c.Status)
  65. if len(c.Names) == 0 {
  66. return nil, fmt.Errorf("unexpected empty container name: %+v", c)
  67. }
  68. metadata, err := parseContainerName(c.Names[0])
  69. if err != nil {
  70. return nil, err
  71. }
  72. labels, annotations := extractLabels(c.Labels)
  73. sandboxID := c.Labels[sandboxIDLabelKey]
  74. // The timestamp in dockertypes.Container is in seconds.
  75. createdAt := c.Created * int64(time.Second)
  76. return &runtimeapi.Container{
  77. Id: c.ID,
  78. PodSandboxId: sandboxID,
  79. Metadata: metadata,
  80. Image: &runtimeapi.ImageSpec{Image: c.Image},
  81. ImageRef: c.ImageID,
  82. State: state,
  83. CreatedAt: createdAt,
  84. Labels: labels,
  85. Annotations: annotations,
  86. }, nil
  87. }
  88. func toDockerContainerStatus(state runtimeapi.ContainerState) string {
  89. switch state {
  90. case runtimeapi.ContainerState_CONTAINER_CREATED:
  91. return "created"
  92. case runtimeapi.ContainerState_CONTAINER_RUNNING:
  93. return "running"
  94. case runtimeapi.ContainerState_CONTAINER_EXITED:
  95. return "exited"
  96. case runtimeapi.ContainerState_CONTAINER_UNKNOWN:
  97. fallthrough
  98. default:
  99. return "unknown"
  100. }
  101. }
  102. func toRuntimeAPIContainerState(state string) runtimeapi.ContainerState {
  103. // Parse the state string in dockertypes.Container. This could break when
  104. // we upgrade docker.
  105. switch {
  106. case strings.HasPrefix(state, libdocker.StatusRunningPrefix):
  107. return runtimeapi.ContainerState_CONTAINER_RUNNING
  108. case strings.HasPrefix(state, libdocker.StatusExitedPrefix):
  109. return runtimeapi.ContainerState_CONTAINER_EXITED
  110. case strings.HasPrefix(state, libdocker.StatusCreatedPrefix):
  111. return runtimeapi.ContainerState_CONTAINER_CREATED
  112. default:
  113. return runtimeapi.ContainerState_CONTAINER_UNKNOWN
  114. }
  115. }
  116. func toRuntimeAPISandboxState(state string) runtimeapi.PodSandboxState {
  117. // Parse the state string in dockertypes.Container. This could break when
  118. // we upgrade docker.
  119. switch {
  120. case strings.HasPrefix(state, libdocker.StatusRunningPrefix):
  121. return runtimeapi.PodSandboxState_SANDBOX_READY
  122. default:
  123. return runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  124. }
  125. }
  126. func containerToRuntimeAPISandbox(c *dockertypes.Container) (*runtimeapi.PodSandbox, error) {
  127. state := toRuntimeAPISandboxState(c.Status)
  128. if len(c.Names) == 0 {
  129. return nil, fmt.Errorf("unexpected empty sandbox name: %+v", c)
  130. }
  131. metadata, err := parseSandboxName(c.Names[0])
  132. if err != nil {
  133. return nil, err
  134. }
  135. labels, annotations := extractLabels(c.Labels)
  136. // The timestamp in dockertypes.Container is in seconds.
  137. createdAt := c.Created * int64(time.Second)
  138. return &runtimeapi.PodSandbox{
  139. Id: c.ID,
  140. Metadata: metadata,
  141. State: state,
  142. CreatedAt: createdAt,
  143. Labels: labels,
  144. Annotations: annotations,
  145. }, nil
  146. }
  147. func checkpointToRuntimeAPISandbox(id string, checkpoint DockershimCheckpoint) *runtimeapi.PodSandbox {
  148. state := runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  149. _, name, namespace, _, _ := checkpoint.GetData()
  150. return &runtimeapi.PodSandbox{
  151. Id: id,
  152. Metadata: &runtimeapi.PodSandboxMetadata{
  153. Name: name,
  154. Namespace: namespace,
  155. },
  156. State: state,
  157. }
  158. }