Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trivy-test #329

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ alloc.svg
allocs.pprof
cpu.pprof
steps-to-test.txt
Dockerfile-grphqlserver-build
27 changes: 20 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
FROM golang:1.19 AS builder
WORKDIR /
COPY ./ ./
# Build the manager binary
FROM golang:1.20 as builder

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o ./build/graphqlserver graphqlserver/server.go
WORKDIR /workspace
# Copy the Go Modules manifests
COPY ./ ./
RUN go mod download
RUN gofmt -w -r '"github.com/googleapis/gnostic/OpenAPIv2" -> "github.com/googleapis/gnostic/openapiv2"' /go/pkg/mod/sigs.k8s.io/kustomize/
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o kubviz_agent agent/kubviz/*.go

FROM scratch
COPY --from=builder ./build/graphqlserver server
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM golang:alpine
WORKDIR /
COPY --from=builder /workspace/kubviz_agent .
COPY --from=zegl/kube-score:v1.16.0 /usr/bin/kube-score /usr/bin/kube-score
COPY --from=bitnami/kubectl:1.22.5 /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/
COPY --from=busybox:1.35.0-uclibc /bin/sh /bin/sh
COPY --from=aquasec/trivy:latest /usr/local/bin/trivy /usr/local/bin/trivy

USER 65532:65532
ENTRYPOINT ["/server"]

ENTRYPOINT ["/kubviz_agent"]
2 changes: 1 addition & 1 deletion agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type AgentConfigurations struct {
RakkessInterval string `envconfig:"RAKKESS_INTERVAL" default:"*/50 * * * *"`
KubePreUpgradeInterval string `envconfig:"KUBEPREUPGRADE_INTERVAL" default:"*/60 * * * *"`
TrivyInterval string `envconfig:"TRIVY_INTERVAL" default:"*/10 * * * *"`
SchedulerEnable bool `envconfig:"SCHEDULER_ENABLE" default:"true"`
SchedulerEnable bool `envconfig:"SCHEDULER_ENABLE" default:"false"`
KuberHealthyEnable bool `envconfig:"KUBERHEALTHY_ENABLE" default:"true"`
}

