kubernetes-client / kubernetes-client/python

exec command is parsing output if output looks like json

Open
#1,032 21 comments 3 reactions 0 assignees View on GitHub
kind/bug lifecycle/frozen
Dominant language
Python
Stars
7.7k
Forks
3.5k
Avg merge
1d 14h
Merged PRs (30d)
18

Description

**What happened (please include outputs or screenshots)**:

Working on creating a scale test where the test orchestrator will exec in each pod and run a curl command to verify the pod is in the expected state. The output of the curl is JSON.

The problem seems to be that the stream client will parse the json as a python dictionary if the output is only json, but return it to the caller as a stringified dictionary. Expecting json, `json.loads` is failing.

To verify I ran
```
In [5]: cmd=["echo", '{"example":"json", "with":null, "and":true}']

In [6]: kstream.stream(api_instance.connect_get_namespaced_pod_exec,
...: name=pod,
...: namespace=ns,
...: container=container,
...: command=cmd,
...: stderr=True,
...: stdin=False,
...: tty=False,
...: stdout=True)
Out[6]: "{'example': 'json', 'with': None, 'and': True}"

In [7]: cmd=["echo", 'but not actually json {"example":"json", "with":null, "and":true}']

In [8]: kstream.stream(api_instance.connect_get_namespaced_pod_exec,
...: name=pod,
...: namespace=ns,
...: container=container,
...: command=cmd,
...: stderr=True,
...: stdin=False,
...: tty=False,
...: stdout=True)
Out[8]: 'but not actually json {"example":"json", "with":null, "and":true}\n'
```

**What you expected to happen**:

I really did not expect the response to be different depending on the output of the command being run.

**How to reproduce it (as minimally and precisely as possible)**:

**Anything else we need to know?**:

We are able to get around the issue by adding `_preload_content=False` to the `stream` function call and handling waiting for the response and reading stdout/stderr ourselves.

I wasn't sure where the rogue json parsing is happening, maybe in WSResponse class?

**Environment**:
- Kubernetes version (`kubectl version`):
```
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.6", GitCommit:"7015f71e75f670eb9e7ebd4b5749639d42e20079", GitTreeState:"clean", BuildDate:"2019-11-19T15:41:24Z", GoVersion:"go1.12.13", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14+", GitVersion:"v1.14.8-eks-b8860f", GitCommit:"b8860f6c40640897e52c143f1b9f011a503d6e46", GitTreeState:"clean", BuildDate:"2019-11-25T00:55:38Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
```
- OS (e.g., MacOS 10.13.6):
Ubuntu 19.10
- Python version (`python --version`)
```
python 3.7.5
```
- Python client version (`pip list | grep kubernetes`)
```
kubernetes==10.0.1
```

Contributor guide

Open the contributing guide

Research direction

Start at the stream entry point used with connect_get_namespaced_pod_exec, then inspect the WSResponse class and the _preload_content path mentioned in the report. Reproduce both echo commands and compare the returned values; done means command output is returned consistently so JSON output can be passed to json.loads without special handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
kubernetes, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.