|
| 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