Skip to content

Commit

Permalink
feat: introduce WithQPS and WithBurst functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thapabishwa committed Dec 5, 2023
1 parent 50a0691 commit d68c121
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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(2000), k8s.WithQPS(2000))
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.Println("Time taken:", end.Sub(start))
printArtifacts(artifacts)

fmt.Println("Scanning kind 'pods' with exclude-owned=true")
Expand Down
44 changes: 44 additions & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func WithKubeConfig(kubeConfig string) ClusterOption {
}
}

func WithQPS(qps float32) ClusterOption {
return func(o *genericclioptions.ConfigFlags) {
fmt.Println("Setting QPS to", qps)
o.WithDiscoveryQPS(qps)
}
}

// WithBurst sets the burst limit for the client
func WithBurst(burst int) ClusterOption {
return func(o *genericclioptions.ConfigFlags) {
fmt.Println("Setting burst to", burst)
o.WithDiscoveryBurst(burst)
}
}

// GetCluster returns a current configured cluster,
func GetCluster(opts ...ClusterOption) (Cluster, error) {
cf := genericclioptions.NewConfigFlags(true)
Expand All @@ -165,6 +180,35 @@ func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper,
return nil, err
}

fmt.Println("kubeconfig QPS:", kubeConfig.QPS)
fmt.Println("kubeconfig Burst:", kubeConfig.Burst)

// kubeConfig.QPS = 2000
// kubeConfig.Burst = 2000
/* uncomment the above lines to manually set QPS and Burst
after manually setting QPS and Burst, your response time will be much faster
example output with WithQPS(2000) and WithBurst(2000) functions:
Setting burst to 2000
Setting QPS to 2000
kubeconfig QPS: 0 (qps and burst are reset to 0)
kubeconfig Burst: 0
Current namespace: default
Scanning cluster
Time taken: 2.057551041s
Name: coredns, Kind: Deployment, Namespace: kube-system, Images: [registry.k8s.io/coredns/coredns:v1.10.1]
example output after manually setting QPS and Burst:
Setting burst to 2000
Setting QPS to 2000
kubeconfig QPS: 2000 (manually set values)
kubeconfig Burst: 2000 (manually set values)
Current namespace: default
Scanning cluster
Time taken: 89.546083ms
Name: coredns, Kind: Deployment, Namespace: kube-system, Images: [registry.k8s.io/coredns/coredns:v1.10.1]
*/

k8sDynamicClient, err := dynamic.NewForConfig(kubeConfig)
if err != nil {
return nil, err
Expand Down

0 comments on commit d68c121

Please sign in to comment.