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

use in-cluster config for client when possible #6

Merged
merged 1 commit into from
Jan 3, 2024
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
50 changes: 37 additions & 13 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package api

import (
"context"
"errors"
"flag"
"path/filepath"

"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"

"github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"
spdxclient "github.com/kubescape/storage/pkg/generated/clientset/versioned"
Expand All @@ -17,26 +24,43 @@ type StorageClientImpl struct {
var _ StorageClient = &StorageClientImpl{}

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

func (sc *StorageClientImpl) Initialize(kubeconfig string) error {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
clusterConfig, err := getConfig()
if err != nil {
return err
logger.L().Fatal("error getting cluster config", helpers.Error(err))
}

// Create the dynamic client
clientset, err := spdxclient.NewForConfig(config)
clientset, err := spdxclient.NewForConfig(clusterConfig)
if err != nil {
return err
logger.L().Fatal("error creating dynamic client", helpers.Error(err))
}
return &StorageClientImpl{
clientset: clientset,
}
}

sc.clientset = clientset
return nil
func getConfig() (*rest.Config, error) {
// try in-cluster config first
clusterConfig, err := rest.InClusterConfig()
if err == nil {
return clusterConfig, nil
}
// fallback to kubeconfig
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
clusterConfig, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err == nil {
return clusterConfig, nil
}
// nothing works
return nil, errors.New("unable to find config")
}

func (sc *StorageClientImpl) GetVulnerabilitySummaries(kubeconfig string) (*v1beta1.VulnerabilitySummaryList, error) {
func (sc *StorageClientImpl) GetVulnerabilitySummaries() (*v1beta1.VulnerabilitySummaryList, error) {
vulnsummary, err := sc.clientset.SpdxV1beta1().VulnerabilitySummaries("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
Expand All @@ -46,7 +70,7 @@ func (sc *StorageClientImpl) GetVulnerabilitySummaries(kubeconfig string) (*v1be

}

func (sc *StorageClientImpl) GetConfigScanSummaries(kubeconfig string) (*v1beta1.ConfigurationScanSummaryList, error) {
func (sc *StorageClientImpl) GetConfigScanSummaries() (*v1beta1.ConfigurationScanSummaryList, error) {
configscan, err := sc.clientset.SpdxV1beta1().ConfigurationScanSummaries("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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
GetVulnerabilitySummaries() (*v1beta1.VulnerabilitySummaryList, error)
GetConfigScanSummaries() (*v1beta1.ConfigurationScanSummaryList, error)
}
22 changes: 4 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"path/filepath"

"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/kubescape/prometheus-exporter/api"
"github.com/kubescape/prometheus-exporter/metrics"
"k8s.io/client-go/util/homedir"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {

var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

var storageClient api.StorageClient
storageClient = api.NewStorageClient()
if err := storageClient.Initialize(*kubeconfig); err != nil {
logger.L().Error("error initializing storage client", helpers.Error(err))
}

// Start Prometheus HTTP server
go func() {
Expand All @@ -41,12 +27,12 @@ func main() {
// monitor the severities in objects
for {

configScanSummaries, err := storageClient.GetConfigScanSummaries(*kubeconfig)
configScanSummaries, err := storageClient.GetConfigScanSummaries()
if err != nil {
logger.L().Error("error getting configuration scan summaries", helpers.Error(err))
}

vulnScanSummaries, err := storageClient.GetVulnerabilitySummaries(*kubeconfig)
vulnScanSummaries, err := storageClient.GetVulnerabilitySummaries()
if err != nil {
logger.L().Error("error getting vulnerability scan summaries", helpers.Error(err))
}
Expand All @@ -57,5 +43,5 @@ func main() {
metrics.ProcessConfigscanClusterMetrics(configScanSummaries)
metrics.ProcessConfigscanNamespaceMetrics(configScanSummaries)
}

}
Loading