Skip to content

Commit

Permalink
Merge pull request #463 from kubescape/norun
Browse files Browse the repository at this point in the history
fix object cache when runtime observability is disabled
  • Loading branch information
matthyx authored Jan 22, 2025
2 parents 3b01650 + 7495bff commit 8f6f8b7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ func main() {

} else {
ruleManager = rulemanager.CreateRuleManagerMock()
objCache = objectcache.NewObjectCacheMock()
apc := &objectcache.ApplicationProfileCacheMock{}
nnc := &objectcache.NetworkNeighborhoodCacheMock{}
dc := &objectcache.DnsCacheMock{}
objCache = objectcachev1.NewObjectCache(k8sObjectCache, apc, nnc, dc)
ruleBindingNotify = make(chan rulebinding.RuleBindingNotify, 1)
processManager = processmanager.CreateProcessManagerMock()
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/objectcache/applicationprofilecache_interface.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package objectcache

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

"github.com/kubescape/node-agent/pkg/watcher"
"github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
)

type ApplicationProfileCache interface {
GetApplicationProfile(containerID string) *v1beta1.ApplicationProfile
WatchResources() []watcher.WatchResource
AddHandler(ctx context.Context, obj runtime.Object)
ModifyHandler(ctx context.Context, obj runtime.Object)
DeleteHandler(ctx context.Context, obj runtime.Object)
}

var _ ApplicationProfileCache = (*ApplicationProfileCacheMock)(nil)
Expand All @@ -14,3 +24,19 @@ type ApplicationProfileCacheMock struct {
func (ap *ApplicationProfileCacheMock) GetApplicationProfile(_ string) *v1beta1.ApplicationProfile {
return nil
}

func (ap *ApplicationProfileCacheMock) WatchResources() []watcher.WatchResource {
return nil
}

func (ap *ApplicationProfileCacheMock) AddHandler(_ context.Context, _ runtime.Object) {
return
}

func (ap *ApplicationProfileCacheMock) ModifyHandler(_ context.Context, _ runtime.Object) {
return
}

func (ap *ApplicationProfileCacheMock) DeleteHandler(_ context.Context, _ runtime.Object) {
return
}
28 changes: 27 additions & 1 deletion pkg/objectcache/networkneighborhoodcache_interface.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package objectcache

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

"github.com/kubescape/node-agent/pkg/watcher"
"github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
)

type NetworkNeighborhoodCache interface {
GetNetworkNeighborhood(containerID string) *v1beta1.NetworkNeighborhood
WatchResources() []watcher.WatchResource
AddHandler(ctx context.Context, obj runtime.Object)
ModifyHandler(ctx context.Context, obj runtime.Object)
DeleteHandler(ctx context.Context, obj runtime.Object)
}

var _ NetworkNeighborhoodCache = (*NetworkNeighborhoodCacheMock)(nil)
Expand All @@ -14,3 +24,19 @@ type NetworkNeighborhoodCacheMock struct {
func (ap *NetworkNeighborhoodCacheMock) GetNetworkNeighborhood(_ string) *v1beta1.NetworkNeighborhood {
return nil
}

func (ap *NetworkNeighborhoodCacheMock) WatchResources() []watcher.WatchResource {
return nil
}

func (ap *NetworkNeighborhoodCacheMock) AddHandler(_ context.Context, _ runtime.Object) {
return
}

func (ap *NetworkNeighborhoodCacheMock) ModifyHandler(_ context.Context, _ runtime.Object) {
return
}

func (ap *NetworkNeighborhoodCacheMock) DeleteHandler(_ context.Context, _ runtime.Object) {
return
}
34 changes: 26 additions & 8 deletions pkg/ruleengine/v1/mock.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ruleengine

import (
corev1 "k8s.io/api/core/v1"
"context"

"github.com/goradd/maps"
"github.com/kubescape/node-agent/pkg/objectcache"
"github.com/kubescape/node-agent/pkg/utils"

"github.com/kubescape/node-agent/pkg/watcher"
"github.com/kubescape/storage/pkg/apis/softwarecomposition/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)

var _ objectcache.ApplicationProfileCache = (*RuleObjectCacheMock)(nil)
Expand Down Expand Up @@ -62,18 +64,18 @@ func (r *RuleObjectCacheMock) GetPods() []*corev1.Pod {
return []*corev1.Pod{{Spec: *r.podSpec, Status: *r.podStatus}}
}

func (k *RuleObjectCacheMock) SetSharedContainerData(containerID string, data *utils.WatchedContainerData) {
k.containerIDToSharedData.Set(containerID, data)
func (r *RuleObjectCacheMock) SetSharedContainerData(containerID string, data *utils.WatchedContainerData) {
r.containerIDToSharedData.Set(containerID, data)
}
func (k *RuleObjectCacheMock) GetSharedContainerData(containerID string) *utils.WatchedContainerData {
if data, ok := k.containerIDToSharedData.Load(containerID); ok {
func (r *RuleObjectCacheMock) GetSharedContainerData(containerID string) *utils.WatchedContainerData {
if data, ok := r.containerIDToSharedData.Load(containerID); ok {
return data
}

return nil
}
func (k *RuleObjectCacheMock) DeleteSharedContainerData(containerID string) {
k.containerIDToSharedData.Delete(containerID)
func (r *RuleObjectCacheMock) DeleteSharedContainerData(containerID string) {
r.containerIDToSharedData.Delete(containerID)
}

func (r *RuleObjectCacheMock) K8sObjectCache() objectcache.K8sObjectCache {
Expand Down Expand Up @@ -107,3 +109,19 @@ func (r *RuleObjectCacheMock) ResolveIpToDomain(ip string) string {

return ""
}

func (r *RuleObjectCacheMock) WatchResources() []watcher.WatchResource {
return nil
}

func (r *RuleObjectCacheMock) AddHandler(_ context.Context, _ runtime.Object) {
return
}

func (r *RuleObjectCacheMock) ModifyHandler(_ context.Context, _ runtime.Object) {
return
}

func (r *RuleObjectCacheMock) DeleteHandler(_ context.Context, _ runtime.Object) {
return
}

0 comments on commit 8f6f8b7

Please sign in to comment.