Skip to content

Commit

Permalink
Merge pull request #198 from intelops/avinash
Browse files Browse the repository at this point in the history
Fix wait group negative counter issue
  • Loading branch information
avinashkna4 authored Sep 1, 2023
2 parents 8439ec1 + dccb1ab commit 51d2f63
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
8 changes: 4 additions & 4 deletions agent/kubviz/k8smetrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ var (
)

func runTrivyScans(config *rest.Config, js nats.JetStreamContext, wg *sync.WaitGroup, trivyImagescanChan, trivySbomcanChan, trivyK8sMetricsChan chan error) {
RunTrivyImageScans(config, js, wg, trivyImagescanChan)
RunTrivySbomScan(config, js, wg, trivySbomcanChan)
RunTrivyK8sClusterScan(wg, js, trivyK8sMetricsChan)
RunTrivyK8sClusterScan(js, trivyK8sMetricsChan)
RunTrivyImageScans(config, js, trivyImagescanChan)
RunTrivySbomScan(config, js, trivySbomcanChan)
wg.Done()
}

Expand Down Expand Up @@ -159,7 +159,7 @@ func main() {
}
}
}()
wg.Add(6) // Initialize the WaitGroup for the seven goroutines
wg.Add(7) // Initialize the WaitGroup for the seven goroutines
// ... start other goroutines ...
go outDatedImages(config, js, &wg, outdatedErrChan)
go KubePreUpgradeDetector(config, js, &wg, kubePreUpgradeChan)
Expand Down
22 changes: 11 additions & 11 deletions agent/kubviz/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package main

import (
"encoding/json"
"log"
"strings"

"github.com/aquasecurity/trivy/pkg/k8s/report"
"github.com/google/uuid"
"github.com/intelops/kubviz/constants"
"github.com/intelops/kubviz/model"
"github.com/nats-io/nats.go"
"log"
"strings"
"sync"
)

func RunTrivyK8sClusterScan(wg *sync.WaitGroup, js nats.JetStreamContext, errCh chan error) {
defer wg.Done()
func RunTrivyK8sClusterScan(js nats.JetStreamContext, errCh chan error) {
var report report.ConsolidatedReport
out, err := executeCommand("trivy k8s --report summary cluster --timeout 60m -f json -q --cache-dir /tmp/.cache")
log.Println("Commnd for k8s cluster scan: trivy k8s --report summary cluster --timeout 60m -f json -q --cache-dir /tmp/.cache")
parts := strings.SplitN(out, "{", 2)
if len(parts) <= 1 {
log.Println("No output from command", err)
log.Println("No output from k8s cluster scan command", err)
errCh <- err
return
}
log.Println("Command logs", parts[0])
log.Println("Command logs for k8s cluster scan", parts[0])
jsonPart := "{" + parts[1]
log.Println("First 200 lines output", jsonPart[:200])
log.Println("Last 200 lines output", jsonPart[len(jsonPart)-200:])
log.Println("First 200 k8s cluster scan lines output", jsonPart[:200])
log.Println("Last 200 k8s cluster scan lines output", jsonPart[len(jsonPart)-200:])
err = json.Unmarshal([]byte(jsonPart), &report)
if err != nil {
log.Printf("Error occurred while Unmarshalling json: %v", err)
log.Printf("Error occurred while Unmarshalling json for k8s cluster scan: %v", err)
errCh <- err
}
publishTrivyK8sReport(report, js, errCh)
Expand All @@ -45,6 +45,6 @@ func publishTrivyK8sReport(report report.ConsolidatedReport, js nats.JetStreamCo
if err != nil {
errCh <- err
}
log.Printf("Trivy report with ID:%s has been published\n", metrics.ID)
log.Printf("Trivy k8s cluster report with ID:%s has been published\n", metrics.ID)
errCh <- nil
}
16 changes: 7 additions & 9 deletions agent/kubviz/trivy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"log"
"strings"
"sync"

"github.com/aquasecurity/trivy/pkg/types"
"github.com/google/uuid"
Expand All @@ -14,8 +13,7 @@ import (
"k8s.io/client-go/rest"
)

func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext, wg *sync.WaitGroup, errCh chan error) {
defer wg.Done()
func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext, errCh chan error) {
images, err := ListImages(config)
if err != nil {
log.Fatal(err)
Expand All @@ -31,18 +29,18 @@ func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext, wg *sync.

parts := strings.SplitN(out, "{", 2)
if len(parts) <= 1 {
log.Println("No output from command", err)
log.Println("No output from image scan command", err)
continue // Move on to the next image if there's no output
}

log.Println("Command logs", parts[0])
log.Println("Command logs for image", parts[0])
jsonPart := "{" + parts[1]
log.Println("First 200 lines output", jsonPart[:200])
log.Println("Last 200 lines output", jsonPart[len(jsonPart)-200:])
log.Println("First 200 image scan lines output", jsonPart[:200])
log.Println("Last 200 image scan lines output", jsonPart[len(jsonPart)-200:])

err = json.Unmarshal([]byte(jsonPart), &report)
if err != nil {
log.Printf("Error occurred while Unmarshalling json: %v", err)
log.Printf("Error occurred while Unmarshalling json for image: %v", err)
continue // Move on to the next image in case of an error
}
publishImageScanReports(report, js, errCh)
Expand All @@ -62,6 +60,6 @@ func publishImageScanReports(report types.Report, js nats.JetStreamContext, errC
if err != nil {
errCh <- err
}
log.Printf("Trivy report with ID:%s has been published\n", metrics.ID)
log.Printf("Trivy image report with ID:%s has been published\n", metrics.ID)
errCh <- nil
}
12 changes: 5 additions & 7 deletions agent/kubviz/trivy_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log"
"os/exec"
"sync"

"github.com/google/uuid"
"github.com/intelops/kubviz/constants"
Expand Down Expand Up @@ -39,15 +38,14 @@ func executeCommandSbom(command string) ([]byte, error) {
err := cmd.Run()

if err != nil {
log.Println("Execute Command Error", err.Error())
log.Println("Execute SBOM Command Error", err.Error())
}

return outc.Bytes(), err
}

func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext, wg *sync.WaitGroup, errCh chan error) {
func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext, errCh chan error) {
log.Println("trivy sbom run started")
defer wg.Done()
images, err := ListImages(config)

if err != nil {
Expand All @@ -60,20 +58,20 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext, wg *sync.Wa
out, err := executeCommandSbom(command)

if err != nil {
log.Printf("Error executing Trivy for image %s: %v", image.PullableImage, err)
log.Printf("Error executing Trivy for image sbom %s: %v", image.PullableImage, err)
continue // Move on to the next image in case of an error
}

// Check if the output is empty or invalid JSON
if len(out) == 0 {
log.Printf("Trivy output is empty for image %s", image.PullableImage)
log.Printf("Trivy output is empty for image sbom %s", image.PullableImage)
continue // Move on to the next image
}

var report model.Sbom
err = json.Unmarshal(out, &report)
if err != nil {
log.Printf("Error unmarshaling JSON data for image %s: %v", image.PullableImage, err)
log.Printf("Error unmarshaling JSON data for image sbom %s: %v", image.PullableImage, err)
continue // Move on to the next image in case of an error
}
log.Println("report", report)
Expand Down

0 comments on commit 51d2f63

Please sign in to comment.