docker_image.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  18. )
  19. // This file implements methods in ImageManagerService.
  20. // ListImages lists existing images.
  21. func (ds *dockerService) ListImages(filter *runtimeApi.ImageFilter) ([]*runtimeApi.Image, error) {
  22. opts := dockertypes.ImageListOptions{}
  23. if filter != nil {
  24. if imgSpec := filter.GetImage(); imgSpec != nil {
  25. opts.MatchName = imgSpec.GetImage()
  26. }
  27. }
  28. images, err := ds.client.ListImages(opts)
  29. if err != nil {
  30. return nil, err
  31. }
  32. result := []*runtimeApi.Image{}
  33. for _, i := range images {
  34. apiImage, err := toRuntimeAPIImage(&i)
  35. if err != nil {
  36. // TODO: log an error message?
  37. continue
  38. }
  39. result = append(result, apiImage)
  40. }
  41. return result, nil
  42. }
  43. // ImageStatus returns the status of the image.
  44. func (ds *dockerService) ImageStatus(image *runtimeApi.ImageSpec) (*runtimeApi.Image, error) {
  45. images, err := ds.ListImages(&runtimeApi.ImageFilter{Image: image})
  46. if err != nil {
  47. return nil, err
  48. }
  49. if len(images) != 1 {
  50. return nil, fmt.Errorf("ImageStatus returned more than one image: %+v", images)
  51. }
  52. return images[0], nil
  53. }
  54. // PullImage pulls an image with authentication config.
  55. func (ds *dockerService) PullImage(image *runtimeApi.ImageSpec, auth *runtimeApi.AuthConfig) error {
  56. // TODO: add default tags for images or should this be done by kubelet?
  57. return ds.client.PullImage(image.GetImage(),
  58. dockertypes.AuthConfig{
  59. Username: auth.GetUsername(),
  60. Password: auth.GetPassword(),
  61. ServerAddress: auth.GetServerAddress(),
  62. IdentityToken: auth.GetIdentityToken(),
  63. RegistryToken: auth.GetRegistryToken(),
  64. },
  65. dockertypes.ImagePullOptions{},
  66. )
  67. }
  68. // RemoveImage removes the image.
  69. func (ds *dockerService) RemoveImage(image *runtimeApi.ImageSpec) error {
  70. _, err := ds.client.RemoveImage(image.GetImage(), dockertypes.ImageRemoveOptions{PruneChildren: true})
  71. return err
  72. }