Skip to content
Open
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
8 changes: 1 addition & 7 deletions pkg/watcher/dynamicwatcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ func (wh *WatchHandler) Stop(_ context.Context) {

func (wh *WatchHandler) chooseWatcher(res schema.GroupVersionResource, opts metav1.ListOptions) (watch.Interface, error) {
switch res.Resource {
case "applicationprofiles":
opts.ResourceVersion = softwarecomposition.ResourceVersionFullSpec
return wh.storageClient.ApplicationProfiles("").Watch(context.Background(), opts)
case "networkneighborhoods":
opts.ResourceVersion = softwarecomposition.ResourceVersionFullSpec
return wh.storageClient.NetworkNeighborhoods("").Watch(context.Background(), opts)
case "pods":
return wh.k8sClient.GetKubernetesClient().CoreV1().Pods("").Watch(context.Background(), opts)
case "runtimerulealertbindings":
Expand All @@ -152,7 +146,7 @@ func (wh *WatchHandler) chooseWatcher(res schema.GroupVersionResource, opts meta
default:
// Make sure the resource version is not our storage, if so we panic.
if res.Group == kubescapeCustomResourceGroup {
return nil, fmt.Errorf("resource must use the storage client %s: %w", res.Resource, errNotImplemented)
return nil, fmt.Errorf("watch is not implemented by the kubescape storage client %s: %w", res.Resource, errNotImplemented)
}

return wh.k8sClient.GetDynamicClient().Resource(res).Watch(context.Background(), opts)
Expand Down
106 changes: 1 addition & 105 deletions pkg/watcher/dynamicwatcher/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
)

var (
resourcePod = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
resourceNetworkNeighborhood = schema.GroupVersionResource{Group: "spdx.softwarecomposition.kubescape.io", Version: "v1beta1", Resource: "networkneighborhoods"}
resourceApplicationProfile = schema.GroupVersionResource{Group: "spdx.softwarecomposition.kubescape.io", Version: "v1beta1", Resource: "applicationprofiles"}
resourcePod = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
)

func init() {
Expand Down Expand Up @@ -207,113 +205,11 @@ func getKey(obj runtime.Object) string {

func TestStart_1(t *testing.T) {
tt := []testObj{
{
name: "list ApplicationProfiles",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
preCreatedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection)},
},
{
name: "list NetworkNeighborhoods",
resources: []schema.GroupVersionResource{resourceNetworkNeighborhood},
preCreatedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindNN, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindNN, mocks.TestCollection)},
},
{
name: "watch Pods",
resources: []schema.GroupVersionResource{resourcePod},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindPod, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindPod, mocks.TestCollection)},
},
{
name: "watch ApplicationProfiles",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection)},
},
{
name: "watch NetworkNeighborhoods",
resources: []schema.GroupVersionResource{resourceNetworkNeighborhood},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindNN, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindNN, mocks.TestCollection)},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
startTest(t, tc)
})
}
}

func TestStart_2(t *testing.T) {
tt := []testObj{
{
name: "list and modify",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
preCreatedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
modifiedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
},
{
name: "watch and modify",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
modifiedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
startTest(t, tc)
})
}
}

func TestStart_3(t *testing.T) {
tt := []testObj{

{
name: "list and watch",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
preCreatedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection)},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
},
{
name: "list and delete",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
preCreatedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
deleteObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
startTest(t, tc)
})
}
}
func TestStart_4(t *testing.T) {
tt := []testObj{
{
name: "watch, modify, and delete",
resources: []schema.GroupVersionResource{resourceApplicationProfile},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
modifiedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
deleteObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx)},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
startTest(t, tc)
})
}
}

func TestStart_5(t *testing.T) {
tt := []testObj{
{
name: "multi watch, modify, and delete",
resources: []schema.GroupVersionResource{resourceApplicationProfile, resourceNetworkNeighborhood, resourcePod},
createObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindNN, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindNN, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindPod, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindPod, mocks.TestCollection)},
modifiedObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindNN, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindNN, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindPod, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindPod, mocks.TestCollection)},
deleteObjects: []runtime.Object{mocks.GetRuntime(mocks.TestKindAP, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindAP, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindNN, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindNN, mocks.TestCollection), mocks.GetRuntime(mocks.TestKindPod, mocks.TestNginx), mocks.GetRuntime(mocks.TestKindPod, mocks.TestCollection)},
},
}

for _, tc := range tt {
Expand Down
Loading