Skip to content

Commit

Permalink
feat: enable retention policies
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Dec 16, 2023
1 parent 22597d2 commit d278cc1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
45 changes: 45 additions & 0 deletions internal/harbor/harbor22x.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ func (h *Harbor) CreateProjectV2(ctx context.Context, projectName string) (*harb
// fmt.Println(x)
// }

retentionPolicyEnable := true
if retentionPolicyEnable {
// always set the retention policy to what it should be
retention := h.generateRetentionPolicy(int64(project.ProjectID), "", 5, 1)
policy, err := h.ClientV5.GetRetentionPolicyByProject(ctx, projectName)
if err != nil {
h.Log.Info(fmt.Sprintf("Error creating retention policy %s: %v", project.Name, err))
}
if policy.Rules == nil {
err := h.ClientV5.NewRetentionPolicy(ctx, retention)
if err != nil {
h.Log.Info(fmt.Sprintf("Error creating retention policy %s: %v", project.Name, err))
}
} else {
err = h.ClientV5.UpdateRetentionPolicy(ctx, retention)
if err != nil {
h.Log.Info(fmt.Sprintf("Error updating retention policy %s: %v", project.Name, err))
}
}
}

if h.WebhookAddition {
wps, err := h.ClientV5.ListProjectWebhookPolicies(ctx, int(project.ProjectID))
if err != nil {
Expand Down Expand Up @@ -278,6 +299,14 @@ func (h *Harbor) DeleteRepository(ctx context.Context, projectName, branch strin
if err != nil {
h.Log.Info(fmt.Sprintf("Error deleting harbor repository %s", repo.Name))
}
h.Log.Info(
fmt.Sprintf(
"Deleted harbor repository %s in project %s, environment %s",
repo.Name,
projectName,
environmentName,
),
)
}
}
if len(listRepositories) > 100 {
Expand All @@ -293,6 +322,14 @@ func (h *Harbor) DeleteRepository(ctx context.Context, projectName, branch strin
if err != nil {
h.Log.Info(fmt.Sprintf("Error deleting harbor repository %s", repo.Name))
}
h.Log.Info(
fmt.Sprintf(
"Deleted harbor repository %s in project %s, environment %s",
repo.Name,
projectName,
environmentName,
),
)
}
}
}
Expand Down Expand Up @@ -321,6 +358,14 @@ func (h *Harbor) DeleteRobotAccount(ctx context.Context, projectName, branch str
h.Log.Info(fmt.Sprintf("Error deleting project %s robot account %s", projectName, robot.Name))
return
}
h.Log.Info(
fmt.Sprintf(
"Deleted harbor robot account %s in project %s, environment %s",
robot.Name,
projectName,
environmentName,
),
)
}
}
}
Expand Down
66 changes: 66 additions & 0 deletions internal/harbor/harbor_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"time"

harborclientv5model "github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/uselagoon/remote-controller/internal/helpers"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -204,3 +205,68 @@ func (h *Harbor) UpsertHarborSecret(ctx context.Context, cl client.Client, ns, n
}
return false, nil
}

func (h *Harbor) generateRetentionPolicy(projectID int64, schedule string, branchRetention, prRetention int) *harborclientv5model.RetentionPolicy {
return &harborclientv5model.RetentionPolicy{
Rules: []*harborclientv5model.RetentionRule{
{ // create a retention policy for all images
Action: "retain",
Params: map[string]interface{}{
"latestPulledN": branchRetention,
},
ScopeSelectors: map[string][]harborclientv5model.RetentionSelector{
"repository": {
{
Decoration: "repoMatches",
Kind: "doublestar",
Pattern: "[^pr\\-]*/*", // exclude pullrequest repository images https://github.com/bmatcuk/doublestar#patterns
},
},
},
TagSelectors: []*harborclientv5model.RetentionSelector{
{
Decoration: "matches",
Extras: "{\"untagged\":true}",
Kind: "doublestar",
Pattern: "**",
},
},
Template: "latestPulledN",
},
{ // create a retention policy specifically for pullrequests
Action: "retain",
Params: map[string]interface{}{
"latestPulledN": prRetention,
},
ScopeSelectors: map[string][]harborclientv5model.RetentionSelector{
"repository": {
{
Decoration: "repoMatches",
Kind: "doublestar",
Pattern: "pr-*",
},
},
},
TagSelectors: []*harborclientv5model.RetentionSelector{
{
Decoration: "matches",
Extras: "{\"untagged\":true}",
Kind: "doublestar",
Pattern: "**",
},
},
Template: "latestPulledN",
},
},
Scope: &harborclientv5model.RetentionPolicyScope{
Level: "project",
Ref: projectID,
},
Trigger: &harborclientv5model.RetentionRuleTrigger{
Kind: "schedule",
Settings: map[string]string{
"cron": schedule,
},
},
}
}

0 comments on commit d278cc1

Please sign in to comment.