mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-17 07:45:24 +01:00
Merge pull request #12541 from stonezdj/20200720_tag_ret_proxy
Add default retention policy
This commit is contained in:
commit
112e38a080
@ -17,6 +17,7 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -56,6 +57,7 @@ type ProjectAPI struct {
|
||||
const projectNameMaxLen int = 255
|
||||
const projectNameMinLen int = 1
|
||||
const restrictedNameChars = `[a-z0-9]+(?:[._-][a-z0-9]+)*`
|
||||
const defaultDaysToRetention = 7
|
||||
|
||||
// Prepare validates the URL and the user
|
||||
func (p *ProjectAPI) Prepare() {
|
||||
@ -241,6 +243,14 @@ func (p *ProjectAPI) Post() {
|
||||
}
|
||||
}
|
||||
|
||||
// create a default retention policy for proxy project
|
||||
if pro.RegistryID > 0 {
|
||||
if err := p.addRetentionPolicyForProxy(projectID); err != nil {
|
||||
p.SendInternalServerError(fmt.Errorf("failed to add tag retention policy for project: %v", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// fire event
|
||||
evt.BuildAndPublish(&metadata.CreateProjectEventMetadata{
|
||||
ProjectID: projectID,
|
||||
@ -251,6 +261,18 @@ func (p *ProjectAPI) Post() {
|
||||
p.Redirect(http.StatusCreated, strconv.FormatInt(projectID, 10))
|
||||
}
|
||||
|
||||
func (p *ProjectAPI) addRetentionPolicyForProxy(projID int64) error {
|
||||
plc := policy.WithNDaysSinceLastPull(projID, defaultDaysToRetention)
|
||||
retID, err := retentionController.CreateRetention(plc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ProjectMgr.GetMetadataManager().Add(projID, map[string]string{"retention_id": strconv.FormatInt(retID, 10)}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Head ...
|
||||
func (p *ProjectAPI) Head() {
|
||||
|
||||
|
@ -16,6 +16,7 @@ package policy
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/goharbor/harbor/src/lib/selector/selectors/doublestar"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule/index"
|
||||
)
|
||||
@ -112,3 +113,47 @@ type Scope struct {
|
||||
// 0 for 'system', project ID for 'project' and repo ID for 'repository'
|
||||
Reference int64 `json:"ref" valid:"Required"`
|
||||
}
|
||||
|
||||
// WithNDaysSinceLastPull build a retention rule to keep images n days to since last pull
|
||||
func WithNDaysSinceLastPull(projID int64, n int) *Metadata {
|
||||
return &Metadata{
|
||||
Algorithm: "or",
|
||||
Rules: []rule.Metadata{
|
||||
{
|
||||
ID: 1,
|
||||
Priority: 1,
|
||||
Action: "retain",
|
||||
Template: "nDaysSinceLastPull",
|
||||
TagSelectors: []*rule.Selector{
|
||||
{
|
||||
Kind: doublestar.Kind,
|
||||
Decoration: doublestar.Matches,
|
||||
Pattern: "**",
|
||||
},
|
||||
},
|
||||
ScopeSelectors: map[string][]*rule.Selector{
|
||||
"repository": {
|
||||
{
|
||||
Kind: doublestar.Kind,
|
||||
Decoration: doublestar.RepoMatches,
|
||||
Pattern: "**",
|
||||
},
|
||||
},
|
||||
},
|
||||
Parameters: rule.Parameters{
|
||||
"nDaysSinceLastPull": n,
|
||||
},
|
||||
},
|
||||
},
|
||||
Trigger: &Trigger{
|
||||
Kind: "Schedule",
|
||||
Settings: map[string]interface{}{
|
||||
"cron": "0 0 0 * * *",
|
||||
},
|
||||
},
|
||||
Scope: &Scope{
|
||||
Level: "project",
|
||||
Reference: projID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user