Skip to content

Instantly share code, notes, and snippets.

@jseguillon
Last active March 6, 2024 17:21
Show Gist options
  • Save jseguillon/0a4b89c361a017cab035af37cd683e7c to your computer and use it in GitHub Desktop.
Save jseguillon/0a4b89c361a017cab035af37cd683e7c to your computer and use it in GitHub Desktop.

Cluster State Documentation

Table of Contents

Namespace: kube-system

Namespace: kube-public

DEPLOYMENTS

No deployments found.

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

No svc found.

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d

Services and their endpoints (including pod names)

No services or endpoints found.

Namespace: kube-node-lease

DEPLOYMENTS

No deployments found.

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

No svc found.

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d

Services and their endpoints (including pod names)

No services or endpoints found.

Namespace: default

DEPLOYMENTS

No deployments found.

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 443/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
default ClusterIP Error fetching Error fetching pod

Namespace: tigera-operator

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
tigera-operator 1/1 1 1 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
tigera-operator tigera-operator quay.io/tigera/operator:v1.32.3

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

No svc found.

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
tigera-operator 0 20d

Services and their endpoints (including pod names)

No services or endpoints found.

Namespace: calico-system

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
calico-kube-controllers 0/1 1 0 20d
calico-typha 1/1 1 1 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
calico-system calico-kube-controllers docker.io/calico/kube-controllers:v3.27.0
calico-system calico-typha docker.io/calico/typha:v3.27.0

DS

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
csi-node-driver 1 1 0 1 0 kubernetes.io/os=linux 20d
calico-node 1 1 1 1 1 kubernetes.io/os=linux 20d

DS Container Images

NAMESPACE NAME CONTAINER IMAGES
calico-system csi-node-driver docker.io/calico/csi:v3.27.0, docker.io/calico/node-driver-registrar:v3.27.0
calico-system calico-node docker.io/calico/node:v3.27.0

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-typha ClusterIP 10.43.156.99 5473/TCP 20d
calico-kube-controllers-metrics ClusterIP None 9094/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
calico-typha 0 20d
calico-node 0 20d
calico-cni-plugin 0 20d
calico-kube-controllers 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
calico-system ClusterIP Error fetching Error fetching pod
calico-system ClusterIP Error fetching Error fetching pod

Namespace: calico-apiserver

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
calico-apiserver 0/2 2 0 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
calico-apiserver calico-apiserver docker.io/calico/apiserver:v3.27.0

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-api ClusterIP 10.43.39.252 443/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
calico-apiserver 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
calico-apiserver ClusterIP Error fetching Error fetching pod

Namespace: cdi

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
cdi-apiserver 0/1 1 0 20d
cdi-deployment 0/1 1 0 20d
cdi-operator 0/1 1 0 20d
cdi-uploadproxy 0/1 1 0 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
cdi cdi-apiserver quay.io/kubevirt/cdi-apiserver:v1.58.1
cdi cdi-deployment quay.io/kubevirt/cdi-controller:v1.58.1
cdi cdi-operator quay.io/kubevirt/cdi-operator:v1.58.1
cdi cdi-uploadproxy quay.io/kubevirt/cdi-uploadproxy:v1.58.1

DS

No ds found.

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cdi-api ClusterIP 10.43.125.239 443/TCP 20d
cdi-prometheus-metrics ClusterIP 10.43.223.94 8080/TCP 20d
cdi-uploadproxy ClusterIP 10.43.63.130 443/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
cdi-operator 0 20d
cdi-apiserver 0 20d
cdi-sa 0 20d
cdi-uploadproxy 0 20d
cdi-cronjob 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
cdi ClusterIP Error fetching Error fetching pod
cdi ClusterIP Error fetching Error fetching pod
cdi ClusterIP Error fetching Error fetching pod

Namespace: kubevirt

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
virt-api 0/1 1 0 20d
virt-controller 0/2 2 0 20d
virt-operator 0/2 2 0 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
kubevirt virt-api quay.io/kubevirt/virt-api:v1.1.1
kubevirt virt-controller quay.io/kubevirt/virt-controller:v1.1.1
kubevirt virt-operator quay.io/kubevirt/virt-operator:v1.1.1

DS

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
virt-handler 1 1 0 1 0 kubernetes.io/os=linux 20d

DS Container Images

