kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.8k stars 3.27k forks source link

container_stream.write_stdin BrokenPipeError: [Errno 32] Broken pipe #790

Closed dockersky closed 5 years ago

dockersky commented 5 years ago

### Errors:* Traceback (most recent call last): File "/usr/local/python35/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/local/python35/lib/python3.5/threading.py", line 862, in run self._target(self._args, self._kwargs) File "/root/webssh/django_webssh/tools/k8sclient.py", line 69, in write self.container_stream.write_stdin("date\n") File "/usr/local/python35/lib/python3.5/site-packages/kubernetes/stream/ws_client.py", line 160, in write_stdin self.write_channel(STDIN_CHANNEL, data) File "/usr/local/python35/lib/python3.5/site-packages/kubernetes/stream/ws_client.py", line 114, in write_channel self.sock.send(chr(channel) + data) File "/usr/local/python35/lib/python3.5/site-packages/websocket/_core.py", line 234, in send return self.send_frame(frame) File "/usr/local/python35/lib/python3.5/site-packages/websocket/_core.py", line 259, in send_frame l = self._send(data) File "/usr/local/python35/lib/python3.5/site-packages/websocket/_core.py", line 423, in _send return send(self.sock, data) File "/usr/local/python35/lib/python3.5/site-packages/websocket/_socket.py", line 116, in send return sock.send(data) File "/usr/local/python35/lib/python3.5/ssl.py", line 869, in send return self._sslobj.write(data) File "/usr/local/python35/lib/python3.5/ssl.py", line 594, in write return self._sslobj.write(data) BrokenPipeError: [Errno 32] Broken pipe

**Can you help me ?

pip list:**

[root@localhost ~]# python3 -m pip list Package Version Location


adal 1.2.1
alembic 0.9.5
aliyun-log-python-sdk 0.6.36
aliyun-python-sdk-cdn 2.8.0
aliyun-python-sdk-core-v3 2.9.1
amqp 1.4.9
anyjson 0.3.3
apsw 3.9.2.post1 arrow 0.13.1
asgiref 2.3.2
asn1crypto 0.24.0
async-timeout 3.0.1
attrs 19.1.0
autobahn 19.2.1
Automat 0.7.0
bcrypt 3.1.6
beautifulsoup4 4.4.1
billiard 3.3.0.23
blinker 1.4
cached-property 1.3.0
cachetools 3.1.0
caterpillar 0.15.10
celery 3.1.23
certifi 2019.3.9
cffi 1.12.2
channels 2.1.5
chardet 3.0.4
click 6.7
colorama 0.3.9
constantly 15.1.0
cryptography 2.6.1
daphne 2.2.5
dateparser 0.7.0
Django 2.1.7
dnspython 1.15.0
docker 2.5.1
docker-compose 1.15.0
docker-pycreds 0.2.1
dockerpty 0.4.1
docopt 0.6.2
dominate 2.3.1
EasyProcess 0.2.3
elasticsearch 6.3.1
eventlet 0.24.1
Flask 0.12.1
Flask-Bootstrap 3.3.7.1
Flask-Celery-Helper 1.1.0
Flask-Migrate 2.0.3
Flask-Script 2.0.5
Flask-SQLAlchemy 2.2
Flask-SSE 0.2.1
Flask-WTF 0.14.2
future 0.16.0
google-auth 1.6.3
greenlet 0.4.15
gunicorn 19.7.1
hiredis 0.2.0
hyperlink 18.0.0
idna 2.6
incremental 17.5.0
itchat 1.3.9 /root/wechat-admin/src/itchat itsdangerous 0.24
Jinja2 2.9.6
jsonschema 2.6.0
kombu 3.0.37
kubernetes 8.0.1
livereload 2.5.1
lxml 3.4.4
Mako 1.0.7
Markdown 2.6.9
MarkupSafe 1.0
mkdocs 0.16.3
mkdocs-material 1.7.4
monotonic 1.5
msgpack-python 0.4.8
MySQL-python 1.2.5
mysqlclient 1.3.10
nltk 3.4
numpy 1.13.3
oauthlib 3.0.1
oss2 2.0.5
paho-mqtt 1.2
paramiko 2.4.1
pbr 3.1.1
pip 19.0.3
protobuf 3.6.1
pyasn1 0.4.5
pyasn1-modules 0.2.4
pycparser 2.19
pycryptodome 3.6.6
Pygments 2.2.0
PyHamcrest 1.9.0
PyJWT 1.7.1
pymdown-extensions 3.5
PyMySQL 0.7.11
PyNaCl 1.3.0
pypng 0.0.18
PyQRCode 1.2.1
python-dateutil 2.8.0
python-editor 1.0.3
pytz 2018.5
PyVirtualDisplay 0.1.5
PyYAML 3.12
redis 2.10.3
regex 2018.11.22
requests 2.9.1
requests-oauthlib 1.2.0
rsa 4.0
selenium 2.52.0
setuptools 36.0.1
singledispatch 3.4.0.3
six 1.10.0
snownlp 0.12.3
SQLAlchemy 1.1.13
stevedore 1.25.0
texttable 0.8.8
tornado 4.5.1
Twisted 18.9.0
txaio 18.8.1
tzlocal 1.5.1
urllib3 1.24.1
uWSGI 2.0.15
vine 1.1.4
virtualenv 15.1.0
virtualenv-clone 0.2.6
virtualenvwrapper 4.7.2
visitor 0.1.3
walrus 0.4.1
websocket-client 0.39.0
Werkzeug 0.12.2
WTForms 2.1
wxpy 0.3.9.8 /root/wechat-admin/src/wxpy
xlrd 1.1.0
zope.interface 4.6.0

