kubernetes/pkg/kubelet
Sascha Grunert b296f82c69
Sort kubelet pods by their creation time
There is a corner case when blocking Pod termination via a lifecycle
preStop hook, for example by using this StateFulSet:

```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: ubi
  serviceName: "ubi"
  replicas: 1
  template:
    metadata:
      labels:
        app: ubi
    spec:
      terminationGracePeriodSeconds: 1000
      containers:
      - name: ubi
        image: ubuntu:22.04
        command: ['sh', '-c', 'echo The app is running! && sleep 360000']
        ports:
        - containerPort: 80
          name: web
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - -c
              - 'echo aaa; trap : TERM INT; sleep infinity & wait'
```

After creation, downscaling, forced deletion and upscaling of the
replica like this:

```
> kubectl apply -f sts.yml
> kubectl scale sts web --replicas=0
> kubectl delete pod web-0 --grace-period=0 --force
> kubectl scale sts web --replicas=1
```

We will end up having two pods running by the container runtime, while
the API only reports one:

```
> kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          92s
```

```
> sudo crictl pods
POD ID              CREATED              STATE     NAME     NAMESPACE     ATTEMPT     RUNTIME
e05bb7dbb7e44       12 minutes ago       Ready     web-0    default       0           (default)
d90088614c73b       12 minutes ago       Ready     web-0    default       0           (default)
```

When now running `kubectl exec -it web-0 -- ps -ef`, there is a random chance that we hit the wrong
container reporting the lifecycle command `/bin/sh -c echo aaa; trap : TERM INT; sleep infinity & wait`.

This is caused by the container lookup via its name (and no podUID) at:
02109414e8/pkg/kubelet/kubelet_pods.go (L1905-L1914)

And more specifiy by the conversion of the pod result map to a slice in `GetPods`:
02109414e8/pkg/kubelet/kuberuntime/kuberuntime_manager.go (L407-L411)

