Skip to content

Instantly share code, notes, and snippets.

@initcron
Created September 5, 2024 16:05
Show Gist options
  • Save initcron/38dd1d70291b8898a46fd50bcfcf5358 to your computer and use it in GitHub Desktop.
Save initcron/38dd1d70291b8898a46fd50bcfcf5358 to your computer and use it in GitHub Desktop.
Show the CoreDNS Service Entries in Tabular Format
from kubernetes import client, config
from prettytable import PrettyTable
# Load kube config from the default location (~/.kube/config)
config.load_kube_config()
# Initialize the Kubernetes API client
v1 = client.CoreV1Api()
# Create a table for displaying the output
table = PrettyTable()
table.field_names = ["Service Name", "Namespace", "Cluster IP", "DNS Entry"]
# Get all services in all namespaces
services = v1.list_service_for_all_namespaces()
# Loop through the services and generate DNS entries
for svc in services.items:
service_name = svc.metadata.name
namespace = svc.metadata.namespace
cluster_ip = svc.spec.cluster_ip
# Kubernetes DNS entry format: <service-name>.<namespace>.svc.cluster.local
dns_entry = f"{service_name}.{namespace}.svc.cluster.local"
# Add row to table
table.add_row([service_name, namespace, cluster_ip, dns_entry])
# Print the table
print(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment