Kubernetes agents in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run Jenkins jobs on Kubernetes agents changes as we add more jobs or agents.
How does the system handle more work and what costs grow with more agents?
Analyze the time complexity of the following Jenkins pipeline code that runs jobs on Kubernetes agents.
pipeline {
agent {
kubernetes {
label 'k8s-agent'
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent
'''
}
}
stages {
stage('Build') {
steps {
sh 'make build'
}
}
}
}
This pipeline runs a build stage on a Kubernetes pod agent created for the job.
Look for repeated actions that affect time.
- Primary operation: Creating a Kubernetes pod agent for each job run.
- How many times: Once per job execution, repeated for each job in the queue.
As the number of jobs (n) increases, the system creates more pod agents, each taking time to start.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pod creations and job runs |
| 100 | 100 pod creations and job runs |
| 1000 | 1000 pod creations and job runs |
Pattern observation: The time grows roughly in direct proportion to the number of jobs because each job needs its own pod agent.
Time Complexity: O(n)
This means the total time grows linearly as you add more jobs, since each job requires creating and running on a separate Kubernetes agent pod.
[X] Wrong: "Adding more jobs will not increase total time because pods run in parallel."
[OK] Correct: While pods can run in parallel, the cluster resources and Jenkins controller limits mean pods may queue or delay, so total time still grows with more jobs.
Understanding how Jenkins uses Kubernetes agents helps you explain how cloud resources scale with workload, a key skill in DevOps roles.
What if we changed from creating a new pod agent per job to reusing a fixed number of pod agents? How would the time complexity change?