Skip to content

Commit

Permalink
feat: allow adjustment of QPS/Burst values (#263)
Browse files Browse the repository at this point in the history
* feat: allow adjustment of  QPS/Burst values

* chore: add function to combine multiple functions

* chore: rename symbols to generate cleaner diff

* chore: address PR review comments

* chore: increase QPS/burst to 100/100
  • Loading branch information
thapabishwa authored Jan 8, 2024
1 parent f921fb0 commit a47c86a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/aquasecurity/trivy-kubernetes/pkg/artifacts"
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
Expand All @@ -23,7 +24,7 @@ func main() {

ctx := context.Background()

cluster, err := k8s.GetCluster()
cluster, err := k8s.GetCluster(k8s.WithBurst(100), k8s.WithQPS(100))
if err != nil {
log.Fatal(err)
}
Expand All @@ -34,10 +35,13 @@ func main() {
fmt.Println("Scanning cluster")

//trivy k8s #cluster
start := time.Now()
artifacts, err := trivyk8s.ListArtifacts(ctx)
end := time.Now()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Scan took %v\n", end.Sub(start))
printArtifacts(artifacts)

fmt.Println("Scanning kind 'pods' with exclude-owned=true")
Expand Down
41 changes: 36 additions & 5 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ func WithKubeConfig(kubeConfig string) ClusterOption {
c.KubeConfig = &kubeConfig
}
}
func WithQPS(qps float32) ClusterOption {
return func(o *genericclioptions.ConfigFlags) {
o.WrapConfigFn = combineConfigFns(o.WrapConfigFn, func(c *rest.Config) *rest.Config {
c.QPS = qps
return c
})
}
}

func WithBurst(burst int) ClusterOption {
return func(o *genericclioptions.ConfigFlags) {
o.WrapConfigFn = combineConfigFns(o.WrapConfigFn, func(c *rest.Config) *rest.Config {
c.Burst = burst
return c
})
}
}

// Helper function to combine multiple config functions
func combineConfigFns(existing, newFn func(*rest.Config) *rest.Config) func(*rest.Config) *rest.Config {
if existing == nil {
return newFn
}
return func(c *rest.Config) *rest.Config {
if modified := existing(c); modified != nil {
return newFn(modified)
}
return newFn(c)
}
}

// GetCluster returns a current configured cluster,
func GetCluster(opts ...ClusterOption) (Cluster, error) {
Expand All @@ -156,15 +186,16 @@ func GetCluster(opts ...ClusterOption) (Cluster, error) {
return nil, err
}

return getCluster(clientConfig, restMapper, *cf.Context, false)
}

func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper, currentContext string, fakeConfig bool) (*cluster, error) {
kubeConfig, err := clientConfig.ClientConfig()
kubeConfig, err := cf.ToRESTConfig()
if err != nil {
return nil, err
}

return getCluster(clientConfig, kubeConfig, restMapper, *cf.Context, false)
}

func getCluster(clientConfig clientcmd.ClientConfig, kubeConfig *rest.Config, restMapper meta.RESTMapper, currentContext string, fakeConfig bool) (*cluster, error) {

k8sDynamicClient, err := dynamic.NewForConfig(kubeConfig)
if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestGetCurrentNamespace(t *testing.T) {
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
fakeConfig := createValidTestConfig(test.Namespace)
cluster, err := getCluster(fakeConfig, nil, "", true)
fakeKubeconfig, err := fakeConfig.ClientConfig()
assert.NoError(t, err)
cluster, err := getCluster(fakeConfig, fakeKubeconfig, nil, "", true)
assert.NoError(t, err)
assert.Equal(t, test.ExpectedNamespace, cluster.GetCurrentNamespace())
})
Expand Down Expand Up @@ -66,7 +68,10 @@ func TestGetGVR(t *testing.T) {

fakeConfig := createValidTestConfig("")

cluster, err := getCluster(fakeConfig, mapper, "", true)
fakeKubeconfig, err := fakeConfig.ClientConfig()
assert.NoError(t, err)

cluster, err := getCluster(fakeConfig, fakeKubeconfig, mapper, "", true)
assert.NoError(t, err)

gvr, err := cluster.GetGVR(test.Resource)
Expand Down

0 comments on commit a47c86a

Please sign in to comment.