NAMESPACE NAME CONTAINER IMAGES
kubevirt virt-handler quay.io/kubevirt/virt-handler:v1.1.1

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubevirt-prometheus-metrics ClusterIP None 443/TCP 20d
virt-api ClusterIP 10.43.22.145 443/TCP 20d
kubevirt-operator-webhook ClusterIP 10.43.126.96 443/TCP 20d
virt-exportproxy ClusterIP 10.43.189.95 443/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
kubevirt-operator 0 20d
kubevirt-apiserver 0 20d
kubevirt-controller 0 20d
kubevirt-handler 0 20d
kubevirt-exportproxy 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
kubevirt ClusterIP Error fetching Error fetching pod
kubevirt ClusterIP Error fetching Error fetching pod
kubevirt ClusterIP Error fetching Error fetching pod
kubevirt ClusterIP Error fetching Error fetching pod

Namespace: cluster-network-addons

DEPLOYMENTS

NAME READY UP-TO-DATE AVAILABLE AGE
cluster-network-addons-operator 0/1 1 0 20d
kubemacpool-mac-controller-manager 0/1 1 0 20d
secondary-dns 0/1 1 0 20d
kubemacpool-cert-manager 0/1 1 0 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
cluster-network-addons cluster-network-addons-operator quay.io/kubevirt/cluster-network-addons-operator:v0.90.1, quay.io/openshift/origin-kube-rbac-proxy@sha256:baedb268ac66456018fb30af395bb3d69af5fff3252ff5d549f0231b1ebb6901
cluster-network-addons kubemacpool-mac-controller-manager quay.io/kubevirt/kubemacpool@sha256:2f7a4ed0532909176b21eb3b80dec88a14e4f23d0c0aae129acd699636d058a3, quay.io/openshift/origin-kube-rbac-proxy@sha256:baedb268ac66456018fb30af395bb3d69af5fff3252ff5d549f0231b1ebb6901
cluster-network-addons secondary-dns registry.k8s.io/coredns/coredns@sha256:a0ead06651cf580044aeb0a0feba63591858fb2e43ade8c9dea45a6a89ae7e5e, ghcr.io/kubevirt/kubesecondarydns@sha256:e87e829380a1e576384145f78ccaa885ba1d5690d5de7d0b73d40cfb804ea24d
cluster-network-addons kubemacpool-cert-manager quay.io/kubevirt/kubemacpool@sha256:2f7a4ed0532909176b21eb3b80dec88a14e4f23d0c0aae129acd699636d058a3

DS

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
bridge-marker 1 1 1 1 1 kubernetes.io/os=linux 20d
kube-cni-linux-bridge-plugin 1 1 0 1 0 kubernetes.io/os=linux 20d
macvtap-cni 1 1 0 1 0 kubernetes.io/os=linux 20d
multus 1 1 0 1 0 kubernetes.io/os=linux 20d

DS Container Images

NAMESPACE NAME CONTAINER IMAGES
cluster-network-addons bridge-marker quay.io/kubevirt/bridge-marker@sha256:bba066e3b5ff3fb8c5e20861fe8abe51e3c9b50ad6ce3b2616af9cb5479a06d0
cluster-network-addons kube-cni-linux-bridge-plugin quay.io/kubevirt/cni-default-plugins@sha256:825e3f9fec1996c54a52cec806154945b38f76476b160d554c36e38dfffe5e61
cluster-network-addons macvtap-cni quay.io/kubevirt/macvtap-cni@sha256:850b89343ace7c7ea6b18dd8e11964613974e9d1f7377af03854d407fb15230a
cluster-network-addons multus ghcr.io/k8snetworkplumbingwg/multus-cni@sha256:3fbcc32bd4e4d15bd93c96def784a229cd84cca27942bf4858b581f31c97ee02

STS

No sts found.

INGRESS

No ingress found.

SVC

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubemacpool-service ClusterIP 10.43.228.248 443/TCP 20d

PVC

No pvc found.

SA

NAME SECRETS AGE
default 0 20d
cluster-network-addons-operator 0 20d
multus 0 20d
bridge-marker 0 20d
secondary 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
cluster-network-addons ClusterIP Error fetching Error fetching pod

Final Chapter: All Resources (No Namespace Filter)

DEPLOYMENTS

NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
cdi cdi-apiserver 0/1 1 0 20d
calico-apiserver calico-apiserver 0/2 2 0 20d
calico-system calico-kube-controllers 0/1 1 0 20d
cdi cdi-deployment 0/1 1 0 20d
cdi cdi-operator 0/1 1 0 20d
cdi cdi-uploadproxy 0/1 1 0 20d
cluster-network-addons cluster-network-addons-operator 0/1 1 0 20d
cluster-network-addons kubemacpool-mac-controller-manager 0/1 1 0 20d
cluster-network-addons secondary-dns 0/1 1 0 20d
cluster-network-addons kubemacpool-cert-manager 0/1 1 0 20d
kube-system coredns 0/1 1 0 20d
kube-system local-path-provisioner 0/1 1 0 20d
kube-system metrics-server 0/1 1 0 20d
kubevirt virt-api 0/1 1 0 20d
kubevirt virt-controller 0/2 2 0 20d
kubevirt virt-operator 0/2 2 0 20d
tigera-operator tigera-operator 1/1 1 1 20d
calico-system calico-typha 1/1 1 1 20d

DEPLOYMENTS Container Images

NAMESPACE NAME CONTAINER IMAGES
cdi cdi-apiserver quay.io/kubevirt/cdi-apiserver:v1.58.1
calico-apiserver calico-apiserver docker.io/calico/apiserver:v3.27.0
calico-system calico-kube-controllers docker.io/calico/kube-controllers:v3.27.0
cdi cdi-deployment quay.io/kubevirt/cdi-controller:v1.58.1
cdi cdi-operator quay.io/kubevirt/cdi-operator:v1.58.1
cdi cdi-uploadproxy quay.io/kubevirt/cdi-uploadproxy:v1.58.1
cluster-network-addons cluster-network-addons-operator quay.io/kubevirt/cluster-network-addons-operator:v0.90.1, quay.io/openshift/origin-kube-rbac-proxy@sha256:baedb268ac66456018fb30af395bb3d69af5fff3252ff5d549f0231b1ebb6901
cluster-network-addons kubemacpool-mac-controller-manager quay.io/kubevirt/kubemacpool@sha256:2f7a4ed0532909176b21eb3b80dec88a14e4f23d0c0aae129acd699636d058a3, quay.io/openshift/origin-kube-rbac-proxy@sha256:baedb268ac66456018fb30af395bb3d69af5fff3252ff5d549f0231b1ebb6901
cluster-network-addons secondary-dns registry.k8s.io/coredns/coredns@sha256:a0ead06651cf580044aeb0a0feba63591858fb2e43ade8c9dea45a6a89ae7e5e, ghcr.io/kubevirt/kubesecondarydns@sha256:e87e829380a1e576384145f78ccaa885ba1d5690d5de7d0b73d40cfb804ea24d
cluster-network-addons kubemacpool-cert-manager quay.io/kubevirt/kubemacpool@sha256:2f7a4ed0532909176b21eb3b80dec88a14e4f23d0c0aae129acd699636d058a3
kube-system coredns rancher/mirrored-coredns-coredns:1.10.1
kube-system local-path-provisioner rancher/local-path-provisioner:v0.0.24
kube-system metrics-server rancher/mirrored-metrics-server:v0.6.3
kubevirt virt-api quay.io/kubevirt/virt-api:v1.1.1
kubevirt virt-controller quay.io/kubevirt/virt-controller:v1.1.1
kubevirt virt-operator quay.io/kubevirt/virt-operator:v1.1.1
tigera-operator tigera-operator quay.io/tigera/operator:v1.32.3
calico-system calico-typha docker.io/calico/typha:v3.27.0

DS

NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
cluster-network-addons bridge-marker 1 1 1 1 1 kubernetes.io/os=linux 20d
calico-system csi-node-driver 1 1 0 1 0 kubernetes.io/os=linux 20d
cluster-network-addons kube-cni-linux-bridge-plugin 1 1 0 1 0 kubernetes.io/os=linux 20d
cluster-network-addons macvtap-cni 1 1 0 1 0 kubernetes.io/os=linux 20d
cluster-network-addons multus 1 1 0 1 0 kubernetes.io/os=linux 20d
kubevirt virt-handler 1 1 0 1 0 kubernetes.io/os=linux 20d
calico-system calico-node 1 1 1 1 1 kubernetes.io/os=linux 20d

DS Container Images

