@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"log"
6
7
"os"
7
8
"strings"
@@ -15,6 +16,15 @@ import (
15
16
)
16
17
17
18
func RunTrivyImageScans (config * rest.Config , js nats.JetStreamContext ) error {
19
+
20
+ pvcMountPath := "/mnt/agent/kbz"
21
+ trivyImageCacheDir := fmt .Sprintf ("%s/trivy-imagecache" , pvcMountPath )
22
+ err := os .MkdirAll (trivyImageCacheDir , 0755 )
23
+ if err != nil {
24
+ log .Printf ("Error creating Trivy Image cache directory: %v\n " , err )
25
+ return err
26
+ }
27
+
18
28
clearCacheCmd := "trivy image --clear-cache"
19
29
20
30
images , err := ListImages (config )
@@ -25,7 +35,8 @@ func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext) error {
25
35
26
36
for _ , image := range images {
27
37
var report types.Report
28
- out , err := executeCommand ("trivy image " + image .PullableImage + " --timeout 60m -f json -q --cache-dir /tmp/.cache" )
38
+ scanCmd := fmt .Sprintf ("trivy image %s --timeout 60m -f json -q --cache-dir %s" , image .PullableImage , trivyImageCacheDir )
39
+ out , err := executeCommand (scanCmd )
29
40
if err != nil {
30
41
log .Printf ("Error scanning image %s: %v" , image .PullableImage , err )
31
42
continue // Move on to the next image in case of an error
@@ -74,12 +85,3 @@ func publishImageScanReports(report types.Report, js nats.JetStreamContext) erro
74
85
log .Printf ("Trivy image report with ID:%s has been published\n " , metrics .ID )
75
86
return nil
76
87
}
77
-
78
- func cleanupCache (cacheDir string ) {
79
- err := os .RemoveAll (cacheDir )
80
- if err != nil {
81
- log .Printf ("Failed to clean up cache directory %s: %v" , cacheDir , err )
82
- } else {
83
- log .Printf ("Cache directory %s cleaned up successfully" , cacheDir )
84
- }
85
- }
0 commit comments