Created
March 6, 2024 12:00
-
-
Save shaposhnikoff/f3ecf57ec765884832a78ff17e3fa3c6 to your computer and use it in GitHub Desktop.
This file contains 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
from kubernetes import client, config | |
import requests,sys | |
# list all PVCs in a namespace | |
def list_all_pvc(env, namespace): | |
config.load_kube_config(config_file='~/.kube/config', context=env) | |
v1 = client.CoreV1Api() | |
pvc_list = v1.list_namespaced_persistent_volume_claim(namespace) | |
return pvc_list | |
# get PVC volume size from k8s | |
def get_pvc_vulume_size(env, namespace, pvc_name): | |
config.load_kube_config(config_file='~/.kube/config', context=env) | |
v1 = client.CoreV1Api() | |
try: | |
pvc = v1.read_namespaced_persistent_volume_claim(name=pvc_name, namespace=namespace) | |
volume_size = pvc.spec.resources.requests['storage'] | |
except KeyError as e: | |
print(f"PVC {pvc_name} not found in {namespace}") | |
sys.exit(1) | |
return volume_size | |
# remote call prometheus metric kubelet_volume_stats_used_bytes | |
def get_pvc_size(env, namespace, pvc_name): | |
url = f"http://prometheus.{env}.prometheus.url/api/v1/query" | |
query = f"kubelet_volume_stats_used_bytes{{persistentvolumeclaim='{pvc_name}',namespace='{namespace}'}}" | |
response = requests.get(url, params={'query': query}) | |
data = response.json() | |
return data['data']['result'][0]['value'][1] | |
#envs = ["prod"] | |
envs = ["dev", "test", "prod"] | |
for env in envs: | |
# create separate list for PVCs with no data in kubelet_metrics | |
problem_pvc = [] | |
for pvc in list_all_pvc(env, 'master').items: | |
try: | |
print(f"{pvc.metadata.name} : {pvc.spec.resources.requests['storage']}") | |
print(f"{pvc.metadata.name} : {(get_pvc_size(env, 'master', pvc.metadata.name))} ") | |
print(f"\n") | |
except IndexError as e: | |
print(f"Error: {e}, {pvc.metadata.name} not found in kubelet_metrics") | |
problem_pvc.append(pvc.metadata.name) | |
continue | |
print(f"Problem PVCs: {problem_pvc},env: {env}") | |
print(f"{len(problem_pvc)} PVCs have no data in kubelet_metrics") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment