[ Cmii ] [ Operator ] - optimize Image pull

This commit is contained in:
zeaslity
2024-01-25 12:07:17 +08:00
parent a09515c82d
commit a0369bda1e
5 changed files with 71 additions and 51 deletions

View File

@@ -6,15 +6,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/klauspost/pgzip"
"io"
"os"
"strings"
"wdd.io/cmii_operator/tools"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
var apiClient = newClient()
@@ -158,35 +156,10 @@ func ImagePullFromCmiiHarbor(imageName string) (pullResult io.ReadCloser) {
return pullResult
}
func ImagePullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode bool) {
func ImagePullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode bool) (errorPullImageList []string) {
var fs uintptr
for image, tag := range imageVersionMap {
s := CmiiHarborPrefix + image + ":" + tag
pullResult := ImagePullFromCmiiHarbor(s)
if pullResult == nil {
continue
}
if silentMode {
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)
}
}
} else {
_ = tools.DisplayJSONMessagesStream(pullResult, os.Stdout, fs, true, func(message tools.JSONMessage) {
})
}
fmt.Println()
fmt.Println()
}
fullImageNameList := convertCMiiImageMapToList(imageVersionMap)
return ImagePullFromFullNameList(fullImageNameList)
}
@@ -225,13 +198,14 @@ func ImagePullFromFileJson(filePathName string) {
}
func ImagePullFromList(depContainerList []string) {
for _, dep := range depContainerList {
func ImagePullFromFullNameList(fullImageNameList []string) (errorPullImageList []string) {
for _, dep := range fullImageNameList {
loginToDockerHub()
pullResult := ImagePullFromCmiiHarbor(dep)
if pullResult == nil {
errorPullImageList = append(errorPullImageList, dep)
continue
}
scanner := bufio.NewScanner(pullResult)
@@ -246,11 +220,13 @@ func ImagePullFromList(depContainerList []string) {
}
fmt.Println()
}
return errorPullImageList
}
func ImagePullFromListAndCompressSplit(imageNameList []string, gzipFolder string) {
func ImagePullFromListAndCompressSplit(fullImageNameList []string, gzipFolder string) (errorPullImageList, errorGzipImageList []string) {
ImagePullFromList(imageNameList)
errorPullImageList = ImagePullFromFullNameList(fullImageNameList)
// generate a project folder
err := os.MkdirAll(gzipFolder, os.ModeDir)
@@ -260,10 +236,13 @@ func ImagePullFromListAndCompressSplit(imageNameList []string, gzipFolder string
}
}
for _, image := range imageNameList {
ImageSaveToTarGZ(image, gzipFolder)
for _, image := range fullImageNameList {
if !ImageSaveToTarGZ(image, gzipFolder) {
errorGzipImageList = append(errorGzipImageList, image)
}
}
return errorPullImageList, errorGzipImageList
}
func ImageSaveToTarGZ(targetImageName, folderPathPrefix string) bool {
@@ -350,6 +329,16 @@ func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
return gzipFileName
}
func convertCMiiImageMapToList(cmiiImageVersionMap map[string]string) (fullImageNameList []string) {
for image, tag := range cmiiImageVersionMap {
s := CmiiHarborPrefix + image + ":" + tag
fullImageNameList = append(fullImageNameList, s)
}
return fullImageNameList
}
func loginToDockerHub() {
login, err := apiClient.RegistryLogin(context.TODO(), types.AuthConfig{