| 43 | } |
| 44 | |
| 45 | func 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 { |