rebase and fix name conflicts among packages and vars

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-07-19 22:37:54 +08:00
commit deb021eb48
6 changed files with 124 additions and 91 deletions

View File

@ -15,9 +15,9 @@
package api
import (
"errors"
"net/http"
"errors"
"github.com/ghodss/yaml"
"github.com/goharbor/harbor/src/common/api"
"github.com/goharbor/harbor/src/common/security"
@ -25,10 +25,21 @@ import (
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/filter"
"github.com/goharbor/harbor/src/core/promgr"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository"
)
const (
yamlFileContentType = "application/x-yaml"
// ReplicationJobType ...
ReplicationJobType = "replication"
// ScanJobType ...
ScanJobType = "scan"
)
var (
projectMgr project.Manager
repositoryMgr repository.Manager
)
// BaseController ...
@ -41,13 +52,6 @@ type BaseController struct {
ProjectMgr promgr.ProjectManager
}
const (
// ReplicationJobType ...
ReplicationJobType = "replication"
// ScanJobType ...
ScanJobType = "scan"
)
// Prepare inits security context and project manager from request
// context
func (b *BaseController) Prepare() {
@ -91,6 +95,22 @@ func (b *BaseController) WriteYamlData(object interface{}) {
// Init related objects/configurations for the API controllers
func Init() error {
registerHealthCheckers()
// init chart controller
if err := initChartController(); err != nil {
return err
}
// init project manager
initProjectManager()
// init repository manager
initRepositoryManager()
return nil
}
func initChartController() error {
// If chart repository is not enabled then directly return
if !config.WithChartMuseum() {
return nil
@ -102,6 +122,13 @@ func Init() error {
}
chartController = chartCtl
return nil
}
func initProjectManager() {
projectMgr = project.New()
}
func initRepositoryManager() {
repositoryMgr = repository.New(projectMgr, chartController)
}

View File

@ -28,7 +28,7 @@ import (
var (
resourceLabelAPIBasePath = "/api/repositories"
repository = "library/hello-world"
repo = "library/hello-world"
tag = "latest"
proLibraryLabelID int64
)
@ -63,7 +63,7 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
},
code: http.StatusUnauthorized,
@ -72,13 +72,13 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
credential: projGuest,
},
code: http.StatusForbidden,
},
// 404 repository doesn't exist
// 404 repo doesn't exist
{
request: &testingRequest{
url: fmt.Sprintf("%s/library/non-exist-repo/tags/%s/labels", resourceLabelAPIBasePath, tag),
@ -90,7 +90,7 @@ func TestAddToImage(t *testing.T) {
// 404 image doesn't exist
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/non-exist-tag/labels", resourceLabelAPIBasePath, repository),
url: fmt.Sprintf("%s/%s/tags/non-exist-tag/labels", resourceLabelAPIBasePath, repo),
method: http.MethodPost,
credential: projDeveloper,
},
@ -99,7 +99,7 @@ func TestAddToImage(t *testing.T) {
// 400
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath, repository, tag),
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath, repo, tag),
method: http.MethodPost,
credential: projDeveloper,
},
@ -109,7 +109,7 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
credential: projDeveloper,
bodyJSON: struct {
@ -124,7 +124,7 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
credential: projDeveloper,
bodyJSON: struct {
@ -139,7 +139,7 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
credential: projDeveloper,
bodyJSON: struct {
@ -154,7 +154,7 @@ func TestAddToImage(t *testing.T) {
{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodPost,
credential: projDeveloper,
bodyJSON: struct {
@ -172,7 +172,7 @@ func TestAddToImage(t *testing.T) {
func TestGetOfImage(t *testing.T) {
labels := []*models.Label{}
err := handleAndParse(&testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath, repository, tag),
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath, repo, tag),
method: http.MethodGet,
credential: projDeveloper,
}, &labels)
@ -185,7 +185,7 @@ func TestRemoveFromImage(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels/%d", resourceLabelAPIBasePath,
repository, tag, proLibraryLabelID),
repo, tag, proLibraryLabelID),
method: http.MethodDelete,
credential: projDeveloper,
},
@ -195,7 +195,7 @@ func TestRemoveFromImage(t *testing.T) {
labels := []*models.Label{}
err := handleAndParse(&testingRequest{
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
repository, tag),
repo, tag),
method: http.MethodGet,
credential: projDeveloper,
}, &labels)
@ -206,7 +206,7 @@ func TestRemoveFromImage(t *testing.T) {
func TestAddToRepository(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
method: http.MethodPost,
bodyJSON: struct {
ID int64
@ -222,7 +222,7 @@ func TestAddToRepository(t *testing.T) {
func TestGetOfRepository(t *testing.T) {
labels := []*models.Label{}
err := handleAndParse(&testingRequest{
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
method: http.MethodGet,
credential: projDeveloper,
}, &labels)
@ -235,7 +235,7 @@ func TestRemoveFromRepository(t *testing.T) {
runCodeCheckingCases(t, &codeCheckingCase{
request: &testingRequest{
url: fmt.Sprintf("%s/%s/labels/%d", resourceLabelAPIBasePath,
repository, proLibraryLabelID),
repo, proLibraryLabelID),
method: http.MethodDelete,
credential: projDeveloper,
},
@ -244,7 +244,7 @@ func TestRemoveFromRepository(t *testing.T) {
labels := []*models.Label{}
err := handleAndParse(&testingRequest{
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
method: http.MethodGet,
credential: projDeveloper,
}, &labels)

View File

@ -21,11 +21,6 @@ import (
"github.com/goharbor/harbor/src/common/models"
)
var (
// Mgr is an instance of the default project Manager
Mgr = New()
)
// Manager is used for project management
// currently, the interface only defines the methods needed for tag retention
// will expand it when doing refactor

View File

@ -18,15 +18,9 @@ import (
"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/core/api"
"github.com/goharbor/harbor/src/pkg/project"
)
var (
// Mgr is an instance of the default repository Manager
Mgr = New()
)
// Manager is used for repository management
// currently, the interface only defines the methods needed for tag retention
// will expand it when doing refactor
@ -38,11 +32,17 @@ type Manager interface {
}
// New returns a default implementation of Manager
func New() Manager {
return &manager{}
func New(projectMgr project.Manager, chartCtl *chartserver.Controller) Manager {
return &manager{
projectMgr: projectMgr,
chartCtl: chartCtl,
}
}
type manager struct{}
type manager struct {
projectMgr project.Manager
chartCtl *chartserver.Controller
}
// List image repositories under the project specified by the ID
func (m *manager) ListImageRepositories(projectID int64) ([]*models.RepoRecord, error) {
@ -53,9 +53,9 @@ func (m *manager) ListImageRepositories(projectID int64) ([]*models.RepoRecord,
// List chart repositories under the project specified by the ID
func (m *manager) ListChartRepositories(projectID int64) ([]*chartserver.ChartInfo, error) {
project, err := project.Mgr.Get(projectID)
project, err := m.projectMgr.Get(projectID)
if err != nil {
return nil, err
}
return api.GetChartController().ListCharts(project.Name)
return m.chartCtl.ListCharts(project.Name)
}

View File

@ -16,25 +16,20 @@ package retention
import (
"fmt"
"github.com/goharbor/harbor/src/pkg/retention/dep"
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository"
"github.com/goharbor/harbor/src/pkg/retention/dep"
"github.com/goharbor/harbor/src/pkg/retention/policy"
"github.com/goharbor/harbor/src/pkg/retention/res"
"github.com/goharbor/harbor/src/pkg/retention/res/selectors"
"github.com/pkg/errors"
)
// TODO init the client
var (
client dep.Client
mgr Manager
)
// Launcher provides function to launch the async jobs to run retentions based on the provided policy.
type Launcher interface {
// Launch async jobs for the retention policy
@ -51,11 +46,21 @@ type Launcher interface {
}
// NewLauncher returns an instance of Launcher
func NewLauncher() Launcher {
return &launcher{}
func NewLauncher(projectMgr project.Manager, repositoryMgr repository.Manager,
retentionMgr Manager, retentionClient dep.Client) Launcher {
return &launcher{
projectMgr: projectMgr,
repositoryMgr: repositoryMgr,
retentionMgr: retentionMgr,
retentionClient: retentionClient,
}
}
type launcher struct {
retentionMgr Manager
retentionClient dep.Client
projectMgr project.Manager
repositoryMgr repository.Manager
}
type jobData struct {
@ -83,7 +88,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
var err error
if level == "system" {
// get projects
projectCandidates, err = getProjects()
projectCandidates, err = getProjects(l.projectMgr)
if err != nil {
return 0, launcherError(err)
}
@ -113,7 +118,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
var repositoryCandidates []*res.Candidate
// get repositories of projects
for _, projectCandidate := range projectCandidates {
repositories, err := getRepositories(projectCandidate.NamespaceID)
repositories, err := getRepositories(l.projectMgr, l.repositoryMgr, projectCandidate.NamespaceID)
if err != nil {
return 0, launcherError(err)
}
@ -133,17 +138,17 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
}
for _, repositoryCandidate := range repositoryCandidates {
repository := res.Repository{
reposit := res.Repository{
Namespace: repositoryCandidate.Namespace,
Name: repositoryCandidate.Repository,
Kind: repositoryCandidate.Kind,
}
if repositoryRules[repository] == nil {
repositoryRules[repository] = &lwp.Metadata{
if repositoryRules[reposit] == nil {
repositoryRules[reposit] = &lwp.Metadata{
Algorithm: ply.Algorithm,
}
}
repositoryRules[repository].Rules = append(repositoryRules[repository].Rules, &rule)
repositoryRules[reposit].Rules = append(repositoryRules[reposit].Rules, &rule)
}
}
// no tasks need to be submitted
@ -152,24 +157,24 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
}
// create task records
jobDatas := []*jobData{}
for repository, policy := range repositoryRules {
taskID, err := mgr.CreateTask(&Task{
jobDatas := make([]*jobData, 0)
for repo, p := range repositoryRules {
taskID, err := l.retentionMgr.CreateTask(&Task{
ExecutionID: executionID,
})
if err != nil {
return 0, launcherError(err)
}
jobDatas = append(jobDatas, &jobData{
repository: &repository,
policy: policy,
repository: &repo,
policy: p,
taskID: taskID,
})
}
allFailed := true
for _, jobData := range jobDatas {
_, err := client.SubmitTask(jobData.taskID, jobData.repository, jobData.policy)
_, err := l.retentionClient.SubmitTask(jobData.taskID, jobData.repository, jobData.policy)
if err != nil {
log.Error(launcherError(fmt.Errorf("failed to submit task %d: %v", jobData.taskID, err)))
continue
@ -186,34 +191,34 @@ func launcherError(err error) error {
return errors.Wrap(err, "launcher")
}
func getProjects() ([]*res.Candidate, error) {
projects, err := project.Mgr.List()
func getProjects(projectMgr project.Manager) ([]*res.Candidate, error) {
projects, err := projectMgr.List()
if err != nil {
return nil, err
}
var candidates []*res.Candidate
for _, project := range projects {
for _, pro := range projects {
candidates = append(candidates, &res.Candidate{
NamespaceID: project.ProjectID,
Namespace: project.Name,
NamespaceID: pro.ProjectID,
Namespace: pro.Name,
})
}
return candidates, nil
}
func getRepositories(projectID int64) ([]*res.Candidate, error) {
func getRepositories(projectMgr project.Manager, repositoryMgr repository.Manager, projectID int64) ([]*res.Candidate, error) {
var candidates []*res.Candidate
project, err := project.Mgr.Get(projectID)
pro, err := projectMgr.Get(projectID)
if err != nil {
return nil, err
}
// get image repositories
imageRepositories, err := repository.Mgr.ListImageRepositories(projectID)
imageRepositories, err := repositoryMgr.ListImageRepositories(projectID)
if err != nil {
return nil, err
}
for _, repository := range imageRepositories {
namespace, repo := utils.ParseRepository(repository.Name)
for _, r := range imageRepositories {
namespace, repo := utils.ParseRepository(r.Name)
candidates = append(candidates, &res.Candidate{
Namespace: namespace,
Repository: repo,
@ -221,11 +226,11 @@ func getRepositories(projectID int64) ([]*res.Candidate, error) {
})
}
// get chart repositories
chartRepositories, err := repository.Mgr.ListChartRepositories(projectID)
for _, repository := range chartRepositories {
chartRepositories, err := repositoryMgr.ListChartRepositories(projectID)
for _, r := range chartRepositories {
candidates = append(candidates, &res.Candidate{
Namespace: project.Name,
Repository: repository.Name,
Namespace: pro.Name,
Repository: r.Name,
Kind: "chart",
})
}

View File

@ -16,10 +16,12 @@ package retention
import (
"fmt"
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
"strconv"
"testing"
"github.com/goharbor/harbor/src/pkg/retention/dep"
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
"github.com/goharbor/harbor/src/chartserver"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/project"
@ -44,18 +46,18 @@ func (f *fakeProjectManager) List(...*models.ProjectQueryParam) ([]*models.Proje
func (f *fakeProjectManager) Get(idOrName interface{}) (*models.Project, error) {
id, ok := idOrName.(int64)
if ok {
for _, project := range f.projects {
if project.ProjectID == id {
return project, nil
for _, pro := range f.projects {
if pro.ProjectID == id {
return pro, nil
}
}
return nil, nil
}
name, ok := idOrName.(string)
if ok {
for _, project := range f.projects {
if project.Name == name {
return project, nil
for _, pro := range f.projects {
if pro.Name == name {
return pro, nil
}
}
return nil, nil
@ -138,6 +140,10 @@ func (f *fakeRetentionManager) ListHistories(executionID int64, query *q.Query)
type launchTestSuite struct {
suite.Suite
projectMgr project.Manager
repositoryMgr repository.Manager
retentionMgr Manager
retentionClient dep.Client
}
func (l *launchTestSuite) SetupTest() {
@ -145,11 +151,11 @@ func (l *launchTestSuite) SetupTest() {
ProjectID: 1,
Name: "library",
}
project.Mgr = &fakeProjectManager{
l.projectMgr = &fakeProjectManager{
projects: []*models.Project{
pro,
}}
repository.Mgr = &fakeRepositoryManager{
l.repositoryMgr = &fakeRepositoryManager{
imageRepositories: []*models.RepoRecord{
{
Name: "library/image",
@ -161,12 +167,12 @@ func (l *launchTestSuite) SetupTest() {
},
},
}
client = &fakeClient{}
mgr = &fakeRetentionManager{}
l.retentionMgr = &fakeRetentionManager{}
l.retentionClient = &fakeClient{}
}
func (l *launchTestSuite) TestGetProjects() {
projects, err := getProjects()
projects, err := getProjects(l.projectMgr)
require.Nil(l.T(), err)
assert.Equal(l.T(), 1, len(projects))
assert.Equal(l.T(), int64(1), projects[0].NamespaceID)
@ -174,7 +180,7 @@ func (l *launchTestSuite) TestGetProjects() {
}
func (l *launchTestSuite) TestGetRepositories() {
repositories, err := getRepositories(1)
repositories, err := getRepositories(l.projectMgr, l.repositoryMgr, 1)
require.Nil(l.T(), err)
assert.Equal(l.T(), 2, len(repositories))
assert.Equal(l.T(), "library", repositories[0].Namespace)
@ -186,7 +192,7 @@ func (l *launchTestSuite) TestGetRepositories() {
}
func (l *launchTestSuite) TestLaunch() {
launcher := NewLauncher()
launcher := NewLauncher(l.projectMgr, l.repositoryMgr, l.retentionMgr, l.retentionClient)
var ply *policy.Metadata
// nil policy
n, err := launcher.Launch(ply, 1)