mirror of
https://github.com/1Panel-dev/KubePi.git
synced 2025-10-28 09:31:37 +08:00
feat: 增加docker-registry支持
This commit is contained in:
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
|||||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||||
github.com/coreos/etcd v3.3.13+incompatible
|
github.com/coreos/etcd v3.3.13+incompatible
|
||||||
github.com/creack/pty v1.1.15 // indirect
|
github.com/creack/pty v1.1.15 // indirect
|
||||||
|
github.com/docker/distribution v2.7.1+incompatible // indirect
|
||||||
github.com/go-ldap/ldap v3.0.3+incompatible
|
github.com/go-ldap/ldap v3.0.3+incompatible
|
||||||
github.com/go-logr/logr v0.4.0
|
github.com/go-logr/logr v0.4.0
|
||||||
github.com/gofrs/flock v0.8.1
|
github.com/gofrs/flock v0.8.1
|
||||||
|
|||||||
@@ -14,19 +14,20 @@ func NewClient(config Config) RepoClient {
|
|||||||
return repos.NewNexusClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
|
return repos.NewNexusClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
|
||||||
case "Harbor":
|
case "Harbor":
|
||||||
return repos.NewHarborClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
|
return repos.NewHarborClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
|
||||||
|
case "DockerRegistry":
|
||||||
|
return repos.NewDockerRegistryClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Type string
|
Type string
|
||||||
EndPoint string
|
EndPoint string
|
||||||
//DownloadUrl string
|
//DownloadUrl string
|
||||||
Credential Credential
|
Credential Credential
|
||||||
}
|
}
|
||||||
|
|
||||||
type Credential struct {
|
type Credential struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
pkg/util/imagerepo/repos/basictransport.go
Normal file
23
pkg/util/imagerepo/repos/basictransport.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package repos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BasicTransport struct {
|
||||||
|
Transport http.RoundTripper
|
||||||
|
URL string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *BasicTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
if strings.HasPrefix(req.URL.String(), t.URL) {
|
||||||
|
if t.Username != "" || t.Password != "" {
|
||||||
|
req.SetBasicAuth(t.Username, t.Password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resp, err := t.Transport.RoundTrip(req)
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
93
pkg/util/imagerepo/repos/docker_registry.go
Normal file
93
pkg/util/imagerepo/repos/docker_registry.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package repos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
|
"github.com/docker/distribution/registry/client"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewDockerRegistryClient(endpoint, username, password string) *dockerRegistryClient {
|
||||||
|
return &dockerRegistryClient{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
|
EndPoint: endpoint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dockerRegistryClient struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
EndPoint string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *dockerRegistryClient) ListRepos() (names []string, err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *dockerRegistryClient) ListImages(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) {
|
||||||
|
ref, err1 := reference.Parse(imageName)
|
||||||
|
if err1 != nil {
|
||||||
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
repo, err2 := client.NewRepository(ref.(reference.Named), c.EndPoint, transport)
|
||||||
|
if err2 != nil {
|
||||||
|
err = err2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
tagService := repo.Tags(ctx)
|
||||||
|
return tagService.All(ctx)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user