From f82a1ccaf221dd0d52eb458cb1bd7c4e14301b48 Mon Sep 17 00:00:00 2001 From: Karl DeBisschop Date: Thu, 5 May 2022 18:05:11 -0400 Subject: [PATCH 1/2] Add pod counter to provide index attribute for pods in a deployment ReplicaSet --- contents/pods-resource-model.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/contents/pods-resource-model.py b/contents/pods-resource-model.py index 485a8e9..fed03d9 100644 --- a/contents/pods-resource-model.py +++ b/contents/pods-resource-model.py @@ -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(',')) @@ -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) @@ -256,6 +259,9 @@ def main(): node_set = [] + # Used to count child pods, particularly of a (possibly autoscaling) ReplicaSet. + parents = {} + ret = collect_pods_from_api(namespace_filter, label_selector, field_selector) for i in ret.items: @@ -266,12 +272,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: From 4ec636681503638b0690eab4e780a9277ee69a93 Mon Sep 17 00:00:00 2001 From: Karl DeBisschop Date: Mon, 6 Jun 2022 08:48:01 -0400 Subject: [PATCH 2/2] Provide an example in the comment explaining how a ReplicaSet is indexed by pod. --- contents/pods-resource-model.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/contents/pods-resource-model.py b/contents/pods-resource-model.py index fed03d9..f1bd5d1 100644 --- a/contents/pods-resource-model.py +++ b/contents/pods-resource-model.py @@ -273,8 +273,14 @@ def main(): 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. + # token stripped off. If we have seen the ReplicaSet already, increment the counter. Otherwise, initialize + # this as the first pod in the ReplicaSet. + # + # For example, the deployment "my-deployment" might create a ReplicaSet named "my-deployment-5ffd8f676d" + # and one of the pods within the ReplicaSet might be identified as "my-deployment-5ffd8f676d-ccwbf". By + # removing the suffix that identifies the pod, we index resources within the parent group. In this way, we + # can ensure that only one pod per ReplicaSet runs a command by filtering the nodes in Rundeck to include + # only those where index is equal to 1. parent_name = '-'.join(i.metadata.name.split('-')[0:-1]) if parent_name in parents: parents[parent_name] += 1