docker_sandbox.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. dockertypes "github.com/docker/engine-api/types"
  17. dockercontainer "github.com/docker/engine-api/types/container"
  18. dockerfilters "github.com/docker/engine-api/types/filters"
  19. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  20. )
  21. const (
  22. defaultSandboxImage = "gcr.io/google_containers/pause-amd64:3.0"
  23. // Various default sandbox resources requests/limits.
  24. defaultSandboxCPUshares int64 = 2
  25. defaultSandboxOOMScore int = -999
  26. // Termination grace period
  27. defaultSandboxGracePeriod int = 10
  28. )
  29. // CreatePodSandbox creates a pod-level sandbox.
  30. // The definition of PodSandbox is at https://github.com/kubernetes/kubernetes/pull/25899
  31. // For docker, PodSandbox is implemented by a container holding the network
  32. // namespace for the pod.
  33. // Note: docker doesn't use LogDirectory (yet).
  34. func (ds *dockerService) CreatePodSandbox(config *runtimeApi.PodSandboxConfig) (string, error) {
  35. // Step 1: Pull the image for the sandbox.
  36. // TODO: How should we handle pulling custom pod infra container image
  37. // (with credentials)?
  38. image := defaultSandboxImage
  39. if err := ds.client.PullImage(image, dockertypes.AuthConfig{}, dockertypes.ImagePullOptions{}); err != nil {
  40. return "", fmt.Errorf("unable to pull image for the sandbox container: %v", err)
  41. }
  42. // Step 2: Create the sandbox container.
  43. createConfig := makeSandboxDockerConfig(config, image)
  44. createResp, err := ds.client.CreateContainer(*createConfig)
  45. if err != nil || createResp == nil {
  46. return "", fmt.Errorf("failed to create a sandbox for pod %q: %v", config.Metadata.GetName(), err)
  47. }
  48. // Step 3: Start the sandbox container.
  49. // Assume kubelet's garbage collector would remove the sandbox later, if
  50. // startContainer failed.
  51. err = ds.StartContainer(createResp.ID)
  52. return createResp.ID, err
  53. }
  54. // StopPodSandbox stops the sandbox. If there are any running containers in the
  55. // sandbox, they should be force terminated.
  56. func (ds *dockerService) StopPodSandbox(podSandboxID string) error {
  57. return ds.client.StopContainer(podSandboxID, defaultSandboxGracePeriod)
  58. // TODO: Stop all running containers in the sandbox.
  59. }
  60. // RemovePodSandbox removes the sandbox. If there are running containers in the
  61. // sandbox, they should be forcibly removed.
  62. func (ds *dockerService) RemovePodSandbox(podSandboxID string) error {
  63. return ds.client.RemoveContainer(podSandboxID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
  64. // TODO: remove all containers in the sandbox.
  65. }
  66. // PodSandboxStatus returns the status of the PodSandbox.
  67. func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeApi.PodSandboxStatus, error) {
  68. // Inspect the container.
  69. r, err := ds.client.InspectContainer(podSandboxID)
  70. if err != nil {
  71. return nil, err
  72. }
  73. // Parse the timstamps.
  74. createdAt, _, _, err := getContainerTimestamps(r)
  75. if err != nil {
  76. return nil, fmt.Errorf("failed to parse timestamp for container %q: %v", podSandboxID, err)
  77. }
  78. ct := createdAt.Unix()
  79. // Translate container to sandbox state.
  80. state := runtimeApi.PodSandBoxState_NOTREADY
  81. if r.State.Running {
  82. state = runtimeApi.PodSandBoxState_READY
  83. }
  84. // TODO: We can't really get the IP address from the network plugin, which
  85. // is handled by kubelet as of now. Should we amend the interface? How is
  86. // this handled in the new remote runtime integration?
  87. // See DockerManager.determineContainerIP() for more details.
  88. // For now, just assume that there is no network plugin.
  89. // Related issue: https://github.com/kubernetes/kubernetes/issues/28667
  90. var IP string
  91. if r.NetworkSettings != nil {
  92. IP = r.NetworkSettings.IPAddress
  93. // Fall back to IPv6 address if no IPv4 address is present
  94. if IP == "" {
  95. IP = r.NetworkSettings.GlobalIPv6Address
  96. }
  97. }
  98. network := &runtimeApi.PodSandboxNetworkStatus{Ip: &IP}
  99. netNS := getNetworkNamespace(r)
  100. podName, podNamespace, podUID, attempt, err := parseSandboxName(r.Name)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return &runtimeApi.PodSandboxStatus{
  105. Id: &r.ID,
  106. State: &state,
  107. CreatedAt: &ct,
  108. Metadata: &runtimeApi.PodSandboxMetadata{
  109. Name: &podName,
  110. Namespace: &podNamespace,
  111. Uid: &podUID,
  112. Attempt: &attempt,
  113. },
  114. // TODO: We write annotations as labels on the docker containers. All
  115. // these annotations will be read back as labels. Need to fix this.
  116. // Also filter out labels only relevant to this shim.
  117. Labels: r.Config.Labels,
  118. Network: network,
  119. Linux: &runtimeApi.LinuxPodSandboxStatus{Namespaces: &runtimeApi.Namespace{Network: &netNS}},
  120. }, nil
  121. }
  122. // ListPodSandbox returns a list of Sandbox.
  123. func (ds *dockerService) ListPodSandbox(filter *runtimeApi.PodSandboxFilter) ([]*runtimeApi.PodSandbox, error) {
  124. // By default, list all containers whether they are running or not.
  125. opts := dockertypes.ContainerListOptions{All: true}
  126. filterOutReadySandboxes := false
  127. opts.Filter = dockerfilters.NewArgs()
  128. f := newDockerFilter(&opts.Filter)
  129. if filter != nil {
  130. if filter.Id != nil {
  131. f.Add("id", filter.GetId())
  132. }
  133. if filter.State != nil {
  134. if filter.GetState() == runtimeApi.PodSandBoxState_READY {
  135. // Only list running containers.
  136. opts.All = false
  137. } else {
  138. // runtimeApi.PodSandBoxState_NOTREADY can mean the
  139. // container is in any of the non-running state (e.g., created,
  140. // exited). We can't tell docker to filter out running
  141. // containers directly, so we'll need to filter them out
  142. // ourselves after getting the results.
  143. filterOutReadySandboxes = true
  144. }
  145. }
  146. if filter.LabelSelector != nil {
  147. for k, v := range filter.LabelSelector {
  148. f.AddLabel(k, v)
  149. }
  150. }
  151. // Filter out sandbox containers.
  152. f.AddLabel(containerTypeLabelKey, containerTypeLabelSandbox)
  153. }
  154. containers, err := ds.client.ListContainers(opts)
  155. if err != nil {
  156. return nil, err
  157. }
  158. // Convert docker containers to runtime api sandboxes.
  159. result := []*runtimeApi.PodSandbox{}
  160. for _, c := range containers {
  161. if len(filter.GetName()) > 0 {
  162. sandboxName, _, _, _, err := parseSandboxName(c.Names[0])
  163. if err != nil || sandboxName != filter.GetName() {
  164. continue
  165. }
  166. }
  167. s := toRuntimeAPISandbox(&c)
  168. if filterOutReadySandboxes && s.GetState() == runtimeApi.PodSandBoxState_READY {
  169. continue
  170. }
  171. result = append(result, s)
  172. }
  173. return result, nil
  174. }
  175. func makeSandboxDockerConfig(c *runtimeApi.PodSandboxConfig, image string) *dockertypes.ContainerCreateConfig {
  176. // Merge annotations and labels because docker supports only labels.
  177. labels := makeLabels(c.GetLabels(), c.GetAnnotations())
  178. // Apply a label to distinguish sandboxes from regular containers.
  179. labels[containerTypeLabelKey] = containerTypeLabelSandbox
  180. hc := &dockercontainer.HostConfig{}
  181. createConfig := &dockertypes.ContainerCreateConfig{
  182. Name: buildSandboxName(c),
  183. Config: &dockercontainer.Config{
  184. Hostname: c.GetHostname(),
  185. // TODO: Handle environment variables.
  186. Image: image,
  187. Labels: labels,
  188. },
  189. HostConfig: hc,
  190. }
  191. // Apply linux-specific options.
  192. if lc := c.GetLinux(); lc != nil {
  193. // Apply Cgroup options.
  194. // TODO: Check if this works with per-pod cgroups.
  195. hc.CgroupParent = lc.GetCgroupParent()
  196. // Apply namespace options.
  197. hc.NetworkMode, hc.UTSMode, hc.PidMode = "", "", ""
  198. nsOpts := lc.GetNamespaceOptions()
  199. if nsOpts != nil {
  200. if nsOpts.GetHostNetwork() {
  201. hc.NetworkMode = namespaceModeHost
  202. } else {
  203. // Assume kubelet uses either the cni or the kubenet plugin.
  204. // TODO: support docker networking.
  205. hc.NetworkMode = "none"
  206. }
  207. if nsOpts.GetHostIpc() {
  208. hc.IpcMode = namespaceModeHost
  209. }
  210. if nsOpts.GetHostPid() {
  211. hc.PidMode = namespaceModeHost
  212. }
  213. }
  214. }
  215. // Set port mappings.
  216. exposedPorts, portBindings := makePortsAndBindings(c.GetPortMappings())
  217. createConfig.Config.ExposedPorts = exposedPorts
  218. hc.PortBindings = portBindings
  219. // Set DNS options.
  220. if dnsOpts := c.GetDnsOptions(); dnsOpts != nil {
  221. hc.DNS = dnsOpts.GetServers()
  222. hc.DNSSearch = dnsOpts.GetSearches()
  223. }
  224. // Apply resource options.
  225. setSandboxResources(hc)
  226. // Set security options.
  227. hc.SecurityOpt = []string{getSeccompOpts()}
  228. return createConfig
  229. }
  230. func setSandboxResources(hc *dockercontainer.HostConfig) {
  231. hc.Resources = dockercontainer.Resources{
  232. MemorySwap: -1, // Always disable memory swap.
  233. CPUShares: defaultSandboxCPUshares,
  234. // Use docker's default cpu quota/period.
  235. }
  236. hc.OomScoreAdj = defaultSandboxOOMScore
  237. }