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 5c53eb8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/harbor/harbor22x.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ 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))
}
fmt.Println(policy)
f, _ := json.Marshal(policy)
fmt.Println(string(f))
if policy != nil {
err := h.ClientV5.NewRetentionPolicy(ctx, retention)
if err != nil {
f, _ := json.Marshal(err)
h.Log.Info(fmt.Sprintf("Error creating retention policy %s: %v", project.Name, string(f)))
}
} else {
policy.Rules = retention.Rules
policy.Trigger = retention.Trigger
err = h.ClientV5.UpdateRetentionPolicy(ctx, retention)
if err != nil {
f, _ := json.Marshal(err)
h.Log.Info(fmt.Sprintf("Error updating retention policy %s: %v", project.Name, string(f)))
}
}
}

if h.WebhookAddition {
wps, err := h.ClientV5.ListProjectWebhookPolicies(ctx, int(project.ProjectID))
if err != nil {
Expand Down Expand Up @@ -278,6 +306,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 +329,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 +365,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
67 changes: 67 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,69 @@ 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{
Algorithm: "or",
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 5c53eb8

Please sign in to comment.