Last active
March 23, 2024 09:43
-
-
Save johnandersen777/1bf3d25898be14e85b7da38b2ec2fae9 to your computer and use it in GitHub Desktop.
Python Kubernetes service discovery EndpointSlices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Updated version: https://github.com/intel/Multi-llms-Chatbot-CloudNative-LangChain/blob/1bd6a844ebc57245f9fba8e7a87cde489cc4734d/2__LLMs_Proxy/server.py#L12-L34 | |
import pathlib | |
from pprint import pprint | |
import yaml | |
import kubernetes | |
import kubernetes.client | |
from kubernetes.client.rest import ApiException | |
from kubernetes import client, config | |
def kubernetes_ipv4_address_for_pod_name(pod_name): | |
# Load the service account kubeconfig | |
configuration = kubernetes.client.Configuration() | |
config.load_incluster_config(client_configuration=configuration) | |
namespace = pathlib.Path("/var/run/secrets/kubernetes.io/serviceaccount/namespace").read_text() | |
with kubernetes.client.ApiClient(configuration) as api_client: | |
# Create an instance of the API class | |
api_instance = kubernetes.client.DiscoveryV1Api(api_client) | |
api_response = api_instance.list_namespaced_endpoint_slice(namespace) | |
found_endpoint = None | |
for endpoint_slice in api_response.items: | |
for endpoint in endpoint_slice.endpoints: | |
if endpoint.target_ref.name == pod_name: | |
found_endpoint = endpoint | |
break | |
if found_endpoint: | |
break | |
if not found_endpoint: | |
raise Exception(f"Pod {pod_name} not found") | |
# TODO Handle more cases than zeroith index? | |
return found_endpoint.addresses[0] | |
print(kubernetes_ipv4_address_for_pod_name("backend-pod")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: frontend-service-account | |
namespace: default | |
--- | |
apiVersion: rbac.authorization.k8s.io/v1 | |
kind: Role | |
metadata: | |
namespace: default | |
name: pod-reader | |
rules: | |
# EndpointSlices are used for Service-based network policy rule | |
# enforcement. | |
- apiGroups: ["discovery.k8s.io"] | |
resources: | |
- endpointslices | |
verbs: | |
- watch | |
- list | |
--- | |
apiVersion: rbac.authorization.k8s.io/v1 | |
kind: RoleBinding | |
metadata: | |
name: read-pods | |
namespace: default | |
subjects: | |
- kind: ServiceAccount | |
name: frontend-service-account | |
namespace: default | |
roleRef: | |
kind: Role | |
name: pod-reader | |
apiGroup: rbac.authorization.k8s.io | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: backend-pod | |
namespace: default | |
labels: | |
app: backend | |
spec: | |
containers: | |
- name: backend | |
image: python:latest | |
command: ["python", "-m", "http.server", "8080"] | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: frontend-pod | |
namespace: default | |
labels: | |
app: frontend | |
spec: | |
serviceAccountName: frontend-service-account | |
containers: | |
- name: frontend | |
image: ubuntu:latest | |
command: ["bash", "-c", "sleep infinity"] | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: backend-service | |
namespace: default | |
spec: | |
selector: | |
app: backend | |
ports: | |
- protocol: TCP | |
port: 80 | |
targetPort: 8080 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment