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

update ssh service to find nodeport #88

Merged
merged 1 commit into from
May 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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