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

refactor: use slog #326

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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
10 changes: 3 additions & 7 deletions examples/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ import (
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
tk "github.com/aquasecurity/trivy-kubernetes/pkg/trivyk8s"

"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"

"context"
)

func main() {

logger, _ := zap.NewProduction()
defer logger.Sync()

ctx := context.Background()

cluster, err := k8s.GetCluster(k8s.WithBurst(100), k8s.WithQPS(100))
Expand All @@ -31,7 +26,7 @@ func main() {

fmt.Println("Current namespace:", cluster.GetCurrentNamespace())

trivyk8s := tk.New(cluster, logger.Sugar(), tk.WithExcludeOwned(true))
trivyk8s := tk.New(cluster, tk.WithExcludeOwned(true))
fmt.Println("Scanning cluster")

//trivy k8s #cluster
Expand Down Expand Up @@ -109,7 +104,8 @@ func main() {
ar, err := trivyk8s.ListArtifactAndNodeInfo(ctx, []tk.NodeCollectorOption{
tk.WithScanJobNamespace("trivy-temp"),
tk.WithIgnoreLabels(map[string]string{"chen": "test"}),
tk.WithTolerations(tolerations)}...)
tk.WithTolerations(tolerations),
}...)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/google/go-containerregistry v0.19.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
Expand Down Expand Up @@ -61,7 +60,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca h1:VdD38733bfYv5tUZwEIskMM93VanwNIi5bIKnDrJdEY=
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
45 changes: 34 additions & 11 deletions pkg/trivyk8s/trivyk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"slices"
"strings"
"time"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/aquasecurity/trivy-kubernetes/pkg/bom"
"github.com/aquasecurity/trivy-kubernetes/pkg/jobs"
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -49,7 +49,6 @@ type client struct {
namespace string
resources []string
allNamespaces bool
logger *zap.SugaredLogger
excludeOwned bool
scanJobParams scanJobParams
nodeConfig bool // feature flag to enable/disable node config collection
Expand Down Expand Up @@ -97,8 +96,10 @@ func WithIncludeNamespaces(includeNamespaces []string) K8sOption {
}

// New creates a trivyK8S client
func New(cluster k8s.Cluster, logger *zap.SugaredLogger, opts ...K8sOption) TrivyK8S {
c := &client{cluster: cluster, logger: logger}
func New(cluster k8s.Cluster, opts ...K8sOption) TrivyK8S {
c := &client{
cluster: cluster,
}
for _, opt := range opts {
opt(c)
}
Expand Down Expand Up @@ -173,7 +174,7 @@ func (c *client) ListArtifacts(ctx context.Context) ([]*artifacts.Artifact, erro
lerr := fmt.Errorf("failed listing resources for gvr: %v - %w", gvr, err)

if errors.IsNotFound(err) {
c.logger.Error(lerr)
slog.Error("Unable to list resources", "error", lerr)
// if a resource is not found, we log and continue
continue
}
Expand Down Expand Up @@ -347,7 +348,11 @@ func (c *client) ListArtifactAndNodeInfo(ctx context.Context,
if err != nil {
return nil, err
}
artifactList = append(artifactList, &artifacts.Artifact{Kind: "NodeInfo", Name: resource.Name, RawResource: nodeInfo})
artifactList = append(artifactList, &artifacts.Artifact{
Kind: "NodeInfo",
Name: resource.Name,
RawResource: nodeInfo,
})
}
return artifactList, err
}
Expand Down Expand Up @@ -383,20 +388,38 @@ func BomToArtifacts(b *bom.Result) ([]*artifacts.Artifact, error) {
if err != nil {
return []*artifacts.Artifact{}, err
}
artifactList = append(artifactList, &artifacts.Artifact{Kind: "ControlPlaneComponents", Namespace: c.Namespace, Name: c.Name, RawResource: rawResource})
artifactList = append(artifactList, &artifacts.Artifact{
Kind: "ControlPlaneComponents",
Namespace: c.Namespace,
Name: c.Name,
RawResource: rawResource,
})
}
for _, ni := range b.NodesInfo {
rawResource, err := rawResource(&ni)
if err != nil {
return []*artifacts.Artifact{}, err
}
artifactList = append(artifactList, &artifacts.Artifact{Kind: "NodeComponents", Name: ni.NodeName, RawResource: rawResource})
}
cr, err := rawResource(&bom.Result{ID: b.ID, Type: "ClusterInfo", Version: b.Version, Properties: b.Properties})
artifactList = append(artifactList, &artifacts.Artifact{
Kind: "NodeComponents",
Name: ni.NodeName,
RawResource: rawResource,
})
}
cr, err := rawResource(&bom.Result{
ID: b.ID,
Type: "ClusterInfo",
Version: b.Version,
Properties: b.Properties,
})
if err != nil {
return []*artifacts.Artifact{}, err
}
artifactList = append(artifactList, &artifacts.Artifact{Kind: "Cluster", Name: b.ID, RawResource: cr})
artifactList = append(artifactList, &artifacts.Artifact{
Kind: "Cluster",
Name: b.ID,
RawResource: cr,
})
return artifactList, nil
}

Expand Down
Loading