Skip to content

Commit

Permalink
Add NetworkPolicyPeer of type NamespaceSelector to the Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
steveteuber committed Sep 21, 2021
1 parent bfe0178 commit 66c943d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/graph/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (g *NetworkingV1Graph) NetworkPolicy(obj *v1.NetworkPolicy) (*Node, error)
// NetworkPolicyPeer adds a v1.NetworkPolicyPeer resource to the Graph.
func (g *NetworkingV1Graph) NetworkPolicyPeer(obj *v1.NetworkPolicy, policyType v1.PolicyType, peer v1.NetworkPolicyPeer) (*Node, error) {
switch {
case peer.NamespaceSelector != nil:
return g.NetworkPolicyPeerNamespaceSelector(obj, policyType, peer)
case peer.PodSelector != nil:
return g.NetworkPolicyPeerPodSelector(obj, policyType, peer)
}
Expand Down Expand Up @@ -157,3 +159,29 @@ func (g *NetworkingV1Graph) NetworkPolicyPeerPodSelector(obj *v1.NetworkPolicy,

return nil, nil
}

// NetworkPolicyPeerNamespaceSelector adds a v1.NetworkPolicyPeer of type NamespaceSelector to the Graph.
func (g *NetworkingV1Graph) NetworkPolicyPeerNamespaceSelector(obj *v1.NetworkPolicy, policyType v1.PolicyType, peer v1.NetworkPolicyPeer) (*Node, error) {
n := g.graph.Node(obj.GroupVersionKind(), obj)

selector, err := metav1.LabelSelectorAsSelector(peer.NamespaceSelector)
if err != nil {
return nil, err
}

options := metav1.ListOptions{LabelSelector: selector.String()}
namespaces, err := g.graph.clientset.CoreV1().Namespaces().List(context.TODO(), options)
if err != nil {
return nil, err
}

for _, namespace := range namespaces.Items {
ns, err := g.graph.CoreV1().Namespace(&namespace)
if err != nil {
return nil, err
}
g.Relationship(ns, policyType, n)
}

return n, nil
}

0 comments on commit 66c943d

Please sign in to comment.