diff --git a/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go b/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go index aca782e7e..a000c977d 100644 --- a/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go +++ b/cmd/kk/apis/kubekey/v1alpha2/cluster_types.go @@ -108,6 +108,7 @@ type System struct { // RegistryConfig defines the configuration information of the image's repository. type RegistryConfig struct { Type string `yaml:"type" json:"type,omitempty"` + Version string `yaml:"version" json:"version,omitempty"` RegistryMirrors []string `yaml:"registryMirrors" json:"registryMirrors,omitempty"` InsecureRegistries []string `yaml:"insecureRegistries" json:"insecureRegistries,omitempty"` PrivateRegistry string `yaml:"privateRegistry" json:"privateRegistry,omitempty"` diff --git a/cmd/kk/pkg/binaries/registry.go b/cmd/kk/pkg/binaries/registry.go index ec31028a2..00df25155 100644 --- a/cmd/kk/pkg/binaries/registry.go +++ b/cmd/kk/pkg/binaries/registry.go @@ -36,8 +36,22 @@ func RegistryPackageDownloadHTTP(kubeConf *common.KubeConf, path, arch string, p switch kubeConf.Cluster.Registry.Type { case common.Harbor: + //当配置了harbor版本时,使用配置的版本 + harborVersion := kubekeyapiv1alpha2.DefaultHarborVersion + if len(kubeConf.Cluster.Registry.Version) > 0 { + //验证配置的版本是否在列表中 + if _, ok := files.FileSha256["harbor"][arch][kubeConf.Cluster.Registry.Version]; !ok { + supportVersion := "" + for key := range files.FileSha256["harbor"][arch] { + supportVersion += fmt.Sprintf("%s ", key) + } + logger.Log.Warningf("Harbor Version only supports %s, the KubeKey artifact will not contain the Harbor Version:%s,use default version:%s", supportVersion, kubeConf.Cluster.Registry.Version, kubekeyapiv1alpha2.DefaultHarborVersion) + } else { + harborVersion = kubeConf.Cluster.Registry.Version + } + } // TODO: Harbor only supports amd64, so there is no need to consider other architectures at present. - harbor := files.NewKubeBinary("harbor", arch, kubekeyapiv1alpha2.DefaultHarborVersion, path, kubeConf.Arg.DownloadCommand) + harbor := files.NewKubeBinary("harbor", arch, harborVersion, path, kubeConf.Arg.DownloadCommand) compose := files.NewKubeBinary("compose", arch, kubekeyapiv1alpha2.DefaultDockerComposeVersion, path, kubeConf.Arg.DownloadCommand) docker := files.NewKubeBinary("docker", arch, kubekeyapiv1alpha2.DefaultDockerVersion, path, kubeConf.Arg.DownloadCommand) binaries = []*files.KubeBinary{harbor, docker, compose} @@ -85,11 +99,26 @@ func RegistryBinariesDownload(manifest *common.ArtifactManifest, path, arch stri if m.Components.Harbor.Version != "" { harbor := files.NewKubeBinary("harbor", arch, kubekeyapiv1alpha2.DefaultHarborVersion, path, manifest.Arg.DownloadCommand) + + //允许下载version/components.json中规定的版本 + if _, ok := files.FileSha256["harbor"][arch][m.Components.Harbor.Version]; !ok { + supportVersion := "" + for key := range files.FileSha256["harbor"][arch] { + supportVersion += fmt.Sprintf("%s ", key) + } + logger.Log.Warningf("Harbor Version only supports %s, the KubeKey artifact will not contain the Harbor Version:%s,use default version:%s", supportVersion, m.Components.Harbor.Version, kubekeyapiv1alpha2.DefaultHarborVersion) + + } else { + //harbor := files.NewKubeBinary("harbor", arch, kubekeyapiv1alpha2.DefaultHarborVersion, path, manifest.Arg.DownloadCommand) + harbor = files.NewKubeBinary("harbor", arch, m.Components.Harbor.Version, path, manifest.Arg.DownloadCommand) + + } if arch == "amd64" { binaries = append(binaries, harbor) } else { logger.Log.Warningf("Harbor only supports amd64, the KubeKey artifact will not contain the Harbor %s", arch) } + } if m.Components.DockerCompose.Version != "" { diff --git a/cmd/kk/pkg/bootstrap/registry/tasks.go b/cmd/kk/pkg/bootstrap/registry/tasks.go index 469007878..e6ab03159 100644 --- a/cmd/kk/pkg/bootstrap/registry/tasks.go +++ b/cmd/kk/pkg/bootstrap/registry/tasks.go @@ -263,6 +263,10 @@ type StartHarbor struct { func (g *StartHarbor) Execute(runtime connector.Runtime) error { startCmd := "cd /opt/harbor && chmod +x install.sh && export PATH=$PATH:/usr/local/bin; ./install.sh --with-trivy && systemctl daemon-reload && systemctl enable harbor && systemctl restart harbor" + //harbor 2.8.0开始移除--with-chartmuseum + if VersionCompare(g.KubeConf.Cluster.Registry.Version, "v2.8.0") { + startCmd = "cd /opt/harbor && chmod +x install.sh && export PATH=$PATH:/usr/local/bin; ./install.sh --with-notary --with-trivy --with-chartmuseum && systemctl daemon-reload && systemctl enable harbor && systemctl restart harbor" + } if _, err := runtime.GetRunner().SudoCmd(startCmd, false); err != nil { return errors.Wrap(errors.WithStack(err), "start harbor failed") } @@ -273,3 +277,66 @@ func (g *StartHarbor) Execute(runtime connector.Runtime) error { return nil } + +// VersionCompare +// @description: 版本比较,v2版本是否大于v1版本 +// @param: v1 string 当前版本 +// @param: v2 string 最新版本 +// @author: GJing +// @email: gjing1st@gmail.com +// @date: 2024/8/29 14:10 +// @success: v2>v1返回true +func VersionCompare(v1, v2 string) (res bool) { + if len(v1) == 0 { + return true + } + if len(v2) == 0 { + return false + } + if v1[0] == 'v' || v1[0] == 'V' { + v1 = v1[1:] + } + if v2[0] == 'v' || v2[0] == 'V' { + v2 = v2[1:] + } + v1Arr := strings.Split(v1, ".") + v2Arr := strings.Split(v2, ".") + if len(v1Arr) == len(v2Arr) { + //版本位数相同 + for i, v := range v2Arr { + if v > v1Arr[i] { + res = true + return + } else if v < v1Arr[i] { + return + } + } + } else if len(v1Arr) < len(v2Arr) { + //新版本位数更多 + for i, v := range v1Arr { + if v2Arr[i] > v { + res = true + return + } else if v2Arr[i] < v { + return + } + if i == len(v1Arr)-1 { + //v1的版本位数跟v2的一样,v2的位数更多,返回true + res = true + return + } + } + + } else if len(v1Arr) > len(v2Arr) { + //旧版本位数多,新版本位数少 + for i, v := range v2Arr { + if v > v1Arr[i] { + res = true + return + } else if v < v1Arr[i] { + return + } + } + } + return +} diff --git a/version/components.json b/version/components.json index 8e6ad9783..32eb53e45 100644 --- a/version/components.json +++ b/version/components.json @@ -1162,6 +1162,7 @@ "harbor": { "amd64": { "v2.5.3": "c536eaf5dcb35a1f2a5b1c4278380bde254a288700aa2ba59c1fd464bf2fcbf1", + "v2.7.4": "4e192a076a15fb7137c6d4626b2c69175a59d6f1a378366a7c080151e0aea4da", "v2.10.1": "aed3fe341e563e11286fb243b2bfe4162d0c5816d548df049df07fabceea038f" } },