Skip to content

Commit 2132357

Browse files
authored
Merge pull request #277 from intelops/minorchanges
added cache clearing command for trivy image, trivy sbom, and trivy k8s
2 parents 7289008 + e564391 commit 2132357

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

agent/kubviz/trivy.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,12 @@ func executeCommandTrivy(command string) ([]byte, error) {
3131
func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
3232
var report report.ConsolidatedReport
3333
cmdString := "trivy k8s --report summary cluster --exclude-nodes kubernetes.io/arch:amd64 --timeout 60m -f json --cache-dir /tmp/.cache --debug"
34-
35-
// Log the command before execution
36-
log.Printf("Executing command: %s\n", cmdString)
37-
38-
// Execute the command
34+
clearCacheCmd := "trivy k8s --clear-cache"
3935
out, err := executeCommandTrivy(cmdString)
40-
41-
// Handle errors and process the command output as needed
4236
if err != nil {
4337
log.Printf("Error executing command: %v\n", err)
38+
return err
4439
}
45-
// Log the command output for debugging purposes
4640
log.Printf("Command output: %s\n", out)
4741
outStr := string(out)
4842
parts := strings.SplitN(outStr, "{", 2)
@@ -59,11 +53,15 @@ func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
5953
log.Printf("Error occurred while Unmarshalling json for k8s cluster scan: %v", err)
6054
return err
6155
}
56+
_, err = executeCommandTrivy(clearCacheCmd)
57+
if err != nil {
58+
log.Printf("Error executing command: %v\n", err)
59+
return err
60+
}
6261
err = publishTrivyK8sReport(report, js)
6362
if err != nil {
6463
return err
6564
}
66-
cleanupCache("/tmp/.cache")
6765
return nil
6866
}
6967

agent/kubviz/trivy_image.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import (
1515
)
1616

1717
func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext) error {
18+
clearCacheCmd := "trivy image --clear-cache"
19+
1820
images, err := ListImages(config)
1921
if err != nil {
20-
log.Fatal(err)
22+
log.Println("error occured while trying to list images, error :", err.Error())
23+
return err
2124
}
2225

2326
for _, image := range images {
@@ -44,11 +47,15 @@ func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext) error {
4447
log.Printf("Error occurred while Unmarshalling json for image: %v", err)
4548
continue // Move on to the next image in case of an error
4649
}
50+
_, err = executeCommandTrivy(clearCacheCmd)
51+
if err != nil {
52+
log.Printf("Error executing command: %v\n", err)
53+
return err
54+
}
4755
err = publishImageScanReports(report, js)
4856
if err != nil {
4957
return err
5058
}
51-
cleanupCache("/tmp/.cache")
5259
}
5360
return nil
5461
}

agent/kubviz/trivy_sbom.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func executeCommandSbom(command string) ([]byte, error) {
4646
}
4747

4848
func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {
49+
clearCacheCmd := "trivy image --clear-cache"
50+
4951
log.Println("trivy sbom run started")
5052
images, err := ListImages(config)
5153

@@ -75,10 +77,13 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {
7577
continue // Move on to the next image in case of an error
7678
}
7779
// log.Println("report", report)
78-
80+
_, err = executeCommandTrivy(clearCacheCmd)
81+
if err != nil {
82+
log.Printf("Error executing command: %v\n", err)
83+
return err
84+
}
7985
// Publish the report using the given function
8086
publishTrivySbomReport(report, js)
81-
cleanupCache("/tmp/.cache")
8287
}
8388
return nil
8489
}

0 commit comments

Comments
 (0)