0
0
Jenkinsdevops~5 mins

Kubernetes agents in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Kubernetes agents
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of jobs (n) increases, the system creates more pod agents, each taking time to start.

Input Size (n)Approx. Operations
1010 pod creations and job runs
100100 pod creations and job runs
10001000 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.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how Jenkins uses Kubernetes agents helps you explain how cloud resources scale with workload, a key skill in DevOps roles.

Self-Check

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?