Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always set RequeueAfter when reconciling CIS #198

Merged
merged 4 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package config
import (
"regexp"
"time"

stasv1alpha1 "github.com/statnett/image-scanner-operator/api/stas/v1alpha1"
)

type Config struct {
Expand All @@ -18,11 +16,3 @@ type Config struct {
ScanWorkloadResources []string `mapstructure:"scan-workload-resources"`
TrivyImage string `mapstructure:"trivy-image"`
}

func (c Config) TimeUntilNextScan(cis *stasv1alpha1.ContainerImageScan) time.Duration {
if cis.Status.ObservedGeneration != cis.Generation || cis.Status.LastScanTime.IsZero() {
return 0
}

return time.Until(cis.Status.LastScanTime.Add(c.ScanInterval))
}
15 changes: 10 additions & 5 deletions internal/controller/stas/containerimagescan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stas
import (
"context"
"fmt"
"time"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -49,12 +50,16 @@ func (r *ContainerImageScanReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, staserrors.Ignore(err, apierrors.IsNotFound)
}

timeUntilNextScan := r.TimeUntilNextScan(cis)
if timeUntilNextScan > 0 {
return ctrl.Result{RequeueAfter: timeUntilNextScan}, nil
timeUntilNextScan := r.ScanInterval

if !cis.Status.LastScanTime.IsZero() {
d := time.Until(cis.Status.LastScanTime.Add(r.ScanInterval))
if d > 0 {
timeUntilNextScan = d
}
}

return ctrl.Result{}, r.reconcile(ctx, cis)
return ctrl.Result{RequeueAfter: timeUntilNextScan}, r.reconcile(ctx, cis)
}

return controller.Reconcile(ctx, fn)
Expand All @@ -74,7 +79,7 @@ func (r *ContainerImageScanReconciler) SetupWithManager(mgr ctrl.Manager) error
return ctrl.NewControllerManagedBy(mgr).
For(&stasv1alpha1.ContainerImageScan{},
builder.WithPredicates(
predicate.GenerationChangedPredicate{},
predicate.Or(predicate.GenerationChangedPredicate{}, cisRescanDue(r.ScanInterval)),
ignoreDeletionPredicate(),
)).
WithEventFilter(predicate.And(predicates...)).
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/stas/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stas

import (
"regexp"
"time"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -47,6 +48,20 @@ func podContainerStatusImagesChanged() predicate.Predicate {
}
}

func cisRescanDue(scanInterval time.Duration) predicate.Predicate {
return predicate.NewPredicateFuncs(func(object client.Object) bool {
cis := object.(*stasv1alpha1.ContainerImageScan)
lastScanTime := cis.Status.LastScanTime
if lastScanTime.IsZero() {
return true
}
if time.Since(lastScanTime.Time) > scanInterval {
return true
}
return false
})
}

func ignoreCreationPredicate() predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
Expand Down