k8s.py 859 B

1234567891011121314151617181920212223242526272829
  1. from kubernetes import client, config
  2. from easyfl.registry.vclient import VirtualClient
  3. def get_clients():
  4. """Get clients in kubernetes based on client field selector.
  5. Returns:
  6. list[:obj:`VirtualClient`]: A list of clients.
  7. """
  8. config.load_kube_config()
  9. v1 = client.CoreV1Api()
  10. ret = v1.list_namespaced_endpoints('easyfl', watch=False, field_selector="metadata.name=easyfl-client-svc")
  11. clients = []
  12. for record in ret.items:
  13. for subset in record.subsets:
  14. port = subset.ports[0].port
  15. for index, address in enumerate(subset.addresses):
  16. addr = "{}:{}".format(address.ip, port)
  17. c = VirtualClient(address.target_ref.name, addr, index)
  18. clients.append(c)
  19. return clients
  20. if __name__ == '__main__':
  21. get_clients()