def state_nodes(): datanodes = [] load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() nodes = core_v1.list_node().items for node in nodes: #print(node) datanodes.append({ 'name': node.metadata.name, 'status': node.status.phase, }) return datanodes
def state_namespaces(): datanamespaces = [] load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() namespaces = core_v1.list_namespace().items # print(datanamespaces) for namespace in namespaces: # print(namespace) datanamespaces.append({ 'name': namespace.metadata.name, 'status': namespace.status.phase, }) return datanamespaces
def state_pods(): data_pods = [] load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() pods = core_v1.list_pod_for_all_namespaces().items for pod in pods: # print(pod) data_pods.append({ 'name': pod.metadata.name, 'namespace': pod.metadata.namespace, 'host_ip': pod.status.host_ip, 'pod_ip': pod.status.pod_ip, 'phase': pod.status.phase }) return data_pods
def hexagons_data(): hexa_data = [] # doing this computation within a kubernetes cluster load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() try: nodes_list = core_v1.list_node().items except Exception as e: print("Error listing nodes") #print("Something bad happened: " + e) for node in nodes_list: #print(node) node_name = node.metadata.name node_labels = node.metadata.labels # print(node_labels) if "node-role.kubernetes.io/master" in node_labels: if node_labels['node-role.kubernetes.io/master'] == 'true': node_role = "Master" else: node_role = "Non master" else: node_role = "Non master" allocatable = node.status.allocatable node_info = node.status.node_info #print("------") #print("------") #print("------") #print(node_info.architecture) #print("------") #print("------") #print("------") hdata = {} hdata['name'] = node_name hdata['role'] = node_role hdata['cpu'] = allocatable["cpu"] hdata['ephstorage'] = allocatable["ephemeral-storage"] hdata['mem'] = allocatable["memory"] hdata['maxpods'] = allocatable["pods"] hdata['arch'] = node_info.architecture hdata['crver'] = node_info.container_runtime_version hdata['kernelver'] = node_info.kernel_version hdata['kubeproxyver'] = node_info.kube_proxy_version hdata['kubeletver'] = node_info.kubelet_version hdata['os'] = node_info.operating_system state_info = compute_node_resources(node_name) """ state_info = {'pods': {'allocatable': 200, 'allocated': 100, 'percentage': 50}, 'cpu': {'allocatable': 4000, 'allocated': 1250, 'percentage': 25}, 'mem': {'allocatable': 65000, 'allocated': 5000, 'percentage': 75}, 'storage': {'allocatable': 1250000, 'allocated': 2653, 'percentage': 10} } """ hdata['state_info'] = state_info max_pods = int(int(allocatable["pods"]) * 1.5) field_selector = ("spec.nodeName=" + node_name) pods = core_v1.list_pod_for_all_namespaces( limit=max_pods, field_selector=field_selector).items hdata['pods'] = [] for pod in pods: # print(pod.metadata.name) # print(pod) hdata['pods'].append({ 'name': pod.metadata.name, 'namespace': pod.metadata.namespace, 'host_ip': pod.status.host_ip, 'pod_ip': pod.status.pod_ip, 'phase': pod.status.phase }) hexa_data.append(hdata) # print(hexa_data) return hexa_data
def hexagons_data(): hexa_data = [] # doing this computation within a kubernetes cluster load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() try: nodes_list = core_v1.list_node().items except Exception as e: print("Something bad happened: " + e) for node in nodes_list: # print(node) node_name = node.metadata.name node_labels = node.metadata.labels # print(node_labels) if node_labels['node-role.kubernetes.io/master'] == 'true': node_role = "Master" else: node_role = "Non master" allocatable = node.status.allocatable hdata = {} hdata['name'] = node_name hdata['role'] = node_role max_pods = int(int(allocatable["pods"]) * 1.5) field_selector = ("status.phase!=Succeeded," + "spec.nodeName=" + node_name) pods = core_v1.list_pod_for_all_namespaces( limit=max_pods, field_selector=field_selector).items hdata['pods'] = [] for pod in pods: # print(pod.metadata.name) # print(pod) hdata['pods'].append({ 'name': pod.metadata.name, 'namespace': pod.metadata.namespace, 'host_ip': pod.status.host_ip, 'pod_ip': pod.status.pod_ip, 'phase': pod.status.phase }) hexa_data.append(hdata) print(hexa_data) aux = [{ "name": "node-00", "role": "Master", "pods": [{ "name": "coredns-66bf467f8-lzn8z", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.2", "phase": "Running" }, { "name": "coredns-66bff67f8-vhxp4", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.3", "phase": "Running" }, { "name": "etcd-minikube", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "kube-apiserver-minikube", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "kube-controller-manager-minikube", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "kube-proxy-5r5txd", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "kube-scheduler-minikube", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "storage-provisioner", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "dashboard-metrics-scraper-84b5fdf55ff-qq6j2", "namespace": "kubernetes-dashboard", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.4", "phase": "Running" }, { "name": "kubernetes-dashboard-bc4456cc64-nm69d", "namespace": "kubernetes-dashboard", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.5", "phase": "Running" }, { "name": "pystol-controller-667df549448-t8brm", "namespace": "pystol", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.6", "phase": "Running" }, { "name": "pystol-ui-8554cbb6558-hg8kh", "namespace": "pystol", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.7", "phase": "Running" }] }, { "name": "node-01", "role": "Non master", "pods": [{ "name": "coredns-66bff467f8-lzn8z", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.2", "phase": "Running" }, { "name": "coredns-66bff467f8-vhxp4", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.3", "phase": "Running" }, { "name": "kube-proxy-5rtxd", "namespace": "kube-system", "host_ip": "192.168.39.125", "pod_ip": "192.168.39.125", "phase": "Running" }, { "name": "dashboard-metrics-scraper-84bfdf55ff-qq6j2", "namespace": "kubernetes-dashboard", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.4", "phase": "Running" }, { "name": "kubernetes-dashboard-bc446cc64-nm69d", "namespace": "kubernetes-dashboard", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.5", "phase": "Running" }, { "name": "pystol-controller-667df49448-t8brm", "namespace": "pystol", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.6", "phase": "Running" }, { "name": "pystol-ui-8554cbb658-hg8kh", "namespace": "pystol", "host_ip": "192.168.39.125", "pod_ip": "172.17.0.7", "phase": "Running" }] }] return hexa_data
def compute_allocated_resources(): ureg = UnitRegistry() # Mem ureg.define("kmemunits = 1 = [kmemunits]") ureg.define("Ki = 1024 * kmemunits") ureg.define("Mi = Ki^2") ureg.define("Gi = Ki^3") ureg.define("Ti = Ki^4") ureg.define("Pi = Ki^5") ureg.define("Ei = Ki^6") # CPU ureg.define("kcpuunits = 1 = [kcpuunits]") ureg.define("m = 1/1000 * kcpuunits") ureg.define("k = 1000 * kcpuunits") ureg.define("M = k^2") ureg.define("G = k^3") ureg.define("T = k^4") ureg.define("P = k^5") ureg.define("E = k^6") Q_ = ureg.Quantity data = {} state_info = { 'pods': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'cpu': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'mem': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'storage': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 } } # doing this computation within a kubernetes cluster load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() global_pods_allocatable = 0 global_pods_allocated = 0 global_cpu_allocatable = 0 global_cpu_allocated = 0 global_mem_allocatable = 0 global_mem_allocated = 0 global_storage_allocatable = 10000 global_storage_allocated = 1750 try: nodes_list = core_v1.list_node().items except Exception as e: print("Something bad happened: " + e) for node in nodes_list: stats = {} node_name = node.metadata.name allocatable = node.status.allocatable max_pods = int(int(allocatable["pods"]) * 1.5) global_pods_allocatable = global_pods_allocatable + max_pods field_selector = ("status.phase!=Succeeded,status.phase!=Failed," + "spec.nodeName=" + node_name) stats["cpu_alloc"] = Q_(allocatable["cpu"]) stats["mem_alloc"] = Q_(allocatable["memory"]) stats["storage_alloc"] = Q_(allocatable["ephemeral-storage"]) global_cpu_allocatable = global_cpu_allocatable + Q_( allocatable["cpu"]) global_mem_allocatable = global_mem_allocatable + Q_( allocatable["memory"]) # global_storage_allocatable = global_storage_allocatable + Q_(allocatable["ephemeral-storage"]) pods = core_v1.list_pod_for_all_namespaces( limit=max_pods, field_selector=field_selector).items # compute the allocated resources cpureqs, cpulmts, memreqs, memlmts, storagereqs = [], [], [], [], [] global_pods_allocated = global_pods_allocated + len(pods) for pod in pods: #print("****") #print(pod.stats) #print("****") for container in pod.spec.containers: res = container.resources reqs = defaultdict(lambda: 0, res.requests or {}) lmts = defaultdict(lambda: 0, res.limits or {}) cpureqs.append(Q_(reqs["cpu"])) memreqs.append(Q_(reqs["memory"])) storagereqs.append(Q_(reqs["ephemeral-storage"])) cpulmts.append(Q_(lmts["cpu"])) memlmts.append(Q_(lmts["memory"])) #print("----") #print(res) #print(Q_(reqs["ephemeral-storage"])) #print(Q_(lmts["ephemeral-storage"])) #print("----") # storagelmts.append(Q_(lmts["ephemeral-storage"])) stats["cpu_req"] = sum(cpureqs) stats["cpu_lmt"] = sum(cpulmts) stats["cpu_req_per"] = (stats["cpu_req"] / stats["cpu_alloc"] * 100) stats["cpu_lmt_per"] = (stats["cpu_lmt"] / stats["cpu_alloc"] * 100) stats["mem_req"] = sum(memreqs) stats["mem_lmt"] = sum(memlmts) stats["mem_req_per"] = (stats["mem_req"] / stats["mem_alloc"] * 100) stats["mem_lmt_per"] = (stats["mem_lmt"] / stats["mem_alloc"] * 100) #stats["storage_req"] = sum(storagereqs) #stats["storage_lmt"] = sum(storagelmts) #stats["storage_req_per"] = (stats["storage_req"] / stats["storage_alloc"] * 100) #stats["storage_lmt_per"] = (stats["storage_lmt"] / stats["storage_alloc"] * 100) global_cpu_allocated = global_cpu_allocated + sum(cpureqs) global_mem_allocated = global_mem_allocated + sum(memreqs) data[node_name] = stats state_info['pods']['allocatable'] = global_pods_allocatable state_info['pods']['allocated'] = global_pods_allocated state_info['pods']['percentage'] = (global_pods_allocated * 100) // global_pods_allocatable state_info['cpu']['allocatable'] = (global_cpu_allocatable.magnitude * 1000) state_info['cpu']['allocated'] = global_cpu_allocated state_info['cpu']['percentage'] = ((global_cpu_allocated * 100) // global_cpu_allocatable).magnitude state_info['mem']['allocatable'] = global_mem_allocatable.to(ureg.Mi) state_info['mem']['allocated'] = global_mem_allocated.to(ureg.Mi) state_info['mem']['percentage'] = ( (int(global_mem_allocated.to(ureg.Mi).magnitude) * 100) // int(global_mem_allocatable.to(ureg.Mi).magnitude)) state_info['storage']['allocatable'] = global_storage_allocatable state_info['storage']['allocated'] = global_storage_allocated state_info['storage']['percentage'] = (global_storage_allocated * 100) // global_storage_allocatable return state_info
def compute_allocated_resources(): state_info = { 'pods': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'cpu': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'mem': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'storage': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 } } load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() try: nodes_list = core_v1.list_node().items except Exception as e: print("Problem listing nodes") # print("Something bad happened: " + e) for node in nodes_list: node_name = node.metadata.name node_stats = compute_node_resources(node_name) state_info['pods']['allocatable'] = state_info['pods'][ 'allocatable'] + node_stats['pods']['allocatable'] state_info['pods']['allocated'] = state_info['pods'][ 'allocated'] + node_stats['pods']['allocated'] state_info['cpu']['allocatable'] = state_info['cpu'][ 'allocatable'] + node_stats['cpu']['allocatable'] state_info['cpu']['allocated'] = state_info['cpu'][ 'allocated'] + node_stats['cpu']['allocated'] state_info['mem']['allocatable'] = state_info['mem'][ 'allocatable'] + node_stats['mem']['allocatable'] state_info['mem']['allocated'] = state_info['mem'][ 'allocated'] + node_stats['mem']['allocated'] state_info['storage']['allocatable'] = state_info['storage'][ 'allocatable'] + node_stats['storage']['allocatable'] state_info['storage']['allocated'] = state_info['storage'][ 'allocated'] + node_stats['storage']['allocated'] state_info['pods']['percentage'] = ( int(state_info['pods']['allocated'].magnitude) * 100) // int( state_info['pods']['allocatable'].magnitude) state_info['cpu']['percentage'] = ( int(state_info['cpu']['allocated'].magnitude) * 100) // int( state_info['cpu']['allocatable'].magnitude) state_info['mem']['percentage'] = ( int(state_info['mem']['allocated'].magnitude) * 100) // int( state_info['cpu']['allocatable'].magnitude) state_info['storage']['percentage'] = ( int(state_info['storage']['allocated'].magnitude) * 100) // int( state_info['storage']['allocatable'].magnitude) return state_info
def compute_node_resources(node_name): state_info = { 'pods': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'cpu': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'mem': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 }, 'storage': { 'allocatable': 0, 'allocated': 0, 'percentage': 0 } } load_kubernetes_config() core_v1 = kubernetes.client.CoreV1Api() field_selector = ("metadata.name=" + node_name) try: node = core_v1.list_node(field_selector=field_selector).items[0] except Exception as e: print("Problem listing nodes") # print("Something bad happened: " + e) stats = {} node_name = node.metadata.name allocatable = node.status.allocatable max_pods = int(int(allocatable["pods"]) * 1.5) field_selector = ("status.phase!=Succeeded,status.phase!=Failed," + "spec.nodeName=" + node_name) cpu_allocatable = Q_(allocatable["cpu"]) cpu_allocatable.ito(ureg.m) state_info["cpu"]["allocatable"] = cpu_allocatable mem_allocatable = Q_(allocatable["memory"]) mem_allocatable.ito(ureg.Mi) state_info["mem"]["allocatable"] = mem_allocatable # https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ storage_allocatable = Q_(allocatable["ephemeral-storage"]) storage_allocatable.ito(ureg.Mi) state_info["storage"]["allocatable"] = storage_allocatable state_info["pods"]["allocatable"] = max_pods * ureg.pods pods = core_v1.list_pod_for_all_namespaces( limit=max_pods, field_selector=field_selector).items state_info["pods"]["allocated"] = len(pods) * ureg.pods # compute the allocated resources cpureqs, memreqs, storagereqs = [], [], [] #cpulmts,memlmts,storagelmts = [], [], [] for pod in pods: for container in pod.spec.containers: res = container.resources reqs = defaultdict(lambda: 0, res.requests or {}) lmts = defaultdict(lambda: 0, res.limits or {}) cpureqs.append(Q_(reqs["cpu"])) memreqs.append(Q_(reqs["memory"])) storagereqs.append(Q_(reqs["ephemeral-storage"])) #cpulmts.append(Q_(lmts["cpu"])) #memlmts.append(Q_(lmts["memory"])) #storagelmts.append(Q_(lmts["ephemeral-storage"])) cpu_allocated = sum(cpureqs) cpu_allocated.ito(ureg.m) state_info["cpu"]["allocated"] = cpu_allocated mem_allocated = sum(memreqs) mem_allocated.ito(ureg.Mi) state_info["mem"]["allocated"] = mem_allocated storage_allocated = sum(storagereqs) storage_allocated.ito(ureg.Mi) state_info["storage"]["allocated"] = storage_allocated state_info['pods']['percentage'] = ( int(state_info['pods']['allocated'].magnitude) * 100) // int( state_info['pods']['allocatable'].magnitude) state_info['cpu']['percentage'] = ( int(state_info['cpu']['allocated'].magnitude) * 100) // int( state_info['cpu']['allocatable'].magnitude) state_info['mem']['percentage'] = ( int(state_info['mem']['allocated'].magnitude) * 100) // int( state_info['mem']['allocatable'].magnitude) state_info['storage']['percentage'] = ( int(state_info['storage']['allocated'].magnitude) * 100) // int( state_info['storage']['allocatable'].magnitude) return state_info