Expand Down
156 changes: 80 additions & 76 deletions agent/kubviz/k8smetrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ package main
import (
"log"
"os"
"os/signal"
"syscall"

// "os/signal"
// "syscall"
"time"

"github.com/go-co-op/gocron"
// "github.com/go-co-op/gocron"
"github.com/nats-io/nats.go"

"context"
// "context"

"github.com/intelops/kubviz/pkg/mtlsnats"
"github.com/intelops/kubviz/pkg/opentelemetry"
// "github.com/intelops/kubviz/pkg/opentelemetry"

"k8s.io/client-go/kubernetes"
// "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"github.com/intelops/kubviz/agent/config"
// "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"

"github.com/intelops/kubviz/agent/kubviz/plugins/kuberhealthy"
"github.com/intelops/kubviz/agent/kubviz/plugins/kubescore"
"github.com/intelops/kubviz/agent/kubviz/plugins/outdated"
"github.com/intelops/kubviz/agent/kubviz/plugins/rakkess"
// "github.com/intelops/kubviz/agent/kubviz/plugins/ketall"
// "github.com/intelops/kubviz/agent/kubviz/plugins/kubepreupgrade"

// "github.com/intelops/kubviz/agent/kubviz/plugins/kuberhealthy"
// "github.com/intelops/kubviz/agent/kubviz/plugins/kubescore"
// "github.com/intelops/kubviz/agent/kubviz/plugins/outdated"
// "github.com/intelops/kubviz/agent/kubviz/plugins/rakkess"

"github.com/intelops/kubviz/agent/kubviz/plugins/trivy"
"github.com/intelops/kubviz/agent/kubviz/scheduler"
// "github.com/intelops/kubviz/agent/kubviz/scheduler"

_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand Down Expand Up @@ -62,16 +64,17 @@ var (
)

func main() {
log.Println("trivy image test code")
log.SetFlags(log.LstdFlags | log.Lshortfile)
env := Production
clusterMetricsChan := make(chan error, 1)
cfg, err := config.GetAgentConfigurations()
if err != nil {
log.Fatal("Failed to retrieve agent configurations", err)
}
// clusterMetricsChan := make(chan error, 1)
// cfg, err := config.GetAgentConfigurations()
// if err != nil {
// log.Fatal("Failed to retrieve agent configurations", err)
// }
var (
config *rest.Config
clientset *kubernetes.Clientset
config *rest.Config
//clientset *kubernetes.Clientset
)

var mtlsConfig mtlsnats.MtlsConfig
Expand All @@ -97,8 +100,8 @@ func main() {
}

if nc == nil {
nc, err = nats.Connect(natsurl, nats.Name("K8s Metrics"), nats.Token(token))
events.CheckErr(err)
nc, _ = nats.Connect(natsurl, nats.Name("K8s Metrics"), nats.Token(token))
// events.CheckErr(err)
}
js, err := nc.JetStream()
events.CheckErr(err)
Expand All @@ -109,73 +112,74 @@ func main() {
if err != nil {
log.Fatal(err)
}
clientset = events.GetK8sClient(config)
// clientset = events.GetK8sClient(config)
} else {
config, err = rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
clientset = events.GetK8sClient(config)
// clientset = events.GetK8sClient(config)
}

tp, err := opentelemetry.InitTracer()
if err != nil {
log.Fatal(err)
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()

go events.PublishMetrics(clientset, js, clusterMetricsChan)
if cfg.KuberHealthyEnable {
go kuberhealthy.StartKuberHealthy(js)
}
// tp, err := opentelemetry.InitTracer()
// if err != nil {
// // log.Fatal(err)
// log.Println(err)
// }
// defer func() {
// if err := tp.Shutdown(context.Background()); err != nil {
// log.Printf("Error shutting down tracer provider: %v", err)
// }
// }()

// go events.PublishMetrics(clientset, js, clusterMetricsChan)
// if cfg.KuberHealthyEnable {
// go kuberhealthy.StartKuberHealthy(js)
// }
go server.StartServer()
collectAndPublishMetrics := func() {
err := outdated.OutDatedImages(config, js)
events.LogErr(err)
err = kubepreupgrade.KubePreUpgradeDetector(config, js)
events.LogErr(err)
err = ketall.GetAllResources(config, js)
events.LogErr(err)
err = rakkess.RakeesOutput(config, js)
events.LogErr(err)
err = trivy.RunTrivySbomScan(config, js)
events.LogErr(err)
err = trivy.RunTrivyImageScans(config, js)
events.LogErr(err)
// err := outdated.OutDatedImages(config, js)
// events.LogErr(err)
// err = kubepreupgrade.KubePreUpgradeDetector(config, js)
// events.LogErr(err)
// err = ketall.GetAllResources(config, js)
// events.LogErr(err)
// err = rakkess.RakeesOutput(config, js)
// events.LogErr(err)
// err = trivy.RunTrivyImageScans(config, js)
// events.LogErr(err)
err = trivy.RunTrivyK8sClusterScan(js)
events.LogErr(err)
err = kubescore.RunKubeScore(clientset, js)
err = trivy.RunTrivySbomScan(config, js)
events.LogErr(err)
// err = kubescore.RunKubeScore(clientset, js)
// events.LogErr(err)
}

collectAndPublishMetrics()

if cfg.SchedulerEnable { // Assuming "cfg.Schedule" is a boolean indicating whether to schedule or not.
scheduler := scheduler.InitScheduler(config, js, *cfg, clientset)

// Start the scheduler
scheduler.Start()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals

scheduler.Stop()
} else {
if schedulingIntervalStr == "" {
schedulingIntervalStr = "20m"
}
schedulingInterval, err := time.ParseDuration(schedulingIntervalStr)
if err != nil {
log.Fatalf("Failed to parse SCHEDULING_INTERVAL: %v", err)
}
s := gocron.NewScheduler(time.UTC)
s.Every(schedulingInterval).Do(func() {
collectAndPublishMetrics()
})
s.StartBlocking()
}
// if cfg.SchedulerEnable { // Assuming "cfg.Schedule" is a boolean indicating whether to schedule or not.
// scheduler := scheduler.InitScheduler(config, js, *cfg, clientset)

// // Start the scheduler
// scheduler.Start()
// signals := make(chan os.Signal, 1)
// signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
// <-signals

// scheduler.Stop()
// } else {
// if schedulingIntervalStr == "" {
// schedulingIntervalStr = "20m"
// }
// schedulingInterval, err := time.ParseDuration(schedulingIntervalStr)
// if err != nil {
// log.Fatalf("Failed to parse SCHEDULING_INTERVAL: %v", err)
// }
// s := gocron.NewScheduler(time.UTC)
// s.Every(schedulingInterval).Do(func() {
// collectAndPublishMetrics()
// })
// s.StartBlocking()
// }
}
Loading
Loading