How to recursively get dependent resources of Kubernetes owner resource How to recursively get dependent resources of Kubernetes owner resource kubernetes kubernetes

How to recursively get dependent resources of Kubernetes owner resource


There are no ready solutions for this.

I see two options how this can be proceeded:


1 - probably this is what you already mentioned: "need to use a custom script to do it".

Idea is to get jsons of required resource groups and then process it by any available/known language like bash/python/java/etc and/or using jq. All dependent objects have ownerReference field which allows to match resources.

More information about owners and dependents

jq tool and examples


2 - Write your own tool based on kubernetes garbage collector

Kubernetes garbage collector works based on graph built by GraphBuilder:

garbage collector source code

Graph is always up to date by using `reflectors:

GarbageCollector runs reflectors to watch for changes of managed APIobjects, funnels the results to a single-threadeddependencyGraphBuilder, which builds a graph caching the dependenciesamong objects

graph_builder source code to get whole logic of it.

Built graph has node type:

graph data structure

Also it's worth to mention that working with api server is more convenient using kubernetes clients libraries which are available for different languages.