Skip to content

Unable to exec in a pod using kubernetes-cli in Python even after using stream api #842

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

Closed
kebab-mai-haddi opened this issue May 28, 2019 · 10 comments
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@kebab-mai-haddi
Copy link

I am following the official documentation and to my surprise, the request just won't work.

I tried reading multiple questions and answers but in vain. I had set stream api to use but the error did not go away.

My code is:

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint
from kubernetes import client, config, stream
stream = stream.stream

# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.host = "https://my_aws_server.amazonaws.com"
configuration.verify_ssl = False
configuration.api_key['authorization'] = "some_token"
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.debug = True

api_instance = kubernetes.client.CoreV1Api(
    kubernetes.client.ApiClient(configuration))
name = 'jupyter-ankita'  # str | name of the PodExecOptions
namespace = 'jhub'  # str | object name and auth scope, such as for teams and projects
# str | Command is the remote command to execute. argv array. Not executed within a shell. (optional)
command = 'echo "hail aviral"'
# bool | Redirect the standard error stream of the pod for this call. Defaults to true. (optional)
stderr = True
# bool | Redirect the standard input stream of the pod for this call. Defaults to false. (optional)
stdin = True
# bool | Redirect the standard output stream of the pod for this call. Defaults to true. (optional)
stdout = True
# bool | TTY if true indicates that a tty will be allocated for the exec call. Defaults to false. (optional)
tty = True

try:
    api_response = stream(api_instance.connect_post_namespaced_pod_exec(
        name, namespace, command=command, stderr=stderr, stdin=stdin, stdout=stdout)
    )
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CoreV1Api->connect_post_namespaced_pod_exec: %s\n" % e)

I want the command to run but instead, I am facing an error:

Exception when calling CoreV1Api->connect_post_namespaced_pod_exec: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '88df8863-61b1-4fe7-9d39-d0e6059ea993', 'Content-Type': 'application/json', 'Date': 'Tue, 28 May 2019 14:04:38 GMT', 'Content-Length': '139'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}

My SSL client is updated, I am using Python3.7 from the brew on a MacOS.

Also used on Ubuntu, same error.

@kebab-mai-haddi
Copy link
Author

kebab-mai-haddi commented May 28, 2019

Any help @roycaihw @dims @micw523 @djkonro ?
Sorry for direct tagging, need some urgent help. Would be grateful to you guys.
Thanks!

@phracek
Copy link

phracek commented Jun 4, 2019

Is there any chance to get RC of the command executed in POD?
I would like to check if the command was successful or not.
Like $? in bash.

@kareem-elsayed
Copy link

I have the same issue also

@tomplus
Copy link
Member

tomplus commented Jun 6, 2019

@avisrivastava254084 The official documentation is auto-generated from the spec and in many places are not helpful at all (ref: #601).

In your code I see that you try to pass output from the api_instance.connect_post_namespaced_pod_exec to the stream() instead of passing the function as an argument which the stream() will call for you.

api_response = stream(api_instance.connect_post_namespaced_pod_exec,
        name, namespace, command=command, stderr=stderr, stdin=stdin, stdout=stdout
    )

(I removed brackets only).

You can also check this example: https://github.com/kubernetes-client/python/blob/master/examples/exec.py

@phracek I'am afraid getting the RC is currently not supported by this library. Could you create a separate issue for this.

@tomplus
Copy link
Member

tomplus commented Jun 6, 2019

@phracek it's been already reported: #812

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 4, 2019
@EricCat
Copy link

EricCat commented Sep 19, 2019

@avisrivastava254084 Did you find the solution?

@kebab-mai-haddi
Copy link
Author

@EricCat
Yeah I did but as I have left my previous company, I am not able to recall the solution.

@roycaihw
Copy link
Member

Please refer to this example for running exec https://github.com/kubernetes-client/python/blob/master/examples/pod_exec.py

Getting the RC is fixed in #956

/close

@kmmanto
Copy link

kmmanto commented Feb 18, 2020

I'm getting the same error while following the examples.
Using:
python3
kubernetes==10.0.1

HTTP response body: unable to upgrade: missing upgrade headers in request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests

9 participants