1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package images
18
19import (
20	"fmt"
21
22	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
23	kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
24	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
25	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
26
27	"k8s.io/klog/v2"
28)
29
30// GetGenericImage generates and returns a platform agnostic image (backed by manifest list)
31func GetGenericImage(prefix, image, tag string) string {
32	return fmt.Sprintf("%s/%s:%s", prefix, image, tag)
33}
34
35// GetKubernetesImage generates and returns the image for the components managed in the Kubernetes main repository,
36// including the control-plane components and kube-proxy.
37func GetKubernetesImage(image string, cfg *kubeadmapi.ClusterConfiguration) string {
38	repoPrefix := cfg.GetControlPlaneImageRepository()
39	kubernetesImageTag := kubeadmutil.KubernetesVersionToImageTag(cfg.KubernetesVersion)
40	return GetGenericImage(repoPrefix, image, kubernetesImageTag)
41}
42
43// GetDNSImage generates and returns the image for CoreDNS.
44func GetDNSImage(cfg *kubeadmapi.ClusterConfiguration) string {
45	// DNS uses default image repository by default
46	dnsImageRepository := cfg.ImageRepository
47	// unless an override is specified
48	if cfg.DNS.ImageRepository != "" {
49		dnsImageRepository = cfg.DNS.ImageRepository
50	}
51	// Handle the renaming of the official image from "k8s.gcr.io/coredns" to "k8s.gcr.io/coredns/coredns
52	if dnsImageRepository == kubeadmapiv1beta2.DefaultImageRepository {
53		dnsImageRepository = fmt.Sprintf("%s/coredns", dnsImageRepository)
54	}
55	// DNS uses an imageTag that corresponds to the DNS version matching the Kubernetes version
56	dnsImageTag := constants.CoreDNSVersion
57
58	// unless an override is specified
59	if cfg.DNS.ImageTag != "" {
60		dnsImageTag = cfg.DNS.ImageTag
61	}
62	return GetGenericImage(dnsImageRepository, constants.CoreDNSImageName, dnsImageTag)
63}
64
65// GetEtcdImage generates and returns the image for etcd
66func GetEtcdImage(cfg *kubeadmapi.ClusterConfiguration) string {
67	// Etcd uses default image repository by default
68	etcdImageRepository := cfg.ImageRepository
69	// unless an override is specified
70	if cfg.Etcd.Local != nil && cfg.Etcd.Local.ImageRepository != "" {
71		etcdImageRepository = cfg.Etcd.Local.ImageRepository
72	}
73	// Etcd uses an imageTag that corresponds to the etcd version matching the Kubernetes version
74	etcdImageTag := constants.DefaultEtcdVersion
75	etcdVersion, warning, err := constants.EtcdSupportedVersion(constants.SupportedEtcdVersion, cfg.KubernetesVersion)
76	if err == nil {
77		etcdImageTag = etcdVersion.String()
78	}
79	if warning != nil {
80		klog.Warningln(warning)
81	}
82	// unless an override is specified
83	if cfg.Etcd.Local != nil && cfg.Etcd.Local.ImageTag != "" {
84		etcdImageTag = cfg.Etcd.Local.ImageTag
85	}
86	return GetGenericImage(etcdImageRepository, constants.Etcd, etcdImageTag)
87}
88
89// GetControlPlaneImages returns a list of container images kubeadm expects to use on a control plane node
90func GetControlPlaneImages(cfg *kubeadmapi.ClusterConfiguration) []string {
91	imgs := []string{}
92
93	// start with core kubernetes images
94	imgs = append(imgs, GetKubernetesImage(constants.KubeAPIServer, cfg))
95	imgs = append(imgs, GetKubernetesImage(constants.KubeControllerManager, cfg))
96	imgs = append(imgs, GetKubernetesImage(constants.KubeScheduler, cfg))
97	imgs = append(imgs, GetKubernetesImage(constants.KubeProxy, cfg))
98
99	// pause is not available on the ci image repository so use the default image repository.
100	imgs = append(imgs, GetPauseImage(cfg))
101
102	// if etcd is not external then add the image as it will be required
103	if cfg.Etcd.Local != nil {
104		imgs = append(imgs, GetEtcdImage(cfg))
105	}
106
107	// Append the appropriate DNS images
108	imgs = append(imgs, GetDNSImage(cfg))
109
110	return imgs
111}
112
113// GetPauseImage returns the image for the "pause" container
114func GetPauseImage(cfg *kubeadmapi.ClusterConfiguration) string {
115	return GetGenericImage(cfg.ImageRepository, "pause", constants.PauseVersion)
116}
117