Skip to content

Commit

Permalink
refactor: use ttl check with date util (aquasecurity#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-keinan authored Mar 10, 2022
1 parent 0a82ac2 commit 74bd63d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 46 deletions.
23 changes: 5 additions & 18 deletions pkg/operator/controller/ttl_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"time"

"github.com/aquasecurity/starboard/pkg/ext"
"github.com/aquasecurity/starboard/pkg/utils"

"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/operator/etc"
"github.com/aquasecurity/starboard/pkg/operator/predicate"
Expand All @@ -20,6 +23,7 @@ type TTLReportReconciler struct {
logr.Logger
etc.Config
client.Client
ext.Clock
}

func (r *TTLReportReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -63,11 +67,7 @@ func (r *TTLReportReconciler) reconcileReport() reconcile.Func {
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed parsing %v with value %v %w", v1alpha1.TTLReportAnnotation, ttlReportAnnotationStr, err)
}
creationTime := report.Report.UpdateTimestamp
ttlExpired, durationToTTLExpiration, err := ttlIsExpired(reportTTLTime, creationTime.Time)
if err != nil {
return ctrl.Result{}, err
}
ttlExpired, durationToTTLExpiration := utils.IsTTLExpired(reportTTLTime, report.Report.UpdateTimestamp.Time, r.Clock)
if ttlExpired {
log.V(1).Info("Removing vulnerabilityReport with expired TTL")
err := r.Client.Delete(ctx, report, &client.DeleteOptions{})
Expand All @@ -81,16 +81,3 @@ func (r *TTLReportReconciler) reconcileReport() reconcile.Func {
return ctrl.Result{RequeueAfter: durationToTTLExpiration}, nil
}
}

func ttlIsExpired(reportTTL time.Duration, creationTime time.Time) (bool, time.Duration, error) {
expiresAt := creationTime.Add(reportTTL)
currentTime := time.Now()
isExpired := currentTime.After(expiresAt)

if isExpired {
return true, time.Duration(0), nil
}

expiresIn := expiresAt.Sub(currentTime)
return false, expiresIn, nil
}
28 changes: 0 additions & 28 deletions pkg/operator/controller/ttl_report_test.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func Start(ctx context.Context, buildInfo starboard.BuildInfo, operatorConfig et
Logger: ctrl.Log.WithName("reconciler").WithName("ttlreport"),
Config: operatorConfig,
Client: mgr.GetClient(),
Clock: ext.NewSystemClock(),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup TTLreport reconciler: %w", err)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/dateutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ func DurationExceeded(duration time.Duration) bool {
func timeToExpiration(expiresAt time.Time, clock ext.Clock) time.Duration {
return expiresAt.Sub(clock.Now())
}

// IsTTLExpired check whether current time has exceeded creation time + ttl duration
func IsTTLExpired(ttl time.Duration, creationTime time.Time, clock ext.Clock) (bool, time.Duration) {
durationToTTLExpiration := timeToExpiration(creationTime.Add(ttl), clock)
return DurationExceeded(durationToTTLExpiration), durationToTTLExpiration
}
19 changes: 19 additions & 0 deletions pkg/utils/dateutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ func TestDurationExceeded(t *testing.T) {
})
}
}

func TestTTLIsNotExpired(t *testing.T) {
ttlReportAnnotationStr := "10h"
ttlReportTime, _ := time.ParseDuration(ttlReportAnnotationStr)
creationTime := time.Now()
ttlExpired, duration := IsTTLExpired(ttlReportTime, creationTime, ext.NewSystemClock())
assert.True(t, duration > 0)
assert.False(t, ttlExpired)
}

func TestTTLIsExpired(t *testing.T) {
ttlReportAnnotationStr := "10s"
ttlReportTime, _ := time.ParseDuration(ttlReportAnnotationStr)
creationTime := time.Now()
then := creationTime.Add(time.Duration(-10) * time.Minute)
ttlExpired, duration := IsTTLExpired(ttlReportTime, then, ext.NewSystemClock())
assert.True(t, duration <= 0)
assert.True(t, ttlExpired)
}

0 comments on commit 74bd63d

Please sign in to comment.