convert.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2015 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 dockertools
  14. import (
  15. "fmt"
  16. "strings"
  17. dockertypes "github.com/docker/engine-api/types"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. )
  20. // This file contains helper functions to convert docker API types to runtime
  21. // (kubecontainer) types.
  22. const (
  23. statusRunningPrefix = "Up"
  24. statusExitedPrefix = "Exited"
  25. )
  26. func mapState(state string) kubecontainer.ContainerState {
  27. // Parse the state string in dockertypes.Container. This could break when
  28. // we upgrade docker.
  29. switch {
  30. case strings.HasPrefix(state, statusRunningPrefix):
  31. return kubecontainer.ContainerStateRunning
  32. case strings.HasPrefix(state, statusExitedPrefix):
  33. return kubecontainer.ContainerStateExited
  34. default:
  35. return kubecontainer.ContainerStateUnknown
  36. }
  37. }
  38. // Converts dockertypes.Container to kubecontainer.Container.
  39. func toRuntimeContainer(c *dockertypes.Container) (*kubecontainer.Container, error) {
  40. if c == nil {
  41. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime container")
  42. }
  43. dockerName, hash, err := getDockerContainerNameInfo(c)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &kubecontainer.Container{
  48. ID: kubecontainer.DockerID(c.ID).ContainerID(),
  49. Name: dockerName.ContainerName,
  50. Image: c.Image,
  51. ImageID: c.ImageID,
  52. Hash: hash,
  53. // (random-liu) docker uses status to indicate whether a container is running or exited.
  54. // However, in kubernetes we usually use state to indicate whether a container is running or exited,
  55. // while use status to indicate the comprehensive status of the container. So we have different naming
  56. // norm here.
  57. State: mapState(c.Status),
  58. }, nil
  59. }
  60. // Converts dockertypes.Image to kubecontainer.Image.
  61. func toRuntimeImage(image *dockertypes.Image) (*kubecontainer.Image, error) {
  62. if image == nil {
  63. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime image")
  64. }
  65. return &kubecontainer.Image{
  66. ID: image.ID,
  67. RepoTags: image.RepoTags,
  68. RepoDigests: image.RepoDigests,
  69. Size: image.VirtualSize,
  70. }, nil
  71. }