From a2e3a2c1c7b50783fca6a4ff5d0b8856af2454da Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Wed, 25 Oct 2023 10:04:01 +0300 Subject: [PATCH 1/2] get namespace from config file Signed-off-by: David Wertenteil --- main.go | 2 +- pkg/storage/v1/storage_nocache.go | 18 +++++------------- pkg/storage/v1/storage_test.go | 10 +++++----- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index e81d0bfc..e46bde43 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func main() { // Create clients k8sClient := k8sinterface.NewKubernetesApi() - storageClient, err := storage.CreateStorageNoCache() + storageClient, err := storage.CreateStorageNoCache(clusterData.Namespace) if err != nil { logger.L().Ctx(ctx).Fatal("error creating the storage client", helpers.Error(err)) } diff --git a/pkg/storage/v1/storage_nocache.go b/pkg/storage/v1/storage_nocache.go index 27c907d3..5671d333 100644 --- a/pkg/storage/v1/storage_nocache.go +++ b/pkg/storage/v1/storage_nocache.go @@ -18,8 +18,7 @@ import ( ) const ( - KubeConfig = "KUBECONFIG" - defaultNamespace = "kubescape" + KubeConfig = "KUBECONFIG" ) type StorageNoCache struct { @@ -29,7 +28,7 @@ type StorageNoCache struct { var _ storage.StorageClient = (*StorageNoCache)(nil) -func CreateStorageNoCache() (*StorageNoCache, error) { +func CreateStorageNoCache(defaultNamespace string) (*StorageNoCache, error) { var config *rest.Config kubeconfig := os.Getenv(KubeConfig) // use the current context in kubeconfig @@ -48,24 +47,17 @@ func CreateStorageNoCache() (*StorageNoCache, error) { return &StorageNoCache{ StorageClient: clientset.SpdxV1beta1(), - namespace: getNamespace(), + namespace: defaultNamespace, }, nil } -func CreateFakeStorageNoCache() (*StorageNoCache, error) { +func CreateFakeStorageNoCache(defaultNamespace string) (*StorageNoCache, error) { return &StorageNoCache{ StorageClient: fake.NewSimpleClientset().SpdxV1beta1(), - namespace: getNamespace(), + namespace: defaultNamespace, }, nil } -func getNamespace() string { - if ns, ok := os.LookupEnv("NAMESPACE"); ok { - return ns - } - return defaultNamespace -} - func (sc StorageNoCache) CreateApplicationActivity(activity *v1beta1.ApplicationActivity, namespace string) error { _, err := sc.StorageClient.ApplicationActivities(namespace).Create(context.Background(), activity, metav1.CreateOptions{}) if err != nil { diff --git a/pkg/storage/v1/storage_test.go b/pkg/storage/v1/storage_test.go index 85100867..e0125e59 100644 --- a/pkg/storage/v1/storage_test.go +++ b/pkg/storage/v1/storage_test.go @@ -60,7 +60,7 @@ func TestStorageNoCache_GetSBOM(t *testing.T) { want: &v1beta1.SBOMSPDXv2p3{ ObjectMeta: v1.ObjectMeta{ Name: storage.NginxKey, - Namespace: defaultNamespace, + Namespace: "kubescape", }, }, }, @@ -74,9 +74,9 @@ func TestStorageNoCache_GetSBOM(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - sc, _ := CreateFakeStorageNoCache() + sc, _ := CreateFakeStorageNoCache("kubescape") if tt.createSBOM { - _, _ = sc.StorageClient.SBOMSPDXv2p3s(defaultNamespace).Create(context.Background(), tt.want, v1.CreateOptions{}) + _, _ = sc.StorageClient.SBOMSPDXv2p3s("kubescape").Create(context.Background(), tt.want, v1.CreateOptions{}) } got, err := sc.GetSBOM(tt.args.name) if (err != nil) != tt.wantErr { @@ -120,11 +120,11 @@ func TestStorageNoCache_PatchFilteredSBOM(t *testing.T) { Name: tt.args.name, }, } - _, _ = sc.StorageClient.SBOMSPDXv2p3Filtereds(defaultNamespace).Create(context.Background(), filteredSBOM, v1.CreateOptions{}) + _, _ = sc.StorageClient.SBOMSPDXv2p3Filtereds("kubescape").Create(context.Background(), filteredSBOM, v1.CreateOptions{}) if err := sc.PatchFilteredSBOM(tt.args.name, tt.args.SBOM); (err != nil) != tt.wantErr { t.Errorf("PatchFilteredSBOM() error = %v, wantErr %v", err, tt.wantErr) } - got, err := sc.StorageClient.SBOMSPDXv2p3Filtereds(defaultNamespace).Get(context.Background(), tt.args.name, v1.GetOptions{}) + got, err := sc.StorageClient.SBOMSPDXv2p3Filtereds("kubescape").Get(context.Background(), tt.args.name, v1.GetOptions{}) assert.NoError(t, err) assert.Equal(t, 1, len(got.Spec.SPDX.Files)) }) From c2a8a914c190d3cb97d65dcece3ef7a3c7e5c27b Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Wed, 25 Oct 2023 13:53:04 +0300 Subject: [PATCH 2/2] rename defaultNamespace Signed-off-by: David Wertenteil --- pkg/storage/v1/storage_nocache.go | 8 ++++---- pkg/storage/v1/storage_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/storage/v1/storage_nocache.go b/pkg/storage/v1/storage_nocache.go index 5671d333..7a4dadbb 100644 --- a/pkg/storage/v1/storage_nocache.go +++ b/pkg/storage/v1/storage_nocache.go @@ -28,7 +28,7 @@ type StorageNoCache struct { var _ storage.StorageClient = (*StorageNoCache)(nil) -func CreateStorageNoCache(defaultNamespace string) (*StorageNoCache, error) { +func CreateStorageNoCache(namespace string) (*StorageNoCache, error) { var config *rest.Config kubeconfig := os.Getenv(KubeConfig) // use the current context in kubeconfig @@ -47,14 +47,14 @@ func CreateStorageNoCache(defaultNamespace string) (*StorageNoCache, error) { return &StorageNoCache{ StorageClient: clientset.SpdxV1beta1(), - namespace: defaultNamespace, + namespace: namespace, }, nil } -func CreateFakeStorageNoCache(defaultNamespace string) (*StorageNoCache, error) { +func CreateFakeStorageNoCache(namespace string) (*StorageNoCache, error) { return &StorageNoCache{ StorageClient: fake.NewSimpleClientset().SpdxV1beta1(), - namespace: defaultNamespace, + namespace: namespace, }, nil } diff --git a/pkg/storage/v1/storage_test.go b/pkg/storage/v1/storage_test.go index e0125e59..c254fa1f 100644 --- a/pkg/storage/v1/storage_test.go +++ b/pkg/storage/v1/storage_test.go @@ -32,7 +32,7 @@ func TestStorageNoCache_CreateFilteredSBOM(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - sc, _ := CreateFakeStorageNoCache() + sc, _ := CreateFakeStorageNoCache("kubescape") if err := sc.CreateFilteredSBOM(tt.args.SBOM); (err != nil) != tt.wantErr { t.Errorf("CreateFilteredSBOM() error = %v, wantErr %v", err, tt.wantErr) } @@ -114,7 +114,7 @@ func TestStorageNoCache_PatchFilteredSBOM(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - sc, _ := CreateFakeStorageNoCache() + sc, _ := CreateFakeStorageNoCache("kubescape") filteredSBOM := &v1beta1.SBOMSPDXv2p3Filtered{ ObjectMeta: v1.ObjectMeta{ Name: tt.args.name,