[ Cmii ] [ Operator ] - agent operator for DCU part

This commit is contained in:
zeaslity
2024-04-10 17:20:23 +08:00
parent a9a9ab859e
commit b840f0051e
6 changed files with 285 additions and 131 deletions

View File

@@ -69,6 +69,8 @@ func BasicDownloadFileHTTP(downloadUrl string, proxyUrl string, proxyUser string
return true, resultLog
}
// quick sort function
func main() {
//socksProxyUrl := "socks5://192.168.35.71:22888"

View File

@@ -15,6 +15,7 @@ type CmiiMinioOperator struct {
LocalClient *minio.Client
PublicClient *minio.Client
DemoClient *minio.Client
OctopusBucketName string
}
@@ -22,6 +23,7 @@ type CmiiMinioOperator struct {
const (
DefaultLocalEndpoint = "http://10.250.0.100:9000"
DefaultPublicEndpoint = "http://42.192.52.227:9000"
DemoEndpoint = "https://oss.demo.uavcmlc.com:18000"
DefaultAccessKeyID = "cmii"
DefaultSecretAccessKey = "B#923fC7mk"
DefaultOctopusBucketName = "octopus"
@@ -46,6 +48,14 @@ func (op CmiiMinioOperator) buildCmiiMinioOperator() {
}
}
if op.DemoClient == nil {
// 初始化Minio客户端对象。
op.PublicClient, err = minio.New(DemoEndpoint, DefaultAccessKeyID, DefaultSecretAccessKey, false)
if err != nil {
log.ErrorF("[buildCmiiMinioOperator] - build for DemoClient error ! => %s", err.Error())
}
}
log.DebugF("[buildCmiiMinioOperator] - build client success !")
}
@@ -60,8 +70,19 @@ func (op CmiiMinioOperator) UploadToPublicOctopus(filePath, fileName string) boo
return op.uploadToOctopus(op.PublicClient, filePath, fileName)
}
func (op CmiiMinioOperator) UploadToDemo(bucketName, filePath, fileName string) bool {
op.buildCmiiMinioOperator()
return op.uploadToOss(op.PublicClient, bucketName, filePath, fileName)
}
func (op CmiiMinioOperator) uploadToOctopus(client *minio.Client, filePath, fileName string) bool {
return op.uploadToOss(client, DefaultOctopusBucketName, filePath, fileName)
}
func (op CmiiMinioOperator) uploadToOss(client *minio.Client, bucketName, filePath, fileName string) bool {
if !strings.HasSuffix(filePath, "/") {
filePath += "/"
}
@@ -69,17 +90,17 @@ func (op CmiiMinioOperator) uploadToOctopus(client *minio.Client, filePath, file
// 打开要上传的文件
file, err := os.Open(filePath + fileName)
if err != nil {
log.ErrorF("[uploadToOctopus] - file %s open error! %s", filePath+fileName, err.Error())
log.ErrorF("[uploadToOss] - file %s open error! %s", filePath+fileName, err.Error())
}
defer file.Close()
// 使用PutObject上传文件
n, err := client.PutObject(DefaultOctopusBucketName, fileName, file, -1, minio.PutObjectOptions{})
n, err := client.PutObject(bucketName, fileName, file, -1, minio.PutObjectOptions{})
if err != nil {
log.ErrorF("[uploadToOctopus] - upload %s error %s", filePath+fileName, err.Error())
log.ErrorF("[uploadToOss] - upload %s error %s", filePath+fileName, err.Error())
return false
}
log.InfoF("[uploadToOctopus] - uploaded %s of size %d", filePath+fileName, n)
log.InfoF("[uploadToOss] - uploaded %s of size %d", filePath+fileName, n)
return true
}

View File

@@ -18,11 +18,15 @@ const PublicDeployHarborHost = "42.192.52.227"
const DirectPushDeployHarborHost = "36.134.71.138"
type ImageSyncEntity struct {
ProjectName string // 3
ProjectVersion string // 2
CmiiImageList map[string]string // 1
DirectHarborHost string //此参数决定是否能够直连目标主机,如果有则代表直连,可以直接推送景象
PushToDemoMinio bool
ProjectName string // 优先级3
ProjectVersion string // 优先级2
CmiiNameTagList []string // 优先级1 appName:tag的形式
FullNameImageList []string // 优先级1
DownloadImage bool // 下载镜像
CompressImageToGzip bool // 压缩镜像
UploadToDemoMinio bool // 上传镜像
ShouldDirectPushToHarbor bool // 直接推送到对方的主机 || 离线部署机 使用此部门
DirectHarborHost string // 目标Harbor仓库的Host全名称带端口
}
type ImageSyncResult struct {
@@ -38,100 +42,170 @@ type ImageSyncResult struct {
func (sync ImageSyncEntity) PullFromEntityAndSyncConditionally() (imageSyncResult ImageSyncResult) {
var realCmiiImageList []string
var errorPullImageList []string
var errorGzipImageList []string
var allCmiiImageNameList []string
var errorPullImageList []string
var allGzipFileNameList []string
var errorGzipImageList []string
var errorPushImageNameList []string
var gzipFolderFullPath string
// get all image name by Name or Version
// pull images
// compress
if sync.ProjectVersion != "" {
// get version
if sync.DirectHarborHost == "" {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = FetchVersionImages(sync.ProjectVersion, true)
gzipFolderFullPath = OfflineImageGzipFolderPrefix + sync.ProjectVersion
if (sync.CmiiNameTagList == nil && sync.FullNameImageList == nil) || (len(sync.CmiiNameTagList) == 0 && len(sync.FullNameImageList) == 0) {
// 没有指定特定的镜像,那么根据 ProjectVersion 或者从DEMO拉取镜像
// pull images
// compress
if sync.ProjectVersion != "" {
// get version
if sync.DirectHarborHost == "" {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = DownloadCompressUploadFromVersion(sync.ProjectVersion, true, sync.UploadToDemoMinio)
gzipFolderFullPath = OfflineImageGzipFolderPrefix + sync.ProjectVersion
} else {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = DownloadCompressUploadFromVersion(sync.ProjectVersion, false, sync.UploadToDemoMinio)
}
} else {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = FetchVersionImages(sync.ProjectVersion, false)
// get demo images
if sync.DirectHarborHost == "" {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = DownloadCompressUploadFromDemo(sync.ProjectName, sync.CompressImageToGzip, sync.UploadToDemoMinio)
gzipFolderFullPath = OfflineImageGzipFolderPrefix + sync.ProjectName
} else {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = DownloadCompressUploadFromDemo(sync.ProjectName, false, sync.UploadToDemoMinio)
}
}
} else {
// get demo images
if sync.DirectHarborHost == "" {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = FetchDemoImages(sync.ProjectName, true)
gzipFolderFullPath = OfflineImageGzipFolderPrefix + sync.ProjectName
} else {
errorPullImageList, errorGzipImageList, allCmiiImageNameList = FetchDemoImages(sync.ProjectName, false)
}
// 拉取特定的镜像
gzipFolderFullPath = OfflineImageGzipFolderPrefix + "tmp"
// 组装镜像名称
allCmiiImageNameList = concatAndUniformCmiiImage(sync.FullNameImageList, sync.CmiiNameTagList)
// DCU
errorPullImageList, errorGzipImageList, realCmiiImageList, allGzipFileNameList = DownloadCompressUploadFromFullNameList(allCmiiImageNameList, sync.CompressImageToGzip, gzipFolderFullPath, sync.UploadToDemoMinio)
}
backAllImageNameList := allCmiiImageNameList[:]
realCmiiImageList = slices.DeleteFunc(backAllImageNameList, func(imageName string) bool {
return slices.Contains(errorPullImageList, imageName)
})
realCmiiImageList = slices.DeleteFunc(backAllImageNameList, func(imageName string) bool {
return slices.Contains(errorGzipImageList, imageName)
})
// direct push if can
if sync.DirectHarborHost != "" {
// 直接传输到目标Harbor仓库
if sync.ShouldDirectPushToHarbor {
if sync.DirectHarborHost == "" {
log.ErrorF("DirectHarborHost is null ! can't push to target harbor !")
}
// push to
errorPushImageNameList = image.TagFromListAndPushToCHarbor(realCmiiImageList, sync.DirectHarborHost)
// build
imageSyncResult.AllCmiiImageNameList = allCmiiImageNameList
imageSyncResult.ErrorPullImageList = errorPullImageList
imageSyncResult.ErrorGzipImageList = errorGzipImageList
imageSyncResult.ErrorPushImageNameList = errorPushImageNameList
imageSyncResult.RealImageNameList = realCmiiImageList
// no gzip file
return imageSyncResult
}
// get gzip file name list
err := filepath.WalkDir(gzipFolderFullPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.ErrorF("error getting gzip file name list 1! %s", err.Error())
}
if !d.IsDir() {
allGzipFileNameList = append(allGzipFileNameList, d.Name())
}
return nil
})
if err != nil {
log.ErrorF("error getting gzip file name list 2! %s", err.Error())
}
// push to demo minio
if sync.PushToDemoMinio {
log.InfoF("pretend to push to minio !")
// create path
// push
}
// build
// build result
imageSyncResult.AllCmiiImageNameList = allCmiiImageNameList
imageSyncResult.ErrorPullImageList = errorPullImageList
imageSyncResult.ErrorGzipImageList = errorGzipImageList
imageSyncResult.ErrorPushImageNameList = errorPushImageNameList
imageSyncResult.RealGzipFileNameList = allGzipFileNameList
imageSyncResult.RealImageNameList = realCmiiImageList
// no gzip file
imageSyncResult.RealImageNameList = realCmiiImageList
imageSyncResult.ErrorPullImageList = errorPullImageList
imageSyncResult.RealGzipFileNameList = allGzipFileNameList
imageSyncResult.ErrorGzipImageList = errorGzipImageList
imageSyncResult.ErrorPushImageNameList = errorPushImageNameList
return imageSyncResult
}
func FetchDemoImages(projectName string, gzipSplit bool) (errorPullImageList, errorGzipImageList, allCmiiImageName []string) {
func concatAndUniformCmiiImage(fullImageList []string, cmiiImageList []string) []string {
if cmiiImageList != nil || len(cmiiImageList) > 0 {
// cmiiImageList has content
if fullImageList == nil {
fullImageList = []string{}
}
for _, cmiiImage := range cmiiImageList {
fullImageList = append(fullImageList, image2.CmiiHarborPrefix+cmiiImage)
}
}
return fullImageList
}
func DownloadCompressUploadFromFullNameList(fullNameList []string, shouldGzip bool, gzipFolderFullPath string, shouldOss bool) (errorPullImageList, errorGzipImageList, realCmiiImageName, allGzipFileNameList []string) {
// Download
log.Info("DOWNLOAD START !")
errorPullImageList = image.PullFromFullNameList(fullNameList)
// remove failed
fullNameList = slices.DeleteFunc(fullNameList, func(imageName string) bool {
return slices.Contains(errorPullImageList, imageName)
})
// Compress
if shouldGzip {
// mkdir folder
err := os.MkdirAll(gzipFolderFullPath, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
log.ErrorF("create folder error of %s", gzipFolderFullPath)
panic(err)
}
}
// 循环遍历压缩
log.Info("COMPRESS START")
for _, imageFullName := range fullNameList {
if !image.SaveToTarGZ(imageFullName, gzipFolderFullPath) {
errorGzipImageList = append(errorGzipImageList, imageFullName)
}
}
// remove failed
fullNameList = slices.DeleteFunc(fullNameList, func(imageName string) bool {
return slices.Contains(errorGzipImageList, imageName)
})
}
// Upload
if shouldOss {
//uploadGzipFileToDemoMinio()
// get gzip file name list
log.Info("UPLOAD OSS START !")
err := filepath.WalkDir(gzipFolderFullPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.ErrorF("error getting gzip file name list 1! %s", err.Error())
}
if !d.IsDir() {
allGzipFileNameList = append(allGzipFileNameList, d.Name())
}
return nil
})
if err != nil {
log.ErrorF("error getting gzip file name list 2! %s", err.Error())
}
// start to upload
// extract demo oss location suffix from gzipFolderFullPath
trimPrefix := strings.TrimPrefix(gzipFolderFullPath, OfflineImageGzipFolderPrefix)
bucketName := "cmlc-installation/" + trimPrefix
log.InfoF("gzip file location in demo oss is %s", DemoEndpoint+"/"+bucketName)
minioOperator := CmiiMinioOperator{}
for _, gzipFileName := range allGzipFileNameList {
if !minioOperator.UploadToDemo(bucketName, gzipFolderFullPath, gzipFileName) {
log.ErrorF("upload of %s to demo oss error !", gzipFolderFullPath+gzipFileName)
}
}
}
return errorPullImageList, errorGzipImageList, fullNameList, allGzipFileNameList
}
func uploadGzipFileToDemoMinio() {
}
// DownloadCompressUploadFromDemo 获取DEMO环境的全部镜像
func DownloadCompressUploadFromDemo(projectName string, shouldGzip bool, shouldOss bool) (errorPullImageList, errorGzipImageList, realCmiiImageName []string) {
// generate a project folder
err := os.MkdirAll(OfflineImageGzipFolderPrefix+projectName, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
log.ErrorF("[FetchDemoImages] - create folder of %s error %s", OfflineImageGzipFolderPrefix+projectName, err.Error())
return errorPullImageList, errorGzipImageList, allCmiiImageName
log.ErrorF("[Download_Compress_Upload_From_Demo] - create folder of %s error %s", OfflineImageGzipFolderPrefix+projectName, err.Error())
return errorPullImageList, errorGzipImageList, realCmiiImageName
}
}
@@ -168,25 +242,25 @@ func FetchDemoImages(projectName string, gzipSplit bool) (errorPullImageList, er
frontendFullNameList, frontendPull := image.PullFromCmiiHarborByMap(frontendMap, true)
srsFullNameList, srsPull := image.PullFromCmiiHarborByMap(srsMap, true)
allCmiiImageName = append(allCmiiImageName, backendFullNameList...)
allCmiiImageName = append(allCmiiImageName, frontendFullNameList...)
allCmiiImageName = append(allCmiiImageName, srsFullNameList...)
realCmiiImageName = append(realCmiiImageName, backendFullNameList...)
realCmiiImageName = append(realCmiiImageName, frontendFullNameList...)
realCmiiImageName = append(realCmiiImageName, srsFullNameList...)
// compress image
if gzipSplit {
for image_name, tag := range backendMap {
if !image.SaveToTarGZ(image_name+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+image_name+":"+tag)
if shouldGzip {
for imageName, tag := range backendMap {
if !image.SaveToTarGZ(imageName+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+imageName+":"+tag)
}
}
for image_name, tag := range frontendMap {
if !image.SaveToTarGZ(image_name+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+image_name+":"+tag)
for imageName, tag := range frontendMap {
if !image.SaveToTarGZ(imageName+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+imageName+":"+tag)
}
}
for image_name, tag := range srsMap {
if !image.SaveToTarGZ(image_name+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+image_name+":"+tag)
for imageName, tag := range srsMap {
if !image.SaveToTarGZ(imageName+":"+tag, OfflineImageGzipFolderPrefix+projectName+"/app/") {
errorGzipImageList = append(errorGzipImageList, image2.CmiiHarborPrefix+imageName+":"+tag)
}
}
}
@@ -198,17 +272,18 @@ func FetchDemoImages(projectName string, gzipSplit bool) (errorPullImageList, er
errorPullImageList = append(errorPullImageList, frontendPull...)
errorPullImageList = append(errorPullImageList, srsPull...)
return errorPullImageList, errorGzipImageList, allCmiiImageName
return errorPullImageList, errorGzipImageList, realCmiiImageName
}
func FetchVersionImages(cmiiVersion string, shouldGzip bool) (errorPullImageList, errorGzipImageList, allCmiiImageName []string) {
// DownloadCompressUploadFromVersion 根据版本下载全部的CMII镜像
func DownloadCompressUploadFromVersion(cmiiVersion string, shouldGzip bool, shouldOss bool) (errorPullImageList, errorGzipImageList, realCmiiImageName []string) {
// generate a project folder
err := os.MkdirAll(OfflineImageGzipFolderPrefix+cmiiVersion, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
log.ErrorF("[FetchDemoImages] - create folder of %s error %s", OfflineImageGzipFolderPrefix+cmiiVersion, err.Error())
return errorPullImageList, errorGzipImageList, allCmiiImageName
log.ErrorF("[Download_Compress_Upload_From_Demo] - create folder of %s error %s", OfflineImageGzipFolderPrefix+cmiiVersion, err.Error())
return errorPullImageList, errorGzipImageList, realCmiiImageName
}
}
@@ -222,39 +297,39 @@ func FetchVersionImages(cmiiVersion string, shouldGzip bool) (errorPullImageList
frontendMap[app] = cmiiVersion
}
allCmiiImageName = append(allCmiiImageName, image.ConvertCMiiImageMapToList(backendMap)...)
allCmiiImageName = append(allCmiiImageName, image.ConvertCMiiImageMapToList(frontendMap)...)
realCmiiImageName = append(realCmiiImageName, image.ConvertCMiiImageMapToList(backendMap)...)
realCmiiImageName = append(realCmiiImageName, image.ConvertCMiiImageMapToList(frontendMap)...)
for key, value := range CmiiSrsAppMap {
var app *CmiiDeploymentInterface
if strings.Contains(value, "deployment") {
app = CmiiOperator.DeploymentOneInterface(demo, key)
if app != nil {
allCmiiImageName = append(allCmiiImageName, app.Image)
realCmiiImageName = append(realCmiiImageName, app.Image)
}
} else if strings.Contains(value, "state") {
app = CmiiOperator.StatefulSetOneInterface(demo, key)
if app != nil {
for _, imageName := range app.ContainerImageMap {
allCmiiImageName = append(allCmiiImageName, imageName)
realCmiiImageName = append(realCmiiImageName, imageName)
}
}
}
}
utils.BeautifulPrintListWithTitle(allCmiiImageName, "Cmii Version Image => "+cmiiVersion)
utils.BeautifulPrintListWithTitle(realCmiiImageName, "Cmii Version Image => "+cmiiVersion)
// do work
if shouldGzip {
errorPullImageList, errorGzipImageList = image.PullFromListAndCompressSplit(allCmiiImageName, OfflineImageGzipFolderPrefix+cmiiVersion)
errorPullImageList, errorGzipImageList = image.PullFromListAndCompressSplit(realCmiiImageName, OfflineImageGzipFolderPrefix+cmiiVersion)
} else {
errorPullImageList = image.PullFromFullNameList(allCmiiImageName)
errorPullImageList = image.PullFromFullNameList(realCmiiImageName)
}
return errorPullImageList, errorGzipImageList, allCmiiImageName
return errorPullImageList, errorGzipImageList, realCmiiImageName
}
func FetchDependencyRepos(gzipSplit bool) (errorPullImageList, errorGzipImageList []string) {
func DownloadCompressUploadDependency(shouldGzip bool, shouldOss bool) (errorPullImageList, errorGzipImageList []string) {
err := os.MkdirAll(OfflineImageGzipFolderPrefix, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
@@ -262,10 +337,13 @@ func FetchDependencyRepos(gzipSplit bool) (errorPullImageList, errorGzipImageLis
}
}
// pull middleware images
errorPullImageList, errorGzipImageList = image.PullFromListAndCompressSplit(image.MiddlewareAmd64, OfflineImageGzipFolderPrefix+"middle/")
// pull rke images
pull, gzipImageList := image.PullFromListAndCompressSplit(image.Rancher1204Amd64, OfflineImageGzipFolderPrefix+"rke/")
// result
return append(errorPullImageList, pull...), append(errorGzipImageList, gzipImageList...)
}

View File

@@ -2,32 +2,36 @@ package main
import (
"testing"
image2 "wdd.io/agent-common/image"
"wdd.io/agent-common/utils"
)
/* 拉取
*/
func TestFetchDemoImages(t *testing.T) {
errorPullImageList, errorGzipImageList, allCmiiImageName := FetchDemoImages("shls", true)
utils.BeautifulPrintListWithTitle(errorPullImageList, "cmii errorPullImageList")
utils.BeautifulPrintListWithTitle(errorGzipImageList, "cmii errorGzipImageList")
utils.BeautifulPrintListWithTitle(allCmiiImageName, "cmii allCmiiImageName")
//errorPullImageList, errorGzipImageList, allCmiiImageName := DownloadCompressUploadFromDemo("shls", true)
//
//utils.BeautifulPrintListWithTitle(errorPullImageList, "cmii errorPullImageList")
//utils.BeautifulPrintListWithTitle(errorGzipImageList, "cmii errorGzipImageList")
//utils.BeautifulPrintListWithTitle(allCmiiImageName, "cmii allCmiiImageName")
}
func TestFetchVersionImages(t *testing.T) {
errorPullImageList, errorGzipImageList, allCmiiImageName := FetchVersionImages("5.4.0", true)
utils.BeautifulPrintListWithTitle(errorPullImageList, "cmii errorPullImageList")
utils.BeautifulPrintListWithTitle(errorGzipImageList, "cmii errorGzipImageList")
utils.BeautifulPrintListWithTitle(allCmiiImageName, "cmii allCmiiImageName")
//errorPullImageList, errorGzipImageList, allCmiiImageName := DownloadCompressUploadFromVersion("5.4.0", true)
//
//utils.BeautifulPrintListWithTitle(errorPullImageList, "cmii errorPullImageList")
//utils.BeautifulPrintListWithTitle(errorGzipImageList, "cmii errorGzipImageList")
//utils.BeautifulPrintListWithTitle(allCmiiImageName, "cmii allCmiiImageName")
}
func TestFetchDependencyRepos(t *testing.T) {
errorPullImageList, errorGzipImageList := FetchDependencyRepos(true)
utils.BeautifulPrintListWithTitle(errorPullImageList, "dep errorPullImageList")
utils.BeautifulPrintListWithTitle(errorGzipImageList, "dep errorGzipImageList")
//errorPullImageList, errorGzipImageList := FetchDependencyRepos(true)
//
//utils.BeautifulPrintListWithTitle(errorPullImageList, "dep errorPullImageList")
//utils.BeautifulPrintListWithTitle(errorGzipImageList, "dep errorGzipImageList")
}
func TestLoadSplitGzipImageToTargetHarbor(t *testing.T) {
@@ -44,6 +48,48 @@ func TestLoadSplitDepGzipImageToTargetHarbor(t *testing.T) {
utils.BeautifulPrintListWithTitle(errorPushImageNameList, "errorPushImageNameList")
}
func TestPullFromEntityAndSyncConditionally(t *testing.T) {
// 创建一个模拟的sync对象用于测试函数的行为。这里需要根据你的实际需求来设置mock数据和预期结果。
sync := ImageSyncEntity{
CmiiNameTagList: []string{
"cmii-uav-gateway:5.4.0",
},
FullNameImageList: nil,
ProjectVersion: "",
DirectHarborHost: "",
CompressImageToGzip: false,
UploadToDemoMinio: false,
ShouldDirectPushToHarbor: false,
}
// 调用函数并获取结果。这里需要根据你的实际需求来验证返回的结果是否符合预期。
result := sync.PullFromEntityAndSyncConditionally()
utils.BeautifulPrint(sync)
// 添加断言以检查函数的输出,例如:
if len(result.ErrorPullImageList) != 0 {
t.Errorf("Expected no error pulling images, got %v", result.ErrorPullImageList)
}
// ...其他验证逻辑...
}
func TestConcatAndUniformCmiiImage(t *testing.T) {
// 创建一个模拟的fullImageList和cmiiImageList用于测试函数的行为。这里需要根据你的实际需求来设置mock数据和预期结果。
fullImageList := []string{"image3", "image4"}
cmiiImageList := []string{"image1", "image2"}
// 调用函数并获取结果。这里需要根据你的实际需求来验证返回的结果是否符合预期。
result := concatAndUniformCmiiImage(fullImageList, cmiiImageList)
// 添加断言以检查函数的输出,例如:
expectedResult := []string{"image3", "image4", image2.CmiiHarborPrefix + "image1", image2.CmiiHarborPrefix + "image2"}
if len(result) != len(expectedResult) {
t.Errorf("Expected %v, got %v", expectedResult, result)
}
// ...其他验证逻辑...
}
func TestImageSyncEntity_PullFromEntityAndSyncConditionally(t *testing.T) {
imageSyncEntity := ImageSyncEntity{
ProjectVersion: "5.4.0",

View File

@@ -311,22 +311,22 @@ func PullCmiiFromFileJson(filePathName string) {
// PullFromFullNameList 根据镜像名列表拉取全部的镜像
func PullFromFullNameList(fullImageNameList []string) (errorPullImageList []string) {
for _, dep := range fullImageNameList {
for _, fullImageName := range fullImageNameList {
pullResult := PullFromCmiiHarbor(dep)
pullResult := PullFromCmiiHarbor(fullImageName)
if pullResult == nil {
errorPullImageList = append(errorPullImageList, dep)
errorPullImageList = append(errorPullImageList, fullImageName)
continue
}
scanner := bufio.NewScanner(pullResult)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "\"status\":\"Pulling from") {
fmt.Println(line)
}
if strings.Contains(line, "Status: Image is up to date for") {
fmt.Println(line)
}
//line := scanner.Text()
//if strings.Contains(line, "\"status\":\"Pulling from") {
// fmt.Println(line)
//}
//if strings.Contains(line, "Status: Image is up to date for") {
// fmt.Println(line)
//}
}
fmt.Println()
}

View File

@@ -12,8 +12,7 @@ var LocalKubeConfigFile = "/root/.kube/config"
// C:\Users\wddsh\go\bin\gox.exe -osarch="linux/amd64" -output "build/agent-operator_{{.OS}}_{{.Arch}}"
// C:\Users\wddsh\go\bin\gox.exe -osarch="linux/amd64 linux/arm64" -output "build/agent-operator_{{.OS}}_{{.Arch}}"
func main() {
func RealProjectRunner() {
// build from local LocalKubeConfigFile
if !utils.FileExists(LocalKubeConfigFile) {
log.ErrorF("%s not exits! error!", LocalKubeConfigFile)
@@ -65,5 +64,13 @@ func main() {
//ScaleCmiiBackendDeploymentToDesiredReplicas(realNamespace, 1)
// update from map
}
func CmiiRunner() {
}
func main() {
CmiiRunner()
}