175 lines
5.1 KiB
Go
175 lines
5.1 KiB
Go
package image
|
||
|
||
import (
|
||
"strings"
|
||
"wdd.io/agent-common/logger"
|
||
)
|
||
|
||
var log = logger.Log
|
||
|
||
var unSupported = "UN-SUPPORT-APP-NAME"
|
||
|
||
const CmiiHarborPrefix = "harbor.cdcyy.com.cn/cmii/"
|
||
|
||
func ImageFullNameToAppName(imageFullName string) (appName string) {
|
||
|
||
// harbor.cdcyy.cn/cmii/cmii-uav-platform:5.4.0 ==> cmii-uav-platform
|
||
|
||
// 10.1.1.1:8033/cmii/ok:1.2 不支持 不允许存在
|
||
// rancher/fleet:v0.3.4
|
||
|
||
// ossr/srs:v5.0.1 ==> docker=cmii=srs=v5.0.1.tar.gz
|
||
// nginx:latest
|
||
// bitnami/minio:2022.5.4
|
||
// simonrupf/chronyd:0.4.3
|
||
|
||
if strings.HasPrefix(imageFullName, CmiiHarborPrefix) {
|
||
return strings.Split(strings.TrimPrefix(imageFullName, CmiiHarborPrefix), ":")[0]
|
||
}
|
||
|
||
return ""
|
||
}
|
||
|
||
func ImageFullNameToImageTag(imageFullName string) (imageTag string) {
|
||
s := strings.Split(imageFullName, ":")[1]
|
||
|
||
if strings.Contains(s, ":") {
|
||
// 10.1.1.1:8033/cmii/ok:1.2
|
||
return strings.Split(imageFullName, ":")[2]
|
||
}
|
||
|
||
return s
|
||
}
|
||
|
||
// ImageFullNameToGzipFileName 必须输出长度为4的内容 =出现得次数为3
|
||
func ImageFullNameToGzipFileName(imageFullName string) (gzipFileName string) {
|
||
|
||
// harbor.cdcyy.cn/cmii/cmii-uav-platform:5.4.0 ==> cmlc=cmii=cmii-uav-platform=5.4.0.tar.gz
|
||
// rancher/fleet:v0.3.4
|
||
|
||
// ossr/srs:v5.0.1 ==> docker=cmii=srs=v5.0.1.tar.gz
|
||
// nginx:latest
|
||
// bitnami/minio:2022.5.4
|
||
// simonrupf/chronyd:0.4.3
|
||
|
||
// 10.1.1.1:8033/cmii/ok:1.2 不支持 不允许存在
|
||
|
||
split := strings.Split(imageFullName, ":")
|
||
//log.DebugF(" %s to %s", imageRepoTag, split)
|
||
if len(split) == 1 {
|
||
// nginx
|
||
return "docker=library=" + imageFullName + "=latest.tar.gz"
|
||
}
|
||
|
||
first := strings.Split(split[0], "/")
|
||
//log.DebugF(" split[0] %s to %s", split[0], first)
|
||
if len(first) == 3 {
|
||
// harbor.cdcyy.cn/cmii/cmii-uav-platform:5.4.0
|
||
// docker.io/ossr/srs:v5.0.1
|
||
if strings.HasPrefix(split[0], CmiiHarborPrefix) {
|
||
gzipFileName += "cmlc=cmii="
|
||
} else {
|
||
gzipFileName += "docker=cmii="
|
||
}
|
||
gzipFileName += first[2]
|
||
gzipFileName += "="
|
||
|
||
} else if len(first) == 4 {
|
||
// harbor.cdcyy.cn/cmii/ossr/srs:v5.0.1
|
||
// harbor.cdcyy.com.cn/cmii/cmlc-ai/cmlc-ai-operator:v5.2.0-t4-no-dino
|
||
if !strings.HasPrefix(split[0], CmiiHarborPrefix) {
|
||
return imageFullName
|
||
}
|
||
gzipFileName += "cmlc=cmii="
|
||
gzipFileName += first[3]
|
||
gzipFileName += "="
|
||
} else if len(first) == 2 {
|
||
// bitnami/redis
|
||
// ossrs/srs
|
||
gzipFileName += "docker="
|
||
gzipFileName += first[0]
|
||
gzipFileName += "="
|
||
gzipFileName += first[1]
|
||
gzipFileName += "="
|
||
} else if len(first) == 1 {
|
||
// nginx:latest
|
||
return "docker=library=" + split[0] + "=" + split[1] + ".tar.gz"
|
||
}
|
||
|
||
gzipFileName += split[1]
|
||
gzipFileName += ".tar.gz"
|
||
|
||
return gzipFileName
|
||
}
|
||
|
||
// ImageNameToTargetImageFullName 将ImageName转换为目标TargetHosts的全名称,ImageName的格式为 短名称或者长名称 均可
|
||
func ImageNameToTargetImageFullName(imageFullName, targetHostFullName string) string {
|
||
|
||
if strings.HasPrefix(imageFullName, CmiiHarborPrefix) {
|
||
imageFullName = strings.TrimPrefix(imageFullName, CmiiHarborPrefix)
|
||
} else if strings.HasPrefix(imageFullName, "docker.io") {
|
||
imageFullName = strings.TrimPrefix(imageFullName, "docker.io")
|
||
}
|
||
// rancher/123:v123
|
||
if strings.HasPrefix(imageFullName, "rancher") {
|
||
return targetHostFullName + "/" + imageFullName
|
||
}
|
||
// ossr/srs:v4.0.5
|
||
if strings.Contains(imageFullName, "/") {
|
||
imageFullName = strings.Split(imageFullName, "/")[1]
|
||
}
|
||
|
||
// srs:v4.0.5
|
||
// cmii-uav-platform:5.4.0
|
||
s := targetHostFullName + "/cmii/" + imageFullName
|
||
log.InfoF("ImageFullName: [%s] to TargetImageFullName: [%s]", imageFullName, s)
|
||
return s
|
||
}
|
||
|
||
func GzipFileNameToImageFullName(gzipFileName string) (imageFullName string) {
|
||
if !strings.HasSuffix(gzipFileName, ".tar.gz") {
|
||
log.ErrorF(" %s is not end with .tar.gz", gzipFileName)
|
||
return ""
|
||
}
|
||
gzipFileName = strings.TrimSuffix(gzipFileName, ".tar.gz")
|
||
|
||
if strings.HasPrefix(gzipFileName, "docker=library") {
|
||
// docker=library=busybox=latest.tar.gz
|
||
return strings.Split(gzipFileName, "=")[2] + ":" + strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
if strings.HasPrefix(gzipFileName, "docker") {
|
||
return strings.Split(gzipFileName, "=")[1] + "/" + strings.Split(gzipFileName, "=")[2] + ":" + strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
if strings.HasPrefix(gzipFileName, "cmlc=cmii=") {
|
||
return strings.Split(gzipFileName, "=")[2] + ":" + strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
return gzipFileName
|
||
}
|
||
|
||
func GzipFileNameToImageNameAndTag(gzipFileName string) (imageName, imageTag string) {
|
||
if !strings.HasSuffix(gzipFileName, ".tar.gz") {
|
||
log.ErrorF(" %s is not end with .tar.gz", gzipFileName)
|
||
return "", ""
|
||
}
|
||
gzipFileName = strings.TrimSuffix(gzipFileName, ".tar.gz")
|
||
|
||
if strings.HasPrefix(gzipFileName, "docker=library") {
|
||
// docker=library=busybox=latest.tar.gz
|
||
return strings.Split(gzipFileName, "=")[2], strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
if strings.HasPrefix(gzipFileName, "docker") {
|
||
// docker=kubernetes=kubernetes-dashboard=v2.4.0.tar.gz
|
||
return strings.Split(gzipFileName, "=")[1] + "/" + strings.Split(gzipFileName, "=")[2], strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
if strings.HasPrefix(gzipFileName, "cmlc=cmii=") {
|
||
return strings.Split(gzipFileName, "=")[2], strings.Split(gzipFileName, "=")[3]
|
||
}
|
||
|
||
return "", ""
|
||
}
|