Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels to the cypher output format #15

Merged
merged 1 commit into from
Oct 22, 2021
Merged
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
31 changes: 30 additions & 1 deletion pkg/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/md5"
"fmt"
"io"
"regexp"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -64,7 +65,29 @@ type Graph struct {
}

// Node represents a node in the graph.
type Node v1.ObjectReference
type Node struct {
APIVersion string
Kind string
Labels Labels
Name string
Namespace string
UID types.UID
}

// Labels is a map of key:value.
type Labels map[string]string

// String returns all fields listed as a Neo4j property string.
func (labels Labels) String() string {
selector := make([]string, 0, len(labels))
for key, value := range labels {
property := regexp.MustCompile(`[^a-z0-9]+`).ReplaceAllString(strings.ToLower(key), "_")
selector = append(selector, fmt.Sprintf("node.Label_%s = \"%s\"", property, value))
}

sort.StringSlice(selector).Sort()
return strings.Join(selector, ", ")
}

// Relationship represents a relationship between nodes in the graph.
type Relationship struct {
Expand Down Expand Up @@ -171,11 +194,17 @@ func (g *Graph) Node(gvk schema.GroupVersionKind, obj metav1.Object) *Node {
if g.Nodes[obj.GetClusterName()][obj.GetNamespace()] == nil {
g.Nodes[obj.GetClusterName()][obj.GetNamespace()] = make(map[types.UID]*Node)
}
if n, ok := g.Nodes[obj.GetClusterName()][obj.GetNamespace()][obj.GetUID()]; ok {
if len(n.Labels) != 0 {
obj.SetLabels(n.Labels)
}
}

apiVersion, kind := gvk.ToAPIVersionAndKind()
node := &Node{
APIVersion: apiVersion,
Kind: kind,
Labels: obj.GetLabels(),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
UID: obj.GetUID(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/graph/templates/cypher.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ MERGE (node:Cluster {UID: "{{ $cluster }}"}) ON CREATE SET node.Name = "{{ $clus
{{- if $namespace }}
MERGE (node:Namespace {UID: "{{ $namespace }}"}) ON CREATE SET node.ClusterName = "{{ $cluster }}", node.Name = "{{ $namespace }}";
{{- range . }}
MERGE (node:{{ .Kind }} {UID: "{{ .UID }}"}) ON CREATE SET node.ClusterName = "{{ $cluster }}", node.Namespace = "{{ .Namespace }}", node.Name = "{{ .Name }}";
MERGE (node:{{ .Kind }} {UID: "{{ .UID }}"}) ON CREATE SET node.ClusterName = "{{ $cluster }}", node.Namespace = "{{ .Namespace }}", node.Name = "{{ .Name }}"{{ if .Labels }}, {{ .Labels }}{{ end }};
{{- end }}
{{- else }}
{{- range . }}
MERGE (node:{{ .Kind }} {UID: "{{ .UID }}"}) ON CREATE SET node.ClusterName = "{{ $cluster }}", node.Name = "{{ .Name }}";
MERGE (node:{{ .Kind }} {UID: "{{ .UID }}"}) ON CREATE SET node.ClusterName = "{{ $cluster }}", node.Name = "{{ .Name }}"{{ if .Labels }}, {{ .Labels }}{{ end }};
{{- end }}
{{- end }}
{{- end }}
Expand Down