• Kubernetes Submit Queue's avatar
    Merge pull request #64015 from cofyc/improvetests · 0a22c159
    Kubernetes Submit Queue authored
    Automatic merge from submit-queue (batch tested with PRs 62756, 63862, 61419, 64015, 64063). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.
    
    Handle TERM signal to reduce pod terminating time.
    
    **What this PR does / why we need it**:
    
    Register signal handler for `TERM`.
    
    For container, process is run as PID 1. PID 1 is special in linux, it ignore any signal with the default action. So process will not terminate on `SIGINT` or `SIGTERM` unless it is coded to do so.
    
    By default, docker use `TERM` signal to termiante pods, it will wait 10 seconds before sending `KILL` signal.
    
    ```
    $ docker run -d --rm --name test busybox sh -c 'while true; do sleep 1; done'
    aa827df5d7bfffc5ca4fae2429d0b761a5a142c57ba3e1faa59b305c92f1c875
    $ time docker stop test
    test
    
    real	0m10.408s
    user	0m0.048s
    sys	0m0.020s
    ```
    
    It's better to register a exit handler for `TERM`, it reduces a lot of time in waiting pods to termiante.
    
    ```
    $ docker run -d --rm --name test busybox sh -c 'trap exit TERM; while true; do sleep 1; done'
    e331bff454dba8e45df6065c3bd2a928e1c41303aafdf88ede38def3e4e5781f
    $ time docker stop test
    test
    
    real	0m0.690s
    user	0m0.048s
    sys	0m0.020s
    ```
    
    **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
    Fixes #
    
    **Special notes for your reviewer**:
    
    **Release note**:
    
    ```release-note
    NONE
    ```
    0a22c159
util.go 182 KB