Skip to content

Commit

Permalink
Don't reconcile if trivy-metadata file yet not uploaded (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <arnob@appscode.com>
  • Loading branch information
ArnobKumarSaha authored Oct 9, 2023
1 parent c991a65 commit 23c381b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (c completedConfig) New(ctx context.Context) (*ScannerServer, error) {
c.ExtraConfig.TrivyImage,
c.ExtraConfig.TrivyDBCacherImage,
c.ExtraConfig.FileServerAddr,
c.ExtraConfig.FileServerFilesDir,
c.ExtraConfig.ScanRequestTTLPeriod,
c.ExtraConfig.Workspace,
)).SetupWithManager(mgr); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/controllers/scanreport/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type ImageScanReportReconciler struct {
}

func (r *ImageScanReportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if !fileserver.MetadataFileExists(r.FileServerDir) {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}
log := log.FromContext(ctx)

var isrp api.ImageScanReport
Expand Down
6 changes: 1 addition & 5 deletions pkg/controllers/scanrequest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ func EnsureScanReport(kc client.Client, imageRef string, resp trivy.BackendRespo
klog.Infof("%v ImageScanReport has been created\n", obj.GetName())
}

_, err = cu.PatchStatus(context.TODO(), kc, &api.ImageScanReport{
ObjectMeta: metav1.ObjectMeta{
Name: api.GetReportName(img.Name),
},
}, func(obj client.Object) client.Object {
_, err = cu.PatchStatus(context.TODO(), kc, obj, func(obj client.Object) client.Object {
rep := obj.(*api.ImageScanReport)
rep.Status.Version = resp.TrivyVersion
rep.Status.Report = resp.Report
Expand Down
11 changes: 9 additions & 2 deletions pkg/controllers/scanrequest/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
api "kubeops.dev/scanner/apis/scanner/v1alpha1"
"kubeops.dev/scanner/apis/trivy"
"kubeops.dev/scanner/pkg/backend"
"kubeops.dev/scanner/pkg/fileserver"

"github.com/nats-io/nats.go"
batch "k8s.io/api/batch/v1"
Expand All @@ -44,6 +45,7 @@ type Reconciler struct {
trivyImage string
trivyDBCacherImage string
fileServerAddr string
fileServerDir string
scanRequestTTLPeriod time.Duration
workspace string
}
Expand All @@ -57,7 +59,7 @@ type RequestReconciler struct {
func NewImageScanRequestReconciler(
kc client.Client,
nc *nats.Conn,
scannedImage, trivyImage, trivyDBCacherImage, fsAddr string,
scannedImage, trivyImage, trivyDBCacherImage, fsAddr, fileServerDir string,
garbageCol time.Duration,
workspace string,
) *Reconciler {
Expand All @@ -68,12 +70,17 @@ func NewImageScanRequestReconciler(
trivyImage: trivyImage,
trivyDBCacherImage: trivyDBCacherImage,
fileServerAddr: fsAddr,
fileServerDir: fileServerDir,
scanRequestTTLPeriod: garbageCol,
workspace: workspace,
}
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if !fileserver.MetadataFileExists(r.fileServerDir) {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}

log := log.FromContext(ctx)

log.Info("Reconciling for ", "req", req)
Expand Down Expand Up @@ -140,7 +147,7 @@ func (r *RequestReconciler) freshScanRequired() (bool, error) {
if err != nil {
return true, client.IgnoreNotFound(err)
}
if isrp.Status.Phase == api.ImageScanReportPhaseOutdated {
if isrp.Status.Phase == api.ImageScanReportPhaseOutdated || isrp.Status.Phase == "" {
return true, nil
}
return false, r.updateStatusAsReportAlreadyExists(&isrp)
Expand Down
6 changes: 6 additions & 0 deletions pkg/fileserver/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ func VulnerabilityDBLastUpdatedAt(fsDir string) (*trivy.Time, error) {
}
return &ver.UpdatedAt, nil
}

func MetadataFileExists(fsDir string) bool {
fileName := filepath.Join(fsDir, "trivy", "metadata.json")
_, err := os.Stat(fileName)
return !os.IsNotExist(err)
}

0 comments on commit 23c381b

Please sign in to comment.