From 7253b4ad2a684c2d6a7f7188380f0d46948184d8 Mon Sep 17 00:00:00 2001 From: Xiao Liu <46879761+liangzai006@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:42:09 +0800 Subject: [PATCH] support multi-level image warehouse path push --- cmd/kk/apis/kubekey/v1alpha2/cluster_types.go | 9 ++++++++- cmd/kk/pkg/bootstrap/os/templates/init_script.go | 4 ++-- cmd/kk/pkg/bootstrap/registry/certs.go | 6 +++--- cmd/kk/pkg/bootstrap/registry/module.go | 12 ++++++------ cmd/kk/pkg/bootstrap/registry/tasks.go | 14 +++++++------- cmd/kk/pkg/images/tasks.go | 2 +- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go b/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go index 2a3cfeddb..b5b3bc2af 100644 --- a/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go +++ b/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go @@ -146,7 +146,7 @@ func (cfg *ClusterSpec) GenerateCertSANs() []string { if InternalIPv4Address != host.Address && InternalIPv4Address != cfg.ControlPlaneEndpoint.Address { extraCertSANs = append(extraCertSANs, InternalIPv4Address) } - if len(nodeAddresses)==2 { + if len(nodeAddresses) == 2 { InternalIPv6Address := nodeAddresses[1] extraCertSANs = append(extraCertSANs, InternalIPv6Address) } @@ -310,3 +310,10 @@ func (c *ControlPlaneEndpoint) EnableExternalDNS() bool { } return *c.ExternalDNS } + +func (r *RegistryConfig) GetHost() string { + if r.PrivateRegistry == "" { + return "" + } + return strings.Split(r.PrivateRegistry, "/")[0] +} diff --git a/cmd/kk/pkg/bootstrap/os/templates/init_script.go b/cmd/kk/pkg/bootstrap/os/templates/init_script.go index 09e14c878..ca671010c 100644 --- a/cmd/kk/pkg/bootstrap/os/templates/init_script.go +++ b/cmd/kk/pkg/bootstrap/os/templates/init_script.go @@ -249,9 +249,9 @@ func GenerateHosts(runtime connector.ModuleRuntime, kubeConf *common.KubeConf) [ if len(runtime.GetHostsByRole(common.Registry)) > 0 { if kubeConf.Cluster.Registry.PrivateRegistry != "" { - hostsList = append(hostsList, fmt.Sprintf("%s %s", runtime.GetHostsByRole(common.Registry)[0].GetInternalIPv4Address(), kubeConf.Cluster.Registry.PrivateRegistry)) + hostsList = append(hostsList, fmt.Sprintf("%s %s", runtime.GetHostsByRole(common.Registry)[0].GetInternalIPv4Address(), kubeConf.Cluster.Registry.GetHost())) if runtime.GetHostsByRole(common.Registry)[0].GetInternalIPv6Address() != "" { - hostsList = append(hostsList, fmt.Sprintf("%s %s", runtime.GetHostsByRole(common.Registry)[0].GetInternalIPv6Address(), kubeConf.Cluster.Registry.PrivateRegistry)) + hostsList = append(hostsList, fmt.Sprintf("%s %s", runtime.GetHostsByRole(common.Registry)[0].GetInternalIPv6Address(), kubeConf.Cluster.Registry.GetHost())) } } else { diff --git a/cmd/kk/pkg/bootstrap/registry/certs.go b/cmd/kk/pkg/bootstrap/registry/certs.go index 690cb4bfa..883a05a81 100644 --- a/cmd/kk/pkg/bootstrap/registry/certs.go +++ b/cmd/kk/pkg/bootstrap/registry/certs.go @@ -105,7 +105,7 @@ func (g *GenerateCerts) Execute(runtime connector.Runtime) error { var altName cert.AltNames - dnsList := []string{"localhost", g.KubeConf.Cluster.Registry.PrivateRegistry} + dnsList := []string{"localhost", g.KubeConf.Cluster.Registry.GetHost()} ipList := []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback} for _, h := range runtime.GetHostsByRole(common.Registry) { @@ -115,13 +115,13 @@ func (g *GenerateCerts) Execute(runtime connector.Runtime) error { altName.DNSNames = dnsList altName.IPs = ipList - files := []string{"ca.pem", "ca-key.pem", fmt.Sprintf("%s.pem", g.KubeConf.Cluster.Registry.PrivateRegistry), fmt.Sprintf("%s-key.pem", g.KubeConf.Cluster.Registry.PrivateRegistry)} + files := []string{"ca.pem", "ca-key.pem", fmt.Sprintf("%s.pem", g.KubeConf.Cluster.Registry.GetHost()), fmt.Sprintf("%s-key.pem", g.KubeConf.Cluster.Registry.GetHost())} // CA certsList := []*certs.KubekeyCert{KubekeyCertRegistryCA()} // Certs - certsList = append(certsList, KubekeyCertRegistryServer(g.KubeConf.Cluster.Registry.PrivateRegistry, &altName)) + certsList = append(certsList, KubekeyCertRegistryServer(g.KubeConf.Cluster.Registry.GetHost(), &altName)) var lastCACert *certs.KubekeyCert for _, c := range certsList { diff --git a/cmd/kk/pkg/bootstrap/registry/module.go b/cmd/kk/pkg/bootstrap/registry/module.go index 284b88bac..2afb4d429 100644 --- a/cmd/kk/pkg/bootstrap/registry/module.go +++ b/cmd/kk/pkg/bootstrap/registry/module.go @@ -131,8 +131,8 @@ func InstallRegistry(i *InstallRegistryModule) []task.Interface { Template: templates.RegistryConfigTempl, Dst: "/etc/kubekey/registry/config.yaml", Data: util.Data{ - "Certificate": fmt.Sprintf("%s.pem", i.KubeConf.Cluster.Registry.PrivateRegistry), - "Key": fmt.Sprintf("%s-key.pem", i.KubeConf.Cluster.Registry.PrivateRegistry), + "Certificate": fmt.Sprintf("%s.pem", i.KubeConf.Cluster.Registry.GetHost()), + "Key": fmt.Sprintf("%s-key.pem", i.KubeConf.Cluster.Registry.GetHost()), }, }, Parallel: true, @@ -250,10 +250,10 @@ func InstallHarbor(i *InstallRegistryModule) []task.Interface { } generateHarborConfig := &task.RemoteTask{ - Name: "GenerateHarborConfig", - Desc: "Generate harbor config", - Hosts: i.Runtime.GetHostsByRole(common.Registry), - Action: new(GenerateHarborConfig), + Name: "GenerateHarborConfig", + Desc: "Generate harbor config", + Hosts: i.Runtime.GetHostsByRole(common.Registry), + Action: new(GenerateHarborConfig), Parallel: true, Retry: 1, } diff --git a/cmd/kk/pkg/bootstrap/registry/tasks.go b/cmd/kk/pkg/bootstrap/registry/tasks.go index 1185cb11e..c216c5e71 100644 --- a/cmd/kk/pkg/bootstrap/registry/tasks.go +++ b/cmd/kk/pkg/bootstrap/registry/tasks.go @@ -89,7 +89,7 @@ func (s *SyncCertsToAllNodes) Execute(runtime connector.Runtime) error { } } - if err := runtime.GetRunner().SudoScp(filepath.Join(dir, fileName), filepath.Join(filepath.Join("/etc/docker/certs.d", s.KubeConf.Cluster.Registry.PrivateRegistry), dstFileName)); err != nil { + if err := runtime.GetRunner().SudoScp(filepath.Join(dir, fileName), filepath.Join(filepath.Join("/etc/docker/certs.d", s.KubeConf.Cluster.Registry.GetHost()), dstFileName)); err != nil { return errors.Wrap(errors.WithStack(err), "scp registry certs file to /etc/docker/certs.d/ failed") } @@ -144,7 +144,7 @@ func (g *StartRegistryService) Execute(runtime connector.Runtime) error { } fmt.Println() - fmt.Println(fmt.Sprintf("Local image registry created successfully. Address: %s", g.KubeConf.Cluster.Registry.PrivateRegistry)) + fmt.Println(fmt.Sprintf("Local image registry created successfully. Address: %s", g.KubeConf.Cluster.Registry.GetHost())) fmt.Println() return nil @@ -221,7 +221,7 @@ type GenerateHarborConfig struct { } func (g *GenerateHarborConfig) Execute(runtime connector.Runtime) error { - registryDomain := g.KubeConf.Cluster.Registry.PrivateRegistry + registryDomain := g.KubeConf.Cluster.Registry.GetHost() if g.KubeConf.Cluster.Registry.Type == "harbor-ha" { host := runtime.RemoteHost() @@ -233,9 +233,9 @@ func (g *GenerateHarborConfig) Execute(runtime connector.Runtime) error { Dst: "/opt/harbor/harbor.yml", Data: util.Data{ "Domain": registryDomain, - "Certificate": fmt.Sprintf("%s.pem", g.KubeConf.Cluster.Registry.PrivateRegistry), - "Key": fmt.Sprintf("%s-key.pem", g.KubeConf.Cluster.Registry.PrivateRegistry), - "Password": templates.Password(g.KubeConf, g.KubeConf.Cluster.Registry.PrivateRegistry), + "Certificate": fmt.Sprintf("%s.pem", g.KubeConf.Cluster.Registry.GetHost()), + "Key": fmt.Sprintf("%s-key.pem", g.KubeConf.Cluster.Registry.GetHost()), + "Password": templates.Password(g.KubeConf, g.KubeConf.Cluster.Registry.GetHost()), }, } templateAction.Init(nil, nil) @@ -256,7 +256,7 @@ func (g *StartHarbor) Execute(runtime connector.Runtime) error { } fmt.Println() - fmt.Println(fmt.Sprintf("Local image registry created successfully. Address: %s", g.KubeConf.Cluster.Registry.PrivateRegistry)) + fmt.Println(fmt.Sprintf("Local image registry created successfully. Address: %s", g.KubeConf.Cluster.Registry.GetHost())) fmt.Println() return nil diff --git a/cmd/kk/pkg/images/tasks.go b/cmd/kk/pkg/images/tasks.go index fee9a2862..33b938cd4 100644 --- a/cmd/kk/pkg/images/tasks.go +++ b/cmd/kk/pkg/images/tasks.go @@ -289,7 +289,7 @@ func (c *CopyImagesToRegistry) Execute(runtime connector.Runtime) error { } auth := new(registry.DockerRegistryEntry) - if config, ok := auths[c.KubeConf.Cluster.Registry.PrivateRegistry]; ok { + if config, ok := auths[c.KubeConf.Cluster.Registry.GetHost()]; ok { auth = config }