code: import os from kubernetes import config from kubernetes.client import Configuration from kubernetes.client.apis import core_v1_api from kubernetes.client.rest import ApiException from kubernetes.stream import stream

from django.conf import settings

from threading import Thread import json

kubeconfig = '{}/.kube/config'.format(os.path.expanduser('~'))

class K8SClient: def int(self): print('K8sclient init start') def new(self): config.load_kube_config() c= Configuration() c.assert_hostname = False Configuration.set_default(c) self.k8s = core_v1_api.CoreV1Api() print('K8sclient init end')

def connect(self,namespace, podname,containername,socketer,pty_width=80, pty_height=24):
    config.load_kube_config()
    self.command=''
    self.socketer = socketer
    self.new()

    command = [
        '/bin/sh',
        '-c',
        'echo Welcome to container WebSSH']

    container_stream = stream(
        self.k8s.connect_get_namespaced_pod_exec,
        name=podname,
        namespace=namespace,
        command=command,
        stderr=True, stdin=True,
        stdout=True, tty=True,
        _preload_content=False
    )
    self.container_stream = container_stream
    print("container_stream:",container_stream)
    return container_stream

def write(self,command):
    message = {}
    message['status'] = 0
    message['message'] = command
    socket_message = json.dumps(message)
    self.socketer.send(socket_message)
    if command =="\r":
        print("command:", self.command)
        self.container_stream.write_stdin("date\n")
        self.command=''
    else:
        self.command=self.command+command

def read(self):
    while self.container_stream.is_open():
        self.container_stream.update(timeout=1)
        if self.container_stream.peek_stdout():
            str=self.container_stream.read_stdout()
            message={}
            message['status']=0
            message['message']=str
            socket_message=json.dumps(message)
            print('stdout:',socket_message)
            self.socketer.send(socket_message)

def shell(self,command):
    Thread(target=self.write, args=(command,)).start()
    Thread(target=self.read).start()

def close(self):
    self.socketer.close()
    self.container_stream.close()

def resize_pty(self,cols, rows):
    RESIZE_CHANNEL = 4
    print('resize_pty')
    self.container_stream.write_channel(RESIZE_CHANNEL,json.dumps({"Height": int(rows),"Width": int(cols)}))

def readlogs(self,podname,namespace):
    self.new()
    try:
        logs = self.k8s.read_namespaced_pod_log(name=podname,namespace=namespace,tail_lines=100)
        return logs
    except ApiException as e:
        print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
fejta-bot commented 5 years ago

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

fejta-bot commented 5 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

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 rotten

fejta-bot commented 5 years ago

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

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

k8s-ci-robot commented 5 years ago

@fejta-bot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/python/issues/790#issuecomment-522312764): >Rotten issues close after 30d of inactivity. >Reopen the issue with `/reopen`. >Mark the issue as fresh with `/remove-lifecycle rotten`. > >Send feedback to sig-testing, kubernetes/test-infra and/or [fejta](https://github.com/fejta). >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.