diff --git a/.gitignore b/.gitignore index 227d9c3..38bc3b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea/ -/.DS_store \ No newline at end of file +/.DS_store +port-log.txt diff --git a/Dockerfile b/Dockerfile index 91b5359..5a441b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,20 @@ -FROM ubuntu:16.04 +FROM alpine:3.9 -WORKDIR /code +ENV KUBE_LATEST_VERSION="v1.13.0" -RUN apt-get update && \ - apt-get install -y apt-transport-https curl python3-dev nginx && \ - curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \ - touch /etc/apt/sources.list.d/kubernetes.list && \ - echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list && \ - apt-get update && \ - apt-get install -y kubectl=1.11.1-00 +RUN echo 'http://nl.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories \ + && apk add --update kubernetes python3 \ + && mkdir -p /root/.kube \ + && rm -rf /usr/bin/kubelet \ + /usr/bin/kubeadm \ + /usr/bin/kube-scheduler \ + /usr/bin/kube-proxy \ + /usr/bin/kube-controller-manager \ + /usr/bin/kube-apiserver \ + /usr/bin/hyperkube COPY . /code +WORKDIR /code RUN chmod +x map_services.py entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index 631aa39..cf342f9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,13 @@ -version: '2' +version: '3' services: kube_forwards: build: . entrypoint: ['tail', '-f', '/dev/null'] environment: - - SERVICES=authenticationservice:8000, srservice:8001:8000 + - SERVICES=stock-service:3000 ports: - - 8011:8000 - - 8012:8001 + - 3000:3000 volumes: - ~/.kube/config:/root/.kube/config - .:/code diff --git a/map_services.py b/map_services.py index 4932c85..d6c93a7 100644 --- a/map_services.py +++ b/map_services.py @@ -57,42 +57,35 @@ def validate_service_spec(service_spec): def port_forward_service(service_spec): - # kubectl get pods --selector=app=authenticationservice --output=jsonpath={.items..metadata.name} - service_parts = service_spec.strip().split(':') - output = run(['kubectl', 'get', 'pods', '--selector=app={}'.format(service_parts[0].strip()), - '--output=jsonpath={.items..metadata.name}'], stdout=PIPE) + # kubectl get pods --selector=app=health --output=jsonpath={.items..metadata.name} | | | + # TODO: Provide better mechanism to specify selector as everything might not be app= + # This utility needs to provide forwarding + + pods_output = Popen([ + 'kubectl', + 'get', + 'pods', + '--selector=app={selector}'.format(selector=service_parts[0].strip()), + '--output=jsonpath={.items..metadata.name}', + ], stdout=PIPE) + + pod_names = pods_output.communicate()[0].decode().split() - if not output.stdout.decode().strip(): + if len(pod_names) <= 0: logging.error('No pods found for selector {}'.format(service_parts[0])) exit(1) + port_forwarding_pod = pod_names[0] + local_port = service_parts[1] - local_proxy_port = int(local_port) + 1000 remote_port = local_port if (len(service_parts) == 2) else service_parts[2] - global nginx_directives - nginx_directives += """ - server {{ - listen {listen_port}; - - server_name _; - - location / {{ - proxy_pass http://127.0.0.1:{proxy_port}; - proxy_pass_header Server; - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Scheme $scheme; - proxy_set_header X-Forwarded-Proto https; - }} - }} - """.format(listen_port=local_port, proxy_port=local_proxy_port) - - Popen('kubectl port-forward {} {}:{} >> port-log.txt &'.format( - output.stdout.decode().strip(), - local_proxy_port, - service_parts[1] if (len(service_parts) == 2) else service_parts[2] + + Popen('kubectl port-forward --address 0.0.0.0 {} {}:{} >> port-log.txt &'.format( + port_forwarding_pod, + local_port, + remote_port, ), close_fds=True, shell=True) print('Port Forwarding: {} to local port {} from port {}'.format( @@ -101,13 +94,6 @@ def port_forward_service(service_spec): remote_port, )) -def update_nginx_configs(): - file = open('/etc/nginx/sites-enabled/default', 'w+') - file.truncate(0) - file.write(nginx_directives) - file.close() - - if __name__ == "__main__": services = os.environ.get('SERVICES', None) @@ -118,12 +104,8 @@ def update_nginx_configs(): service_list = services.split(',') for service in service_list: - # Validate will throw error and exit if services i oncorrectly defined. + # Validate will throw error and exit if services incorrectly defined. validate_service_spec(service) for service in service_list: port_forward_service(service) - - update_nginx_configs() - - Popen('service nginx restart', shell=True)