class_docstr: ClusterResolver for Kubernetes.
  
  This is an implementation of cluster resolvers for Kubernetes. When given the
  the Kubernetes namespace and label selector for pods, we will retrieve the
  pod IP addresses of all running pods matching the selector, and return a
  ClusterSpec based on that information.
  
  Note: it cannot retrieve `task_type`, `task_id` or `rpc_layer`. To use it
  with some distribution strategies like
  `tf.distribute.experimental.MultiWorkerMirroredStrategy`, you will need to
  specify `task_type` and `task_id` by setting these attributes.
  
  Usage example with tf.distribute.Strategy:
  
    ```Python
    # On worker 0
    cluster_resolver = KubernetesClusterResolver(
        {"worker": ["job-name=worker-cluster-a", "job-name=worker-cluster-b"]})
    cluster_resolver.task_type = "worker"
    cluster_resolver.task_id = 0
    strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy(
        cluster_resolver=cluster_resolver)
  
    # On worker 1
    cluster_resolver = KubernetesClusterResolver(
        {"worker": ["job-name=worker-cluster-a", "job-name=worker-cluster-b"]})
    cluster_resolver.task_type = "worker"
    cluster_resolver.task_id = 1
    strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy(
        cluster_resolver=cluster_resolver)
    ```
docstr: Returns a ClusterSpec object based on the latest info from Kubernetes.
  
  We retrieve the information from the Kubernetes master every time this
  method is called.
  
  Returns:
    A ClusterSpec containing host information returned from Kubernetes.
  
  Raises:
    RuntimeError: If any of the pods returned by the master is not in the
      `Running` phase.
code: def cluster_spec(self):
      """Returns a ClusterSpec object based on the latest info from Kubernetes.
  
      We retrieve the information from the Kubernetes master every time this
      method is called.
  
      Returns:
        A ClusterSpec containing host information returned from Kubernetes.
  
      Raises:
        RuntimeError: If any of the pods returned by the master is not in the
          `Running` phase.
      """
      if self._override_client:
          client = self._override_client
      else:
          from kubernetes import (
              config as k8sconfig,
          )  # pylint: disable=g-import-not-at-top
          from kubernetes import (
              client as k8sclient,
          )  # pylint: disable=g-import-not-at-top
  
          k8sconfig.load_kube_config()
          client = k8sclient.CoreV1Api()
  
      cluster_map = {}
  
      for tf_job in self._job_to_label_mapping:
          all_pods = []
          for selector in self._job_to_label_mapping[tf_job]:
              ret = client.list_pod_for_all_namespaces(
                  label_selector=selector
              )
              selected_pods = []
  
              # Sort the list by the name to make sure it doesn't change call to call.
              for pod in sorted(
                  ret.items, key=lambda x: x.metadata.name
              ):
                  if pod.status.phase == "Running":
                      selected_pods.append(
                          "%s:%s"
                          % (pod.status.host_ip, self._tf_server_port)
                      )
                  else:
                      raise RuntimeError(
                          'Pod "%s" is not running; phase: "%s"'
                          % (pod.metadata.name, pod.status.phase)
                      )
              all_pods.extend(selected_pods)
          cluster_map[tf_job] = all_pods
  
      return server_lib.ClusterSpec(cluster_map)
  
file_path: tensorflow-upstream/tensorflow/python/distribute/cluster_resolver/kubernetes_cluster_resolver.py
imports: from tensorflow.python.distribute.cluster_resolver.cluster_resolver import ClusterResolver
  from tensorflow.python.distribute.cluster_resolver.cluster_resolver import format_master_url
  from tensorflow.python.training import server_lib
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 3 for calling the KubernetesClusterResolver obj's underlying member method code with example usage: obj.cluster_spec() and returns: server_lib