NAMESPACE NAME CONTAINER IMAGES
cluster-network-addons bridge-marker quay.io/kubevirt/bridge-marker@sha256:bba066e3b5ff3fb8c5e20861fe8abe51e3c9b50ad6ce3b2616af9cb5479a06d0
calico-system csi-node-driver docker.io/calico/csi:v3.27.0, docker.io/calico/node-driver-registrar:v3.27.0
cluster-network-addons kube-cni-linux-bridge-plugin quay.io/kubevirt/cni-default-plugins@sha256:825e3f9fec1996c54a52cec806154945b38f76476b160d554c36e38dfffe5e61
cluster-network-addons macvtap-cni quay.io/kubevirt/macvtap-cni@sha256:850b89343ace7c7ea6b18dd8e11964613974e9d1f7377af03854d407fb15230a
cluster-network-addons multus ghcr.io/k8snetworkplumbingwg/multus-cni@sha256:3fbcc32bd4e4d15bd93c96def784a229cd84cca27942bf4858b581f31c97ee02
kubevirt virt-handler quay.io/kubevirt/virt-handler:v1.1.1
calico-system calico-node docker.io/calico/node:v3.27.0

STS

No sts found.

INGRESS

No ingress found.

SVC

NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.43.0.1 443/TCP 20d
kube-system kube-dns ClusterIP 10.43.0.10 53/UDP,53/TCP,9153/TCP 20d
kube-system metrics-server ClusterIP 10.43.63.165 443/TCP 20d
calico-system calico-typha ClusterIP 10.43.156.99 5473/TCP 20d
calico-system calico-kube-controllers-metrics ClusterIP None 9094/TCP 20d
calico-apiserver calico-api ClusterIP 10.43.39.252 443/TCP 20d
cdi cdi-api ClusterIP 10.43.125.239 443/TCP 20d
cdi cdi-prometheus-metrics ClusterIP 10.43.223.94 8080/TCP 20d
cdi cdi-uploadproxy ClusterIP 10.43.63.130 443/TCP 20d
cluster-network-addons kubemacpool-service ClusterIP 10.43.228.248 443/TCP 20d
kubevirt kubevirt-prometheus-metrics ClusterIP None 443/TCP 20d
kubevirt virt-api ClusterIP 10.43.22.145 443/TCP 20d
kubevirt kubevirt-operator-webhook ClusterIP 10.43.126.96 443/TCP 20d
kubevirt virt-exportproxy ClusterIP 10.43.189.95 443/TCP 20d

PVC

No pvc found.

SA

NAMESPACE NAME SECRETS AGE
kube-system ephemeral-volume-controller 0 20d
kube-system expand-controller 0 20d
kube-system resourcequota-controller 0 20d
kube-system horizontal-pod-autoscaler 0 20d
kube-system disruption-controller 0 20d
kube-system node-controller 0 20d
kube-system persistent-volume-binder 0 20d
kube-system root-ca-cert-publisher 0 20d
kube-system endpoint-controller 0 20d
kube-system namespace-controller 0 20d
kube-system ttl-controller 0 20d
kube-system endpointslicemirroring-controller 0 20d
kube-system job-controller 0 20d
kube-system deployment-controller 0 20d
kube-system replicaset-controller 0 20d
kube-system token-cleaner 0 20d
kube-system clusterrole-aggregation-controller 0 20d
kube-system service-account-controller 0 20d
kube-system pv-protection-controller 0 20d
kube-system statefulset-controller 0 20d
kube-system cronjob-controller 0 20d
kube-system certificate-controller 0 20d
kube-system coredns 0 20d
kube-system local-path-provisioner-service-account 0 20d
kube-system svclb 0 20d
kube-system metrics-server 0 20d
kube-system pvc-protection-controller 0 20d
kube-system pod-garbage-collector 0 20d
kube-system attachdetach-controller 0 20d
kube-system ttl-after-finished-controller 0 20d
kube-system generic-garbage-collector 0 20d
kube-system daemon-set-controller 0 20d
kube-system endpointslice-controller 0 20d
kube-system replication-controller 0 20d
default default 0 20d
kube-node-lease default 0 20d
kube-public default 0 20d
kube-system default 0 20d
tigera-operator default 0 20d
tigera-operator tigera-operator 0 20d
calico-system default 0 20d
calico-system calico-typha 0 20d
calico-system calico-node 0 20d
calico-system calico-cni-plugin 0 20d
calico-system calico-kube-controllers 0 20d
calico-apiserver default 0 20d
calico-apiserver calico-apiserver 0 20d
kubevirt default 0 20d
kubevirt kubevirt-operator 0 20d
cdi default 0 20d
cdi cdi-operator 0 20d
cdi cdi-apiserver 0 20d
cdi cdi-sa 0 20d
cdi cdi-uploadproxy 0 20d
cdi cdi-cronjob 0 20d
cluster-network-addons default 0 20d
cluster-network-addons cluster-network-addons-operator 0 20d
cluster-network-addons multus 0 20d
cluster-network-addons bridge-marker 0 20d
cluster-network-addons secondary 0 20d
kubevirt kubevirt-apiserver 0 20d
kubevirt kubevirt-controller 0 20d
kubevirt kubevirt-handler 0 20d
kubevirt kubevirt-exportproxy 0 20d

