feat: 镜像仓库增加harbor类型

This commit is contained in:
wangzhengkun
2021-11-18 15:48:05 +08:00
parent 5103fe14e8
commit 7874ed3d5c
8 changed files with 163 additions and 11 deletions

View File

@@ -3,23 +3,25 @@ package imagerepo
import "github.com/KubeOperator/kubepi/pkg/util/imagerepo/repos" import "github.com/KubeOperator/kubepi/pkg/util/imagerepo/repos"
type RepoClient interface { type RepoClient interface {
Auth() error //Auth() error
ListRepos() ([]string, error) ListRepos() ([]string, error)
ListImages(repository string) (images []string, err error)
} }
func NewClient(config Config) RepoClient { func NewClient(config Config) RepoClient {
switch config.Type { switch config.Type {
case "Nexus": case "Nexus":
return repos.NewNexusClient(config.EndPoint, config.Credential.Username, config.Credential.Password, config.DownloadUrl) return repos.NewNexusClient(config.EndPoint, config.Credential.Username, config.Credential.Password)
case "Harbor":
return repos.NewHarborClient(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
} }
@@ -27,3 +29,4 @@ type Credential struct {
Username string Username string
Password string Password string
} }

View File

@@ -18,4 +18,33 @@ func TestNexus(t *testing.T) {
if err != nil { if err != nil {
fmt.Println(err) 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)
}

View File

@@ -0,0 +1,82 @@
package repos
import (
"encoding/json"
"fmt"
"strings"
)
const (
baseUrl = "api/v2.0"
projectUrl = baseUrl + "/projects"
repositoryUrl = "/repositories"
artifactUrl = "/artifacts"
)
func NewHarborClient(endpoint, username, password string) *harborClient {
return &harborClient{
EndPoint: endpoint,
HttpClient: &HttpClient{
Username: username,
Password: password,
Host: endpoint,
},
}
}
type harborClient struct {
EndPoint string
HttpClient *HttpClient
}
type artifacts struct {
Type string
Tags []NameResult
}
func (c *harborClient) ListRepos() (names []string, err error) {
body, _, err1 := c.HttpClient.Get(projectUrl)
if err1 != nil {
err = err1
return
}
var result []NameResult
if err = json.Unmarshal(body, &result); err != nil {
return
}
for _, r := range result {
names = append(names, r.Name)
}
return
}
func (c *harborClient) ListImages(project string) (images []string, err error) {
repoUrl := fmt.Sprintf("%s/%s/%s", projectUrl, project, repositoryUrl)
body, _, err1 := c.HttpClient.Get(repoUrl)
if err1 != nil {
err = err1
return
}
var result []NameResult
if err = json.Unmarshal(body, &result); err != nil {
return
}
for _, r := range result {
repoName := strings.Replace(r.Name,project+"/","",-1)
body, _, err2 := c.HttpClient.Get(fmt.Sprintf("%s/%s/%s", repoUrl, repoName, artifactUrl))
if err2 != nil {
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)
}
}
}
return
}

View File

@@ -14,6 +14,11 @@ type HttpClient struct {
Host string Host string
} }
type NameResult struct {
Name string
Version string
}
func (h *HttpClient) NewRequest(method,endpoint string) (request *http.Request, err error) { func (h *HttpClient) NewRequest(method,endpoint string) (request *http.Request, err error) {
url := fmt.Sprintf("%s/%s", h.Host, endpoint) url := fmt.Sprintf("%s/%s", h.Host, endpoint)
request, err = http.NewRequest(method, url, nil) request, err = http.NewRequest(method, url, nil)

View File

@@ -2,19 +2,20 @@ package repos
import ( import (
"encoding/json" "encoding/json"
"fmt"
) )
const ( const (
BaseUrl = "service/rest" BaseUrl = "service/rest"
RepoUrl = BaseUrl + "/v1/repositories" RepoUrl = BaseUrl + "/v1/repositories"
ComponentUrl = BaseUrl + "/v1/components?repository="
) )
func NewNexusClient(endpoint, username, password, downloadUrl string) *nexusClient { func NewNexusClient(endpoint, username, password string) *nexusClient {
return &nexusClient{ return &nexusClient{
Username: username, Username: username,
EndPoint: endpoint, EndPoint: endpoint,
Password: password, Password: password,
DownloadUrl: downloadUrl,
HttpClient: &HttpClient{ HttpClient: &HttpClient{
Username: username, Username: username,
Password: password, Password: password,
@@ -27,12 +28,11 @@ type nexusClient struct {
Username string Username string
Password string Password string
EndPoint string EndPoint string
DownloadUrl string
HttpClient *HttpClient HttpClient *HttpClient
} }
type NameResult struct { type ItemResult struct {
Name string Items []NameResult
} }
func (c *nexusClient) Auth() error { func (c *nexusClient) Auth() error {
@@ -54,3 +54,20 @@ func (c *nexusClient) ListRepos() (names []string, err error) {
} }
return return
} }
func (c *nexusClient) ListImages(repository string) (images []string, err error) {
body, _, err1 := c.HttpClient.Get(fmt.Sprintf("%s%s", ComponentUrl, repository))
fmt.Println(fmt.Sprintf("%s%s", ComponentUrl, repository))
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)
}
return
}

View File

@@ -19,6 +19,8 @@
</el-table-column> </el-table-column>
<el-table-column :label="$t('business.image_repos.endpoint')" prop="endPoint" min-width="100" fix> <el-table-column :label="$t('business.image_repos.endpoint')" prop="endPoint" min-width="100" fix>
</el-table-column> </el-table-column>
<el-table-column :label="$t('business.image_repos.repo')" prop="repoName" min-width="100" fix>
</el-table-column>
<el-table-column :label="$t('commons.table.created_time')" min-width="100" fix> <el-table-column :label="$t('commons.table.created_time')" min-width="100" fix>
<template v-slot:default="{row}"> <template v-slot:default="{row}">
{{ row.createAt | ageFormat }} {{ row.createAt | ageFormat }}

View File

@@ -122,8 +122,11 @@ export default {
) )
}, },
list () { list () {
this.loading = true
listInternalRepos(this.form).then(res => { listInternalRepos(this.form).then(res => {
this.repos = res.data this.repos = res.data
}).finally(() => {
this.loading = false
}) })
}, },
onCancel () { onCancel () {

View File

@@ -187,6 +187,17 @@ const message = {
operation: "Operation", operation: "Operation",
operation_domain: "Resource", operation_domain: "Resource",
specific_information: "Informations" specific_information: "Informations"
},
image_repos: {
list: "Mirror Warehouse",
name: "Name",
endpoint: "Address",
downloadUrl: "Mirror download URL",
username: "Username",
password: "Password",
type: "Type",
repo: "Mirror Library",
load_repo: "Load image library",
} }
}, },
} }