images.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 images
  14. import (
  15. "fmt"
  16. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  17. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  18. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  19. )
  20. // GetGenericImage generates and returns a platform agnostic image (backed by manifest list)
  21. func GetGenericImage(prefix, image, tag string) string {
  22. return fmt.Sprintf("%s/%s:%s", prefix, image, tag)
  23. }
  24. // GetKubernetesImage generates and returns the image for the components managed in the Kubernetes main repository,
  25. // including the control-plane components ad kube-proxy. If specified, the HyperKube image will be used.
  26. func GetKubernetesImage(image string, cfg *kubeadmapi.ClusterConfiguration) string {
  27. if cfg.UseHyperKubeImage {
  28. image = constants.HyperKube
  29. }
  30. repoPrefix := cfg.GetControlPlaneImageRepository()
  31. kubernetesImageTag := kubeadmutil.KubernetesVersionToImageTag(cfg.KubernetesVersion)
  32. return GetGenericImage(repoPrefix, image, kubernetesImageTag)
  33. }
  34. // GetDNSImage generates and returns the image for the DNS, that can be CoreDNS or kube-dns.
  35. // Given that kube-dns uses 3 containers, an additional imageName parameter was added
  36. func GetDNSImage(cfg *kubeadmapi.ClusterConfiguration, imageName string) string {
  37. // DNS uses default image repository by default
  38. dnsImageRepository := cfg.ImageRepository
  39. // unless an override is specified
  40. if cfg.DNS.ImageRepository != "" {
  41. dnsImageRepository = cfg.DNS.ImageRepository
  42. }
  43. // DNS uses an imageTag that corresponds to the DNS version matching the Kubernetes version
  44. dnsImageTag := constants.GetDNSVersion(cfg.DNS.Type)
  45. // unless an override is specified
  46. if cfg.DNS.ImageTag != "" {
  47. dnsImageTag = cfg.DNS.ImageTag
  48. }
  49. return GetGenericImage(dnsImageRepository, imageName, dnsImageTag)
  50. }
  51. // GetEtcdImage generates and returns the image for etcd
  52. func GetEtcdImage(cfg *kubeadmapi.ClusterConfiguration) string {
  53. // Etcd uses default image repository by default
  54. etcdImageRepository := cfg.ImageRepository
  55. // unless an override is specified
  56. if cfg.Etcd.Local != nil && cfg.Etcd.Local.ImageRepository != "" {
  57. etcdImageRepository = cfg.Etcd.Local.ImageRepository
  58. }
  59. // Etcd uses an imageTag that corresponds to the etcd version matching the Kubernetes version
  60. etcdImageTag := constants.DefaultEtcdVersion
  61. etcdVersion, err := constants.EtcdSupportedVersion(cfg.KubernetesVersion)
  62. if err == nil {
  63. etcdImageTag = etcdVersion.String()
  64. }
  65. // unless an override is specified
  66. if cfg.Etcd.Local != nil && cfg.Etcd.Local.ImageTag != "" {
  67. etcdImageTag = cfg.Etcd.Local.ImageTag
  68. }
  69. return GetGenericImage(etcdImageRepository, constants.Etcd, etcdImageTag)
  70. }
  71. // GetPauseImage returns the image for the "pause" container
  72. func GetPauseImage(cfg *kubeadmapi.ClusterConfiguration) string {
  73. return GetGenericImage(cfg.ImageRepository, "pause", constants.PauseVersion)
  74. }
  75. // GetControlPlaneImages returns a list of container images kubeadm expects to use on a control plane node
  76. func GetControlPlaneImages(cfg *kubeadmapi.ClusterConfiguration) []string {
  77. imgs := []string{}
  78. // start with core kubernetes images
  79. if cfg.UseHyperKubeImage {
  80. imgs = append(imgs, GetKubernetesImage(constants.HyperKube, cfg))
  81. } else {
  82. imgs = append(imgs, GetKubernetesImage(constants.KubeAPIServer, cfg))
  83. imgs = append(imgs, GetKubernetesImage(constants.KubeControllerManager, cfg))
  84. imgs = append(imgs, GetKubernetesImage(constants.KubeScheduler, cfg))
  85. imgs = append(imgs, GetKubernetesImage(constants.KubeProxy, cfg))
  86. }
  87. // pause is not available on the ci image repository so use the default image repository.
  88. imgs = append(imgs, GetPauseImage(cfg))
  89. // if etcd is not external then add the image as it will be required
  90. if cfg.Etcd.Local != nil {
  91. imgs = append(imgs, GetEtcdImage(cfg))
  92. }
  93. // Append the appropriate DNS images
  94. if cfg.DNS.Type == kubeadmapi.CoreDNS {
  95. imgs = append(imgs, GetDNSImage(cfg, constants.CoreDNSImageName))
  96. } else {
  97. imgs = append(imgs, GetDNSImage(cfg, constants.KubeDNSKubeDNSImageName))
  98. imgs = append(imgs, GetDNSImage(cfg, constants.KubeDNSSidecarImageName))
  99. imgs = append(imgs, GetDNSImage(cfg, constants.KubeDNSDnsMasqNannyImageName))
  100. }
  101. return imgs
  102. }