Skip to content

Commit

Permalink
trivy
Browse files Browse the repository at this point in the history
  • Loading branch information
an1l4 committed Feb 29, 2024
1 parent 0676feb commit 974da43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions agent/kubviz/k8smetrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/intelops/kubviz/agent/config"
"github.com/intelops/kubviz/agent/kubviz/plugins/events"

//"github.com/intelops/kubviz/agent/kubviz/plugins/ketall"
//"github.com/intelops/kubviz/agent/kubviz/plugins/kubepreupgrade"

Expand Down Expand Up @@ -146,8 +147,8 @@ func main() {
events.LogErr(err)
// err = trivy.RunTrivyImageScans(config, js)
// events.LogErr(err)
// err = trivy.RunTrivyK8sClusterScan(js)
// events.LogErr(err)
err = trivy.RunTrivyK8sClusterScan(js)
events.LogErr(err)
// err = kubescore.RunKubeScore(clientset, js)
// events.LogErr(err)
}
Expand Down
32 changes: 17 additions & 15 deletions agent/kubviz/plugins/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package trivy

import (
"bytes"
"context"
// "context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,21 +14,22 @@ import (
"github.com/google/uuid"
"github.com/intelops/kubviz/constants"
"github.com/intelops/kubviz/model"
"github.com/intelops/kubviz/pkg/opentelemetry"

// "github.com/intelops/kubviz/pkg/opentelemetry"
"github.com/nats-io/nats.go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
// "go.opentelemetry.io/otel"
// "go.opentelemetry.io/otel/attribute"
)

var ClusterName string = os.Getenv("CLUSTER_NAME")

func executeCommandTrivy(command string) ([]byte, error) {

ctx := context.Background()
tracer := otel.Tracer("trivy-cluster")
_, span := tracer.Start(opentelemetry.BuildContext(ctx), "executeCommandTrivy")
span.SetAttributes(attribute.String("trivy-k8s-agent", "command-running"))
defer span.End()
// ctx := context.Background()
// tracer := otel.Tracer("trivy-cluster")
// _, span := tracer.Start(opentelemetry.BuildContext(ctx), "executeCommandTrivy")
// span.SetAttributes(attribute.String("trivy-k8s-agent", "command-running"))
// defer span.End()

cmd := exec.Command("/bin/sh", "-c", command)
var outc, errc bytes.Buffer
Expand All @@ -53,13 +54,13 @@ func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
}
var report report.ConsolidatedReport

ctx := context.Background()
tracer := otel.Tracer("trivy-cluster")
_, span := tracer.Start(opentelemetry.BuildContext(ctx), "RunTrivyK8sClusterScan")
span.SetAttributes(attribute.String("cluster-name", report.ClusterName))
defer span.End()
// ctx := context.Background()
// tracer := otel.Tracer("trivy-cluster")
// _, span := tracer.Start(opentelemetry.BuildContext(ctx), "RunTrivyK8sClusterScan")
// span.SetAttributes(attribute.String("cluster-name", report.ClusterName))
// defer span.End()

cmdString := fmt.Sprintf("trivy k8s --report summary cluster --exclude-nodes kubernetes.io/arch:amd64 --timeout 60m -f json --cache-dir %s --debug", trivyCacheDir)
cmdString := fmt.Sprintf("trivy k8s --report=summary cluster --exclude-nodes kubernetes.io/arch:amd64 --timeout 60m -f json --cache-dir %s --debug", trivyCacheDir)
// clearCacheCmd := "trivy k8s --clear-cache"
out, err := executeCommandTrivy(cmdString)
if err != nil {
Expand Down Expand Up @@ -106,5 +107,6 @@ func PublishTrivyK8sReport(report report.ConsolidatedReport, js nats.JetStreamCo
return err
}
log.Printf("Trivy k8s cluster report with ID:%s has been published\n", metrics.ID)
log.Printf("Trivy k8s cluster report with ID:%s has been published\n", metrics.Report)
return nil
}

0 comments on commit 974da43

Please sign in to comment.