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

Get namespace from config file #140

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
18 changes: 5 additions & 13 deletions pkg/storage/v1/storage_nocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
)

const (
KubeConfig = "KUBECONFIG"
defaultNamespace = "kubescape"
KubeConfig = "KUBECONFIG"
)

type StorageNoCache struct {
Expand All @@ -29,7 +28,7 @@ type StorageNoCache struct {

var _ storage.StorageClient = (*StorageNoCache)(nil)

func CreateStorageNoCache() (*StorageNoCache, error) {
func CreateStorageNoCache(defaultNamespace string) (*StorageNoCache, error) {
dwertent marked this conversation as resolved.
Show resolved Hide resolved
var config *rest.Config
kubeconfig := os.Getenv(KubeConfig)
// use the current context in kubeconfig
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/storage/v1/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc, _ := CreateFakeStorageNoCache()

Check failure on line 35 in pkg/storage/v1/storage_test.go

View workflow job for this annotation

GitHub Actions / pr-created / test / Create cross-platform build

not enough arguments in call to CreateFakeStorageNoCache
if err := sc.CreateFilteredSBOM(tt.args.SBOM); (err != nil) != tt.wantErr {
t.Errorf("CreateFilteredSBOM() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down Expand Up @@ -60,7 +60,7 @@
want: &v1beta1.SBOMSPDXv2p3{
ObjectMeta: v1.ObjectMeta{
Name: storage.NginxKey,
Namespace: defaultNamespace,
Namespace: "kubescape",
},
},
},
Expand All @@ -74,9 +74,9 @@
}
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 {
Expand Down Expand Up @@ -114,17 +114,17 @@
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc, _ := CreateFakeStorageNoCache()

Check failure on line 117 in pkg/storage/v1/storage_test.go

View workflow job for this annotation

GitHub Actions / pr-created / test / Create cross-platform build

not enough arguments in call to CreateFakeStorageNoCache
filteredSBOM := &v1beta1.SBOMSPDXv2p3Filtered{
ObjectMeta: v1.ObjectMeta{
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))
})
Expand Down
Loading