mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-11 02:17:42 +01:00
In order to integrate with gitlab it is necessary to get a list of available repositories using gitlab api. This is because gitlab registry does not give access to _catalog
Signed-off-by: lxShaDoWxl <lxshadowxkingxl@gmail.com>
This commit is contained in:
parent
6937731744
commit
a40b0f349b
@ -27,7 +27,7 @@ type adapter struct {
|
||||
url string
|
||||
username string
|
||||
token string
|
||||
clientGitlabApi *Client
|
||||
clientGitlabAPI *Client
|
||||
}
|
||||
|
||||
func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||
@ -44,7 +44,7 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||
|
||||
dockerRegistryAdapter, err := native.NewAdapterWithCustomizedAuthorizer(&model.Registry{
|
||||
Name: registry.Name,
|
||||
URL: registry.URL, // specify the URL of Gitlab registry service
|
||||
URL: registry.URL,
|
||||
Credential: registry.Credential,
|
||||
Insecure: registry.Insecure,
|
||||
}, authorizer)
|
||||
@ -55,7 +55,7 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||
return &adapter{
|
||||
registry: registry,
|
||||
url: registry.URL,
|
||||
clientGitlabApi: NewClient(registry),
|
||||
clientGitlabAPI: NewClient(registry),
|
||||
Adapter: dockerRegistryAdapter,
|
||||
}, nil
|
||||
}
|
||||
@ -102,7 +102,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
names, ok := util.IsSpecificPathComponent(projectPattern)
|
||||
if ok {
|
||||
for _, name := range names {
|
||||
var projectsByName, err = a.clientGitlabApi.getProjectsByName(name)
|
||||
var projectsByName, err = a.clientGitlabAPI.getProjectsByName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -114,7 +114,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
}
|
||||
}
|
||||
if len(projects) == 0 {
|
||||
projects, err = a.clientGitlabApi.getProjects()
|
||||
projects, err = a.clientGitlabAPI.getProjects()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -129,7 +129,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
if !existPatterns(project.FullPath, pathPatterns) {
|
||||
continue
|
||||
}
|
||||
repositories, err := a.clientGitlabApi.getRepositories(project.ID)
|
||||
repositories, err := a.clientGitlabAPI.getRepositories(project.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -140,7 +140,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
if !existPatterns(repository.Path, pathPatterns) {
|
||||
continue
|
||||
}
|
||||
vTags, err := a.clientGitlabApi.getTags(project.ID, repository.ID)
|
||||
vTags, err := a.clientGitlabAPI.getTags(project.ID, repository.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -188,31 +188,3 @@ func existPatterns(path string, patterns []string) bool {
|
||||
}
|
||||
return correct
|
||||
}
|
||||
|
||||
////TODO maybe remove and add input form to registry host
|
||||
//func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
||||
// //for _, resource := range resources {
|
||||
// // var location, err = url.Parse(fmt.Sprintf("%v", resource.Metadata.Repository.Metadata["location"]))
|
||||
// // if err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // endpoint := a.Adapter.Registry.Endpoint
|
||||
// // endpoint.Host = location.Host
|
||||
// // a.Adapter.Registry.Endpoint = endpoint
|
||||
// // break
|
||||
// //}
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
|
||||
//// PullManifest ...
|
||||
//func (a *adapter) PullManifest(repository, reference string, accepttedMediaTypes []string) (distribution.Manifest, string, error) {
|
||||
// //var location, err = url.Parse(repository)
|
||||
// //if err != nil {
|
||||
// // return nil, "", err
|
||||
// //}
|
||||
// //endpoint := a.Adapter.Registry.Endpoint
|
||||
// //endpoint.Host = location.Host
|
||||
// //a.Adapter.Registry.Endpoint = endpoint
|
||||
// return a.Adapter.PullManifest(repository, reference, accepttedMediaTypes)
|
||||
//}
|
||||
|
@ -80,7 +80,7 @@ func ping(client *http.Client, endpoint string) (string, string, error) {
|
||||
func buildPingURL(endpoint string) string {
|
||||
return fmt.Sprintf("%s/v2/", endpoint)
|
||||
}
|
||||
func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
|
||||
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -91,8 +91,8 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request,
|
||||
|
||||
func (c *Client) getProjects() ([]*Project, error) {
|
||||
var projects []*Project
|
||||
urlApi := fmt.Sprintf("%s/api/v4/projects?membership=1&per_page=50", c.url)
|
||||
if err := c.GetAndIteratePagination(urlApi, &projects); err != nil {
|
||||
urlAPI := fmt.Sprintf("%s/api/v4/projects?membership=1&per_page=50", c.url)
|
||||
if err := c.GetAndIteratePagination(urlAPI, &projects); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return projects, nil
|
||||
@ -100,16 +100,16 @@ func (c *Client) getProjects() ([]*Project, error) {
|
||||
|
||||
func (c *Client) getProjectsByName(name string) ([]*Project, error) {
|
||||
var projects []*Project
|
||||
urlApi := fmt.Sprintf("%s/api/v4/projects?search=%s&membership=1&per_page=50", c.url, name)
|
||||
if err := c.GetAndIteratePagination(urlApi, &projects); err != nil {
|
||||
urlAPI := fmt.Sprintf("%s/api/v4/projects?search=%s&membership=1&per_page=50", c.url, name)
|
||||
if err := c.GetAndIteratePagination(urlAPI, &projects); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return projects, nil
|
||||
}
|
||||
func (c *Client) getRepositories(projectID int64) ([]*Repository, error) {
|
||||
var repositories []*Repository
|
||||
urlApi := fmt.Sprintf("%s/api/v4/projects/%d/registry/repositories?per_page=50", c.url, projectID)
|
||||
if err := c.GetAndIteratePagination(urlApi, &repositories); err != nil {
|
||||
urlAPI := fmt.Sprintf("%s/api/v4/projects/%d/registry/repositories?per_page=50", c.url, projectID)
|
||||
if err := c.GetAndIteratePagination(urlAPI, &repositories); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repositories, nil
|
||||
@ -117,8 +117,8 @@ func (c *Client) getRepositories(projectID int64) ([]*Repository, error) {
|
||||
|
||||
func (c *Client) getTags(projectID int64, repositoryID int64) ([]*Tag, error) {
|
||||
var tags []*Tag
|
||||
urlApi := fmt.Sprintf("%s/api/v4/projects/%d/registry/repositories/%d/tags?per_page=50", c.url, projectID, repositoryID)
|
||||
if err := c.GetAndIteratePagination(urlApi, &tags); err != nil {
|
||||
urlAPI := fmt.Sprintf("%s/api/v4/projects/%d/registry/repositories/%d/tags?per_page=50", c.url, projectID, repositoryID)
|
||||
if err := c.GetAndIteratePagination(urlAPI, &tags); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tags, nil
|
||||
@ -127,7 +127,7 @@ func (c *Client) getTags(projectID int64, repositoryID int64) ([]*Tag, error) {
|
||||
// GetAndIteratePagination iterates the pagination header and returns all resources
|
||||
// The parameter "v" must be a pointer to a slice
|
||||
func (c *Client) GetAndIteratePagination(endpoint string, v interface{}) error {
|
||||
urlApi, err := url.Parse(endpoint)
|
||||
urlAPI, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -143,7 +143,7 @@ func (c *Client) GetAndIteratePagination(endpoint string, v interface{}) error {
|
||||
|
||||
resources := reflect.Indirect(reflect.New(elemType))
|
||||
for len(endpoint) > 0 {
|
||||
req, err := c.NewRequest(http.MethodGet, endpoint, nil)
|
||||
req, err := c.newRequest(http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -172,9 +172,9 @@ func (c *Client) GetAndIteratePagination(endpoint string, v interface{}) error {
|
||||
|
||||
nextPage := resp.Header.Get("X-Next-Page")
|
||||
if len(nextPage) > 0 {
|
||||
query := urlApi.Query()
|
||||
query := urlAPI.Query()
|
||||
query.Set("page", nextPage)
|
||||
endpoint = urlApi.Scheme + "://" + urlApi.Host + urlApi.Path + "?" + query.Encode()
|
||||
endpoint = urlAPI.Scheme + "://" + urlAPI.Host + urlAPI.Path + "?" + query.Encode()
|
||||
}
|
||||
}
|
||||
rv.Elem().Set(resources)
|
||||
|
@ -1,9 +1,11 @@
|
||||
package gitlab
|
||||
|
||||
// TokenResp is response of login.
|
||||
type TokenResp struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// Project describes a project in Gitlab
|
||||
type Project struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@ -12,6 +14,7 @@ type Project struct {
|
||||
RegistryEnabled bool `json:"container_registry_enabled"`
|
||||
}
|
||||
|
||||
// Repository describes a repository in Gitlab
|
||||
type Repository struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@ -19,6 +22,7 @@ type Repository struct {
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
// Tag describes a tag in Gitlab
|
||||
type Tag struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
|
Loading…
Reference in New Issue
Block a user