From bc0f281995bbad6063d0a67a59e2482ad0163bbd Mon Sep 17 00:00:00 2001 From: ptyin Date: Sun, 26 Nov 2023 19:08:10 +0800 Subject: [PATCH] refactor: replace rest client with controller-runtime clientset for Trivy analyzers Signed-off-by: ptyin --- pkg/integration/trivy/analyzer.go | 32 +++++++------------------------ 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/pkg/integration/trivy/analyzer.go b/pkg/integration/trivy/analyzer.go index f8c2e21e78..d65920e81c 100644 --- a/pkg/integration/trivy/analyzer.go +++ b/pkg/integration/trivy/analyzer.go @@ -15,12 +15,12 @@ package trivy import ( "fmt" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" "strings" "github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1" "github.com/k8sgpt-ai/k8sgpt/pkg/common" "github.com/k8sgpt-ai/k8sgpt/pkg/util" - "k8s.io/client-go/rest" ) type TrivyAnalyzer struct { @@ -32,18 +32,9 @@ func (TrivyAnalyzer) analyzeVulnerabilityReports(a common.Analyzer) ([]common.Re // Get all trivy VulnerabilityReports result := &v1alpha1.VulnerabilityReportList{} - config := a.Client.GetConfig() - // Add group version to sceheme - config.ContentConfig.GroupVersion = &v1alpha1.SchemeGroupVersion - config.UserAgent = rest.DefaultKubernetesUserAgent() - config.APIPath = "/apis" - - restClient, err := rest.UnversionedRESTClientFor(config) - if err != nil { - return nil, err - } - err = restClient.Get().Resource("vulnerabilityreports").Namespace(a.Namespace).Do(a.Context).Into(result) - if err != nil { + client := a.Client.CtrlClient + v1alpha1.AddToScheme(client.Scheme()) + if err := client.List(a.Context, result, &ctrl.ListOptions{}); err != nil { return nil, err } @@ -93,18 +84,9 @@ func (t TrivyAnalyzer) analyzeConfigAuditReports(a common.Analyzer) ([]common.Re // Get all trivy ConfigAuditReports result := &v1alpha1.ConfigAuditReportList{} - config := a.Client.GetConfig() - // Add group version to sceheme - config.ContentConfig.GroupVersion = &v1alpha1.SchemeGroupVersion - config.UserAgent = rest.DefaultKubernetesUserAgent() - config.APIPath = "/apis" - - restClient, err := rest.UnversionedRESTClientFor(config) - if err != nil { - return nil, err - } - err = restClient.Get().Resource("configauditreports").Namespace(a.Namespace).Do(a.Context).Into(result) - if err != nil { + client := a.Client.CtrlClient + v1alpha1.AddToScheme(client.Scheme()) + if err := client.List(a.Context, result, &ctrl.ListOptions{}); err != nil { return nil, err }