Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
update ssh service to find nodeport (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayadebellis authored May 5, 2021
1 parent 63b4693 commit 48622bd
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions show_ssh_info/show_ssh_info/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,37 @@ class GetHandler(IPythonHandler):
"""
def get(self):

# Get Port from Kubernetes
host = os.environ.get('KUBERNETES_SERVICE_HOST')
host_port = os.environ.get('KUBERNETES_PORT_443_TCP_PORT')
workspace_id = os.environ.get('CHE_WORKSPACE_ID')
namespace = os.environ.get('CHE_WORKSPACE_NAMESPACE')

with open ("/var/run/secrets/kubernetes.io/serviceaccount/token", "r") as t:
token=t.read()

headers = {
'Authorization': 'Bearer ' + token,
}

request_string = 'https://' + host + ':' + host_port + '/api/v1/namespaces/' + namespace + '-' + workspace_id + '/services/ws'
response = requests.get(request_string, headers=headers, verify=False)

data = response.json()
port = data['spec']['ports'][0]['nodePort']


# Get external IP address
ip = get_maap_config(host)['ade_server']

self.finish({'ip': ip, 'port': port})
return
try:
svc_host = os.environ.get('KUBERNETES_SERVICE_HOST')
svc_host_https_port = os.environ.get('KUBERNETES_SERVICE_PORT_HTTPS')
namespace = os.environ.get('CHE_WORKSPACE_NAMESPACE') + '-che'
che_workspace_id = os.environ.get('CHE_WORKSPACE_ID')
sshport_name = 'sshport'

ip = requests.get('https://api.ipify.org').text

with open ("/var/run/secrets/kubernetes.io/serviceaccount/token", "r") as t:
token=t.read()

headers = {
'Authorization': 'Bearer ' + token,
}

request_string = 'https://' + svc_host + ':' + svc_host_https_port + '/api/v1/namespaces/' + namespace + '/services/'
response = requests.get(request_string, headers=headers, verify=False)
data = response.json()
endpoints = data['items']

# Ssh service is running on a seperate container from the user workspace. Query the kubernetes host service to find the container where the nodeport has been set.
for endpoint in endpoints:
if sshport_name in endpoint['metadata']['name']:
if che_workspace_id == endpoint['metadata']['labels']['che.workspace_id']:
port = endpoint['spec']['ports'][0]['nodePort']
self.finish({'ip': ip, 'port': port})

self.finish({"status": 500, "message": "failed to get ip and port"})
except:
self.finish({"status": 500, "message": "failed to get ip and port"})


"""
Expand Down

0 comments on commit 48622bd

Please sign in to comment.