Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit a3fb8fb

Browse files
arkocalwagmarcel
authored andcommitted
k8s/util: Added script to run service on localhost
In order to run a service on localhost, 3 steps should be taken: 1. Run this script to replace the k8s service to proxy requests to localhost 2. Run kubefwd to allow localhost access k8s services 3. Import environment varianles and files such as keys from k8s. Signed-off-by: Ali Rasim Kocal <[email protected]>
1 parent fe2e505 commit a3fb8fb

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

kubernetes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

kubernetes/util/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
endpoint.json
2+
service.json
3+
ethereal_creds
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/python3
2+
"""A script to proxy an OISP service running on k8s to localhost.
3+
4+
This assumes the cluster is configured in the default kubeconfig."""
5+
6+
from argparse import ArgumentParser
7+
from os import system
8+
import subprocess
9+
import json
10+
11+
parser = ArgumentParser(description="Proxy kubernetes service to localhost.")
12+
parser.add_argument("service", metavar="Service", type=str,
13+
help="Service to proxy")
14+
args = parser.parse_args()
15+
16+
service_conf = json.loads(subprocess.check_output(['kubectl', "-n", "oisp",
17+
"-ojson", "get", "service",
18+
args.service]))
19+
endpoint_ports = [{"port":p["port"], "name":str(p["port"])}
20+
for p in service_conf["spec"]["ports"]]
21+
22+
new_service_conf = {
23+
"kind": "Service",
24+
"apiVersion": "v1",
25+
"metadata": {
26+
"name": service_conf["metadata"]["name"],
27+
"namespace": service_conf["metadata"]["namespace"]
28+
},
29+
"spec": {
30+
"ports": endpoint_ports
31+
}
32+
}
33+
34+
endpoint_conf = {
35+
"kind": "Endpoints",
36+
"apiVersion": "v1",
37+
"metadata": {
38+
"name": service_conf["metadata"]["name"],
39+
"namespace": service_conf["metadata"]["namespace"]
40+
},
41+
"subsets": [{
42+
"addresses": [{"ip": "172.17.0.1"}],
43+
"ports": endpoint_ports
44+
}]
45+
}
46+
47+
json.dump(new_service_conf, (open("service.json", "w")))
48+
json.dump(endpoint_conf, (open("endpoint.json", "w")))
49+
50+
system("kubectl -n oisp delete service {}".format(args.service))
51+
system("kubectl apply -f service.json")
52+
system("kubectl apply -f endpoint.json")

0 commit comments

Comments
 (0)