From af756c8ad20f1a560c4533d988346728b57f7a71 Mon Sep 17 00:00:00 2001 From: gj Date: Thu, 29 Aug 2024 15:47:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9Eharbor=202.7.4=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=94=AF=E6=8C=81=E3=80=82=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=88=B6=E5=93=81=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89harbor=E7=89=88=E6=9C=AC=EF=BC=8C=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=9B=E5=BB=BA=E4=BB=93=E5=BA=93=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89harbor=E7=89=88=E6=9C=AC=E3=80=82=20?= =?UTF-8?q?=E4=B8=8D=E9=85=8D=E7=BD=AE=E6=97=B6=E8=B5=B0=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/kk/apis/kubekey/v1alpha2/cluster_types.go | 1 + cmd/kk/pkg/binaries/registry.go | 35 ++++++++-- cmd/kk/pkg/bootstrap/registry/tasks.go | 67 +++++++++++++++++++ version/components.json | 1 + 4 files changed, 99 insertions(+), 5 deletions(-) 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..e03d38d26 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} @@ -84,12 +98,23 @@ func RegistryBinariesDownload(manifest *common.ArtifactManifest, path, arch stri } if m.Components.Harbor.Version != "" { - harbor := files.NewKubeBinary("harbor", arch, kubekeyapiv1alpha2.DefaultHarborVersion, path, manifest.Arg.DownloadCommand) - if arch == "amd64" { - binaries = append(binaries, harbor) + //允许下载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", supportVersion, m.Components.Harbor.Version) } else { - logger.Log.Warningf("Harbor only supports amd64, the KubeKey artifact will not contain the Harbor %s", arch) + //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" } }, From 84cc36c7b2517b83b5501d26886a0b631a451632 Mon Sep 17 00:00:00 2001 From: gj Date: Fri, 30 Aug 2024 09:32:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=88=B6=E5=93=81?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=A6=82=E6=9E=9Charbor=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E5=88=97=E8=A1=A8=E4=B8=AD=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=BB=98=E8=AE=A4=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/kk/pkg/binaries/registry.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/kk/pkg/binaries/registry.go b/cmd/kk/pkg/binaries/registry.go index e03d38d26..00df25155 100644 --- a/cmd/kk/pkg/binaries/registry.go +++ b/cmd/kk/pkg/binaries/registry.go @@ -98,21 +98,25 @@ 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", supportVersion, m.Components.Harbor.Version) + 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) - } + 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) } }