Skip to content

Commit

Permalink
Add pod counter to provide index attribute for pods in a deployment R…
Browse files Browse the repository at this point in the history
…eplicaSet
  • Loading branch information
Karl DeBisschop committed May 23, 2022
1 parent 7824480 commit 7335393
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions contents/pods-resource-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get(self, path, default=None):
log = logging.getLogger('kubernetes-model-source')


def nodeCollectData(pod, container, defaults, taglist, mappingList, boEmoticon):
def nodeCollectData(pod, container, defaults, taglist, mappingList, boEmoticon, index):
tags = []
tags.extend(taglist.split(','))

Expand Down Expand Up @@ -105,6 +105,9 @@ def nodeCollectData(pod, container, defaults, taglist, mappingList, boEmoticon):
mappings = []
custom_attributes = {}

# Count pods within their owner
custom_attributes['index'] = index

# custom mapping attributes
if mappingList:
log.debug('Mapping: %s', mappingList)
Expand Down Expand Up @@ -206,6 +209,8 @@ def main():
label_selector = os.environ.get('RD_CONFIG_LABEL_SELECTOR')

node_set = []
# Used to count child pods, particularly of a (possibly autoscaling) ReplicaSet.
parents = {}
v1 = client.CoreV1Api()

log.debug(label_selector)
Expand Down Expand Up @@ -244,12 +249,22 @@ def main():
i.metadata.name,
container.name)

# For scalable pods in a deployment, the ReplicaSet of a pod is the pod name with the last dash-separated
# token stripped off. If we have seen the ReplicaSet already, add one. Otherwise, initialize this as the
# first pod in the ReplicaSet.
parent_name = '-'.join(i.metadata.name.split('-')[0:-1])
if parent_name in parents:
parents[parent_name] += 1
else:
parents[parent_name] = 1

node_data = nodeCollectData(i,
container,
defaults,
tags,
mappingList,
boEmoticon
boEmoticon,
parents[parent_name]
)

if running is False:
Expand Down

0 comments on commit 7335393

Please sign in to comment.