Skip to content

Commit

Permalink
fix: k8s all-namespaces support (#4096)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-keinan committed Apr 23, 2023
1 parent bd0c603 commit 107752d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/docs/references/configuration/cli/trivy_kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trivy kubernetes [flags] { cluster | all | specific resources like kubectl. eg:
### Options

```
-A, --all-namespaces fetch resources from all cluster namespaces
--cache-backend string cache backend (e.g. redis://localhost:6379) (default "fs")
--cache-ttl duration cache TTL when using redis as cache backend
--clear-cache clear image caches without scanning
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/aquasecurity/tml v0.6.1
github.com/aquasecurity/trivy-db v0.0.0-20230411140759-3c2ee2168575
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230413111230-522e0fca9814
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295
github.com/aws/aws-sdk-go v1.44.234
github.com/aws/aws-sdk-go-v2 v1.17.7
github.com/aws/aws-sdk-go-v2/config v1.18.15
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ github.com/aquasecurity/trivy-db v0.0.0-20230411140759-3c2ee2168575 h1:8Y/qLPXGF
github.com/aquasecurity/trivy-db v0.0.0-20230411140759-3c2ee2168575/go.mod h1:zn8GepvD5wBkCmmtBDwh0BWfiMUxS6xfGRcTPmXRVXo=
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728 h1:0eS+V7SXHgqoT99tV1mtMW6HL4HdoB9qGLMCb1fZp8A=
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728/go.mod h1:Ldya37FLi0e/5Cjq2T5Bty7cFkzUDwTcPeQua+2M8i8=
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230413111230-522e0fca9814 h1:50r4mAGLHB0yx/OX7/MY0GMN5hCLG2OcZsa1JgQfwvE=
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230413111230-522e0fca9814/go.mod h1:oGiNSpa6b+3E9SxzTuaneysOP/47eQUiem5R0x0HG58=
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295 h1:ZdQMyXrUTNhsjKMiGLNtwIpGkn0Aj7r6eRPzaJlDbYc=
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295/go.mod h1:FPtS3hhfzykyaIiAIUg3vovniDP5loM9hHRa8W2+PuU=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
12 changes: 12 additions & 0 deletions pkg/flag/kubernetes_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ var (
Value: []string{},
Usage: "specify node-collector job tolerations (example: key1=value1:NoExecute,key2=value2:NoSchedule)",
}
AllNamespaces = Flag{
Name: "all-namespaces",
ConfigName: "kubernetes.all.namespaces",
Shorthand: "A",
Value: false,
Usage: "fetch resources from all cluster namespaces",
}
)

type K8sFlagGroup struct {
Expand All @@ -72,6 +79,7 @@ type K8sFlagGroup struct {
K8sVersion *Flag
Parallel *Flag
Tolerations *Flag
AllNamespaces *Flag
}

type K8sOptions struct {
Expand All @@ -82,6 +90,7 @@ type K8sOptions struct {
K8sVersion string
Parallel int
Tolerations []corev1.Toleration
AllNamespaces bool
}

func NewK8sFlagGroup() *K8sFlagGroup {
Expand All @@ -93,6 +102,7 @@ func NewK8sFlagGroup() *K8sFlagGroup {
K8sVersion: &K8sVersionFlag,
Parallel: &ParallelFlag,
Tolerations: &TolerationsFlag,
AllNamespaces: &AllNamespaces,
}
}

Expand All @@ -109,6 +119,7 @@ func (f *K8sFlagGroup) Flags() []*Flag {
f.K8sVersion,
f.Parallel,
f.Tolerations,
f.AllNamespaces,
}
}

Expand All @@ -133,6 +144,7 @@ func (f *K8sFlagGroup) ToOptions() (K8sOptions, error) {
K8sVersion: getString(f.K8sVersion),
Parallel: parallel,
Tolerations: tolerations,
AllNamespaces: getBool(f.AllNamespaces),
}, nil
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/k8s/commands/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ func namespaceRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) e
if err := validateReportArguments(opts); err != nil {
return err
}
var trivyk trivyk8s.TrivyK8S
if opts.AllNamespaces {
trivyk = trivyk8s.New(cluster, log.Logger).AllNamespaces()
} else {
trivyk = trivyk8s.New(cluster, log.Logger).Namespace(getNamespace(opts, cluster.GetCurrentNamespace()))
}

trivyk8s := trivyk8s.New(cluster, log.Logger).Namespace(getNamespace(opts, cluster.GetCurrentNamespace()))

artifacts, err := trivyk8s.ListArtifacts(ctx)
artifacts, err := trivyk.ListArtifacts(ctx)
if err != nil {
return xerrors.Errorf("get k8s artifacts error: %w", err)
}
Expand Down

0 comments on commit 107752d

Please sign in to comment.