Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Feb 9, 2024
1 parent 51e3f59 commit bb6acbe
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions pkg/graph/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ func _renderPageBlock(kc client.Client, oc openvizcs.Interface, srcRID *kmapi.Re
var objs []unstructured.Unstructured

// handle FalcoEvent list call
if vars[sharedapi.GraphQueryVarTargetGroup] == falco.GroupName && vars[sharedapi.GraphQueryVarTargetKind] == falcov1alpha1.ResourceKindFalcoEvent {
objs, err = handleFalcoEventListCall(kc, block, srcID)
if vars[sharedapi.GraphQueryVarTargetGroup] == falco.GroupName &&
vars[sharedapi.GraphQueryVarTargetKind] == falcov1alpha1.ResourceKindFalcoEvent {
objs, err = listFalcoEvents(kc, block, srcID)
if err != nil {
return &out, err
}
Expand Down Expand Up @@ -348,51 +349,41 @@ func _renderPageBlock(kc client.Client, oc openvizcs.Interface, srcRID *kmapi.Re
return &out, nil
}

func handleFalcoEventListCall(kc client.Client, block *rsapi.PageBlockLayout, srcID *kmapi.ObjectID) ([]unstructured.Unstructured, error) {
var objs []unstructured.Unstructured

func listFalcoEvents(kc client.Client, block *rsapi.PageBlockLayout, srcID *kmapi.ObjectID) ([]unstructured.Unstructured, error) {
var refs []kmapi.ObjectReference
var err error
if srcID.Kind == "Pod" {
selector := labels.SelectorFromSet(map[string]string{
"k8s.pod.name": srcID.Name,
"k8s.ns.name": srcID.Namespace,
})

var list unstructured.UnstructuredList
list.SetGroupVersionKind(falcov1alpha1.SchemeGroupVersion.WithKind(falcov1alpha1.ResourceKindFalcoEvent))

err := kc.List(context.TODO(), &list, &client.ListOptions{LabelSelector: selector})
if meta.IsNoMatchError(err) {
refs = []kmapi.ObjectReference{
{
Name: srcID.Name,
Namespace: srcID.Namespace,
},
}
} else {
// list connected pods with this src
refs, err = listPods(block, srcID)
if err != nil {
return nil, err
} else if err == nil {
objs = append(objs, list.Items...)
}
return objs, nil
}

// list connected pods with this src
pods, err := listPods(block, srcID)
if err != nil {
return nil, err
}

for _, pod := range pods {
var events []unstructured.Unstructured
for _, pod := range refs {
selector := labels.SelectorFromSet(map[string]string{
"k8s.pod.name": pod.Name,
"k8s.ns.name": pod.Namespace,
})

var list unstructured.UnstructuredList
list.SetGroupVersionKind(falcov1alpha1.SchemeGroupVersion.WithKind(falcov1alpha1.ResourceKindFalcoEvent))

err = kc.List(context.TODO(), &list, &client.ListOptions{LabelSelector: selector})
if meta.IsNoMatchError(err) {
return nil, err
} else if err == nil {
objs = append(objs, list.Items...)
events = append(events, list.Items...)
}
}

return objs, nil
return events, nil
}

func listPods(block *rsapi.PageBlockLayout, srcID *kmapi.ObjectID) ([]kmapi.ObjectReference, error) {
Expand Down

0 comments on commit bb6acbe

Please sign in to comment.