kubernetes_asyncio icon indicating copy to clipboard operation
kubernetes_asyncio copied to clipboard

Malformed JSON response, the 'object' and/or 'type' field is missing in JSON

Open Qubad786 opened this issue 3 years ago • 1 comments

we maintaining this client's fork @ https://github.com/truenas/kubernetes_asyncio – I have faced an issue recently where client is unable to parse out 400 error and instead raise a bare exception.

Steps to reproduce

  1. Create a pod which is stuck in initialization e.g.
apiVersion: v1
kind: Pod
metadata:
  name: busybox-79d6c664ff-zsr9f
spec:
  initContainers:
  - name: bb
    command:
    - sh
    - -c
    - tail -f /dev/null
    image: busybox
    imagePullPolicy: Always
  containers:
  - image: busybox
    imagePullPolicy: Always
    name: busybox
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-zxjnv
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  restartPolicy: Always
  1. Make sure its in PodInitializing:
$ kubectl get po
busybox-79d6c664ff-zsr9f   0/1     Init:0/1           0          27m

$ kubectl logs po/busybox-79d6c664ff-zsr9f -c busybox
Error from server (BadRequest): container "busybox" in pod "busybox-79d6c664ff-zsr9f" is waiting to start: PodInitializing
  1. k8s client snippet that results in a bare exception:
import asyncio
from kubernetes_asyncio import client, config
from kubernetes_asyncio.watch import Watch
from kubernetes_asyncio.client.api_client import ApiClient


async def main():
    await config.load_kube_config()
    api = ApiClient()
    watch = Watch()
    async with watch.stream(
        client.CoreV1Api(api).read_namespaced_pod_log,
        name="busybox-79d6c664ff-zsr9f",
        container="busybox",
        namespace="default"
    ) as stream:
        # v1 = client.CoreV1Api(api)
        print([
            e async for e in stream
        ])

if __name__ == '__main__':
    asyncio.run(main())

---    
Exception: Malformed JSON response, the 'object' and/or 'type' field is missing. JSON: {'kind': 'Status', 'apiVersion': 'v1', 'metadata': {}, 'status': 'Failure', 'message': 'container "busybox" in pod "busybox-79d6c664ff-zsr9f" is waiting to start: PodInitializing', 'reason': 'BadRequest', 'code': 400}

Actual

Exception: Malformed JSON response, the 'object' and/or 'type' field is missing. JSON: {'kind': 'Status', 'apiVersion': 'v1', 'metadata': {}, 'status': 'Failure', 'message': 'container "busybox" in pod "busybox-79d6c664ff-zsr9f" is waiting to start: PodInitializing', 'reason': 'BadRequest', 'code': 400}

Expected

A meaningful exception scheme that can be handled by calling body to extract information and present to the end user

Thanks!

Qubad786 avatar Dec 02 '21 08:12 Qubad786

Thanks for reporting it.

tomplus avatar Dec 02 '21 21:12 tomplus