feat: 增加docker-registry支持

This commit is contained in:
wangzhengkun
2021-11-18 17:27:26 +08:00
parent 7874ed3d5c
commit d42fcbc569
4 changed files with 122 additions and 4 deletions

1
go.mod
View File

@@ -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

View File

@@ -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
} }

View 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
}

View 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)
}