rundeck-plugins / kubernetes

52 stars 61 forks source link

Default container name to reading pod logs seems not necessary or should be omitted conditionally #57

Closed wildmouse closed 4 years ago

wildmouse commented 4 years ago

When I try to fetch logs from rundeck using kubernetes plugin, I got an below error.

ERROR: kubernetes-model-source: Exception error creating: (400)
20:05:58 | Reason: Bad Request
20:05:58 | HTTP response headers: HTTPHeaderDict({'Content-Length': '171', 'Access-Control-Allow-Headers': 'X-Auth-Token,Content-Type,Authorization', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains', 'Server': 'nginx/1.15.9', 'Connection': 'keep-alive', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Mon, 06 Jan 2020 11:05:58 GMT', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, PATCH, OPTIONS', 'Content-Type': 'application/json'})
20:05:58 | HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"container None is not valid for pod app-xxxxxxxxxx-xxxxx","reason":"BadRequest","code":400}
20:05:58

I've investigate cause and found that os.environ.get('RD_NODE_DEFAULT_CONTAINER_NAME') returns None and Kubernetes client use it to select container inside pod.

Below code is a current master branch code.

https://github.com/rundeck-plugins/kubernetes/blob/885762da5a949c78632dac17d3cd1df43830ddff/contents/pods-read-logs.py#L22-L37

But we can't specify default container name on Rundeck job window and I've fetched pod logs successfully if I omitted default container name.

So my opinion is that we can remove this default container name and fetch without container name like this:

    data = {}
    data["name"] = os.environ.get('RD_CONFIG_NAME')
    data["namespace"] = os.environ.get('RD_CONFIG_NAMESPACE')

    common.connect()

    try:
        v1 = client.CoreV1Api()
        ret = v1.read_namespaced_pod_log(
            namespace=data["namespace"],
            name=data["name"],
            _preload_content=False
        )
        print(ret.read())

    except ApiException as e:
        log.error("Exception error creating: %s\n" % e)
        sys.exit(1)
ltamaster commented 4 years ago

fix with https://github.com/rundeck-plugins/kubernetes/pull/67