MCPcopy Create free account
hub / github.com/cozystack/cozystack / WalkOwnershipGraph

Function WalkOwnershipGraph

pkg/lineage/lineage.go:45–151  ·  view source on GitHub ↗
(
	ctx context.Context,
	client dynamic.Interface,
	mapper meta.RESTMapper,
	appMapper AppMapper,
	obj *unstructured.Unstructured,
	memory ...interface{},
)

Source from the content-addressed store, hash-verified

43}
44
45func WalkOwnershipGraph(
46 ctx context.Context,
47 client dynamic.Interface,
48 mapper meta.RESTMapper,
49 appMapper AppMapper,
50 obj *unstructured.Unstructured,
51 memory ...interface{},
52) (out []ObjectID) {
53
54 id := ObjectID{APIVersion: obj.GetAPIVersion(), Kind: obj.GetKind(), Namespace: obj.GetNamespace(), Name: obj.GetName()}
55 out = []ObjectID{}
56 l := log.FromContext(ctx)
57
58 l.Info("processing object", "apiVersion", obj.GetAPIVersion(), "kind", obj.GetKind(), "name", obj.GetName())
59 var visited map[ObjectID]bool
60 var ok bool
61 if len(memory) == 1 {
62 visited, ok = memory[0].(map[ObjectID]bool)
63 if !ok {
64 l.Error(
65 fmt.Errorf("invalid argument"), "could not parse visited map in WalkOwnershipGraph call",
66 "received", memory[0], "expected", "map[ObjectID]bool",
67 )
68 return out
69 }
70 }
71
72 if len(memory) == 0 {
73 visited = make(map[ObjectID]bool)
74 }
75
76 if len(memory) != 0 && len(memory) != 1 {
77 l.Error(
78 fmt.Errorf("invalid argument count"), "could not parse variadic arguments to WalkOwnershipGraph",
79 "args passed", len(memory)+5, "expected args", "4|5",
80 )
81 return out
82 }
83
84 if visited[id] {
85 return out
86 }
87
88 visited[id] = true
89
90 ownerRefs := obj.GetOwnerReferences()
91 for _, owner := range ownerRefs {
92 ownerObj, err := getUnstructuredObject(ctx, client, mapper, owner.APIVersion, owner.Kind, obj.GetNamespace(), owner.Name)
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "Could not fetch owner %s/%s (%s): %v\n", obj.GetNamespace(), owner.Name, owner.Kind, err)
95 continue
96 }
97
98 out = append(out, WalkOwnershipGraph(ctx, client, mapper, appMapper, ownerObj, visited)...)
99 }
100
101 // if object has owners, it couldn't be owned directly by the custom app
102 if len(ownerRefs) > 0 {

Callers 2

getOwnerMethod · 0.92

Calls 8

getUnstructuredObjectFunction · 0.85
GetAPIVersionMethod · 0.80
GetKindMethod · 0.80
GetNamespaceMethod · 0.65
GetNameMethod · 0.65
MapMethod · 0.65
ErrorMethod · 0.45

Tested by 1