Watch events using kubernetes python client and write into the file behaves strangely

Hello.
I have written a simple code to watch events in the k8s using python client. I want to write all the events into a file. Here is the code:

import os
from kubernetes import client, config, watch

config.load_kube_config(
    os.path.join(os.environ["HOME"], '.kube/config'))

v1 = client.CoreV1Api()

namespace = "default"
out_file = './contdir/afile'

with open(out_file, 'w') as o:
	o.write("The events:\n")

with open(out_file, 'a+') as o:
	o.write("----------\n")
	while True :
		stream = watch.Watch().stream(v1.list_namespaced_endpoints, namespace)
		o.write("some stream")
		o.write("\n")
		for event in stream:
			o.write("an event\n")

When I open the output file (afile) it only has the first line which is The events:. But I expect the file to contain at least this:
The events:
----------
some stream

So, I commented those two lines of code which is waiting for events:
#for event in stream:
#o.write(“an event\n”)

and then I can see the file output as expected: it has a lot of the following blocks:
The events:
----------
some stream

So it seems that this line of code for event in stream: is doing something to the file which I don’t understand. I am confused why the code behaves like this.
Does the event in K8s works correctly?

Thanks a lot

Cluster information:

Client Version: version.Info{Major:“1”, Minor:“18”, GitVersion:“v1.18.0”, GitCommit:“9e991415386e4cf155a24b1da15becaa390438d8”, GitTreeState:“clean”, BuildDate:“2020-03-25T14:58:59Z”, GoVersion:“go1.13.8”, Compiler:“gc”, Platform:“linux/amd64”}

Server Version: version.Info{Major:“1”, Minor:“18”, GitVersion:“v1.18.10”, GitCommit:“62876fc6d93e891aa7fbe19771e6a6c03773b0f7”, GitTreeState:“clean”, BuildDate:“2020-10-15T01:43:56Z”, GoVersion:“go1.13.15”, Compiler:“gc”, Platform:“linux/amd64”}

Hi @george,

The file is being buffered before being written to disk.
Try adding a flush to force it to write to disk before buffer is full.

		for event in stream:
			o.write("an event\n")
			o.flush()

Kind regards,
Stephen

1 Like

Thanks stephendotcarter
It worked well