Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pod counter to provide index attribute for pods in a ReplicaSet #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 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 @@ -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:
Expand All @@ -266,12 +272,28 @@ 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, 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
else:
parents[parent_name] = 1

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

if running is False:
Expand Down