Skip to content

Commit

Permalink
Merge pull request #13 from steveteuber/ingress
Browse files Browse the repository at this point in the history
Add v1.Ingress resource to the Graph
  • Loading branch information
steveteuber authored Oct 13, 2021
1 parent ceab107 commit 430cbb7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/graph/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ func (g *CoreV1Graph) ObjectReference(obj *v1.ObjectReference) (*Node, error) {
return n, nil
}

// TypedLocalObjectReference adds a v1.TypedLocalObjectReference resource to the Graph.
func (g *CoreV1Graph) TypedLocalObjectReference(obj *v1.TypedLocalObjectReference, namespace string) (*Node, error) {
n := g.graph.Node(
schema.FromAPIVersionAndKind(v1.GroupName, *obj.APIGroup),
&metav1.ObjectMeta{
UID: ToUID(obj.APIGroup, obj.Kind, obj.Name),
Name: obj.Name,
Namespace: namespace,
},
)

return n, nil
}

// Service adds a v1.Service resource to the Graph.
func (g *CoreV1Graph) Service(obj *v1.Service) (*Node, error) {
switch obj.Spec.Type {
Expand Down
63 changes: 63 additions & 0 deletions pkg/graph/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (g *Graph) NetworkingV1() *NetworkingV1Graph {
// Unstructured adds an unstructured node to the Graph.
func (g *NetworkingV1Graph) Unstructured(unstr *unstructured.Unstructured) (err error) {
switch unstr.GetKind() {
case "Ingress":
obj := &v1.Ingress{}
if err = FromUnstructured(unstr, obj); err != nil {
return err
}
_, err = g.Ingress(obj)
case "NetworkPolicy":
obj := &v1.NetworkPolicy{}
if err = FromUnstructured(unstr, obj); err != nil {
Expand Down Expand Up @@ -75,6 +81,63 @@ func (g *NetworkingV1Graph) Relationship(from *Node, policyType v1.PolicyType, t
return r.Attribute("style", "dashed")
}

// Ingress adds a v1.Ingress resource to the Graph.
func (g *NetworkingV1Graph) Ingress(obj *v1.Ingress) (*Node, error) {
n := g.graph.Node(obj.GroupVersionKind(), obj)

for _, rule := range obj.Spec.Rules {
if rule.HTTP != nil {
for _, path := range rule.HTTP.Paths {
b, err := g.IngressBackend(obj, path.Backend)
if err != nil {
return nil, err
}
g.Relationship(b, v1.PolicyTypeIngress, n)
}
}

h, err := g.Host(rule.Host)
if err != nil {
return nil, err
}
g.Relationship(n, v1.PolicyTypeIngress, h)
}

return n, nil
}

// IngressBackend adds a v1.IngressBackend resource to the Graph.
func (g *NetworkingV1Graph) IngressBackend(obj *v1.Ingress, backend v1.IngressBackend) (*Node, error) {
switch {
case backend.Service != nil:
options := metav1.GetOptions{}
service, err := g.graph.clientset.CoreV1().Services(obj.GetNamespace()).Get(context.TODO(), backend.Service.Name, options)
if err != nil {
return nil, err
}

return g.graph.CoreV1().Service(service)
case backend.Resource != nil:
return g.graph.CoreV1().TypedLocalObjectReference(backend.Resource, obj.GetNamespace())
}

return nil, fmt.Errorf("%v: backend is not supported yet", obj.GroupVersionKind())
}

// Host adds a v1.Host resource to the Graph.
func (g *NetworkingV1Graph) Host(name string) (*Node, error) {
n := g.graph.Node(
schema.FromAPIVersionAndKind(v1.GroupName, "Host"),
&metav1.ObjectMeta{
ClusterName: "External",
UID: ToUID(name),
Name: name,
},
)

return n, nil
}

// NetworkPolicy adds a v1.NetworkPolicy resource to the Graph.
func (g *NetworkingV1Graph) NetworkPolicy(obj *v1.NetworkPolicy) (*Node, error) {
n := g.graph.Node(obj.GroupVersionKind(), obj)
Expand Down

0 comments on commit 430cbb7

Please sign in to comment.