Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io>
  • Loading branch information
Daniel Grunberger committed Nov 8, 2023
1 parent 505de59 commit d4ccc57
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 240 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This command deploys the Kubescape operator to your cluster, which is responsibl

3. Clone the Repository: Clone the Kubescape Exporter repository from GitHub to your local environment.
```
git clone https://github.com/yrs147/kubescape-exporter.git
git clone https://github.com/kubescape/prometheus-exporter.git
```
4. Navigate to the Project Directory: Change your working directory to the cloned repository.
```
Expand Down
54 changes: 23 additions & 31 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,55 @@ package api
import (
"context"

"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"

"github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"
spdxclient "github.com/kubescape/storage/pkg/generated/clientset/versioned"
)

func GetVulnerabilitySummary(kubeconfig string) ([]byte, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}

// Create the dynamic client
clientset, err := spdxclient.NewForConfig(config)
if err != nil {
return nil, err
}

// Get the CRD object from the Kubernetes API server
vulnsummary, err := clientset.SpdxV1beta1().VulnerabilitySummaries("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

crd, err := yaml.Marshal(vulnsummary)
if err != nil {
return nil, err
}
type StorageClientImpl struct {
clientset *spdxclient.Clientset
}

return crd, nil
var _ StorageClient = &StorageClientImpl{}

func NewStorageClient() *StorageClientImpl {
return &StorageClientImpl{}
}

func GetConfigScanSummary(kubeconfig string) ([]byte, error) {
func (sc *StorageClientImpl) Initialize(kubeconfig string) error {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
return err
}

// Create the dynamic client
clientset, err := spdxclient.NewForConfig(config)
if err != nil {
return nil, err
return err
}

// Get the CRD object from the Kubernetes API server
configscan, err := clientset.SpdxV1beta1().ConfigurationScanSummaries("").List(context.TODO(), metav1.ListOptions{})
sc.clientset = clientset
return nil
}

func (sc *StorageClientImpl) GetVulnerabilitySummaries(kubeconfig string) (*v1beta1.VulnerabilitySummaryList, error) {
vulnsummary, err := sc.clientset.SpdxV1beta1().VulnerabilitySummaries("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

crd, err := yaml.Marshal(configscan)
return vulnsummary, nil

}

func (sc *StorageClientImpl) GetConfigScanSummaries(kubeconfig string) (*v1beta1.ConfigurationScanSummaryList, error) {
configscan, err := sc.clientset.SpdxV1beta1().ConfigurationScanSummaries("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

return crd, nil
return configscan, nil

}
9 changes: 9 additions & 0 deletions api/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package api

import "github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"

type StorageClient interface {
GetVulnerabilitySummaries(kubeconfig string) (*v1beta1.VulnerabilitySummaryList, error)
GetConfigScanSummaries(kubeconfig string) (*v1beta1.ConfigurationScanSummaryList, error)
Initialize(kubeconfig string) error
}
24 changes: 0 additions & 24 deletions collect/collect.go

This file was deleted.

30 changes: 29 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ module github.com/yrs147/kubescape-exporter
go 1.20

require (
github.com/kubescape/go-logger v0.0.12
github.com/kubescape/storage v0.0.20
github.com/prometheus/client_golang v1.14.0
github.com/stretchr/testify v1.8.3
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.26.2
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -26,11 +29,14 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -41,15 +47,37 @@ require (
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.18 // indirect
github.com/uptrace/opentelemetry-go-extra/otelzap v0.1.18 // indirect
github.com/uptrace/uptrace-go v1.11.8 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.37.0 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.34.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.34.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.11.2 // indirect
go.opentelemetry.io/otel/sdk/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.26.2 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
Expand Down

0 comments on commit d4ccc57

Please sign in to comment.