Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace rest client with controller-runtime clientset for Trivy analyzers #776

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 0 additions & 15 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ limitations under the License.
package kubernetes

import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubectl/pkg/scheme"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -31,10 +29,6 @@ func (c *Client) GetClient() kubernetes.Interface {
return c.Client
}

func (c *Client) GetRestClient() rest.Interface {
return c.RestClient
}

func (c *Client) GetCtrlClient() ctrl.Client {
return c.CtrlClient
}
Expand Down Expand Up @@ -64,14 +58,6 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
if err != nil {
return nil, err
}
config.APIPath = "/api"
config.GroupVersion = &scheme.Scheme.PrioritizedVersionsForGroup("")[0]
config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}

restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}

ctrlClient, err := ctrl.New(config, ctrl.Options{})
if err != nil {
Expand All @@ -85,7 +71,6 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {

return &Client{
Client: clientSet,
RestClient: restClient,
CtrlClient: ctrlClient,
Config: config,
ServerVersion: serverVersion,
Expand Down
1 change: 0 additions & 1 deletion pkg/kubernetes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

type Client struct {
Client kubernetes.Interface
RestClient rest.Interface
CtrlClient ctrl.Client
Config *rest.Config
ServerVersion *version.Info
Expand Down