mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-05 17:21:28 +00:00
refactor: replace rest client with controller-runtime clientset for Trivy analyzers (#776)
* refactor: replace rest client with controller-runtime clientset for Trivy analyzers Signed-off-by: ptyin <peteryin1604@gmail.com> * refactor: remove rest client Signed-off-by: ptyin <peteryin1604@gmail.com> --------- Signed-off-by: ptyin <peteryin1604@gmail.com>
This commit is contained in:
@@ -15,12 +15,12 @@ package trivy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
|
"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
|
||||||
"k8s.io/client-go/rest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TrivyAnalyzer struct {
|
type TrivyAnalyzer struct {
|
||||||
@@ -32,18 +32,9 @@ func (TrivyAnalyzer) analyzeVulnerabilityReports(a common.Analyzer) ([]common.Re
|
|||||||
// Get all trivy VulnerabilityReports
|
// Get all trivy VulnerabilityReports
|
||||||
result := &v1alpha1.VulnerabilityReportList{}
|
result := &v1alpha1.VulnerabilityReportList{}
|
||||||
|
|
||||||
config := a.Client.GetConfig()
|
client := a.Client.CtrlClient
|
||||||
// Add group version to sceheme
|
v1alpha1.AddToScheme(client.Scheme())
|
||||||
config.ContentConfig.GroupVersion = &v1alpha1.SchemeGroupVersion
|
if err := client.List(a.Context, result, &ctrl.ListOptions{}); err != nil {
|
||||||
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 {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,18 +84,9 @@ func (t TrivyAnalyzer) analyzeConfigAuditReports(a common.Analyzer) ([]common.Re
|
|||||||
// Get all trivy ConfigAuditReports
|
// Get all trivy ConfigAuditReports
|
||||||
result := &v1alpha1.ConfigAuditReportList{}
|
result := &v1alpha1.ConfigAuditReportList{}
|
||||||
|
|
||||||
config := a.Client.GetConfig()
|
client := a.Client.CtrlClient
|
||||||
// Add group version to sceheme
|
v1alpha1.AddToScheme(client.Scheme())
|
||||||
config.ContentConfig.GroupVersion = &v1alpha1.SchemeGroupVersion
|
if err := client.List(a.Context, result, &ctrl.ListOptions{}); err != nil {
|
||||||
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 {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,12 +14,10 @@ limitations under the License.
|
|||||||
package kubernetes
|
package kubernetes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
|
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"k8s.io/kubectl/pkg/scheme"
|
|
||||||
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
|
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,10 +29,6 @@ func (c *Client) GetClient() kubernetes.Interface {
|
|||||||
return c.Client
|
return c.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetRestClient() rest.Interface {
|
|
||||||
return c.RestClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) GetCtrlClient() ctrl.Client {
|
func (c *Client) GetCtrlClient() ctrl.Client {
|
||||||
return c.CtrlClient
|
return c.CtrlClient
|
||||||
}
|
}
|
||||||
@@ -64,14 +58,6 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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{})
|
ctrlClient, err := ctrl.New(config, ctrl.Options{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -85,7 +71,6 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
|
|||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
Client: clientSet,
|
Client: clientSet,
|
||||||
RestClient: restClient,
|
|
||||||
CtrlClient: ctrlClient,
|
CtrlClient: ctrlClient,
|
||||||
Config: config,
|
Config: config,
|
||||||
ServerVersion: serverVersion,
|
ServerVersion: serverVersion,
|
||||||
|
@@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Client kubernetes.Interface
|
Client kubernetes.Interface
|
||||||
RestClient rest.Interface
|
|
||||||
CtrlClient ctrl.Client
|
CtrlClient ctrl.Client
|
||||||
Config *rest.Config
|
Config *rest.Config
|
||||||
ServerVersion *version.Info
|
ServerVersion *version.Info
|
||||||
|
Reference in New Issue
Block a user