From a40b0f349b3cc20be2cfaac3d532e2d8666dad05 Mon Sep 17 00:00:00 2001 From: lxShaDoWxl Date: Tue, 10 Sep 2019 20:22:05 +0600 Subject: [PATCH] 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 --- src/replication/adapter/gitlab/adapter.go | 42 ++++------------------- src/replication/adapter/gitlab/client.go | 26 +++++++------- src/replication/adapter/gitlab/types.go | 4 +++ 3 files changed, 24 insertions(+), 48 deletions(-) diff --git a/src/replication/adapter/gitlab/adapter.go b/src/replication/adapter/gitlab/adapter.go index b1ab6d39f..b63a7080d 100644 --- a/src/replication/adapter/gitlab/adapter.go +++ b/src/replication/adapter/gitlab/adapter.go @@ -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) -//} diff --git a/src/replication/adapter/gitlab/client.go b/src/replication/adapter/gitlab/client.go index fd7be2403..9b687b094 100644 --- a/src/replication/adapter/gitlab/client.go +++ b/src/replication/adapter/gitlab/client.go @@ -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) diff --git a/src/replication/adapter/gitlab/types.go b/src/replication/adapter/gitlab/types.go index b29f7c01e..5c7828713 100644 --- a/src/replication/adapter/gitlab/types.go +++ b/src/replication/adapter/gitlab/types.go @@ -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"`