naming.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "math/rand"
  17. "strconv"
  18. "strings"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. "k8s.io/kubernetes/pkg/kubelet/leaky"
  21. )
  22. // Container "names" are implementation details that do not concern
  23. // kubelet/CRI. This CRI shim uses names to fulfill the CRI requirement to
  24. // make sandbox/container creation idempotent. CRI states that there can
  25. // only exist one sandbox/container with the given metadata. To enforce this,
  26. // this shim constructs a name using the fields in the metadata so that
  27. // docker will reject the creation request if the name already exists.
  28. //
  29. // Note that changes to naming will likely break the backward compatibility.
  30. // Code must be added to ensure the shim knows how to recognize and extract
  31. // information the older containers.
  32. //
  33. // TODO: Add code to handle backward compatibility, i.e., making sure we can
  34. // recognize older containers and extract information from their names if
  35. // necessary.
  36. const (
  37. // kubePrefix is used to identify the containers/sandboxes on the node managed by kubelet
  38. kubePrefix = "k8s"
  39. // sandboxContainerName is a string to include in the docker container so
  40. // that users can easily identify the sandboxes.
  41. sandboxContainerName = leaky.PodInfraContainerName
  42. // Delimiter used to construct docker container names.
  43. nameDelimiter = "_"
  44. // DockerImageIDPrefix is the prefix of image id in container status.
  45. DockerImageIDPrefix = "docker://"
  46. // DockerPullableImageIDPrefix is the prefix of pullable image id in container status.
  47. DockerPullableImageIDPrefix = "docker-pullable://"
  48. )
  49. func makeSandboxName(s *runtimeapi.PodSandboxConfig) string {
  50. return strings.Join([]string{
  51. kubePrefix, // 0
  52. sandboxContainerName, // 1
  53. s.Metadata.Name, // 2
  54. s.Metadata.Namespace, // 3
  55. s.Metadata.Uid, // 4
  56. fmt.Sprintf("%d", s.Metadata.Attempt), // 5
  57. }, nameDelimiter)
  58. }
  59. func makeContainerName(s *runtimeapi.PodSandboxConfig, c *runtimeapi.ContainerConfig) string {
  60. return strings.Join([]string{
  61. kubePrefix, // 0
  62. c.Metadata.Name, // 1:
  63. s.Metadata.Name, // 2: sandbox name
  64. s.Metadata.Namespace, // 3: sandbox namesapce
  65. s.Metadata.Uid, // 4 sandbox uid
  66. fmt.Sprintf("%d", c.Metadata.Attempt), // 5
  67. }, nameDelimiter)
  68. }
  69. // randomizeName randomizes the container name. This should only be used when we hit the
  70. // docker container name conflict bug.
  71. func randomizeName(name string) string {
  72. return strings.Join([]string{
  73. name,
  74. fmt.Sprintf("%08x", rand.Uint32()),
  75. }, nameDelimiter)
  76. }
  77. func parseUint32(s string) (uint32, error) {
  78. n, err := strconv.ParseUint(s, 10, 32)
  79. if err != nil {
  80. return 0, err
  81. }
  82. return uint32(n), nil
  83. }
  84. // TODO: Evaluate whether we should rely on labels completely.
  85. func parseSandboxName(name string) (*runtimeapi.PodSandboxMetadata, error) {
  86. // Docker adds a "/" prefix to names. so trim it.
  87. name = strings.TrimPrefix(name, "/")
  88. parts := strings.Split(name, nameDelimiter)
  89. // Tolerate the random suffix.
  90. // TODO(random-liu): Remove 7 field case when docker 1.11 is deprecated.
  91. if len(parts) != 6 && len(parts) != 7 {
  92. return nil, fmt.Errorf("failed to parse the sandbox name: %q", name)
  93. }
  94. if parts[0] != kubePrefix {
  95. return nil, fmt.Errorf("container is not managed by kubernetes: %q", name)
  96. }
  97. attempt, err := parseUint32(parts[5])
  98. if err != nil {
  99. return nil, fmt.Errorf("failed to parse the sandbox name %q: %v", name, err)
  100. }
  101. return &runtimeapi.PodSandboxMetadata{
  102. Name: parts[2],
  103. Namespace: parts[3],
  104. Uid: parts[4],
  105. Attempt: attempt,
  106. }, nil
  107. }
  108. // TODO: Evaluate whether we should rely on labels completely.
  109. func parseContainerName(name string) (*runtimeapi.ContainerMetadata, error) {
  110. // Docker adds a "/" prefix to names. so trim it.
  111. name = strings.TrimPrefix(name, "/")
  112. parts := strings.Split(name, nameDelimiter)
  113. // Tolerate the random suffix.
  114. // TODO(random-liu): Remove 7 field case when docker 1.11 is deprecated.
  115. if len(parts) != 6 && len(parts) != 7 {
  116. return nil, fmt.Errorf("failed to parse the container name: %q", name)
  117. }
  118. if parts[0] != kubePrefix {
  119. return nil, fmt.Errorf("container is not managed by kubernetes: %q", name)
  120. }
  121. attempt, err := parseUint32(parts[5])
  122. if err != nil {
  123. return nil, fmt.Errorf("failed to parse the container name %q: %v", name, err)
  124. }
  125. return &runtimeapi.ContainerMetadata{
  126. Name: parts[1],
  127. Attempt: attempt,
  128. }, nil
  129. }