Skip to content

Commit

Permalink
Merge pull request #15 from steveteuber/labels
Browse files Browse the repository at this point in the history
Add labels to the cypher output format
  • Loading branch information
steveteuber authored Oct 22, 2021
2 parents 6259f93 + efe3210 commit be9774f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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

0 comments on commit be9774f

Please sign in to comment.