mirror of
https://github.com/1Panel-dev/KubePi.git
synced 2025-10-12 18:51:04 +08:00
fix(imagerepo): workload 创建加载全部镜像
This commit is contained in:
@@ -2,6 +2,8 @@ package imagerepo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
V1ClusterRepo "github.com/KubeOperator/kubepi/internal/model/v1/clusterrepo"
|
V1ClusterRepo "github.com/KubeOperator/kubepi/internal/model/v1/clusterrepo"
|
||||||
V1ImageRepo "github.com/KubeOperator/kubepi/internal/model/v1/imagerepo"
|
V1ImageRepo "github.com/KubeOperator/kubepi/internal/model/v1/imagerepo"
|
||||||
"github.com/KubeOperator/kubepi/internal/service/v1/common"
|
"github.com/KubeOperator/kubepi/internal/service/v1/common"
|
||||||
@@ -12,7 +14,6 @@ import (
|
|||||||
"github.com/asdine/storm/v3"
|
"github.com/asdine/storm/v3"
|
||||||
"github.com/asdine/storm/v3/q"
|
"github.com/asdine/storm/v3/q"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
@@ -66,9 +67,8 @@ func (s *service) ListImages(repo, cluster string, options common.DBOptions) (na
|
|||||||
if err = query.First(&cRepo); err != nil {
|
if err = query.First(&cRepo); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rp, err1 := s.GetByName(repo, options)
|
rp, err := s.GetByName(repo, options)
|
||||||
if err1 != nil {
|
if err != nil {
|
||||||
err = err1
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client := repoClient.NewClient(repos.Config{
|
client := repoClient.NewClient(repos.Config{
|
||||||
@@ -80,16 +80,11 @@ func (s *service) ListImages(repo, cluster string, options common.DBOptions) (na
|
|||||||
},
|
},
|
||||||
Version: rp.Version,
|
Version: rp.Version,
|
||||||
})
|
})
|
||||||
request := repos.RepoRequest{
|
images, err := client.ListImagesWithoutPage(rp.RepoName)
|
||||||
Repo: rp.RepoName,
|
if err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
res, err2 := client.ListImages(request)
|
|
||||||
if err2 != nil {
|
|
||||||
err = err2
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, image := range res.Items {
|
for _, image := range images {
|
||||||
names = append(names, rp.DownloadUrl+"/"+image)
|
names = append(names, rp.DownloadUrl+"/"+image)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -231,7 +226,7 @@ func (s *service) UpdateRepo(name string, repo *V1ImageRepo.ImageRepo, options c
|
|||||||
repo.CreateAt = old.CreateAt
|
repo.CreateAt = old.CreateAt
|
||||||
repo.UpdateAt = time.Now()
|
repo.UpdateAt = time.Now()
|
||||||
|
|
||||||
if old.Auth == false {
|
if !old.Auth {
|
||||||
repo.Credential.Password = ""
|
repo.Credential.Password = ""
|
||||||
repo.Credential.Username = ""
|
repo.Credential.Username = ""
|
||||||
repo.Credential = V1ImageRepo.Credential{}
|
repo.Credential = V1ImageRepo.Credential{}
|
||||||
|
@@ -5,6 +5,7 @@ import "github.com/KubeOperator/kubepi/pkg/util/imagerepo/repos"
|
|||||||
type RepoClient interface {
|
type RepoClient interface {
|
||||||
ListRepos(request repos.ProjectRequest) ([]string, error)
|
ListRepos(request repos.ProjectRequest) ([]string, error)
|
||||||
ListImages(request repos.RepoRequest) (response repos.RepoResponse, err error)
|
ListImages(request repos.RepoRequest) (response repos.RepoResponse, err error)
|
||||||
|
ListImagesWithoutPage(repository string) (images []string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(config repos.Config) RepoClient {
|
func NewClient(config repos.Config) RepoClient {
|
||||||
|
@@ -1,50 +0,0 @@
|
|||||||
package imagerepo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNexus(t *testing.T) {
|
|
||||||
client := NewClient(Config{
|
|
||||||
Type: "Nexus",
|
|
||||||
EndPoint: "http://172.16.10.185:8081",
|
|
||||||
Credential: Credential{
|
|
||||||
Username: "zhengkun",
|
|
||||||
Password: "Calong@2015",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
_,err := client.ListRepos()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
images,err := client.ListImages("docker-hub-proxy")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Println(images)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHarbor(t *testing.T) {
|
|
||||||
client := NewClient(Config{
|
|
||||||
Type: "Harbor",
|
|
||||||
EndPoint: "http://172.16.10.188",
|
|
||||||
Credential: Credential{
|
|
||||||
Username: "zhengkun",
|
|
||||||
Password: "Calong@2015",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
repos,err := client.ListRepos()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Println(repos)
|
|
||||||
images,err := client.ListImages("kubeapp")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Println(images)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@@ -3,10 +3,11 @@ package repos
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"github.com/docker/distribution/reference"
|
|
||||||
"github.com/docker/distribution/registry/client"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
|
"github.com/docker/distribution/registry/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDockerRegistryClient(endpoint, username, password string) *dockerRegistryClient {
|
func NewDockerRegistryClient(endpoint, username, password string) *dockerRegistryClient {
|
||||||
@@ -87,6 +88,55 @@ func (c *dockerRegistryClient) ListImages(request RepoRequest) (response RepoRes
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *dockerRegistryClient) ListImagesWithoutPage(project string) (images []string, err error) {
|
||||||
|
transport := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true, //nolint:gosec
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
basicTransport := &BasicTransport{
|
||||||
|
Transport: transport,
|
||||||
|
Username: c.Username,
|
||||||
|
Password: c.Password,
|
||||||
|
URL: c.EndPoint,
|
||||||
|
}
|
||||||
|
registry, err1 := client.NewRegistry(c.EndPoint, basicTransport)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allImages := make([]string, 0)
|
||||||
|
var last string
|
||||||
|
for {
|
||||||
|
imageNoTags := make([]string, 10)
|
||||||
|
count, err2 := registry.Repositories(context.Background(), imageNoTags, last)
|
||||||
|
if err2 == io.EOF {
|
||||||
|
allImages = append(allImages, imageNoTags[:count]...)
|
||||||
|
break
|
||||||
|
} else if err2 != nil {
|
||||||
|
err = err2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
last = imageNoTags[count-1]
|
||||||
|
allImages = append(allImages, imageNoTags...)
|
||||||
|
}
|
||||||
|
for _, image := range allImages {
|
||||||
|
tags, err3 := c.listImageTags(image, basicTransport)
|
||||||
|
if err3 != nil {
|
||||||
|
err = err3
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tags) != 0 {
|
||||||
|
for _, tag := range tags {
|
||||||
|
images = append(images, image+":"+tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (c *dockerRegistryClient) listImageTags(imageName string, transport http.RoundTripper) (tags []string, err error) {
|
func (c *dockerRegistryClient) listImageTags(imageName string, transport http.RoundTripper) (tags []string, err error) {
|
||||||
ref, err1 := reference.Parse(imageName)
|
ref, err1 := reference.Parse(imageName)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
|
@@ -202,6 +202,74 @@ func (c *harborClient) ListImages(request RepoRequest) (response RepoResponse, e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *harborClient) ListImagesWithoutPage(project string) (images []string, err error) {
|
||||||
|
if c.Version == "v2" {
|
||||||
|
repoUrl := fmt.Sprintf("%s/%s/%s?page=1&&page_size=100", getProjectUrl(c.Version), project, repositoryUrl)
|
||||||
|
result, _, err1 := c.HttpClient.Get(repoUrl)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var items []harborBody
|
||||||
|
if err1 = json.Unmarshal(result, &items); err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
repoUrl = fmt.Sprintf("%s/%s/%s", getProjectUrl(c.Version), project, repositoryUrl)
|
||||||
|
for _, r := range items {
|
||||||
|
repoName := strings.Replace(r.Name, project+"/", "", -1)
|
||||||
|
body, _, err2 := c.HttpClient.Get(fmt.Sprintf("%s/%s/%s?page=1&&page_size=%d", repoUrl, repoName, artifactUrl, r.TagsCount))
|
||||||
|
if err2 != nil {
|
||||||
|
if strings.Contains(err2.Error(), "404") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = err2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var artifacts []artifacts
|
||||||
|
if err = json.Unmarshal(body, &artifacts); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, art := range artifacts {
|
||||||
|
for _, tag := range art.Tags {
|
||||||
|
images = append(images, r.Name+":"+tag.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result, err1 := c.HttpClient.GetNameResult(getProjectUrl(c.Version))
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var repoUrl string
|
||||||
|
for _, value := range result {
|
||||||
|
if value.Name == project {
|
||||||
|
repoUrl = fmt.Sprintf("%s/%s%s", v1Url, repositoryUrl, "?project_id="+strconv.Itoa(value.ProjectID))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repos, err1 := c.HttpClient.GetNameResult(repoUrl)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, repo := range repos {
|
||||||
|
tagUrl := fmt.Sprintf("%s/%s/%s/%s", v1Url, repositoryUrl, repo.Name, tagUrl)
|
||||||
|
tags, err1 := c.HttpClient.GetNameResult(tagUrl)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, tag := range tags {
|
||||||
|
images = append(images, repo.Name+":"+tag.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func getProjectUrl(version string) string {
|
func getProjectUrl(version string) string {
|
||||||
if version == "v1" {
|
if version == "v1" {
|
||||||
return v1Url + projectUrl
|
return v1Url + projectUrl
|
||||||
|
@@ -80,3 +80,30 @@ func (c *nexusClient) ListImages(request RepoRequest) (response RepoResponse, er
|
|||||||
response.ContinueToken = result.ContinuationToken
|
response.ContinueToken = result.ContinuationToken
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *nexusClient) ListImagesWithoutPage(repository string) (images []string, err error) {
|
||||||
|
continuationToken := ""
|
||||||
|
for {
|
||||||
|
token := ""
|
||||||
|
if continuationToken != "" {
|
||||||
|
token = fmt.Sprintf("&&continuationToken=%s", continuationToken)
|
||||||
|
}
|
||||||
|
body, _, err1 := c.HttpClient.Get(fmt.Sprintf("%s%s", ComponentUrl, repository) + token)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var result ItemResult
|
||||||
|
if err = json.Unmarshal(body, &result); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, r := range result.Items {
|
||||||
|
images = append(images, r.Name+":"+r.Version)
|
||||||
|
}
|
||||||
|
if result.ContinuationToken == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continuationToken = result.ContinuationToken
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@@ -725,7 +725,7 @@ export default {
|
|||||||
},
|
},
|
||||||
onEdit(data) {
|
onEdit(data) {
|
||||||
let ps = []
|
let ps = []
|
||||||
if (this.podMetadata.annotations["operation"] === "update") {
|
if (this.podMetadata.annotations && this.podMetadata.annotations["operation"] === "update") {
|
||||||
ps = this.handleSecret(ps)
|
ps = this.handleSecret(ps)
|
||||||
}
|
}
|
||||||
Promise.all(ps).then(() => {
|
Promise.all(ps).then(() => {
|
||||||
|
Reference in New Issue
Block a user