We now solve that unexpected behavior by tracking the creation time of
the pod and sorting the result based on that. This will cause to always
match the most recently created pod.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-10-13 16:32:44 +02:00
..
apis Use https links for k8s KEPs, issues, PRs, etc 2022-09-23 23:36:24 +00:00
cadvisor Promote Local storage capacity isolation feature to GA 2022-08-02 23:45:48 -07:00
certificate Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
checkpointmanager
client Merge pull request #96004 from serathius/datapolicy-kubelet-pkg 2022-03-04 15:34:51 -08:00
cloudresource
cm Optimize: file /cpuset slice make cap (#112270) 2022-09-30 16:56:25 -07:00
config Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
configmap Generate and format files 2022-07-26 13:14:05 -04:00
container Sort kubelet pods by their creation time 2022-10-13 16:32:44 +02:00
cri Merge pull request #112709 from swagatbora90/kubelet-tracing 2022-10-04 14:12:00 -07:00
envvars
events
eviction Promote Local storage capacity isolation feature to GA 2022-08-02 23:45:48 -07:00
images reword image gc failure log 2022-09-20 21:57:59 -05:00
kubeletconfig Fix comments 2022-09-18 12:51:03 +09:00
kuberuntime Sort kubelet pods by their creation time 2022-10-13 16:32:44 +02:00
leaky
lifecycle Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
logs Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
metrics kubelet: Record a metric for latency of pod status update 2022-09-08 12:17:44 -04:00
network Merge pull request #112414 from pacoxu/kubelet-multi-options 2022-09-29 21:10:28 -07:00
nodeshutdown Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
nodestatus Delete the cloud node IP annotation if it is stale 2022-09-01 16:43:18 -04:00
oom generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
pleg refactor: pleg/getContainersFromPods 2022-04-06 14:12:52 +08:00
pluginmanager Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
pod Remove EphemeralContainers feature-gate checks 2022-07-26 02:55:30 +02:00
preemption migrated preemption.go, stateful.go, resource_allocation.go to structured logging 2021-11-08 22:52:47 +05:30
prober Merge pull request #104484 from jackfrancis/prober-duration-metrics 2022-07-29 13:17:11 -07:00
qos
runtimeclass Generate and format files 2022-07-26 13:14:05 -04:00
secret Generate and format files 2022-07-26 13:14:05 -04:00
server Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
stats Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
status kubelet: Record a metric for latency of pod status update 2022-09-08 12:17:44 -04:00
sysctl fix test order in pkg/kubelet/sysctl/util_test.go 2022-09-08 17:20:22 +08:00
token Merge pull request #99685 from yangjunmyfm192085/run-test24 2022-05-03 17:16:47 -07:00
types Introduce PodHasNetwork condition for pods 2022-08-01 09:51:43 -07:00
util Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
volumemanager Add unit test in volumemanager: Detach a volume that had been mounted by pod should be skipped 2022-08-05 09:03:21 +08:00
winstats Merge pull request #111418 from muyangren2/winstats_assert 2022-07-29 19:29:29 -07:00
active_deadline_test.go Migrate to k8s.io/utils/clock in pkg/kubelet 2021-09-10 12:20:09 +02:00
active_deadline.go Migrate to k8s.io/utils/clock in pkg/kubelet 2021-09-10 12:20:09 +02:00
doc.go
errors.go
kubelet_getters_test.go Add test case for getPodVolumeSubpathsDir 2022-04-27 16:33:28 +08:00
kubelet_getters.go kubelet: increase log level for Path does not exist message 2022-09-21 14:16:44 -05:00
kubelet_network_linux.go Add IPTablesOwnershipCleanup feature to disable kubelet iptables setup 2022-07-27 13:33:09 -04:00
kubelet_network_others.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
kubelet_network_test.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
kubelet_network.go Move iptables consts to kubelet_network_linux.go. 2022-03-26 11:22:51 -04:00
kubelet_node_status_others.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
kubelet_node_status_test.go Promote Local storage capacity isolation feature to GA 2022-08-02 23:45:48 -07:00
kubelet_node_status_windows.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
kubelet_node_status.go Revert "Avoid tainting with NoSchedule when DisableCloudProviders feature is on" 2022-10-03 15:13:43 -04:00
kubelet_pods_linux_test.go Promote Local storage capacity isolation feature to GA 2022-08-02 23:45:48 -07:00
kubelet_pods_test.go Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
kubelet_pods_windows_test.go Clean up logic for deprecated flag --container-runtime in kubelet 2022-02-10 13:26:59 +02:00
kubelet_pods.go Merge pull request #112097 from wongearl/cleanup_loop 2022-09-30 18:04:12 -07:00
kubelet_resources_test.go
kubelet_resources.go
kubelet_test.go Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
kubelet_volumes_linux_test.go Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
kubelet_volumes_test.go Upgrade CSIMigrationGCE feature gate to GA 2022-08-02 09:14:27 -07:00
kubelet_volumes.go remove ioutil in kubelet 2022-04-27 21:08:42 +08:00
kubelet.go Merge pull request #112709 from swagatbora90/kubelet-tracing 2022-10-04 14:12:00 -07:00
OWNERS Check in OWNERS modified by update-yamlfmt.sh 2021-12-09 21:31:26 -05:00
pod_container_deletor_test.go
pod_container_deletor.go
pod_workers_test.go Merge pull request #110071 from gjkim42/deflake-TestStaticPodExclusion 2022-07-29 13:17:43 -07:00
pod_workers.go Generate and format files 2022-07-26 13:14:05 -04:00
reason_cache_test.go
reason_cache.go Generate and format files 2022-07-26 13:14:05 -04:00
runonce_test.go Promote Local storage capacity isolation feature to GA 2022-08-02 23:45:48 -07:00
runonce.go kubelet: Delay writing a terminal phase until the pod is terminated 2022-03-16 13:15:00 -04:00
runtime.go
userns_manager_test.go kubelet: drop bitArray implementation 2022-08-19 16:55:15 +02:00
userns_manager.go kubelet: drop bitArray implementation 2022-08-19 16:55:15 +02:00
volume_host.go kubelet: add GetHostIDsForPod() 2022-08-03 19:53:22 +02:00