This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
graph.go
79 lines (74 loc) · 2.08 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package app
import (
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type resourceLink struct {
Type string `json:"type"`
Source resourceReference `json:"source"`
Target resourceReference `json:"target"`
// ExtraData map[string]interface{} `json:"extraData"`
}
func computeResourceLinks(res unstructured.Unstructured) []resourceLink {
links := make([]resourceLink, 0)
this := unstructuredReference(res)
lonePod := res.GetKind() == "Pod" && res.GetAPIVersion() == "v1"
for _, ref := range res.GetOwnerReferences() {
links = append(links, resourceLink{
Type: "owns",
Source: resourceReference{
UID: string(ref.UID),
Name: ref.Name,
APIVersion: ref.APIVersion,
Kind: ref.Kind,
},
Target: this,
})
if strings.HasPrefix(ref.APIVersion, "apps/") {
lonePod = false
}
}
vols, ok, _ := unstructured.NestedSlice(res.Object, "spec", "template", "spec", "volumes")
if !ok && lonePod {
vols, ok, _ = unstructured.NestedSlice(res.Object, "spec", "volumes")
}
if ok {
for _, x := range vols {
if v, ok := x.(map[string]interface{}); ok {
if s, ok, _ := unstructured.NestedString(v, "secret", "secretName"); ok {
links = append(links, resourceLink{
Type: "mounts",
Source: this,
Target: resourceReference{
Name: s,
APIVersion: "v1",
Kind: "Secret",
},
})
} else if s, ok, _ := unstructured.NestedString(v, "configMap", "name"); ok {
links = append(links, resourceLink{
Type: "mounts",
Source: this,
Target: resourceReference{
Name: s,
APIVersion: "v1",
Kind: "ConfigMap",
},
})
} else if s, ok, _ := unstructured.NestedString(v, "persistentVolumeClaim", "claimName"); ok {
links = append(links, resourceLink{
Type: "mounts",
Source: this,
Target: resourceReference{
Name: s,
APIVersion: "v1",
Kind: "PersistentVolumeClaim",
},
})
}
}
}
}
// TODO(ktravis): add more ownership types
return links
}