mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-20 15:48:26 +01:00
rebase and fix name conflicts among packages and vars
Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
commit
deb021eb48
@ -15,9 +15,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"errors"
|
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
"github.com/goharbor/harbor/src/common/api"
|
"github.com/goharbor/harbor/src/common/api"
|
||||||
"github.com/goharbor/harbor/src/common/security"
|
"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/config"
|
||||||
"github.com/goharbor/harbor/src/core/filter"
|
"github.com/goharbor/harbor/src/core/filter"
|
||||||
"github.com/goharbor/harbor/src/core/promgr"
|
"github.com/goharbor/harbor/src/core/promgr"
|
||||||
|
"github.com/goharbor/harbor/src/pkg/project"
|
||||||
|
"github.com/goharbor/harbor/src/pkg/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
yamlFileContentType = "application/x-yaml"
|
yamlFileContentType = "application/x-yaml"
|
||||||
|
// ReplicationJobType ...
|
||||||
|
ReplicationJobType = "replication"
|
||||||
|
// ScanJobType ...
|
||||||
|
ScanJobType = "scan"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
projectMgr project.Manager
|
||||||
|
repositoryMgr repository.Manager
|
||||||
)
|
)
|
||||||
|
|
||||||
// BaseController ...
|
// BaseController ...
|
||||||
@ -41,13 +52,6 @@ type BaseController struct {
|
|||||||
ProjectMgr promgr.ProjectManager
|
ProjectMgr promgr.ProjectManager
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
// ReplicationJobType ...
|
|
||||||
ReplicationJobType = "replication"
|
|
||||||
// ScanJobType ...
|
|
||||||
ScanJobType = "scan"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Prepare inits security context and project manager from request
|
// Prepare inits security context and project manager from request
|
||||||
// context
|
// context
|
||||||
func (b *BaseController) Prepare() {
|
func (b *BaseController) Prepare() {
|
||||||
@ -91,6 +95,22 @@ func (b *BaseController) WriteYamlData(object interface{}) {
|
|||||||
// Init related objects/configurations for the API controllers
|
// Init related objects/configurations for the API controllers
|
||||||
func Init() error {
|
func Init() error {
|
||||||
registerHealthCheckers()
|
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 chart repository is not enabled then directly return
|
||||||
if !config.WithChartMuseum() {
|
if !config.WithChartMuseum() {
|
||||||
return nil
|
return nil
|
||||||
@ -102,6 +122,13 @@ func Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chartController = chartCtl
|
chartController = chartCtl
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initProjectManager() {
|
||||||
|
projectMgr = project.New()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initRepositoryManager() {
|
||||||
|
repositoryMgr = repository.New(projectMgr, chartController)
|
||||||
|
}
|
||||||
|
@ -28,7 +28,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
resourceLabelAPIBasePath = "/api/repositories"
|
resourceLabelAPIBasePath = "/api/repositories"
|
||||||
repository = "library/hello-world"
|
repo = "library/hello-world"
|
||||||
tag = "latest"
|
tag = "latest"
|
||||||
proLibraryLabelID int64
|
proLibraryLabelID int64
|
||||||
)
|
)
|
||||||
@ -63,7 +63,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
},
|
},
|
||||||
code: http.StatusUnauthorized,
|
code: http.StatusUnauthorized,
|
||||||
@ -72,13 +72,13 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
credential: projGuest,
|
credential: projGuest,
|
||||||
},
|
},
|
||||||
code: http.StatusForbidden,
|
code: http.StatusForbidden,
|
||||||
},
|
},
|
||||||
// 404 repository doesn't exist
|
// 404 repo doesn't exist
|
||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/library/non-exist-repo/tags/%s/labels", resourceLabelAPIBasePath, tag),
|
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
|
// 404 image doesn't exist
|
||||||
{
|
{
|
||||||
request: &testingRequest{
|
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,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
},
|
},
|
||||||
@ -99,7 +99,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
// 400
|
// 400
|
||||||
{
|
{
|
||||||
request: &testingRequest{
|
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,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
},
|
},
|
||||||
@ -109,7 +109,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
bodyJSON: struct {
|
bodyJSON: struct {
|
||||||
@ -124,7 +124,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
bodyJSON: struct {
|
bodyJSON: struct {
|
||||||
@ -139,7 +139,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
bodyJSON: struct {
|
bodyJSON: struct {
|
||||||
@ -154,7 +154,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
{
|
{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
bodyJSON: struct {
|
bodyJSON: struct {
|
||||||
@ -172,7 +172,7 @@ func TestAddToImage(t *testing.T) {
|
|||||||
func TestGetOfImage(t *testing.T) {
|
func TestGetOfImage(t *testing.T) {
|
||||||
labels := []*models.Label{}
|
labels := []*models.Label{}
|
||||||
err := handleAndParse(&testingRequest{
|
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,
|
method: http.MethodGet,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
}, &labels)
|
}, &labels)
|
||||||
@ -185,7 +185,7 @@ func TestRemoveFromImage(t *testing.T) {
|
|||||||
runCodeCheckingCases(t, &codeCheckingCase{
|
runCodeCheckingCases(t, &codeCheckingCase{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels/%d", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels/%d", resourceLabelAPIBasePath,
|
||||||
repository, tag, proLibraryLabelID),
|
repo, tag, proLibraryLabelID),
|
||||||
method: http.MethodDelete,
|
method: http.MethodDelete,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
},
|
},
|
||||||
@ -195,7 +195,7 @@ func TestRemoveFromImage(t *testing.T) {
|
|||||||
labels := []*models.Label{}
|
labels := []*models.Label{}
|
||||||
err := handleAndParse(&testingRequest{
|
err := handleAndParse(&testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/tags/%s/labels", resourceLabelAPIBasePath,
|
||||||
repository, tag),
|
repo, tag),
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
}, &labels)
|
}, &labels)
|
||||||
@ -206,7 +206,7 @@ func TestRemoveFromImage(t *testing.T) {
|
|||||||
func TestAddToRepository(t *testing.T) {
|
func TestAddToRepository(t *testing.T) {
|
||||||
runCodeCheckingCases(t, &codeCheckingCase{
|
runCodeCheckingCases(t, &codeCheckingCase{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
|
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
bodyJSON: struct {
|
bodyJSON: struct {
|
||||||
ID int64
|
ID int64
|
||||||
@ -222,7 +222,7 @@ func TestAddToRepository(t *testing.T) {
|
|||||||
func TestGetOfRepository(t *testing.T) {
|
func TestGetOfRepository(t *testing.T) {
|
||||||
labels := []*models.Label{}
|
labels := []*models.Label{}
|
||||||
err := handleAndParse(&testingRequest{
|
err := handleAndParse(&testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
|
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
}, &labels)
|
}, &labels)
|
||||||
@ -235,7 +235,7 @@ func TestRemoveFromRepository(t *testing.T) {
|
|||||||
runCodeCheckingCases(t, &codeCheckingCase{
|
runCodeCheckingCases(t, &codeCheckingCase{
|
||||||
request: &testingRequest{
|
request: &testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/labels/%d", resourceLabelAPIBasePath,
|
url: fmt.Sprintf("%s/%s/labels/%d", resourceLabelAPIBasePath,
|
||||||
repository, proLibraryLabelID),
|
repo, proLibraryLabelID),
|
||||||
method: http.MethodDelete,
|
method: http.MethodDelete,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
},
|
},
|
||||||
@ -244,7 +244,7 @@ func TestRemoveFromRepository(t *testing.T) {
|
|||||||
|
|
||||||
labels := []*models.Label{}
|
labels := []*models.Label{}
|
||||||
err := handleAndParse(&testingRequest{
|
err := handleAndParse(&testingRequest{
|
||||||
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repository),
|
url: fmt.Sprintf("%s/%s/labels", resourceLabelAPIBasePath, repo),
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
credential: projDeveloper,
|
credential: projDeveloper,
|
||||||
}, &labels)
|
}, &labels)
|
||||||
|
@ -21,11 +21,6 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/common/models"
|
"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
|
// Manager is used for project management
|
||||||
// currently, the interface only defines the methods needed for tag retention
|
// currently, the interface only defines the methods needed for tag retention
|
||||||
// will expand it when doing refactor
|
// will expand it when doing refactor
|
||||||
|
@ -18,15 +18,9 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/chartserver"
|
"github.com/goharbor/harbor/src/chartserver"
|
||||||
"github.com/goharbor/harbor/src/common/dao"
|
"github.com/goharbor/harbor/src/common/dao"
|
||||||
"github.com/goharbor/harbor/src/common/models"
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
"github.com/goharbor/harbor/src/core/api"
|
|
||||||
"github.com/goharbor/harbor/src/pkg/project"
|
"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
|
// Manager is used for repository management
|
||||||
// currently, the interface only defines the methods needed for tag retention
|
// currently, the interface only defines the methods needed for tag retention
|
||||||
// will expand it when doing refactor
|
// will expand it when doing refactor
|
||||||
@ -38,11 +32,17 @@ type Manager interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New returns a default implementation of Manager
|
// New returns a default implementation of Manager
|
||||||
func New() Manager {
|
func New(projectMgr project.Manager, chartCtl *chartserver.Controller) Manager {
|
||||||
return &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
|
// List image repositories under the project specified by the ID
|
||||||
func (m *manager) ListImageRepositories(projectID int64) ([]*models.RepoRecord, error) {
|
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
|
// List chart repositories under the project specified by the ID
|
||||||
func (m *manager) ListChartRepositories(projectID int64) ([]*chartserver.ChartInfo, error) {
|
func (m *manager) ListChartRepositories(projectID int64) ([]*chartserver.ChartInfo, error) {
|
||||||
project, err := project.Mgr.Get(projectID)
|
project, err := m.projectMgr.Get(projectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return api.GetChartController().ListCharts(project.Name)
|
return m.chartCtl.ListCharts(project.Name)
|
||||||
}
|
}
|
||||||
|
@ -16,25 +16,20 @@ package retention
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/dep"
|
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
|
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/utils"
|
"github.com/goharbor/harbor/src/common/utils"
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
"github.com/goharbor/harbor/src/pkg/project"
|
"github.com/goharbor/harbor/src/pkg/project"
|
||||||
"github.com/goharbor/harbor/src/pkg/repository"
|
"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/policy"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/res"
|
"github.com/goharbor/harbor/src/pkg/retention/res"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/res/selectors"
|
"github.com/goharbor/harbor/src/pkg/retention/res/selectors"
|
||||||
"github.com/pkg/errors"
|
"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.
|
// Launcher provides function to launch the async jobs to run retentions based on the provided policy.
|
||||||
type Launcher interface {
|
type Launcher interface {
|
||||||
// Launch async jobs for the retention policy
|
// Launch async jobs for the retention policy
|
||||||
@ -51,11 +46,21 @@ type Launcher interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewLauncher returns an instance of Launcher
|
// NewLauncher returns an instance of Launcher
|
||||||
func NewLauncher() Launcher {
|
func NewLauncher(projectMgr project.Manager, repositoryMgr repository.Manager,
|
||||||
return &launcher{}
|
retentionMgr Manager, retentionClient dep.Client) Launcher {
|
||||||
|
return &launcher{
|
||||||
|
projectMgr: projectMgr,
|
||||||
|
repositoryMgr: repositoryMgr,
|
||||||
|
retentionMgr: retentionMgr,
|
||||||
|
retentionClient: retentionClient,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type launcher struct {
|
type launcher struct {
|
||||||
|
retentionMgr Manager
|
||||||
|
retentionClient dep.Client
|
||||||
|
projectMgr project.Manager
|
||||||
|
repositoryMgr repository.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
type jobData struct {
|
type jobData struct {
|
||||||
@ -83,7 +88,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
|
|||||||
var err error
|
var err error
|
||||||
if level == "system" {
|
if level == "system" {
|
||||||
// get projects
|
// get projects
|
||||||
projectCandidates, err = getProjects()
|
projectCandidates, err = getProjects(l.projectMgr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, launcherError(err)
|
return 0, launcherError(err)
|
||||||
}
|
}
|
||||||
@ -113,7 +118,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
|
|||||||
var repositoryCandidates []*res.Candidate
|
var repositoryCandidates []*res.Candidate
|
||||||
// get repositories of projects
|
// get repositories of projects
|
||||||
for _, projectCandidate := range projectCandidates {
|
for _, projectCandidate := range projectCandidates {
|
||||||
repositories, err := getRepositories(projectCandidate.NamespaceID)
|
repositories, err := getRepositories(l.projectMgr, l.repositoryMgr, projectCandidate.NamespaceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, launcherError(err)
|
return 0, launcherError(err)
|
||||||
}
|
}
|
||||||
@ -133,17 +138,17 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, repositoryCandidate := range repositoryCandidates {
|
for _, repositoryCandidate := range repositoryCandidates {
|
||||||
repository := res.Repository{
|
reposit := res.Repository{
|
||||||
Namespace: repositoryCandidate.Namespace,
|
Namespace: repositoryCandidate.Namespace,
|
||||||
Name: repositoryCandidate.Repository,
|
Name: repositoryCandidate.Repository,
|
||||||
Kind: repositoryCandidate.Kind,
|
Kind: repositoryCandidate.Kind,
|
||||||
}
|
}
|
||||||
if repositoryRules[repository] == nil {
|
if repositoryRules[reposit] == nil {
|
||||||
repositoryRules[repository] = &lwp.Metadata{
|
repositoryRules[reposit] = &lwp.Metadata{
|
||||||
Algorithm: ply.Algorithm,
|
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
|
// no tasks need to be submitted
|
||||||
@ -152,24 +157,24 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64) (int64, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create task records
|
// create task records
|
||||||
jobDatas := []*jobData{}
|
jobDatas := make([]*jobData, 0)
|
||||||
for repository, policy := range repositoryRules {
|
for repo, p := range repositoryRules {
|
||||||
taskID, err := mgr.CreateTask(&Task{
|
taskID, err := l.retentionMgr.CreateTask(&Task{
|
||||||
ExecutionID: executionID,
|
ExecutionID: executionID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, launcherError(err)
|
return 0, launcherError(err)
|
||||||
}
|
}
|
||||||
jobDatas = append(jobDatas, &jobData{
|
jobDatas = append(jobDatas, &jobData{
|
||||||
repository: &repository,
|
repository: &repo,
|
||||||
policy: policy,
|
policy: p,
|
||||||
taskID: taskID,
|
taskID: taskID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
allFailed := true
|
allFailed := true
|
||||||
for _, jobData := range jobDatas {
|
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 {
|
if err != nil {
|
||||||
log.Error(launcherError(fmt.Errorf("failed to submit task %d: %v", jobData.taskID, err)))
|
log.Error(launcherError(fmt.Errorf("failed to submit task %d: %v", jobData.taskID, err)))
|
||||||
continue
|
continue
|
||||||
@ -186,34 +191,34 @@ func launcherError(err error) error {
|
|||||||
return errors.Wrap(err, "launcher")
|
return errors.Wrap(err, "launcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProjects() ([]*res.Candidate, error) {
|
func getProjects(projectMgr project.Manager) ([]*res.Candidate, error) {
|
||||||
projects, err := project.Mgr.List()
|
projects, err := projectMgr.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var candidates []*res.Candidate
|
var candidates []*res.Candidate
|
||||||
for _, project := range projects {
|
for _, pro := range projects {
|
||||||
candidates = append(candidates, &res.Candidate{
|
candidates = append(candidates, &res.Candidate{
|
||||||
NamespaceID: project.ProjectID,
|
NamespaceID: pro.ProjectID,
|
||||||
Namespace: project.Name,
|
Namespace: pro.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return candidates, nil
|
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
|
var candidates []*res.Candidate
|
||||||
project, err := project.Mgr.Get(projectID)
|
pro, err := projectMgr.Get(projectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// get image repositories
|
// get image repositories
|
||||||
imageRepositories, err := repository.Mgr.ListImageRepositories(projectID)
|
imageRepositories, err := repositoryMgr.ListImageRepositories(projectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, repository := range imageRepositories {
|
for _, r := range imageRepositories {
|
||||||
namespace, repo := utils.ParseRepository(repository.Name)
|
namespace, repo := utils.ParseRepository(r.Name)
|
||||||
candidates = append(candidates, &res.Candidate{
|
candidates = append(candidates, &res.Candidate{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Repository: repo,
|
Repository: repo,
|
||||||
@ -221,11 +226,11 @@ func getRepositories(projectID int64) ([]*res.Candidate, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// get chart repositories
|
// get chart repositories
|
||||||
chartRepositories, err := repository.Mgr.ListChartRepositories(projectID)
|
chartRepositories, err := repositoryMgr.ListChartRepositories(projectID)
|
||||||
for _, repository := range chartRepositories {
|
for _, r := range chartRepositories {
|
||||||
candidates = append(candidates, &res.Candidate{
|
candidates = append(candidates, &res.Candidate{
|
||||||
Namespace: project.Name,
|
Namespace: pro.Name,
|
||||||
Repository: repository.Name,
|
Repository: r.Name,
|
||||||
Kind: "chart",
|
Kind: "chart",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,12 @@ package retention
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"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/chartserver"
|
||||||
"github.com/goharbor/harbor/src/common/models"
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
"github.com/goharbor/harbor/src/pkg/project"
|
"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) {
|
func (f *fakeProjectManager) Get(idOrName interface{}) (*models.Project, error) {
|
||||||
id, ok := idOrName.(int64)
|
id, ok := idOrName.(int64)
|
||||||
if ok {
|
if ok {
|
||||||
for _, project := range f.projects {
|
for _, pro := range f.projects {
|
||||||
if project.ProjectID == id {
|
if pro.ProjectID == id {
|
||||||
return project, nil
|
return pro, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
name, ok := idOrName.(string)
|
name, ok := idOrName.(string)
|
||||||
if ok {
|
if ok {
|
||||||
for _, project := range f.projects {
|
for _, pro := range f.projects {
|
||||||
if project.Name == name {
|
if pro.Name == name {
|
||||||
return project, nil
|
return pro, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -138,6 +140,10 @@ func (f *fakeRetentionManager) ListHistories(executionID int64, query *q.Query)
|
|||||||
|
|
||||||
type launchTestSuite struct {
|
type launchTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
|
projectMgr project.Manager
|
||||||
|
repositoryMgr repository.Manager
|
||||||
|
retentionMgr Manager
|
||||||
|
retentionClient dep.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *launchTestSuite) SetupTest() {
|
func (l *launchTestSuite) SetupTest() {
|
||||||
@ -145,11 +151,11 @@ func (l *launchTestSuite) SetupTest() {
|
|||||||
ProjectID: 1,
|
ProjectID: 1,
|
||||||
Name: "library",
|
Name: "library",
|
||||||
}
|
}
|
||||||
project.Mgr = &fakeProjectManager{
|
l.projectMgr = &fakeProjectManager{
|
||||||
projects: []*models.Project{
|
projects: []*models.Project{
|
||||||
pro,
|
pro,
|
||||||
}}
|
}}
|
||||||
repository.Mgr = &fakeRepositoryManager{
|
l.repositoryMgr = &fakeRepositoryManager{
|
||||||
imageRepositories: []*models.RepoRecord{
|
imageRepositories: []*models.RepoRecord{
|
||||||
{
|
{
|
||||||
Name: "library/image",
|
Name: "library/image",
|
||||||
@ -161,12 +167,12 @@ func (l *launchTestSuite) SetupTest() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
client = &fakeClient{}
|
l.retentionMgr = &fakeRetentionManager{}
|
||||||
mgr = &fakeRetentionManager{}
|
l.retentionClient = &fakeClient{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *launchTestSuite) TestGetProjects() {
|
func (l *launchTestSuite) TestGetProjects() {
|
||||||
projects, err := getProjects()
|
projects, err := getProjects(l.projectMgr)
|
||||||
require.Nil(l.T(), err)
|
require.Nil(l.T(), err)
|
||||||
assert.Equal(l.T(), 1, len(projects))
|
assert.Equal(l.T(), 1, len(projects))
|
||||||
assert.Equal(l.T(), int64(1), projects[0].NamespaceID)
|
assert.Equal(l.T(), int64(1), projects[0].NamespaceID)
|
||||||
@ -174,7 +180,7 @@ func (l *launchTestSuite) TestGetProjects() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *launchTestSuite) TestGetRepositories() {
|
func (l *launchTestSuite) TestGetRepositories() {
|
||||||
repositories, err := getRepositories(1)
|
repositories, err := getRepositories(l.projectMgr, l.repositoryMgr, 1)
|
||||||
require.Nil(l.T(), err)
|
require.Nil(l.T(), err)
|
||||||
assert.Equal(l.T(), 2, len(repositories))
|
assert.Equal(l.T(), 2, len(repositories))
|
||||||
assert.Equal(l.T(), "library", repositories[0].Namespace)
|
assert.Equal(l.T(), "library", repositories[0].Namespace)
|
||||||
@ -186,7 +192,7 @@ func (l *launchTestSuite) TestGetRepositories() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *launchTestSuite) TestLaunch() {
|
func (l *launchTestSuite) TestLaunch() {
|
||||||
launcher := NewLauncher()
|
launcher := NewLauncher(l.projectMgr, l.repositoryMgr, l.retentionMgr, l.retentionClient)
|
||||||
var ply *policy.Metadata
|
var ply *policy.Metadata
|
||||||
// nil policy
|
// nil policy
|
||||||
n, err := launcher.Launch(ply, 1)
|
n, err := launcher.Launch(ply, 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user