Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kk增加自定义harbor版本 #2380

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kk/apis/kubekey/v1alpha2/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
31 changes: 30 additions & 1 deletion cmd/kk/pkg/binaries/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 != "" {
Expand Down
67 changes: 67 additions & 0 deletions cmd/kk/pkg/bootstrap/registry/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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: [email protected]
// @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
}
1 change: 1 addition & 0 deletions version/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@
"harbor": {
"amd64": {
"v2.5.3": "c536eaf5dcb35a1f2a5b1c4278380bde254a288700aa2ba59c1fd464bf2fcbf1",
"v2.7.4": "4e192a076a15fb7137c6d4626b2c69175a59d6f1a378366a7c080151e0aea4da",
"v2.10.1": "aed3fe341e563e11286fb243b2bfe4162d0c5816d548df049df07fabceea038f"
}
},
Expand Down