|
| 1 | +--- |
| 2 | +title: "Test 1: ChunkHound Response" |
| 3 | +description: "kubectl apply flow analysis with ChunkHound semantic search" |
| 4 | +--- |
| 5 | + |
| 6 | +# Complete Code Path: kubectl apply -f deployment.yaml to Running Pods |
| 7 | + |
| 8 | +This document traces the complete journey from executing `kubectl apply -f deployment.yaml` to pods running on nodes, showing all major components, key functions, and data structures involved in the process. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The flow involves six major components working together: |
| 13 | +1. **kubectl client** - Parses YAML and sends API requests |
| 14 | +2. **API Server** - Validates and stores Deployment objects |
| 15 | +3. **Deployment Controller** - Creates ReplicaSets from Deployments |
| 16 | +4. **ReplicaSet Controller** - Creates Pods from ReplicaSets |
| 17 | +5. **Scheduler** - Assigns Pods to nodes |
| 18 | +6. **kubelet** - Creates containers on assigned nodes |
| 19 | + |
| 20 | +## 1. kubectl apply Command Implementation |
| 21 | + |
| 22 | +### Entry Point |
| 23 | +- **File**: `cmd/kubectl/kubectl.go:18` |
| 24 | +- **Function**: `main()` → `cmd.NewDefaultKubectlCommand()` |
| 25 | + |
| 26 | +### Core Apply Logic |
| 27 | +- **File**: `staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go` |
| 28 | +- **Key Functions**: |
| 29 | + - `NewCmdApply()` (line 198): Creates apply command with Run function |
| 30 | + - `ApplyOptions.Run()` (line 500): Main apply execution |
| 31 | + - Calls `o.GetObjects()` to parse YAML files into `resource.Info` objects |
| 32 | + - Iterates through each object calling `o.applyOneObject(info)` |
| 33 | + - `applyOneObject()` (line 555): Handles individual resource application |
| 34 | + - Creates `resource.Helper` with client and mapping information |
| 35 | + - For server-side apply: calls `helper.Patch()` with `ApplyPatchType` |
| 36 | + - Uses `staging/src/k8s.io/cli-runtime/pkg/resource/helper.go:241` Patch method |
| 37 | + - Makes REST call to API server: `m.RESTClient.Patch(pt).NamespaceIfScoped(...).Do()` |
| 38 | + |
| 39 | +### Data Flow |
| 40 | +``` |
| 41 | +YAML file → Unstructured objects → REST PATCH request → API server |
| 42 | +``` |
| 43 | + |
| 44 | +## 2. API Server Request Handling for Deployments |
| 45 | + |
| 46 | +### Registry Implementation |
| 47 | +- **File**: `pkg/registry/apps/deployment/storage/storage.go` |
| 48 | +- **Function**: `NewREST()` (line 93): Creates deployment REST storage with `generic.Store` |
| 49 | + |
| 50 | +### Validation Strategy |
| 51 | +- **File**: `pkg/registry/apps/deployment/strategy.go` |
| 52 | +- **Key Functions**: |
| 53 | + - `PrepareForCreate()` (line 73): Sets initial status, generation = 1 |
| 54 | + - `Validate()` (line 83): Calls `appsvalidation.ValidateDeployment()` |
| 55 | + - `ValidateUpdate()` (line 127): Validates deployment updates |
| 56 | + |
| 57 | +### Processing Flow |
| 58 | +1. HTTP PATCH request hits API server |
| 59 | +2. Authentication/authorization checks |
| 60 | +3. Admission controllers run |
| 61 | +4. Validation via deployment strategy |
| 62 | +5. Object stored in etcd |
| 63 | +6. Event sent to watchers |
| 64 | + |
| 65 | +## 3. Deployment Controller Creating ReplicaSets |
| 66 | + |
| 67 | +### Controller Setup |
| 68 | +- **File**: `cmd/kube-controller-manager/app/apps.go:120` |
| 69 | +- **Function**: `newDeploymentController()`: Creates deployment controller with informers |
| 70 | + |
| 71 | +### Core Controller Logic |
| 72 | +- **File**: `pkg/controller/deployment/deployment_controller.go` |
| 73 | +- **Key Functions**: |
| 74 | + - `NewDeploymentController()` (line 101): Sets up informers and event handlers |
| 75 | + - `syncHandler = dc.syncDeployment` (line 149): Main sync function |
| 76 | + |
| 77 | +### ReplicaSet Creation Logic |
| 78 | +- **File**: `pkg/controller/deployment/sync.go` |
| 79 | +- **Key Functions**: |
| 80 | + - `getNewReplicaSet()` (line 200+): Core ReplicaSet creation logic |
| 81 | + - Generates ReplicaSet from deployment template |
| 82 | + - Calls `dc.client.AppsV1().ReplicaSets(d.Namespace).Create()` (line 231) |
| 83 | + - Handles hash collisions and retries |
| 84 | + |
| 85 | +### Data Structures |
| 86 | +- **Input**: `apps.Deployment` object |
| 87 | +- **Output**: `apps.ReplicaSet` with: |
| 88 | + - Template from `deployment.Spec.Template` |
| 89 | + - `OwnerReference` pointing to deployment |
| 90 | + - Generated name with hash suffix |
| 91 | + |
| 92 | +## 4. ReplicaSet Controller Creating Pods |
| 93 | + |
| 94 | +### Controller Setup |
| 95 | +- **File**: `cmd/kube-controller-manager/app/apps.go:93` |
| 96 | +- **Function**: `newReplicaSetController()`: Creates ReplicaSet controller |
| 97 | + |
| 98 | +### Core Logic |
| 99 | +- **File**: `pkg/controller/replicaset/replica_set.go` |
| 100 | +- **Key Functions**: |
| 101 | + - `syncReplicaSet()` (line 702): Main sync function |
| 102 | + - Calls `rsc.claimPods()` to find existing pods |
| 103 | + - Calls `rsc.manageReplicas()` to scale up/down |
| 104 | + - `manageReplicas()` (line 596): Pod creation logic |
| 105 | + - Calculates diff between desired and actual replicas |
| 106 | + - For scale up: calls `slowStartBatch()` with pod creation function |
| 107 | + - **Pod Creation**: `rsc.podControl.CreatePods()` (line 625) |
| 108 | + - Creates pods from `ReplicaSet.Spec.Template` |
| 109 | + - Sets `OwnerReference` to ReplicaSet |
| 110 | + - Calls API server to create pod objects |
| 111 | + |
| 112 | +### Data Flow |
| 113 | +``` |
| 114 | +ReplicaSet → Pod templates → API server pod creation |
| 115 | +``` |
| 116 | + |
| 117 | +## 5. Scheduler Pod Assignment Logic |
| 118 | + |
| 119 | +### Main Scheduler |
| 120 | +- **File**: `pkg/scheduler/schedule_one.go` |
| 121 | +- **Key Functions**: |
| 122 | + - `ScheduleOne()` (line 66): Main scheduling loop |
| 123 | + - Gets next pod from queue via `sched.NextPod()` |
| 124 | + - Calls `sched.schedulingCycle()` which calls `sched.SchedulePod()` |
| 125 | + - `schedulePod()` (line 430): Core scheduling algorithm |
| 126 | + - Updates node snapshot: `sched.Cache.UpdateSnapshot()` |
| 127 | + - **Filtering**: `sched.findNodesThatFitPod()` - runs predicates/filters |
| 128 | + - **Scoring**: `prioritizeNodes()` - scores feasible nodes |
| 129 | + - **Selection**: `selectHost()` - picks highest scoring node |
| 130 | + - Returns `ScheduleResult{SuggestedHost: host}` |
| 131 | + |
| 132 | +### Algorithm Flow |
| 133 | +1. **Pod predicates**: NodeResourcesFit, NodeAffinity, etc. |
| 134 | +2. **Node scoring**: Resource balancing, affinity preferences |
| 135 | +3. **Host selection**: Highest scoring node |
| 136 | +4. **Binding creation**: Creates Binding object to assign pod to node |
| 137 | + |
| 138 | +## 6. kubelet Pod Creation and Container Runtime |
| 139 | + |
| 140 | +### kubelet Main Loop |
| 141 | +- **File**: `pkg/kubelet/kubelet.go` |
| 142 | +- **Key Components**: |
| 143 | + - `podWorkers` (line 1140+): Manages pod lifecycle state machine |
| 144 | + - States: syncing (syncPod), terminating, terminated |
| 145 | + - `UpdatePod()`: Notifies workers of pod changes (line 2958) |
| 146 | + |
| 147 | +### Pod Sync Process |
| 148 | +- **Function**: `SyncPod()` (around line 2050) |
| 149 | + 1. **Pre-sync**: Create pod directories, ensure cgroups exist |
| 150 | + 2. **Volume Setup**: `kl.volumeManager.WaitForAttachAndMount()` |
| 151 | + 3. **Container Runtime**: `kl.containerRuntime.SyncPod()` |
| 152 | + |
| 153 | +### Container Runtime Integration |
| 154 | +- **File**: `pkg/kubelet/kuberuntime/kuberuntime_manager.go` |
| 155 | +- **Key Functions**: |
| 156 | + - `SyncPod()`: Main container sync logic |
| 157 | + - `computePodActions()` (line 1007): Determines what containers need creation/restart |
| 158 | + - **Pod Sandbox**: Creates pod sandbox (network namespace) |
| 159 | + - **Container Creation**: Creates and starts individual containers |
| 160 | + - **CRI Interface**: Communicates with container runtime (Docker, containerd, CRI-O) |
| 161 | + |
| 162 | +## Controller Coordination and Data Flow |
| 163 | + |
| 164 | +### Objects Passed Between Components |
| 165 | +1. **kubectl**: `resource.Info` containing `*apps.Deployment` |
| 166 | +2. **API Server**: `apps.Deployment` → etcd storage |
| 167 | +3. **Deployment Controller**: `apps.Deployment` → `apps.ReplicaSet` |
| 168 | +4. **ReplicaSet Controller**: `apps.ReplicaSet` → `v1.Pod` |
| 169 | +5. **Scheduler**: `v1.Pod` → `v1.Binding` |
| 170 | +6. **kubelet**: `v1.Pod` → Container runtime calls |
| 171 | + |
| 172 | +### Control Flow Coordination |
| 173 | +- **Event-driven Architecture**: All controllers watch API server via informers |
| 174 | +- **Work Queues**: Controllers use rate-limited queues for retries |
| 175 | +- **Owner References**: Child objects point to parents for cleanup |
| 176 | +- **Expectations**: Controllers track expected vs actual state changes |
| 177 | + |
| 178 | +### State Transitions |
| 179 | +``` |
| 180 | +kubectl apply → API Server → Deployment Controller → ReplicaSet Controller → Scheduler → kubelet → Container Runtime |
| 181 | + ↓ ↓ ↓ ↓ ↓ ↓ |
| 182 | + PATCH request → Deployment → ReplicaSet created → Pod created → Pod scheduled → Container running |
| 183 | +``` |
| 184 | + |
| 185 | +## Summary of Key File Locations |
| 186 | + |
| 187 | +| Component | File Path | Key Function | Line | |
| 188 | +|-----------|-----------|--------------|------| |
| 189 | +| kubectl apply | `staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go` | `ApplyOptions.Run()` | 500 | |
| 190 | +| Deployment registry | `pkg/registry/apps/deployment/storage/storage.go` | `NewREST()` | 93 | |
| 191 | +| Deployment controller | `pkg/controller/deployment/sync.go` | ReplicaSet creation | 231 | |
| 192 | +| ReplicaSet controller | `pkg/controller/replicaset/replica_set.go` | Pod creation | 625 | |
| 193 | +| Scheduler | `pkg/scheduler/schedule_one.go` | `schedulePod()` | 430 | |
| 194 | +| kubelet | `pkg/kubelet/kubelet.go` | `SyncPod()` | 2050+ | |
| 195 | + |
| 196 | +This complete trace shows how a single `kubectl apply` command triggers a cascade of controllers, each watching for changes and creating the next level of objects until pods are running on nodes with containers started by the container runtime. |
0 commit comments