Skip to content

Instantly share code, notes, and snippets.

@palmalcheg
Last active March 31, 2022 09:34
Show Gist options
  • Select an option

  • Save palmalcheg/f604b99cb4af0e2a5c8b4b12044fc44d to your computer and use it in GitHub Desktop.

Select an option

Save palmalcheg/f604b99cb4af0e2a5c8b4b12044fc44d to your computer and use it in GitHub Desktop.
from kubernetes import client, config
from kubernetes.client.api import *
def list_exposed_ports(api: CoreV1Api):
values = {}
ret = api.list_service_for_all_namespaces(watch=False)
for svc in ret.items :
values[f"{svc.metadata.namespace}.{svc.metadata.name}"] = extract_port_info(svc)
return values
def extract_port_info(svc: client.V1Service):
ports = []
for v in svc.spec.ports:
info : client.V1ServicePort = v
if info.node_port :
ports.append(info.node_port)
return ports
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
apps_v1 = client.AppsV1Api()
api_instance = client.CoreV1Api()
# Uncomment the following lines to enable debug logging
# c = client.Configuration()
# c.debug = True
# apps_v1 = client.AppsV1Api(api_client=client.ApiClient(configuration=c))
ports = list_exposed_ports(api_instance)
print(ports)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment