Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support resuming from the last log when retrying #230

Merged
merged 4 commits into from
Feb 18, 2023

Conversation

tksm
Copy link
Contributor

@tksm tksm commented Feb 17, 2023

Fixes #229

This PR is to support resuming from the last log when retrying.

Here is the summary of the changes.

  1. Always set PodLogOptions's Timestamps to true
    1. Print timestamps only when "--timestamps" is specified
  2. Remember the last timestamp and line counts during this timestamp when printing log lines
    1. 📝 The timestamp in logs is RFC3339Nano, but PodLogOptions's SinceTime is RFC3339.
  3. When resuming, set the last timestamp to PodLogOptions's SinceTime
    1. Skip lines we've already seen during the timestamp to avoid duplication

I found the rate limiter of retrying was not appropriate while I tested this feature, so I also tweaked it.

How to test

The below steps are the way to test the resuming feature by restarting the kube-apiserver.

  1. Deploy a pod that prints line numbers
apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  terminationGracePeriodSeconds: 0
  containers:
  - name: c
    command:
    - /bin/sh
    - -c
    - |
      N=1
      while true; do
        for _ in $(seq 5); do
          echo "line $N"
          N=$((N+1))
        done
        sleep 10
      done
    image: busybox:1.36.0
  1. Run stern to watch the pod
dist/stern pod/counter
+ counter › c
counter c line 1
counter c line 2
counter c line 3
counter c line 4
counter c line 5
  1. Restart the kube-apiserver in another terminal

If you use kind, you can restart the kube-apiserver by the command below.

docker exec -it kind-control-plane /bin/bash -c 'kill -9 $(pgrep kube-apiserver)'

The connection will be disconnected, but it will retry soon and resume logs from the last line.

counter c line 15
counter c line 16
counter c line 17
counter c line 18
counter c line 19
counter c line 20
- counter › c
failed to tail: unexpected EOF, will retry
+ counter › c
- counter › c
failed to tail: Get "https://127.0.0.1:64321/api/v1/namespaces/default/pods/counter/log?container=c&follow=true&sinceTime=2023-02-17T04%3A50%3A38Z&timestamps=true": EOF, will retry
+ counter › c
- counter › c
failed to tail: Get "https://127.0.0.1:64321/api/v1/namespaces/default/pods/counter/log?container=c&follow=true&sinceTime=2023-02-17T04%3A50%3A38Z&timestamps=true": EOF, will retry
+ counter › c
counter c line 21
counter c line 22
counter c line 23
counter c line 24
counter c line 25

@superbrothers
Copy link
Member

I'll check this PR this weekend.

stern/tail.go Outdated Show resolved Hide resolved
stern/tail.go Outdated Show resolved Hide resolved
Co-authored-by: Kazuki Suda <230185+superbrothers@users.noreply.github.com>
stern/stern.go Outdated
return NewTail(client.CoreV1(), t.Node, t.Namespace, t.Pod, t.Container, config.Template, config.Out, config.ErrOut, &TailOptions{
Timestamps: config.Timestamps,
Location: config.Location,
SinceSeconds: int64(config.Since.Seconds()),
SinceSeconds: &sinceSeconds,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use k8s.io/utils/pointer here.

Suggested change
SinceSeconds: &sinceSeconds,
SinceSeconds: pointer.Int64(int64(config.Since.Seconds())),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it in 51f9032 👍

@superbrothers superbrothers merged commit 52894f8 into stern:master Feb 18, 2023
@tksm
Copy link
Contributor Author

tksm commented Feb 18, 2023

Thank you for the review!

@tksm tksm deleted the resume branch February 18, 2023 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resume from the last log line when it retries
2 participants