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

Empty newlines skipped when watching pod logs #2358

Open
marcelldls opened this issue Feb 19, 2025 · 4 comments · May be fixed by #2359
Open

Empty newlines skipped when watching pod logs #2358

marcelldls opened this issue Feb 19, 2025 · 4 comments · May be fixed by #2359
Assignees
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug.

Comments

@marcelldls
Copy link

What happened (please include outputs or screenshots):
Empty newlines skipped when watching pod logs
What you expected to happen:
Empty newlines not skipped when watching pod logs
How to reproduce it (as minimally and precisely as possible):

Run the following in a kubernetes contexxt:

from kubernetes import client, config, watch

pod_name = "demo-bug"


config.load_kube_config()


api = client.CoreV1Api()
namespace = config.list_kube_config_contexts()[1]["context"]["namespace"]

pod_manifest = {
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": pod_name,
    },
    "spec": {
        "containers": [{"image": "hello-world", "name": pod_name}],
    },
}
api.create_namespaced_pod(body=pod_manifest, namespace=namespace)

input("Submit when running")

w = watch.Watch()
for e in w.stream(
    api.read_namespaced_pod_log,
    name=pod_name,
    namespace=namespace,
    follow=True,
):
    print(e)

The output that returns is:

Submit when running
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

The output that I expect is:

$ podman run --rm hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Anything else we need to know?:

Environment:

  • Kubernetes version (kubectl version):
  • OS (e.g., MacOS 10.13.6):
  • Python version (python --version)
  • Python client version (pip list | grep kubernetes)

Client Version: v1.29.4
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.29.4
Red Hat Enterprise Linux release 8.10
Python 3.11.3
kubernetes 32.0.0+snapshot

@marcelldls marcelldls added the kind/bug Categorizes issue or PR as related to a bug. label Feb 19, 2025
BhaskarSrinivasK pushed a commit to BhaskarSrinivasK/kubectl-python that referenced this issue Feb 23, 2025
@roycaihw
Copy link
Member

Maybe this is related to how to read the log line by line:

# Split by newline (safe for utf-8 because multi-byte sequences cannot contain the newline byte)
next_newline = buffer.find(b'\n')
while next_newline != -1:
# Convert bytes to a valid utf-8 string, replacing any invalid utf-8 with the '�' character
line = buffer[:next_newline].decode(
"utf-8", errors="replace")
buffer = buffer[next_newline+1:]
if line:
yield line
next_newline = buffer.find(b'\n')

@roycaihw
Copy link
Member

/help

@k8s-ci-robot
Copy link
Contributor

@roycaihw:
This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

  • Why are we solving this issue?
  • To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
  • Does this issue have zero to low barrier of entry?
  • How can the assignee reach out to you for help?

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.

In response to this:

/help

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label Feb 26, 2025
@p172913
Copy link

p172913 commented Feb 28, 2025

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants