kubernetes/pkg/kubelet/kuberuntime
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
..
logs Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
util kubelet: set user namespace options 2022-08-03 19:53:22 +02:00
convert_test.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
convert.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
doc.go
fake_kuberuntime_manager.go clarify CPUCFSQuotaPeriod values, set the minimum to 1ms 2022-09-08 23:29:13 +02:00
helpers_linux_test.go update import of generic featuregate code from k8s.io/apiserver/pkg/util/feature -> k8s.io/component-base/featuregate 2019-05-08 10:01:50 -04:00
helpers_linux.go clarify CPUCFSQuotaPeriod values, set the minimum to 1ms 2022-09-08 23:29:13 +02:00
helpers_test.go Merge pull request #111358 from ddebroy/hasnet1 2022-08-01 15:04:52 -07:00
helpers_unsupported.go Fix cpu share issues on systems with large amounts of cpu 2021-11-14 19:49:19 +00:00
helpers.go Merge pull request #111358 from ddebroy/hasnet1 2022-08-01 15:04:52 -07:00
instrumented_services_test.go Add support for CRI verbose fields 2022-02-10 17:12:26 +01:00
instrumented_services.go CRI changes to support implementation of in-place pod resize. 2022-08-02 15:08:25 -07:00
kuberuntime_container_linux_test.go Merge pull request #108832 from waynepeking348/fix_bugs_of_container_cpu_shares 2022-08-23 16:04:03 -07:00
kuberuntime_container_linux.go clarify CPUCFSQuotaPeriod values, set the minimum to 1ms 2022-09-08 23:29:13 +02:00
kuberuntime_container_test.go Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
kuberuntime_container_unsupported.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
kuberuntime_container_windows_test.go unittests: Adds Windows unittests 2022-07-08 17:24:15 +03:00
kuberuntime_container_windows.go Update design-proposals URL 2022-08-02 09:13:38 +08:00
kuberuntime_container.go Merge pull request #111221 from inosato/remove-ioutil-from-kubelet 2022-09-17 21:56:28 -07:00
kuberuntime_gc_test.go Remove ioutil in kubelet and its tests 2022-07-30 12:35:26 +09:00
kuberuntime_gc.go Generate and format files 2022-07-26 13:14:05 -04:00
kuberuntime_image_test.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
kuberuntime_image.go Add support for CRI verbose fields 2022-02-10 17:12:26 +01:00
kuberuntime_logs.go
kuberuntime_manager_test.go Sort kubelet pods by their creation time 2022-10-13 16:32:44 +02:00
kuberuntime_manager.go Sort kubelet pods by their creation time 2022-10-13 16:32:44 +02:00
kuberuntime_sandbox_linux_test.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
kuberuntime_sandbox_linux.go fix bugs of container cpu shares when cpu request set to zero 2022-03-20 21:53:22 +08:00
kuberuntime_sandbox_test.go Partly remove support for seccomp annotations 2022-08-01 09:19:29 +02:00
kuberuntime_sandbox_unsupported.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
kuberuntime_sandbox_windows.go Make CRI v1 the default and allow a fallback to v1alpha2 2021-11-17 11:05:05 -08:00
kuberuntime_sandbox.go kubelet: propagate errors from namespacesForPod 2022-08-03 19:53:22 +02:00
labels_test.go De-share the Handler struct in core API (#105979) 2021-10-29 13:15:11 -07:00
labels.go De-share the Handler struct in core API (#105979) 2021-10-29 13:15:11 -07:00
legacy_test.go
legacy.go Remove unhealthy symlink only for dead containers 2020-04-21 12:30:51 -07:00
security_context_others_test.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
security_context_others.go generated: Run hack/update-gofmt.sh 2021-08-24 15:47:49 -04:00
security_context_windows_test.go Merge pull request #111020 from claudiubelu/adds-unittests-5 2022-07-29 19:29:11 -07:00
security_context_windows.go Windows: ensure runAsNonRoot does case-insensitive comparison on user name 2022-07-18 15:23:13 -07:00
security_context.go kubelet: propagate errors from namespacesForPod 2022-08-03 19:53:22 +02:00