diff --git a/pkg/karmadactl/cmdinit/cmdinit.go b/pkg/karmadactl/cmdinit/cmdinit.go index 2fc5a29a1eec..2c8474a2c69a 100644 --- a/pkg/karmadactl/cmdinit/cmdinit.go +++ b/pkg/karmadactl/cmdinit/cmdinit.go @@ -45,7 +45,9 @@ func NewCmdInit(cmdOut io.Writer, parentCommand string) *cobra.Command { klog.Infof("No default release version found. build version: %s", version.Get().String()) releaseVer = &version.ReleaseVersion{} // initialize to avoid panic } - + // kube image registry + flags.StringVarP(&opts.KubeImageMirrorCountry, "kube-image-mirror-country", "", "", "Country code of the kube image registry to be used. For Chinese mainland users, set it to cn") + flags.StringVarP(&opts.KubeImageRegistry, "kube-image-registry", "", "", "Kube image registry. For Chinese mainland users, you may use local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers to override default kube image registry") // cert flags.StringVar(&opts.ExternalIP, "cert-external-ip", "", "the external IP of Karmada certificate (e.g 192.168.1.2,172.16.1.2)") flags.StringVar(&opts.ExternalDNS, "cert-external-dns", "", "the external DNS of Karmada certificate (e.g localhost,localhost.com)") @@ -57,7 +59,7 @@ func NewCmdInit(cmdOut io.Writer, parentCommand string) *cobra.Command { // etcd flags.StringVarP(&opts.EtcdStorageMode, "etcd-storage-mode", "", "emptyDir", "etcd data storage mode(emptyDir,hostPath,PVC). value is PVC, specify --storage-classes-name") - flags.StringVarP(&opts.EtcdImage, "etcd-image", "", "k8s.gcr.io/etcd:3.5.1-0", "etcd image") + flags.StringVarP(&opts.EtcdImage, "etcd-image", "", "", "etcd image") flags.StringVarP(&opts.EtcdInitImage, "etcd-init-image", "", "docker.io/alpine:3.15.1", "etcd init container image") flags.Int32VarP(&opts.EtcdReplicas, "etcd-replicas", "", 1, "etcd replica set, cluster 3,5...singular") flags.StringVarP(&opts.EtcdHostDataPath, "etcd-data", "", "/var/lib/karmada-etcd", "etcd data path,valid in hostPath mode.") @@ -68,11 +70,11 @@ func NewCmdInit(cmdOut io.Writer, parentCommand string) *cobra.Command { flags.StringVar(&opts.CRDs, "crds", crdURL, "Karmada crds resource.(local file e.g. --crds /root/crds.tar.gz)") flags.Int32VarP(&opts.KarmadaAPIServerNodePort, "port", "p", 32443, "Karmada apiserver service node port") flags.StringVarP(&opts.KarmadaDataPath, "karmada-data", "d", "/etc/karmada", "karmada data path. kubeconfig cert and crds files") - flags.StringVarP(&opts.KarmadaAPIServerImage, "karmada-apiserver-image", "", "k8s.gcr.io/kube-apiserver:v1.21.7", "Kubernetes apiserver image") + flags.StringVarP(&opts.KarmadaAPIServerImage, "karmada-apiserver-image", "", "", "Kubernetes apiserver image") flags.Int32VarP(&opts.KarmadaAPIServerReplicas, "karmada-apiserver-replicas", "", 1, "karmada apiserver replica set") flags.StringVarP(&opts.KarmadaSchedulerImage, "karmada-scheduler-image", "", fmt.Sprintf("swr.ap-southeast-1.myhuaweicloud.com/karmada/karmada-scheduler:%s", releaseVer.PatchRelease()), "karmada scheduler image") flags.Int32VarP(&opts.KarmadaSchedulerReplicas, "karmada-scheduler-replicas", "", 1, "karmada scheduler replica set") - flags.StringVarP(&opts.KubeControllerManagerImage, "karmada-kube-controller-manager-image", "", "k8s.gcr.io/kube-controller-manager:v1.21.7", "Kubernetes controller manager image") + flags.StringVarP(&opts.KubeControllerManagerImage, "karmada-kube-controller-manager-image", "", "", "Kubernetes controller manager image") flags.Int32VarP(&opts.KubeControllerManagerReplicas, "karmada-kube-controller-manager-replicas", "", 1, "karmada kube controller manager replica set") flags.StringVarP(&opts.KarmadaControllerManagerImage, "karmada-controller-manager-image", "", fmt.Sprintf("swr.ap-southeast-1.myhuaweicloud.com/karmada/karmada-controller-manager:%s", releaseVer.PatchRelease()), "karmada controller manager image") flags.Int32VarP(&opts.KarmadaControllerManagerReplicas, "karmada-controller-manager-replicas", "", 1, "karmada controller manager replica set") @@ -90,6 +92,12 @@ func initExample(parentCommand string) string { # The karmada-apiserver binds the master node's IP by default` + "\n" + fmt.Sprintf("%s init", parentCommand) + ` +# China mainland registry mirror can be specified by using kube-image-mirror-country` + "\n" + + fmt.Sprintf("%s init --kube-image-mirror-country=cn", parentCommand) + ` + +# Kube registry can be specified by using kube-image-registry` + "\n" + + fmt.Sprintf("%s init --kube-image-registry=registry.cn-hangzhou.aliyuncs.com/google_containers", parentCommand) + ` + # Specify the URL to download CRD tarball` + "\n" + fmt.Sprintf("%s init --crds https://github.com/karmada-io/karmada/releases/download/v1.1.0/crds.tar.gz", parentCommand) + ` diff --git a/pkg/karmadactl/cmdinit/kubernetes/deploy.go b/pkg/karmadactl/cmdinit/kubernetes/deploy.go index 0a2b21a79f31..aaa2242d18db 100644 --- a/pkg/karmadactl/cmdinit/kubernetes/deploy.go +++ b/pkg/karmadactl/cmdinit/kubernetes/deploy.go @@ -26,19 +26,32 @@ import ( "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils" ) -var certList = []string{ - options.CaCertAndKeyName, - options.EtcdServerCertAndKeyName, - options.EtcdClientCertAndKeyName, - options.KarmadaCertAndKeyName, - options.FrontProxyCaCertAndKeyName, - options.FrontProxyClientCertAndKeyName, -} +var ( + imageRepositories = map[string]string{ + "global": "k8s.gcr.io", + "cn": "registry.cn-hangzhou.aliyuncs.com/google_containers", + } + + certList = []string{ + options.CaCertAndKeyName, + options.EtcdServerCertAndKeyName, + options.EtcdClientCertAndKeyName, + options.KarmadaCertAndKeyName, + options.FrontProxyCaCertAndKeyName, + options.FrontProxyClientCertAndKeyName, + } + + defaultKubeConfig = filepath.Join(homeDir(), ".kube", "config") -var defaultKubeConfig = filepath.Join(homeDir(), ".kube", "config") + defaultEtcdImage = "etcd:3.5.1-0" + defaultKubeAPIServerImage = "kube-apiserver:v1.21.7" + defaultKubeControllerManagerImage = "kube-controller-manager:v1.21.7" +) // CommandInitOption holds all flags options for init. type CommandInitOption struct { + KubeImageRegistry string + KubeImageMirrorCountry string EtcdImage string EtcdReplicas int32 EtcdInitImage string @@ -448,6 +461,49 @@ func (i *CommandInitOption) RunInit(_ io.Writer, parentCommand string) error { return nil } +// get kube components registry +func (i *CommandInitOption) kubeRegistry() string { + registry := i.KubeImageRegistry + mirrorCountry := strings.ToLower(i.KubeImageMirrorCountry) + + if registry != "" { + return registry + } + + if mirrorCountry != "" { + value, ok := imageRepositories[mirrorCountry] + if ok { + return value + } + } + + return imageRepositories["global"] +} + +// get kube-apiserver image +func (i *CommandInitOption) kubeAPIServerImage() string { + if i.KarmadaAPIServerImage != "" { + return i.KarmadaAPIServerImage + } + return i.kubeRegistry() + "/" + defaultKubeAPIServerImage +} + +// get kube-controller-manager image +func (i *CommandInitOption) kubeControllerManagerImage() string { + if i.KubeControllerManagerImage != "" { + return i.KubeControllerManagerImage + } + return i.kubeRegistry() + "/" + defaultKubeControllerManagerImage +} + +// get etcd image +func (i *CommandInitOption) etcdImage() string { + if i.EtcdImage != "" { + return i.EtcdImage + } + return i.kubeRegistry() + "/" + defaultEtcdImage +} + func homeDir() string { if h := os.Getenv("HOME"); h != "" { return h diff --git a/pkg/karmadactl/cmdinit/kubernetes/deployments.go b/pkg/karmadactl/cmdinit/kubernetes/deployments.go index 65edf4f9edda..ed92d82321ac 100644 --- a/pkg/karmadactl/cmdinit/kubernetes/deployments.go +++ b/pkg/karmadactl/cmdinit/kubernetes/deployments.go @@ -160,7 +160,7 @@ func (i *CommandInitOption) makeKarmadaAPIServerDeployment() *appsv1.Deployment Containers: []corev1.Container{ { Name: karmadaAPIServerDeploymentAndServiceName, - Image: i.KarmadaAPIServerImage, + Image: i.kubeAPIServerImage(), Command: i.karmadaAPIServerContainerCommand(), Ports: []corev1.ContainerPort{ { @@ -255,7 +255,7 @@ func (i *CommandInitOption) makeKarmadaKubeControllerManagerDeployment() *appsv1 Containers: []corev1.Container{ { Name: kubeControllerManagerClusterRoleAndDeploymentAndServiceName, - Image: i.KubeControllerManagerImage, + Image: i.kubeControllerManagerImage(), Command: []string{ "kube-controller-manager", "--allocate-node-cidrs=true", diff --git a/pkg/karmadactl/cmdinit/kubernetes/statefulset.go b/pkg/karmadactl/cmdinit/kubernetes/statefulset.go index ff198176f432..437f4ea62cb0 100644 --- a/pkg/karmadactl/cmdinit/kubernetes/statefulset.go +++ b/pkg/karmadactl/cmdinit/kubernetes/statefulset.go @@ -234,7 +234,7 @@ func (i *CommandInitOption) makeETCDStatefulSet() *appsv1.StatefulSet { Containers: []corev1.Container{ { Name: etcdStatefulSetAndServiceName, - Image: i.EtcdImage, + Image: i.etcdImage(), Command: []string{ "/usr/local/bin/etcd", fmt.Sprintf("--config-file=%s/%s", etcdContainerConfigDataMountPath, etcdConfigName),