Services and their endpoints (including pod names)

NAMESPACE NAMETYPE ENDPOINTS POD NAMES
default kubernetes 172.19.34.189:6443 bridge-marker-2vkkt
kube-system kube-dns None
kube-system metrics-server None
calico-system calico-typha 172.19.34.189:5473 bridge-marker-2vkkt
calico-system calico-kube-controllers-metrics None
calico-apiserver calico-api None
cdi cdi-api None
cdi cdi-prometheus-metrics None
cdi cdi-uploadproxy None
cluster-network-addons kubemacpool-service None
kubevirt kubevirt-prometheus-metrics None
kubevirt virt-api None
kubevirt kubevirt-operator-webhook None
kubevirt virt-exportproxy None

aditional chapter just for testing

flowchart TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
Loading
import subprocess
import argparse
import re
import logging
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Generate Kubernetes cluster state documentation with exclusion patterns.')
parser.add_argument('--exclude', action='append', help='Exclusion pattern for resources (can be used multiple times)', default=[])
args = parser.parse_args()
# Convert wildcard patterns to regex patterns
def wildcard_to_regex(pattern):
pattern = re.escape(pattern)
pattern = pattern.replace(r'\*', '.*')
return re.compile(pattern + '$')
# Convert exclusion patterns to regex objects
exclusion_patterns = [wildcard_to_regex(pattern) for pattern in args.exclude]
# Function to check if a namespace/resource matches any exclusion pattern
def is_excluded(namespace, resource):
resource_identifier = f"{namespace}/{resource}"
for pattern in exclusion_patterns:
if pattern.match(resource_identifier) or pattern.match(namespace + "/*") or pattern.match("*/" + resource):
return True
return False
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
def create_markdown_table(headers, data):
markdown = "| " + " | ".join(headers) + " |\n"
markdown += "| " + " | ".join(["---"] * len(headers)) + " |\n"
for row in data:
markdown += "| " + " | ".join(row) + " |\n"
return markdown
def run_kubectl_command(command):
try:
output = subprocess.check_output(command, shell=True, text=True)
return output
except subprocess.CalledProcessError as e:
logging.info(f"No output returned from command: {command}")
return None
def kubectl_data_and_headers(command, namespace=None):
if namespace:
command += f" -n {namespace}"
return run_kubectl_command_parse(command)
def run_kubectl_command_parse(command):
try:
output = subprocess.check_output(command, shell=True, text=True).strip()
if output:
lines = output.split('\n')
headers = lines[0].split()
data = [line.split() for line in lines[1:]]
return headers, data
else:
logging.info(f"No output returned from command: {command}")
return [], []
except subprocess.CalledProcessError as e:
logging.error(f"Error executing command: {e}")
return [], []
def get_container_images(resource_type, namespace=None):
"""
Fetch container images from Deployments or DaemonSets.
Args:
resource_type (str): Either 'deployments' or 'daemonsets'.
namespace (str): The namespace from which to fetch the resources. If None, fetches from all namespaces.
Returns:
list of tuples: Each tuple contains (namespace, name, container_images)
"""
command = f"kubectl get {resource_type} {'-n ' + namespace if namespace else '--all-namespaces'} -o=jsonpath='{{range .items[*]}}{{.metadata.namespace}} {{.metadata.name}} {{range .spec.template.spec.containers[*]}} {{.image}}{{end}}{{\"\\n\"}}{{end}}'"
output = run_kubectl_command(command)
if not output:
return []
data = []
lines = output.strip().split('\n')
for line in lines:
parts = line.rsplit(' ')
ns, name = parts[0], parts[1]
images = parts[2:]
images = [element for element in images if element not in [None, '']]
data.append((ns, name, ", ".join(images)))
return data
def get_pod_names_from_endpoints(namespace, endpoints):
pod_names = []
if "<none>" in endpoints or endpoints == "None" or endpoints == "Error fetching":
return "None"
endpoint_list = endpoints.split(',')
for endpoint in endpoint_list:
ip = endpoint.split(':')[0].strip()
command = f"kubectl get pods --all-namespaces -o wide | grep {ip}"
output = run_kubectl_command(command)
if output:
lines = output.strip().split('\n')
pod_name = lines[0].split()[1] if lines else "Pod not found"
pod_names.append(pod_name)
else:
pod_names.append("Error fetching pod")
return ", ".join(pod_names)
def get_services_and_endpoints_with_pods(namespace=None):
command = "kubectl get svc"
if namespace:
command += f" -n {namespace}"
else:
command += " --all-namespaces"
output = run_kubectl_command(command)
if not output:
return [], []
lines = output.strip().split('\n')
data = []
for line in lines[1:]: # Skip the header row
parts = line.split()
ns = parts[0] if not namespace else namespace
name = parts[1]
describe_command = f"kubectl describe svc {name} -n {ns}"
describe_output = run_kubectl_command(describe_command)
if describe_output:
endpoints_line = [line for line in describe_output.split('\n') if "Endpoints:" in line]
endpoints = endpoints_line[0].split(":", 1)[1].strip() if endpoints_line else "None"
pod_names = get_pod_names_from_endpoints(ns, endpoints)
else:
endpoints = "Error fetching"
pod_names = "Error fetching pod"
data.append([ns, name, endpoints, pod_names])
headers = headers = ["NAMESPACE", "NAME" "TYPE", "ENDPOINTS", "POD NAMES"]
return headers, data
def get_namespaces():
command = "kubectl get namespaces -o=jsonpath='{.items[*].metadata.name}'"
output = run_kubectl_command(command)
if output:
namespaces = output.strip().split()
return namespaces
return []
def generate_markdown_for_namespace(md_file, namespace=None):
resources = ["deployments", "ds", "sts", "ingress", "svc", "pvc", "sa"]
for resource in resources:
if is_excluded(namespace if namespace else "*", resource):
continue # Skip this resource due to an exclusion match
md_file.write(f"## {resource.upper()}\n\n")
command = f"kubectl get {resource} --all-namespaces" if not namespace else f"kubectl get {resource}"
headers, data = kubectl_data_and_headers(command, namespace)
if headers and data:
md_file.write(create_markdown_table(headers, data))
# subchapter for containers images when applying
if resource in ["deployments", "ds", "sts"]:
resource_type = 'deployments' if resource == 'deployments' else 'daemonsets'
images_data = get_container_images(resource_type, namespace)
if images_data:
headers = ["NAMESPACE", "NAME", "CONTAINER IMAGES"]
md_file.write(f"### {resource.upper()} Container Images\n\n")
md_file.write(create_markdown_table(headers, images_data))
else:
md_file.write(f"No container images found for {resource}.\n\n")
else:
md_file.write(f"No {resource} found.\n\n")
if is_excluded(namespace if namespace else "*", "svc-ep-pods"):
return
# Services and endpoints with pods
md_file.write("## Services and their endpoints (including pod names)\n\n")
headers, data = get_services_and_endpoints_with_pods(namespace)
if headers and data:
md_file.write(create_markdown_table(headers, data))
else:
md_file.write("No services or endpoints found.\n\n")
def generate_markdown():
filename = "cluster_state.md"
logging.info(f"Starting to generate markdown documentation in {filename}")
# Start with collecting namespaces to build TOC
namespaces = get_namespaces()
toc_items = [f"- [Namespace: {namespace}](#namespace-{namespace.lower().replace(' ', '-')})" for namespace in namespaces]
toc_items.append("- [Final Chapter: All Resources (No Namespace Filter)](#final-chapter-all-resources-no-namespace-filter)")
with open(filename, 'w') as md_file:
md_file.write("# Cluster State Documentation\n\n")
# Write TOC
md_file.write("## Table of Contents\n\n")
md_file.writelines("\n".join(toc_items))
md_file.write("\n\n")
if not namespaces:
# If no namespaces are found or if we want to generate for all namespaces regardless
generate_markdown_for_namespace(md_file)
else:
for namespace in namespaces:
# Adjust the header to include a linkable ID
md_file.write(f'<a name="namespace-{namespace.lower().replace(" ", "-")}"></a>\n')
md_file.write(f"# Namespace: {namespace}\n\n")
generate_markdown_for_namespace(md_file, namespace)
# Additional section for all resources without namespace filter
md_file.write('<a name="final-chapter-all-resources-no-namespace-filter"></a>\n')
md_file.write("# Final Chapter: All Resources (No Namespace Filter)\n\n")
generate_markdown_for_namespace(md_file, None)
md_file.write("""
# aditional chapter just for testing
```mermaid
flowchart TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
```
""")
logging.info(f"Markdown documentation generated in {filename}")
generate_markdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment