Skip to content

Commit

Permalink
refactor: replace rest client with controller-runtime clientset for T…
Browse files Browse the repository at this point in the history
…rivy analyzers

Signed-off-by: ptyin <peteryin1604@gmail.com>
  • Loading branch information
ptyin committed Nov 27, 2023
1 parent 71ae5a7 commit bc0f281
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions pkg/integration/trivy/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit bc0f281

Please sign in to comment.