-
Notifications
You must be signed in to change notification settings - Fork 10
/
tasks-kubernetes-port-forward-service.yaml
64 lines (57 loc) · 2.9 KB
/
tasks-kubernetes-port-forward-service.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- name: Get service port for local access
block:
- name: "Convert {{ service_name }} to a NodePort service"
shell: >-
kubectl patch svc -n {{ service_namespace }} {{ service_name }} --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"}]'
- name: Get all ports of type NodePort
shell: >-
kubectl get svc -n {{ service_namespace }} {{ service_name }} {% raw %} -o go-template='{{if .items}}{{range .items}}{{range.spec.ports}}{{.nodePort}}{{"\n"}}{{end}}{{end}}{{else}}{{range.spec.ports}}{{.nodePort}}{{"\n"}}{{end}}{{end}}' {% endraw %}
register: service_port
when: service_name != "SKIP"
# TODO what to do if there are multiple nodeports that need to be port forwarded
- name: Setup and enable systemd service for port forwarding
block:
- name: "Copy over the skeleton service file for systemd"
become: True
copy:
src: charts_config/skeleton.service
dest: "{{ service_file_location_base }}{{ service_file_name }}"
# TODO what if minikube IP changes, the IP is set at deployment time here
# Maybe should be part of the systemd service in a script?
- name: Register minikube ssh-key location
shell: minikube ssh-key
register: dir_minikube_ssh_key
- name: Register minikube ip
shell: minikube ip
register: minikube_ip
- name: "Service file edit: Add port forward command for local access"
become: True
# https://gist.github.com/drmalex07/c0f9304deea566842490
lineinfile:
path: "{{ service_file_location_base }}{{ service_file_name }}"
regexp: '^ExecStart=(.*)$'
line: "ExecStart=ssh -NT -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes -o GatewayPorts=true -i {{ dir_minikube_ssh_key.stdout }} docker@{{ minikube_ip.stdout }} -L {{ host_port }}:0.0.0.0:{{ service_port.stdout }}"
backrefs: yes
when: service_name != "SKIP"
- name: "Service file edit: Add port forward command for domain access"
become: True
# https://gist.github.com/drmalex07/c0f9304deea566842490
lineinfile:
path: "{{ service_file_location_base }}{{ service_file_name }}"
regexp: '^ExecStart=(.*)$'
line: "ExecStart=ssh -NT -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes -o GatewayPorts=true -i {{ dir_minikube_ssh_key.stdout }} docker@{{ minikube_ip.stdout }} -L {{ host_port }}:0.0.0.0:{{ service_port }}"
backrefs: yes
when: service_name == "SKIP"
- name: "Service file edit: Change User to be {{ ansible_user }}"
become: True
# https://gist.github.com/drmalex07/c0f9304deea566842490
lineinfile:
path: "{{ service_file_location_base }}{{ service_file_name }}"
regexp: '^User=(.*)$'
line: "User={{ ansible_user }}"
backrefs: yes
- name: Reload systemd service
include_tasks: tasks-reload-systemd-service.yaml
vars:
service_name: "{{ service_file_name }}"
become: True