mirror of
https://github.com/goharbor/harbor
synced 2025-04-27 22:38:14 +00:00
Merge pull request #8256 from goharbor/feat/retention/GH-6661-retain-newer-than-n-days
Retention: Implement Evaluator: Retain if created less than x days ago
This commit is contained in:
commit
0b2f94b0dd
@ -15,6 +15,8 @@
|
|||||||
package lastx
|
package lastx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/policy/action"
|
"github.com/goharbor/harbor/src/pkg/retention/policy/action"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
||||||
@ -37,9 +39,15 @@ type evaluator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process the candidates based on the rule definition
|
// Process the candidates based on the rule definition
|
||||||
func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) {
|
func (e *evaluator) Process(artifacts []*res.Candidate) (retain []*res.Candidate, err error) {
|
||||||
// TODO: REPLACE SAMPLE CODE WITH REAL IMPLEMENTATION
|
cutoff := time.Now().Add(time.Duration(e.x*-24) * time.Hour)
|
||||||
return artifacts, nil
|
for _, a := range artifacts {
|
||||||
|
if time.Unix(a.PushedTime, 0).UTC().After(cutoff) {
|
||||||
|
retain = append(retain, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specify what action is performed to the candidates processed by this evaluator
|
// Specify what action is performed to the candidates processed by this evaluator
|
||||||
@ -51,7 +59,7 @@ func (e *evaluator) Action() string {
|
|||||||
func New(params rule.Parameters) rule.Evaluator {
|
func New(params rule.Parameters) rule.Evaluator {
|
||||||
if params != nil {
|
if params != nil {
|
||||||
if param, ok := params[ParameterX]; ok {
|
if param, ok := params[ParameterX]; ok {
|
||||||
if v, ok := param.(int); ok {
|
if v, ok := param.(int); ok && v >= 0 {
|
||||||
return &evaluator{
|
return &evaluator{
|
||||||
x: v,
|
x: v,
|
||||||
}
|
}
|
||||||
|
78
src/pkg/retention/policy/rule/lastx/evaluator_test.go
Normal file
78
src/pkg/retention/policy/rule/lastx/evaluator_test.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package lastx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
||||||
|
"github.com/goharbor/harbor/src/pkg/retention/res"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EvaluatorTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EvaluatorTestSuite) TestNew() {
|
||||||
|
tests := []struct {
|
||||||
|
Name string
|
||||||
|
args rule.Parameters
|
||||||
|
expectedX int
|
||||||
|
}{
|
||||||
|
{Name: "Valid", args: map[string]rule.Parameter{ParameterX: 3}, expectedX: 3},
|
||||||
|
{Name: "Default If Negative", args: map[string]rule.Parameter{ParameterX: -3}, expectedX: DefaultX},
|
||||||
|
{Name: "Default If Not Set", args: map[string]rule.Parameter{}, expectedX: DefaultX},
|
||||||
|
{Name: "Default If Wrong Type", args: map[string]rule.Parameter{}, expectedX: DefaultX},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
e.T().Run(tt.Name, func(t *testing.T) {
|
||||||
|
e := New(tt.args).(*evaluator)
|
||||||
|
|
||||||
|
require.Equal(t, tt.expectedX, e.x)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EvaluatorTestSuite) TestProcess() {
|
||||||
|
now := time.Now().UTC()
|
||||||
|
data := []*res.Candidate{
|
||||||
|
{PushedTime: now.Add(time.Duration(1*-24) * time.Hour).Unix()},
|
||||||
|
{PushedTime: now.Add(time.Duration(2*-24) * time.Hour).Unix()},
|
||||||
|
{PushedTime: now.Add(time.Duration(3*-24) * time.Hour).Unix()},
|
||||||
|
{PushedTime: now.Add(time.Duration(4*-24) * time.Hour).Unix()},
|
||||||
|
{PushedTime: now.Add(time.Duration(5*-24) * time.Hour).Unix()},
|
||||||
|
{PushedTime: now.Add(time.Duration(99*-24) * time.Hour).Unix()},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
days int
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{days: 0, expected: 0},
|
||||||
|
{days: 1, expected: 0},
|
||||||
|
{days: 2, expected: 1},
|
||||||
|
{days: 3, expected: 2},
|
||||||
|
{days: 4, expected: 3},
|
||||||
|
{days: 5, expected: 4},
|
||||||
|
{days: 6, expected: 5},
|
||||||
|
{days: 7, expected: 5},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
e.T().Run(fmt.Sprintf("%d days - should keep %d", tt.days, tt.expected), func(t *testing.T) {
|
||||||
|
e := New(rule.Parameters{ParameterX: tt.days})
|
||||||
|
|
||||||
|
result, err := e.Process(data)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, result, tt.expected)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEvaluatorSuite(t *testing.T) {
|
||||||
|
suite.Run(t, &EvaluatorTestSuite{})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user