├── .github └── FUNDING.yml ├── .gitignore ├── Dockerfile ├── INSTALL.md ├── K8sPurger.py ├── LICENSE ├── README.md ├── deploy ├── PodMonitor.yaml ├── k8sPurger Dashboard.json ├── manifest.yaml └── prometheusrule.yaml ├── documentation └── grafana_dashbaord.png └── requirements.txt /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | ko_fi: yogeshkunjir # Replace with a single Ko-fi username 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.8-alpine3.12 2 | 3 | WORKDIR /app 4 | 5 | RUN apk add --no-cache curl 6 | COPY requirements.txt requirements.txt 7 | RUN pip3 install -r requirements.txt 8 | 9 | 10 | COPY . . 11 | 12 | CMD [ "python3", "-u", "K8sPurger.py","--type=svc" ] 13 | -------------------------------------------------------------------------------- /INSTALL.md: -------------------------------------------------------------------------------- 1 | 19 | 20 | # Installation and Configuration 21 | 22 | There are two ways we can run this utility. In kubernetes as services and as ad-hoc. 23 | 24 | 25 | ## Installing in Kubernetes as services 26 | 27 | Deploying in Kubernetes itself which will run periodically (every 15 minutes by default) and capture unused resources and expose them as Prometheus metrics. You should configure prometheus or similar tool to scrape this metrics ![Refer this](https://stackoverflow.com/questions/41725767/how-to-scrape-pod-level-info-using-prometheus-kubernetes) 28 | 29 | Once you have all The metrics in prometheus you can configure prometheus rule to trigger alert. Sample alert are provided in deploy folder deploy/prometheusrule.yaml(./deploy/prometheusrule.yaml) 30 | 31 | And you can import k8sPurger Dashboard from deploy folder to create dashbaord like below. 32 | 33 | ![grafana](documentation/grafana_dashbaord.png) 34 | 35 | NOTE :- Service will scan for unused resources every 15 minutes which you can changing by setting REFRESH_INTERVAL(in second) in deployment.yaml 36 | 37 | ``` 38 | git clone https://github.com/yogeshkk/K8sPurger 39 | cd K8sPurger 40 | kubectl apply -f deploy/manifest.yaml 41 | ``` 42 | 43 | 44 | 45 | ## Running From shell 46 | 47 | Second way is to run as ad-hoc command as and when required. You can run this manually it required [Python client for Kuberntes](https://github.com/kubernetes-client/python). We need to install that first also make sure you have kubeconfig in ~/.kube/conf or in KUBECONFIG env variable before runing script. 48 | 49 | ``` 50 | pip3 install -r requirements.txt 51 | python K8sPurger.py 52 | ``` 53 | 54 | Output Will look like below 55 | ``` 56 | 57 | yogesh$ ~/p/K8sPurger> python K8sPurger.py 58 | 59 | This script is created to find unused resource in Kubernetes. 60 | 61 | Getting unused secret it may take couple of minute.. 62 | 63 | Extra Secrets are 6 which are as below 64 | 65 | -------------------------------- 66 | | Secrets | Namespace | 67 | -------------------------------- 68 | | app1-secret | my-apps | 69 | | app2-secret | my-apps | 70 | | app2-new-secret | my-apps | 71 | | postgresql | default | 72 | | dex-b94455424g | kube-addons | 73 | | dex-dbh8fmk699 | kube-addons | 74 | -------------------------------- 75 | 76 | Getting unused ConfigMap it may take couple of minute.. 77 | 78 | Extra ConfigMap are 6 which are as below 79 | 80 | ------------------------------------------- 81 | | ConfigMap | Namespace | 82 | ------------------------------------------- 83 | | app1-configmap | my-apps | 84 | | app2-configmap | my-apps | 85 | | app2-new-configmap | my-apps | 86 | | ss-cm | default | 87 | | cluster-autoscaler-status | kube-addons | 88 | | fluent-bit-config | logging | 89 | ------------------------------------------- 90 | 91 | Getting unused PVC it may take couple of minute.. 92 | 93 | Extra PV Claim are 5 which are as below 94 | --------------------------------- 95 | | PV Claim | Namespace | 96 | --------------------------------- 97 | | data-postgresql-0 | default | 98 | | data-0 | default | 99 | | redis-master-0 | default | 100 | | redis-slave-0 | default | 101 | | redis-slave-1 | default | 102 | -------------------------------- 103 | 104 | Getting unused services it may take couple of minute.. 105 | 106 | Extra Services are 3 which are as below 107 | 108 | ----------------------------- 109 | | Services | Namespace | 110 | ----------------------------- 111 | | app1-services | my-apps | 112 | | app2-services | my-apps | 113 | | app2-headless | my-apps | 114 | ----------------------------- 115 | 116 | Getting unused Ingress it may take couple of minute.. 117 | 118 | Extra Ingress are 4 which are as below 119 | 120 | ---------------------------------------- 121 | | Ingress | Namespace | 122 | ---------------------------------------- 123 | | app1-ingress | my-apps | 124 | | app2-ingress | my-apps | 125 | | app2-ingress-api-gateway | my-apps | 126 | | router |default | 127 | ---------------------------------------- 128 | 129 | Getting unused service account it may take couple of minute.. 130 | 131 | Extra Service Account are 6 which are as below 132 | ---------------------------------- 133 | | Service Account | Namespace | 134 | ---------------------------------- 135 | | app1-svc | my-apps | 136 | | cert-svc | cert-manager | 137 | | log-svc | logging | 138 | | monitor-svc | monitoring | 139 | | default | my-registry | 140 | | default | tools | 141 | ---------------------------------- 142 | 143 | Getting unused Roles Binding it may take couple of minute.. 144 | 145 | Extra Role Binding are 1 which are as below 146 | 147 | --------------------------- 148 | | Role Binding |Namespace | 149 | --------------------------- 150 | | app1-rb |my-apps | 151 | --------------------------- 152 | 153 | 154 | Extra Deployment are 1 which are as below 155 | 156 | -------------------------------- 157 | | Deployment |Namespace | 158 | -------------------------------- 159 | | busybox-deployment |default | 160 | -------------------------------- 161 | 162 | 163 | Extra Stateful Sets are 1 which are as below 164 | 165 | ---------------------------- 166 | | Stateful Sets |Namespace | 167 | ---------------------------- 168 | | nginx-sts-test |default | 169 | ---------------------------- 170 | 171 | ``` 172 | 173 | 174 | 175 | ### NOTE:- You can browse code and if like idea provides star for encouragement or provide feedback to me one below social networks. 176 | 177 | Twitter https://twitter.com/yogeshkunjir LinkedIn https://www.linkedin.com/in/yogeshkunjir/ 178 | 179 | Buy Me a Coffee/Book at ko-fi.com 180 | -------------------------------------------------------------------------------- /K8sPurger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import argparse 4 | import os 5 | import time 6 | 7 | from kubernetes import config, client 8 | from prometheus_client import start_http_server, Gauge 9 | 10 | UsedSecret, UsedConfigMap, UsedPVC, UsedEP, UsedSA, ExtraIng = [], [], [], [], [], [] 11 | Ing, RoleBinding = {}, {} 12 | g = Gauge('k8s_unused_resources', 'show unused resources in k8s', ['type', 'name', 'namespaces']) 13 | 14 | ExcludedNamespacesList = ["kube-system", "kube-public"] 15 | ExcludedSecretTypes = ["kubernetes.io/tls", "kubernetes.io/service-account-token", "kubernetes.io/dockercfg"] 16 | 17 | 18 | def main(svc): 19 | g.clear() 20 | try: 21 | if svc == "svc": 22 | config.load_incluster_config() 23 | else: 24 | config.load_kube_config() 25 | v1 = client.CoreV1Api() 26 | try: 27 | v1IngressApi = client.ExtensionsV1beta1Api() 28 | except: 29 | v1IngressApi = client.NetworkingV1Api() 30 | RbacAuthorizationV1Api = client.RbacAuthorizationV1Api() 31 | AppsV1Api = client.AppsV1Api() 32 | except Exception as e: 33 | print("Not able to read Kubernetes cluster check Kubeconfig") 34 | raise RuntimeError(e) 35 | print("Getting unused secret it may take couple of minute..") 36 | GetUsedResources(v1) 37 | Secrets = DefinedSecret(v1) 38 | ExtraSecret = Diffrance(Secrets, UsedSecret) 39 | PrintList(ExtraSecret, "Secrets") 40 | print("Getting unused ConfigMap it may take couple of minute..") 41 | ConfigMap = DefinedConfigMap(v1) 42 | ExtraConfigMap = Diffrance(ConfigMap, UsedConfigMap) 43 | PrintList(ExtraConfigMap, "ConfigMap") 44 | print("Getting unused PVC it may take couple of minute..") 45 | PVC = DefinedPersistentVolumeClaim(v1) 46 | ExtraPVC = Diffrance(PVC, UsedPVC) 47 | PrintList(ExtraPVC, "PV Claim") 48 | print("Getting unused services it may take couple of minute..") 49 | UsedEP = GetUsedServices(v1) 50 | EP = DefinedSvc(v1) 51 | ExtraSVC = Diffrance(EP, UsedEP) 52 | PrintList(ExtraSVC, "Services") 53 | print("Getting unused Ingress it may take couple of minute..") 54 | DefinedIngress(v1IngressApi) 55 | ExtraIng = GetUnusedIng(EP, ExtraSVC) 56 | PrintList(ExtraIng, "Ingress") 57 | print("Getting unused service account it may take couple of minute..") 58 | SA = DefinedServiceAccount(v1) 59 | ExtraSA = Diffrance(SA, UsedSA) 60 | PrintList(ExtraSA, "Service Account") 61 | print("Getting unused Roles Binding it may take couple of minute..") 62 | _ = DefinedRoleBinding(RbacAuthorizationV1Api) 63 | ExtraRB = GetUnusedRB(SA, ExtraSA) 64 | PrintList(ExtraRB, "Role Binding") 65 | ExtraDep = GetUnusedDeployment(AppsV1Api) 66 | PrintList(ExtraDep, "Deployment") 67 | ExtraSTS = GetUnusedSTS(AppsV1Api) 68 | PrintList(ExtraSTS, "Stateful Sets") 69 | 70 | if svc == "svc": 71 | refresh_interval = (os.environ['REFRESH_INTERVAL']) 72 | time.sleep(int(refresh_interval)) 73 | 74 | 75 | def ExludedNamespace(namespace): 76 | for ens in ExcludedNamespacesList: 77 | if ens in namespace: 78 | return True 79 | return False 80 | 81 | def Diffrance(listA, listB): 82 | return [i for i in listA if i not in listB] 83 | 84 | 85 | def PrintList(Toprint, name): 86 | if len(Toprint) == 0: 87 | print("Hurray You don't have a unused " + name) 88 | else: 89 | print("\nExtra " + name + " are " + str(len(Toprint)) + " which are as below\n") 90 | size1 = max(len(word[0]) for word in Toprint) 91 | size2 = max(len(word[1]) for word in Toprint) 92 | borderchar = '|' 93 | linechar = '-' 94 | print(linechar * (size1 + size2 + 7)) 95 | print('{bc} {:<{}} {bc}'.format(name, size1, bc=borderchar) + '{:<{}} {bc}'.format("Namespace", size2, 96 | bc=borderchar)) 97 | print(linechar * (size1 + size2 + 7)) 98 | for word in Toprint: 99 | print('{bc} {:<{}} {bc}'.format(word[0], size1, bc=borderchar) + '{:<{}} {bc}'.format(word[1], size2, 100 | bc=borderchar)) 101 | 102 | g.labels(name, word[0], word[1]).set(1) 103 | print(linechar * (size1 + size2 + 7)) 104 | print(" ") 105 | 106 | 107 | def GetUsedResources(v1): 108 | try: 109 | ApiResponce = v1.list_pod_for_all_namespaces(watch=False) 110 | except Exception as e: 111 | print("Not able to reach Kubernetes cluster check Kubeconfig") 112 | raise RuntimeError(e) 113 | for i in ApiResponce.items: 114 | if ExludedNamespace(i.metadata.namespace): 115 | pass 116 | else: 117 | container = i.spec.containers 118 | for item in container: 119 | if item.env is not None: 120 | for env in item.env: 121 | if env.value_from is not None: 122 | if env.value_from.secret_key_ref is not None: 123 | UsedSecret.append( 124 | [env.value_from.secret_key_ref.name, i.metadata.namespace]) 125 | elif env.value_from.config_map_key_ref is not None: 126 | UsedConfigMap.append( 127 | [env.value_from.config_map_key_ref.name, i.metadata.namespace]) 128 | if item.env_from is not None: 129 | for env_from in item.env_from: 130 | if env_from.config_map_ref is not None: 131 | UsedConfigMap.append([env_from.config_map_ref.name, i.metadata.namespace]) 132 | elif env_from.secret_ref is not None: 133 | UsedSecret.append([env_from.secret_ref.name, i.metadata.namespace]) 134 | if i.spec.volumes is not None: 135 | for volume in i.spec.volumes: 136 | if volume.secret is not None: 137 | UsedSecret.append([volume.secret.secret_name, i.metadata.namespace]) 138 | elif volume.config_map is not None: 139 | UsedConfigMap.append([volume.config_map.name, i.metadata.namespace]) 140 | elif volume.persistent_volume_claim is not None: 141 | UsedPVC.append([volume.persistent_volume_claim.claim_name, i.metadata.namespace]) 142 | if i.spec.service_account_name is not None: 143 | UsedSA.append([i.spec.service_account_name, i.metadata.namespace]) 144 | 145 | 146 | def DefinedSvc(v1): 147 | EP = [] 148 | try: 149 | ApiResponce = v1.list_service_for_all_namespaces(watch=False) 150 | except Exception as e: 151 | print("Not able to reach Kubernetes cluster check Kubeconfig") 152 | raise RuntimeError(e) 153 | for i in ApiResponce.items: 154 | if ExludedNamespace(i.metadata.namespace): 155 | pass 156 | elif i.spec.external_name is None: 157 | EP.append([i.metadata.name, i.metadata.namespace]) 158 | return EP 159 | 160 | 161 | def GetUsedServices(v1): 162 | UsedEP = [] 163 | try: 164 | ApiResponce = v1.list_endpoints_for_all_namespaces(watch=False) 165 | except Exception as e: 166 | print("Not able to reach Kubernetes cluster check Kubeconfig") 167 | raise RuntimeError(e) 168 | for i in ApiResponce.items: 169 | if ExludedNamespace(i.metadata.namespace): 170 | pass 171 | elif i.subsets is not None: 172 | UsedEP.append([i.metadata.name, i.metadata.namespace]) 173 | return UsedEP 174 | 175 | 176 | def DefinedSecret(v1): 177 | Secrets = [] 178 | try: 179 | ApiResponce = v1.list_secret_for_all_namespaces(watch=False) 180 | except Exception as e: 181 | print("Not able to reach Kubernetes cluster check Kubeconfig") 182 | raise RuntimeError(e) 183 | for i in ApiResponce.items: 184 | if ExludedNamespace(i.metadata.namespace): 185 | pass 186 | elif i.type in ExcludedSecretTypes: 187 | pass 188 | else: 189 | Secrets.append([i.metadata.name, i.metadata.namespace]) 190 | return Secrets 191 | 192 | 193 | def DefinedConfigMap(v1): 194 | ConfigMap = [] 195 | try: 196 | ApiResponce = v1.list_config_map_for_all_namespaces(watch=False) 197 | except Exception as e: 198 | print("Not able to reach Kubernetes cluster check Kubeconfig") 199 | raise RuntimeError(e) 200 | for i in ApiResponce.items: 201 | if ExludedNamespace(i.metadata.namespace): 202 | pass 203 | else: 204 | ConfigMap.append([i.metadata.name, i.metadata.namespace]) 205 | return ConfigMap 206 | 207 | 208 | def DefinedPersistentVolumeClaim(v1): 209 | PVC = [] 210 | try: 211 | ApiResponce = v1.list_persistent_volume_claim_for_all_namespaces(watch=False) 212 | except Exception as e: 213 | print("Not able to reach Kubernetes cluster check Kubeconfig") 214 | raise RuntimeError(e) 215 | for i in ApiResponce.items: 216 | PVC.append([i.metadata.name, i.metadata.namespace]) 217 | return PVC 218 | 219 | 220 | def DefinedServiceAccount(v1): 221 | SA = [] 222 | try: 223 | ApiResponce = v1.list_service_account_for_all_namespaces(watch=False) 224 | except Exception as e: 225 | print("Not able to reach Kubernetes cluster check Kubeconfig") 226 | raise RuntimeError(e) 227 | for i in ApiResponce.items: 228 | if ExludedNamespace(i.metadata.namespace): 229 | pass 230 | elif "default" in i.metadata.name: 231 | pass 232 | else: 233 | SA.append([i.metadata.name, i.metadata.namespace]) 234 | return SA 235 | 236 | 237 | def DefinedIngress(v1IngressApi): 238 | try: 239 | ApiResponce = v1IngressApi.list_ingress_for_all_namespaces(watch=False) 240 | except Exception as e: 241 | print("Not able to reach Kubernetes cluster check Kubeconfig") 242 | raise RuntimeError(e) 243 | for i in ApiResponce.items: 244 | if ExludedNamespace(i.metadata.namespace): 245 | pass 246 | else: 247 | if i.spec.rules is not None: 248 | for rule in i.spec.rules: 249 | if rule.http.paths is not None: 250 | for path in rule.http.paths: 251 | try: 252 | service_name = path.backend.service_name 253 | except: 254 | service_name = path.backend.service.name 255 | Ing[i.metadata.name] = ([service_name, i.metadata.namespace]) 256 | return Ing 257 | 258 | 259 | def GetUnusedIng(EP, ExtraSVC): 260 | global Ing 261 | ExtraIng = [] 262 | for i, j in Ing.items(): 263 | if j not in EP or j in ExtraSVC: 264 | ExtraIng.append([i, j[1]]) 265 | Ing.clear() 266 | return ExtraIng 267 | 268 | 269 | def DefinedRoleBinding(RbacAuthorizationV1Api): 270 | try: 271 | ApiResponce = RbacAuthorizationV1Api.list_role_binding_for_all_namespaces(watch=False) 272 | except Exception as e: 273 | print("Not able to reach Kubernetes cluster check Kubeconfig") 274 | raise RuntimeError(e) 275 | for i in ApiResponce.items: 276 | if ExludedNamespace(i.metadata.namespace): 277 | pass 278 | else: 279 | for sub in i.subjects: 280 | if "ServiceAccount" in sub.kind: 281 | RoleBinding[i.metadata.name] = ([sub.name, i.metadata.namespace]) 282 | return RoleBinding 283 | 284 | 285 | def GetUnusedRB(SA, ExtraSA): 286 | ExtraRoleBinding = [] 287 | for i, j in RoleBinding.items(): 288 | if j not in SA or j in ExtraSA: 289 | ExtraRoleBinding.append([i, j[1]]) 290 | RoleBinding.clear() 291 | return ExtraRoleBinding 292 | 293 | 294 | def GetUnusedDeployment(AppsV1Api): 295 | ExtraDep = [] 296 | try: 297 | ApiResponce = AppsV1Api.list_deployment_for_all_namespaces(watch=False) 298 | except Exception as e: 299 | print("Not able to reach Kubernetes cluster check Kubeconfig") 300 | raise RuntimeError(e) 301 | for i in ApiResponce.items: 302 | if ExludedNamespace(i.metadata.namespace): 303 | pass 304 | else: 305 | if i.spec.replicas == 0: 306 | ExtraDep.append([i.metadata.name, i.metadata.namespace]) 307 | """ 308 | i.status = V1DeploymentStatus, DeploymentStatus is the most recently observed status of the Deployment. 309 | i.status.available_replicas = Total number of available pods (ready for at least minReadySeconds) targeted by this deployment. 310 | i.status.ready_replicas = readyReplicas is the number of pods targeted by this Deployment with a Ready Condition. 311 | i.status.unavailable_replicas = Total number of unavailable pods targeted by this deployment. 312 | """ 313 | if (not i.status.available_replicas) or (not i.status.ready_replicas) or i.status.unavailable_replicas: 314 | if i.metadata.name == "k8spurger": 315 | continue 316 | ExtraDep.append([i.metadata.name, i.metadata.namespace]) 317 | return ExtraDep 318 | 319 | 320 | def GetUnusedSTS(AppsV1Api): 321 | ExtraSTS = [] 322 | try: 323 | ApiResponce = AppsV1Api.list_stateful_set_for_all_namespaces(watch=False) 324 | except Exception as e: 325 | print("Not able to reach Kubernetes cluster check Kubeconfig") 326 | raise RuntimeError(e) 327 | for i in ApiResponce.items: 328 | if ExludedNamespace(i.metadata.namespace): 329 | pass 330 | else: 331 | if i.spec.replicas == 0: 332 | ExtraSTS.append([i.metadata.name, i.metadata.namespace]) 333 | """ 334 | i.status = V1StatefulSetStatus, StatefulSetStatus represents the current state of a StatefulSet. 335 | i.status.available_replicas = Total number of available pods (ready for at least minReadySeconds) targeted by this statefulset. 336 | i.status.ready_replicas = readyReplicas is the number of pods created for this StatefulSet with a Ready Condition. 337 | """ 338 | if (not i.status.available_replicas) or (not i.status.ready_replicas): 339 | ExtraSTS.append([i.metadata.name, i.metadata.namespace, i.metadata.creation_timestamp]) 340 | return ExtraSTS 341 | 342 | 343 | if __name__ == '__main__': 344 | print("\nThis script is created to find unused resource in Kubernetes\n") 345 | parser = argparse.ArgumentParser(description='Parser to get delete value') 346 | parser.add_argument('-t', '--type', help='If need to run as services pass type as svc', required=False) 347 | args = parser.parse_args() 348 | if args.type == "svc": 349 | start_http_server(8000) 350 | while True: 351 | main("svc") 352 | else: 353 | main("standalone") 354 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 19 | # K8SPurger 20 | 21 | 22 | 23 | ## Hunt Unused Resources In Kubernetes. 24 | 25 | ### K8sPurger Demo Video 26 | 27 | We have few video to show K8sPurger in action. 28 | 29 | Quick one show how to install in cluster and get alert in slack. 30 | K8sPurger (Duration 4:23) :- https://www.youtube.com/watch?v=QfDvHcfCihY 31 | 32 | Deep Dive where we have covered all concept and both way to run K8sPurger and many more 33 | K8sPurger Deep Dive (Duration 38:49) :- https://www.youtube.com/watch?v=AAmCz3lMQC8 34 | 35 | We have not shown monitoring setup (prometheus operator) as there are already lot of documentation available for same 36 | 37 | ### NAQ (Nobody asked Question). 38 | 39 | 40 | 1) What this script do? 41 | > This will find all unused resources and show them in a nice format. 42 | 43 | 2) Why you need this? 44 | >When we add a new application or Microservices it is simple as installing a chart or kubectl -f on a big manifest but when we want to remove we don't know what are resources it created. Many times we can't remove them fully because we have 10's or 100's such resources and don’t have enough time to hunt and kill or many times we just inherited a cluster. Having an unused item in the cluster is not good practice as the Etcd DB size grows the performance starts degrading. Also many times it possessed a security risk(unknown SA and rolebinding). 45 | 46 | >Lastly most dear to us saving cost in case of PVC we are paying for them to cloud provider. 47 | 48 | 49 | 3) Is this cause any effect on my cluster? 50 | >This will just list the unused resources according to predefined criteria which are mentioned after NAQ. This will just give the list of resources that are **Potentially** unused so you can focus on them an only instant of looking for a needle in the haystack. 51 | 52 | >*Note:- You should not trust strangers' words on the internet so browse the script as it is under apache 2 License and try on dummy cluster.* 53 | 54 | 4) How this work? Can I just use the kubectl command to do the same? 55 | > The kubectl does not directly give these details you have to invest a lot of time. If you know a short way, Please let me know via raising the issue (sharing is caring). This script will get all pods in all namespaces and scan them for these resources and make a list and then get the resource in Kubernetes and just give you the difference. 56 | 57 | 5) So if I understood correctly it will scan the pod only. what if I have deployment/StatefullSet which has zero replica set? 58 | > Yes, in that case, the resource will be shown as unused. If you have zero replicas means you are not using that resource. 59 | 60 | 6) Why PVC why not PV? 61 | > Normally we use PVC to manage PV and when we delete claims, PV will be deleted or retained as per storage-class configuration. To avoid any potential data loss I choose to work with PVC only. 62 | 63 | 8) What if I hit a bug or required any feature? 64 | > You can raise an issue. I will try to fix the bug. The feature has to look into how much time is required. 65 | 66 | 67 | 68 | ### Installation and Configuration 69 | 70 | There are two ways we can run this utility. Once is ad-hoc another is deploying in Kubernetes itself which will run periodically and capture unused resources and expose them as Prometheus metrics. Once capture in Prometheus one can do all sorts of alerting and visualization. Both ways are covered in the ![Installation](./INSTALL.md) part. 71 | 72 | ## Selection Criteria 73 | - Secret -> If the secret is not mounted on any running pod via env variable or as volume 74 | - ConfigMap -> If ConfigMap is not mounted on any running pod via env variable or as volume 75 | - PVC -> Is PVC is not mounted on any running pod 76 | - Services -> If services do not any endpoint 77 | - ServiceAccount -> If no running pod use that service account 78 | - Ingress -> If ingress pointing to any services which either do not exist or do not have any endpoint 79 | - RoleBinding -> If RoleBinding to any Services account which does not exist or that Services account is not used by any running pod. 80 | - Deployment -> If deployment have zero replica. 81 | - StateFullset -> If StateFullset have zero replica. 82 | 83 | 84 | Exclusion:- All objects in kube-system and kube-system are excluded also all secrets which are token or type TLS are excluded to avoid the high list of false positive. 85 | 86 | 87 | 88 | ### NOTE:- You can browse code and if like idea provides star for encouragement or provide feedback to me one below social networks. 89 | 90 | Twitter https://twitter.com/yogeshkunjir LinkedIn https://www.linkedin.com/in/yogeshkunjir/ 91 | 92 | Buy Me a Coffee/Book at ko-fi.com 93 | -------------------------------------------------------------------------------- /deploy/PodMonitor.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: monitoring.coreos.com/v1 3 | kind: PodMonitor 4 | metadata: 5 | name: k8spurger 6 | labels: 7 | app: k8spurger 8 | spec: 9 | selector: 10 | matchLabels: 11 | app: k8spurger 12 | podMetricsEndpoints: 13 | - port: http 14 | path: "/metrics" -------------------------------------------------------------------------------- /deploy/k8sPurger Dashboard.json: -------------------------------------------------------------------------------- 1 | { 2 | "annotations": { 3 | "list": [ 4 | { 5 | "builtIn": 1, 6 | "datasource": "-- Grafana --", 7 | "enable": true, 8 | "hide": true, 9 | "iconColor": "rgba(0, 211, 255, 1)", 10 | "name": "Annotations & Alerts", 11 | "type": "dashboard" 12 | } 13 | ] 14 | }, 15 | "editable": true, 16 | "gnetId": null, 17 | "graphTooltip": 0, 18 | "id": 1, 19 | "iteration": 1624262851800, 20 | "links": [], 21 | "panels": [ 22 | { 23 | "datasource": null, 24 | "fieldConfig": { 25 | "defaults": { 26 | "mappings": [], 27 | "thresholds": { 28 | "mode": "absolute", 29 | "steps": [ 30 | { 31 | "color": "green", 32 | "value": null 33 | }, 34 | { 35 | "color": "red", 36 | "value": 80 37 | } 38 | ] 39 | } 40 | }, 41 | "overrides": [] 42 | }, 43 | "gridPos": { 44 | "h": 3, 45 | "w": 3, 46 | "x": 0, 47 | "y": 0 48 | }, 49 | "id": 46, 50 | "options": { 51 | "colorMode": "value", 52 | "graphMode": "area", 53 | "justifyMode": "auto", 54 | "orientation": "auto", 55 | "reduceOptions": { 56 | "calcs": [ 57 | "mean" 58 | ], 59 | "fields": "", 60 | "values": false 61 | }, 62 | "text": {}, 63 | "textMode": "auto" 64 | }, 65 | "pluginVersion": "8.0.3", 66 | "targets": [ 67 | { 68 | "expr": "sum(k8s_unused_resources{type=\"Deployment\",namespaces=~\"$namespaces\"})", 69 | "instant": true, 70 | "interval": "", 71 | "legendFormat": "", 72 | "refId": "A" 73 | } 74 | ], 75 | "timeFrom": null, 76 | "timeShift": null, 77 | "title": "Deployment", 78 | "type": "stat" 79 | }, 80 | { 81 | "datasource": null, 82 | "fieldConfig": { 83 | "defaults": { 84 | "mappings": [], 85 | "thresholds": { 86 | "mode": "absolute", 87 | "steps": [ 88 | { 89 | "color": "green", 90 | "value": null 91 | }, 92 | { 93 | "color": "red", 94 | "value": 80 95 | } 96 | ] 97 | } 98 | }, 99 | "overrides": [] 100 | }, 101 | "gridPos": { 102 | "h": 3, 103 | "w": 3, 104 | "x": 3, 105 | "y": 0 106 | }, 107 | "id": 52, 108 | "options": { 109 | "colorMode": "value", 110 | "graphMode": "area", 111 | "justifyMode": "auto", 112 | "orientation": "auto", 113 | "reduceOptions": { 114 | "calcs": [ 115 | "mean" 116 | ], 117 | "fields": "", 118 | "values": false 119 | }, 120 | "text": {}, 121 | "textMode": "auto" 122 | }, 123 | "pluginVersion": "8.0.3", 124 | "targets": [ 125 | { 126 | "expr": "sum(k8s_unused_resources{type=\"Stateful Sets\",namespaces=~\"$namespaces\"})", 127 | "instant": true, 128 | "interval": "", 129 | "legendFormat": "", 130 | "refId": "A" 131 | } 132 | ], 133 | "timeFrom": null, 134 | "timeShift": null, 135 | "title": "StatefullSet", 136 | "type": "stat" 137 | }, 138 | { 139 | "datasource": null, 140 | "fieldConfig": { 141 | "defaults": { 142 | "mappings": [], 143 | "thresholds": { 144 | "mode": "absolute", 145 | "steps": [ 146 | { 147 | "color": "green", 148 | "value": null 149 | }, 150 | { 151 | "color": "red", 152 | "value": 80 153 | } 154 | ] 155 | } 156 | }, 157 | "overrides": [] 158 | }, 159 | "gridPos": { 160 | "h": 3, 161 | "w": 3, 162 | "x": 6, 163 | "y": 0 164 | }, 165 | "id": 16, 166 | "options": { 167 | "colorMode": "value", 168 | "graphMode": "area", 169 | "justifyMode": "auto", 170 | "orientation": "auto", 171 | "reduceOptions": { 172 | "calcs": [ 173 | "mean" 174 | ], 175 | "fields": "", 176 | "values": false 177 | }, 178 | "text": {}, 179 | "textMode": "auto" 180 | }, 181 | "pluginVersion": "8.0.3", 182 | "targets": [ 183 | { 184 | "expr": "sum(k8s_unused_resources{type=\"PV Claim\",namespaces=~\"$namespaces\"})", 185 | "instant": true, 186 | "interval": "", 187 | "legendFormat": "", 188 | "refId": "A" 189 | } 190 | ], 191 | "timeFrom": null, 192 | "timeShift": null, 193 | "title": "PV Claim", 194 | "type": "stat" 195 | }, 196 | { 197 | "datasource": null, 198 | "fieldConfig": { 199 | "defaults": { 200 | "mappings": [], 201 | "thresholds": { 202 | "mode": "absolute", 203 | "steps": [ 204 | { 205 | "color": "green", 206 | "value": null 207 | }, 208 | { 209 | "color": "red", 210 | "value": 80 211 | } 212 | ] 213 | } 214 | }, 215 | "overrides": [] 216 | }, 217 | "gridPos": { 218 | "h": 3, 219 | "w": 3, 220 | "x": 9, 221 | "y": 0 222 | }, 223 | "id": 22, 224 | "options": { 225 | "colorMode": "value", 226 | "graphMode": "area", 227 | "justifyMode": "auto", 228 | "orientation": "auto", 229 | "reduceOptions": { 230 | "calcs": [ 231 | "mean" 232 | ], 233 | "fields": "", 234 | "values": false 235 | }, 236 | "text": {}, 237 | "textMode": "auto" 238 | }, 239 | "pluginVersion": "8.0.3", 240 | "targets": [ 241 | { 242 | "expr": "sum(k8s_unused_resources{type=\"Services\",namespaces=~\"$namespaces\"})", 243 | "instant": true, 244 | "interval": "", 245 | "legendFormat": "", 246 | "refId": "A" 247 | } 248 | ], 249 | "timeFrom": null, 250 | "timeShift": null, 251 | "title": "Services", 252 | "type": "stat" 253 | }, 254 | { 255 | "datasource": null, 256 | "fieldConfig": { 257 | "defaults": { 258 | "mappings": [], 259 | "thresholds": { 260 | "mode": "absolute", 261 | "steps": [ 262 | { 263 | "color": "green", 264 | "value": null 265 | }, 266 | { 267 | "color": "red", 268 | "value": 80 269 | } 270 | ] 271 | } 272 | }, 273 | "overrides": [] 274 | }, 275 | "gridPos": { 276 | "h": 3, 277 | "w": 3, 278 | "x": 12, 279 | "y": 0 280 | }, 281 | "id": 28, 282 | "options": { 283 | "colorMode": "value", 284 | "graphMode": "area", 285 | "justifyMode": "auto", 286 | "orientation": "auto", 287 | "reduceOptions": { 288 | "calcs": [ 289 | "mean" 290 | ], 291 | "fields": "", 292 | "values": false 293 | }, 294 | "text": {}, 295 | "textMode": "auto" 296 | }, 297 | "pluginVersion": "8.0.3", 298 | "targets": [ 299 | { 300 | "expr": "sum(k8s_unused_resources{type=\"Ingress\",namespaces=~\"$namespaces\"})", 301 | "instant": true, 302 | "interval": "", 303 | "legendFormat": "", 304 | "refId": "A" 305 | } 306 | ], 307 | "timeFrom": null, 308 | "timeShift": null, 309 | "title": "Ingress", 310 | "type": "stat" 311 | }, 312 | { 313 | "datasource": null, 314 | "fieldConfig": { 315 | "defaults": { 316 | "mappings": [], 317 | "thresholds": { 318 | "mode": "absolute", 319 | "steps": [ 320 | { 321 | "color": "green", 322 | "value": null 323 | }, 324 | { 325 | "color": "red", 326 | "value": 80 327 | } 328 | ] 329 | } 330 | }, 331 | "overrides": [] 332 | }, 333 | "gridPos": { 334 | "h": 3, 335 | "w": 3, 336 | "x": 15, 337 | "y": 0 338 | }, 339 | "id": 40, 340 | "options": { 341 | "colorMode": "value", 342 | "graphMode": "area", 343 | "justifyMode": "auto", 344 | "orientation": "auto", 345 | "reduceOptions": { 346 | "calcs": [ 347 | "mean" 348 | ], 349 | "fields": "", 350 | "values": false 351 | }, 352 | "text": {}, 353 | "textMode": "auto" 354 | }, 355 | "pluginVersion": "8.0.3", 356 | "targets": [ 357 | { 358 | "expr": "sum(k8s_unused_resources{type=\"Role Binding\",namespaces=~\"$namespaces\"})", 359 | "instant": true, 360 | "interval": "", 361 | "legendFormat": "", 362 | "refId": "A" 363 | } 364 | ], 365 | "timeFrom": null, 366 | "timeShift": null, 367 | "title": "Role Binding", 368 | "type": "stat" 369 | }, 370 | { 371 | "datasource": null, 372 | "fieldConfig": { 373 | "defaults": { 374 | "mappings": [], 375 | "thresholds": { 376 | "mode": "absolute", 377 | "steps": [ 378 | { 379 | "color": "green", 380 | "value": null 381 | }, 382 | { 383 | "color": "red", 384 | "value": 80 385 | } 386 | ] 387 | } 388 | }, 389 | "overrides": [] 390 | }, 391 | "gridPos": { 392 | "h": 3, 393 | "w": 3, 394 | "x": 18, 395 | "y": 0 396 | }, 397 | "id": 10, 398 | "options": { 399 | "colorMode": "value", 400 | "graphMode": "area", 401 | "justifyMode": "auto", 402 | "orientation": "auto", 403 | "reduceOptions": { 404 | "calcs": [ 405 | "mean" 406 | ], 407 | "fields": "", 408 | "values": false 409 | }, 410 | "text": {}, 411 | "textMode": "auto" 412 | }, 413 | "pluginVersion": "8.0.3", 414 | "targets": [ 415 | { 416 | "expr": "sum(k8s_unused_resources{type=\"ConfigMap\",namespaces=~\"$namespaces\"})", 417 | "instant": true, 418 | "interval": "", 419 | "legendFormat": "", 420 | "refId": "A" 421 | } 422 | ], 423 | "timeFrom": null, 424 | "timeShift": null, 425 | "title": "Config Map", 426 | "type": "stat" 427 | }, 428 | { 429 | "datasource": null, 430 | "fieldConfig": { 431 | "defaults": { 432 | "mappings": [], 433 | "thresholds": { 434 | "mode": "absolute", 435 | "steps": [ 436 | { 437 | "color": "green", 438 | "value": null 439 | }, 440 | { 441 | "color": "red", 442 | "value": 80 443 | } 444 | ] 445 | } 446 | }, 447 | "overrides": [] 448 | }, 449 | "gridPos": { 450 | "h": 3, 451 | "w": 3, 452 | "x": 21, 453 | "y": 0 454 | }, 455 | "id": 34, 456 | "options": { 457 | "colorMode": "value", 458 | "graphMode": "area", 459 | "justifyMode": "auto", 460 | "orientation": "auto", 461 | "reduceOptions": { 462 | "calcs": [ 463 | "mean" 464 | ], 465 | "fields": "", 466 | "values": false 467 | }, 468 | "text": {}, 469 | "textMode": "auto" 470 | }, 471 | "pluginVersion": "8.0.3", 472 | "targets": [ 473 | { 474 | "expr": "sum(k8s_unused_resources{type=\"Service Account\",namespaces=~\"$namespaces\"})", 475 | "instant": true, 476 | "interval": "", 477 | "legendFormat": "", 478 | "refId": "A" 479 | } 480 | ], 481 | "timeFrom": null, 482 | "timeShift": null, 483 | "title": "Service Account", 484 | "type": "stat" 485 | }, 486 | { 487 | "datasource": null, 488 | "fieldConfig": { 489 | "defaults": { 490 | "mappings": [], 491 | "thresholds": { 492 | "mode": "absolute", 493 | "steps": [ 494 | { 495 | "color": "green", 496 | "value": null 497 | }, 498 | { 499 | "color": "red", 500 | "value": 80 501 | } 502 | ] 503 | } 504 | }, 505 | "overrides": [] 506 | }, 507 | "gridPos": { 508 | "h": 3, 509 | "w": 3, 510 | "x": 0, 511 | "y": 3 512 | }, 513 | "id": 58, 514 | "options": { 515 | "colorMode": "value", 516 | "graphMode": "area", 517 | "justifyMode": "auto", 518 | "orientation": "auto", 519 | "reduceOptions": { 520 | "calcs": [ 521 | "mean" 522 | ], 523 | "fields": "", 524 | "values": false 525 | }, 526 | "text": {}, 527 | "textMode": "auto" 528 | }, 529 | "pluginVersion": "8.0.3", 530 | "targets": [ 531 | { 532 | "expr": "sum(k8s_unused_resources{type=\"Secrets\",namespaces=~\"$namespaces\"})", 533 | "instant": true, 534 | "interval": "", 535 | "legendFormat": "", 536 | "refId": "A" 537 | } 538 | ], 539 | "timeFrom": null, 540 | "timeShift": null, 541 | "title": "Secret", 542 | "type": "stat" 543 | }, 544 | { 545 | "collapsed": false, 546 | "datasource": null, 547 | "gridPos": { 548 | "h": 1, 549 | "w": 24, 550 | "x": 0, 551 | "y": 6 552 | }, 553 | "id": 50, 554 | "panels": [], 555 | "title": "Deployment", 556 | "type": "row" 557 | }, 558 | { 559 | "aliasColors": {}, 560 | "bars": false, 561 | "dashLength": 10, 562 | "dashes": false, 563 | "datasource": null, 564 | "fill": 1, 565 | "fillGradient": 0, 566 | "gridPos": { 567 | "h": 7, 568 | "w": 24, 569 | "x": 0, 570 | "y": 7 571 | }, 572 | "hiddenSeries": false, 573 | "id": 48, 574 | "legend": { 575 | "avg": false, 576 | "current": false, 577 | "max": false, 578 | "min": false, 579 | "rightSide": true, 580 | "show": true, 581 | "total": false, 582 | "values": false 583 | }, 584 | "lines": true, 585 | "linewidth": 1, 586 | "nullPointMode": "null", 587 | "options": { 588 | "alertThreshold": true 589 | }, 590 | "percentage": false, 591 | "pluginVersion": "8.0.3", 592 | "pointradius": 2, 593 | "points": false, 594 | "renderer": "flot", 595 | "seriesOverrides": [], 596 | "spaceLength": 10, 597 | "stack": false, 598 | "steppedLine": false, 599 | "targets": [ 600 | { 601 | "expr": "k8s_unused_resources{type=\"Deployment\", namespaces=~\"$namespaces\"}", 602 | "interval": "", 603 | "legendFormat": "{{name}}", 604 | "refId": "A" 605 | } 606 | ], 607 | "thresholds": [], 608 | "timeFrom": null, 609 | "timeRegions": [], 610 | "timeShift": null, 611 | "title": "Deployment details", 612 | "tooltip": { 613 | "shared": true, 614 | "sort": 0, 615 | "value_type": "individual" 616 | }, 617 | "type": "graph", 618 | "xaxis": { 619 | "buckets": null, 620 | "mode": "time", 621 | "name": null, 622 | "show": true, 623 | "values": [] 624 | }, 625 | "yaxes": [ 626 | { 627 | "format": "short", 628 | "label": null, 629 | "logBase": 1, 630 | "max": null, 631 | "min": null, 632 | "show": true 633 | }, 634 | { 635 | "format": "short", 636 | "label": null, 637 | "logBase": 1, 638 | "max": null, 639 | "min": null, 640 | "show": true 641 | } 642 | ], 643 | "yaxis": { 644 | "align": false, 645 | "alignLevel": null 646 | } 647 | }, 648 | { 649 | "collapsed": false, 650 | "datasource": null, 651 | "gridPos": { 652 | "h": 1, 653 | "w": 24, 654 | "x": 0, 655 | "y": 14 656 | }, 657 | "id": 56, 658 | "panels": [], 659 | "title": "Statefull set", 660 | "type": "row" 661 | }, 662 | { 663 | "aliasColors": {}, 664 | "bars": false, 665 | "dashLength": 10, 666 | "dashes": false, 667 | "datasource": null, 668 | "fill": 1, 669 | "fillGradient": 0, 670 | "gridPos": { 671 | "h": 7, 672 | "w": 24, 673 | "x": 0, 674 | "y": 15 675 | }, 676 | "hiddenSeries": false, 677 | "id": 54, 678 | "legend": { 679 | "avg": false, 680 | "current": false, 681 | "max": false, 682 | "min": false, 683 | "rightSide": true, 684 | "show": true, 685 | "total": false, 686 | "values": false 687 | }, 688 | "lines": true, 689 | "linewidth": 1, 690 | "nullPointMode": "null", 691 | "options": { 692 | "alertThreshold": true 693 | }, 694 | "percentage": false, 695 | "pluginVersion": "8.0.3", 696 | "pointradius": 2, 697 | "points": false, 698 | "renderer": "flot", 699 | "seriesOverrides": [], 700 | "spaceLength": 10, 701 | "stack": false, 702 | "steppedLine": false, 703 | "targets": [ 704 | { 705 | "expr": "k8s_unused_resources{type=\"Stateful Sets\", namespaces=~\"$namespaces\"}", 706 | "interval": "", 707 | "legendFormat": "{{name}}", 708 | "refId": "A" 709 | } 710 | ], 711 | "thresholds": [], 712 | "timeFrom": null, 713 | "timeRegions": [], 714 | "timeShift": null, 715 | "title": "Statefull Set details", 716 | "tooltip": { 717 | "shared": true, 718 | "sort": 0, 719 | "value_type": "individual" 720 | }, 721 | "type": "graph", 722 | "xaxis": { 723 | "buckets": null, 724 | "mode": "time", 725 | "name": null, 726 | "show": true, 727 | "values": [] 728 | }, 729 | "yaxes": [ 730 | { 731 | "format": "short", 732 | "label": null, 733 | "logBase": 1, 734 | "max": null, 735 | "min": null, 736 | "show": true 737 | }, 738 | { 739 | "format": "short", 740 | "label": null, 741 | "logBase": 1, 742 | "max": null, 743 | "min": null, 744 | "show": true 745 | } 746 | ], 747 | "yaxis": { 748 | "align": false, 749 | "alignLevel": null 750 | } 751 | }, 752 | { 753 | "collapsed": false, 754 | "datasource": null, 755 | "gridPos": { 756 | "h": 1, 757 | "w": 24, 758 | "x": 0, 759 | "y": 22 760 | }, 761 | "id": 20, 762 | "panels": [], 763 | "title": "Physical Volume Claim", 764 | "type": "row" 765 | }, 766 | { 767 | "aliasColors": {}, 768 | "bars": false, 769 | "dashLength": 10, 770 | "dashes": false, 771 | "datasource": null, 772 | "fill": 1, 773 | "fillGradient": 0, 774 | "gridPos": { 775 | "h": 8, 776 | "w": 24, 777 | "x": 0, 778 | "y": 23 779 | }, 780 | "hiddenSeries": false, 781 | "id": 18, 782 | "legend": { 783 | "avg": false, 784 | "current": false, 785 | "max": false, 786 | "min": false, 787 | "rightSide": true, 788 | "show": true, 789 | "total": false, 790 | "values": false 791 | }, 792 | "lines": true, 793 | "linewidth": 1, 794 | "nullPointMode": "null", 795 | "options": { 796 | "alertThreshold": true 797 | }, 798 | "percentage": false, 799 | "pluginVersion": "8.0.3", 800 | "pointradius": 2, 801 | "points": false, 802 | "renderer": "flot", 803 | "seriesOverrides": [], 804 | "spaceLength": 10, 805 | "stack": false, 806 | "steppedLine": false, 807 | "targets": [ 808 | { 809 | "expr": "k8s_unused_resources{type=\"PV Claim\", namespaces=~\"$namespaces\"}", 810 | "interval": "", 811 | "legendFormat": "{{name}}", 812 | "refId": "A" 813 | } 814 | ], 815 | "thresholds": [], 816 | "timeFrom": null, 817 | "timeRegions": [], 818 | "timeShift": null, 819 | "title": "PV claim Details", 820 | "tooltip": { 821 | "shared": true, 822 | "sort": 0, 823 | "value_type": "individual" 824 | }, 825 | "type": "graph", 826 | "xaxis": { 827 | "buckets": null, 828 | "mode": "time", 829 | "name": null, 830 | "show": true, 831 | "values": [] 832 | }, 833 | "yaxes": [ 834 | { 835 | "format": "short", 836 | "label": null, 837 | "logBase": 1, 838 | "max": null, 839 | "min": null, 840 | "show": true 841 | }, 842 | { 843 | "format": "short", 844 | "label": null, 845 | "logBase": 1, 846 | "max": null, 847 | "min": null, 848 | "show": true 849 | } 850 | ], 851 | "yaxis": { 852 | "align": false, 853 | "alignLevel": null 854 | } 855 | }, 856 | { 857 | "collapsed": true, 858 | "datasource": null, 859 | "gridPos": { 860 | "h": 1, 861 | "w": 24, 862 | "x": 0, 863 | "y": 31 864 | }, 865 | "id": 26, 866 | "panels": [ 867 | { 868 | "aliasColors": {}, 869 | "bars": false, 870 | "dashLength": 10, 871 | "dashes": false, 872 | "datasource": null, 873 | "fieldConfig": { 874 | "defaults": { 875 | "custom": {} 876 | }, 877 | "overrides": [] 878 | }, 879 | "fill": 1, 880 | "fillGradient": 0, 881 | "gridPos": { 882 | "h": 8, 883 | "w": 24, 884 | "x": 0, 885 | "y": 12 886 | }, 887 | "hiddenSeries": false, 888 | "id": 24, 889 | "legend": { 890 | "avg": false, 891 | "current": false, 892 | "max": false, 893 | "min": false, 894 | "rightSide": true, 895 | "show": true, 896 | "total": false, 897 | "values": false 898 | }, 899 | "lines": true, 900 | "linewidth": 1, 901 | "nullPointMode": "null", 902 | "percentage": false, 903 | "pluginVersion": "7.1.1", 904 | "pointradius": 2, 905 | "points": false, 906 | "renderer": "flot", 907 | "seriesOverrides": [], 908 | "spaceLength": 10, 909 | "stack": false, 910 | "steppedLine": false, 911 | "targets": [ 912 | { 913 | "expr": "k8s_unused_resources{type=\"Services\", namespaces=~\"$namespaces\"}", 914 | "interval": "", 915 | "legendFormat": "{{name}}", 916 | "refId": "A" 917 | } 918 | ], 919 | "thresholds": [], 920 | "timeFrom": null, 921 | "timeRegions": [], 922 | "timeShift": null, 923 | "title": "Services Details", 924 | "tooltip": { 925 | "shared": true, 926 | "sort": 0, 927 | "value_type": "individual" 928 | }, 929 | "type": "graph", 930 | "xaxis": { 931 | "buckets": null, 932 | "mode": "time", 933 | "name": null, 934 | "show": true, 935 | "values": [] 936 | }, 937 | "yaxes": [ 938 | { 939 | "format": "short", 940 | "label": null, 941 | "logBase": 1, 942 | "max": null, 943 | "min": null, 944 | "show": true 945 | }, 946 | { 947 | "format": "short", 948 | "label": null, 949 | "logBase": 1, 950 | "max": null, 951 | "min": null, 952 | "show": true 953 | } 954 | ], 955 | "yaxis": { 956 | "align": false, 957 | "alignLevel": null 958 | } 959 | } 960 | ], 961 | "title": "Services", 962 | "type": "row" 963 | }, 964 | { 965 | "collapsed": true, 966 | "datasource": null, 967 | "gridPos": { 968 | "h": 1, 969 | "w": 24, 970 | "x": 0, 971 | "y": 32 972 | }, 973 | "id": 32, 974 | "panels": [ 975 | { 976 | "aliasColors": {}, 977 | "bars": false, 978 | "dashLength": 10, 979 | "dashes": false, 980 | "datasource": null, 981 | "fill": 1, 982 | "fillGradient": 0, 983 | "gridPos": { 984 | "h": 8, 985 | "w": 24, 986 | "x": 0, 987 | "y": 33 988 | }, 989 | "hiddenSeries": false, 990 | "id": 30, 991 | "legend": { 992 | "avg": false, 993 | "current": false, 994 | "max": false, 995 | "min": false, 996 | "rightSide": true, 997 | "show": true, 998 | "total": false, 999 | "values": false 1000 | }, 1001 | "lines": true, 1002 | "linewidth": 1, 1003 | "nullPointMode": "null", 1004 | "options": { 1005 | "alertThreshold": true 1006 | }, 1007 | "percentage": false, 1008 | "pluginVersion": "8.0.3", 1009 | "pointradius": 2, 1010 | "points": false, 1011 | "renderer": "flot", 1012 | "seriesOverrides": [], 1013 | "spaceLength": 10, 1014 | "stack": false, 1015 | "steppedLine": false, 1016 | "targets": [ 1017 | { 1018 | "expr": "k8s_unused_resources{type=\"Ingress\", namespaces=~\"$namespaces\"}", 1019 | "interval": "", 1020 | "legendFormat": "{{name}}", 1021 | "refId": "A" 1022 | } 1023 | ], 1024 | "thresholds": [], 1025 | "timeFrom": null, 1026 | "timeRegions": [], 1027 | "timeShift": null, 1028 | "title": "Ingress details", 1029 | "tooltip": { 1030 | "shared": true, 1031 | "sort": 0, 1032 | "value_type": "individual" 1033 | }, 1034 | "type": "graph", 1035 | "xaxis": { 1036 | "buckets": null, 1037 | "mode": "time", 1038 | "name": null, 1039 | "show": true, 1040 | "values": [] 1041 | }, 1042 | "yaxes": [ 1043 | { 1044 | "format": "short", 1045 | "label": null, 1046 | "logBase": 1, 1047 | "max": null, 1048 | "min": null, 1049 | "show": true 1050 | }, 1051 | { 1052 | "format": "short", 1053 | "label": null, 1054 | "logBase": 1, 1055 | "max": null, 1056 | "min": null, 1057 | "show": true 1058 | } 1059 | ], 1060 | "yaxis": { 1061 | "align": false, 1062 | "alignLevel": null 1063 | } 1064 | } 1065 | ], 1066 | "title": "Ingress", 1067 | "type": "row" 1068 | }, 1069 | { 1070 | "collapsed": true, 1071 | "datasource": null, 1072 | "gridPos": { 1073 | "h": 1, 1074 | "w": 24, 1075 | "x": 0, 1076 | "y": 33 1077 | }, 1078 | "id": 44, 1079 | "panels": [ 1080 | { 1081 | "aliasColors": {}, 1082 | "bars": false, 1083 | "dashLength": 10, 1084 | "dashes": false, 1085 | "datasource": null, 1086 | "fill": 1, 1087 | "fillGradient": 0, 1088 | "gridPos": { 1089 | "h": 8, 1090 | "w": 24, 1091 | "x": 0, 1092 | "y": 34 1093 | }, 1094 | "hiddenSeries": false, 1095 | "id": 42, 1096 | "legend": { 1097 | "avg": false, 1098 | "current": false, 1099 | "max": false, 1100 | "min": false, 1101 | "rightSide": true, 1102 | "show": true, 1103 | "total": false, 1104 | "values": false 1105 | }, 1106 | "lines": true, 1107 | "linewidth": 1, 1108 | "nullPointMode": "null", 1109 | "options": { 1110 | "alertThreshold": true 1111 | }, 1112 | "percentage": false, 1113 | "pluginVersion": "8.0.3", 1114 | "pointradius": 2, 1115 | "points": false, 1116 | "renderer": "flot", 1117 | "seriesOverrides": [], 1118 | "spaceLength": 10, 1119 | "stack": false, 1120 | "steppedLine": false, 1121 | "targets": [ 1122 | { 1123 | "expr": "k8s_unused_resources{type=\"Role Binding\", namespaces=~\"$namespaces\"}", 1124 | "interval": "", 1125 | "legendFormat": "{{name}}", 1126 | "refId": "A" 1127 | } 1128 | ], 1129 | "thresholds": [], 1130 | "timeFrom": null, 1131 | "timeRegions": [], 1132 | "timeShift": null, 1133 | "title": "Role Binding Details", 1134 | "tooltip": { 1135 | "shared": true, 1136 | "sort": 0, 1137 | "value_type": "individual" 1138 | }, 1139 | "type": "graph", 1140 | "xaxis": { 1141 | "buckets": null, 1142 | "mode": "time", 1143 | "name": null, 1144 | "show": true, 1145 | "values": [] 1146 | }, 1147 | "yaxes": [ 1148 | { 1149 | "format": "short", 1150 | "label": null, 1151 | "logBase": 1, 1152 | "max": null, 1153 | "min": null, 1154 | "show": true 1155 | }, 1156 | { 1157 | "format": "short", 1158 | "label": null, 1159 | "logBase": 1, 1160 | "max": null, 1161 | "min": null, 1162 | "show": true 1163 | } 1164 | ], 1165 | "yaxis": { 1166 | "align": false, 1167 | "alignLevel": null 1168 | } 1169 | } 1170 | ], 1171 | "title": "Role Binding", 1172 | "type": "row" 1173 | }, 1174 | { 1175 | "collapsed": true, 1176 | "datasource": null, 1177 | "gridPos": { 1178 | "h": 1, 1179 | "w": 24, 1180 | "x": 0, 1181 | "y": 34 1182 | }, 1183 | "id": 14, 1184 | "panels": [ 1185 | { 1186 | "aliasColors": {}, 1187 | "bars": false, 1188 | "dashLength": 10, 1189 | "dashes": false, 1190 | "datasource": null, 1191 | "fieldConfig": { 1192 | "defaults": { 1193 | "custom": {} 1194 | }, 1195 | "overrides": [] 1196 | }, 1197 | "fill": 1, 1198 | "fillGradient": 0, 1199 | "gridPos": { 1200 | "h": 8, 1201 | "w": 24, 1202 | "x": 0, 1203 | "y": 14 1204 | }, 1205 | "hiddenSeries": false, 1206 | "id": 12, 1207 | "legend": { 1208 | "avg": false, 1209 | "current": false, 1210 | "max": false, 1211 | "min": false, 1212 | "rightSide": true, 1213 | "show": true, 1214 | "total": false, 1215 | "values": false 1216 | }, 1217 | "lines": true, 1218 | "linewidth": 1, 1219 | "nullPointMode": "null", 1220 | "percentage": false, 1221 | "pluginVersion": "7.1.1", 1222 | "pointradius": 2, 1223 | "points": false, 1224 | "renderer": "flot", 1225 | "seriesOverrides": [], 1226 | "spaceLength": 10, 1227 | "stack": false, 1228 | "steppedLine": false, 1229 | "targets": [ 1230 | { 1231 | "expr": "k8s_unused_resources{type=\"ConfigMap\", namespaces=~\"$namespaces\"}", 1232 | "interval": "", 1233 | "legendFormat": "{{name}}", 1234 | "refId": "A" 1235 | } 1236 | ], 1237 | "thresholds": [], 1238 | "timeFrom": null, 1239 | "timeRegions": [], 1240 | "timeShift": null, 1241 | "title": "Config Map Details", 1242 | "tooltip": { 1243 | "shared": true, 1244 | "sort": 0, 1245 | "value_type": "individual" 1246 | }, 1247 | "type": "graph", 1248 | "xaxis": { 1249 | "buckets": null, 1250 | "mode": "time", 1251 | "name": null, 1252 | "show": true, 1253 | "values": [] 1254 | }, 1255 | "yaxes": [ 1256 | { 1257 | "format": "short", 1258 | "label": null, 1259 | "logBase": 1, 1260 | "max": null, 1261 | "min": null, 1262 | "show": true 1263 | }, 1264 | { 1265 | "format": "short", 1266 | "label": null, 1267 | "logBase": 1, 1268 | "max": null, 1269 | "min": null, 1270 | "show": true 1271 | } 1272 | ], 1273 | "yaxis": { 1274 | "align": false, 1275 | "alignLevel": null 1276 | } 1277 | } 1278 | ], 1279 | "title": "Config Map", 1280 | "type": "row" 1281 | }, 1282 | { 1283 | "collapsed": true, 1284 | "datasource": null, 1285 | "gridPos": { 1286 | "h": 1, 1287 | "w": 24, 1288 | "x": 0, 1289 | "y": 35 1290 | }, 1291 | "id": 38, 1292 | "panels": [ 1293 | { 1294 | "aliasColors": {}, 1295 | "bars": false, 1296 | "dashLength": 10, 1297 | "dashes": false, 1298 | "datasource": null, 1299 | "fieldConfig": { 1300 | "defaults": { 1301 | "custom": {} 1302 | }, 1303 | "overrides": [] 1304 | }, 1305 | "fill": 1, 1306 | "fillGradient": 0, 1307 | "gridPos": { 1308 | "h": 8, 1309 | "w": 24, 1310 | "x": 0, 1311 | "y": 10 1312 | }, 1313 | "hiddenSeries": false, 1314 | "id": 36, 1315 | "legend": { 1316 | "avg": false, 1317 | "current": false, 1318 | "max": false, 1319 | "min": false, 1320 | "rightSide": true, 1321 | "show": true, 1322 | "total": false, 1323 | "values": false 1324 | }, 1325 | "lines": true, 1326 | "linewidth": 1, 1327 | "nullPointMode": "null", 1328 | "percentage": false, 1329 | "pluginVersion": "7.1.1", 1330 | "pointradius": 2, 1331 | "points": false, 1332 | "renderer": "flot", 1333 | "seriesOverrides": [], 1334 | "spaceLength": 10, 1335 | "stack": false, 1336 | "steppedLine": false, 1337 | "targets": [ 1338 | { 1339 | "expr": "k8s_unused_resources{type=\"Service Account\", namespaces=~\"$namespaces\"}", 1340 | "interval": "", 1341 | "legendFormat": "{{name}}", 1342 | "refId": "A" 1343 | } 1344 | ], 1345 | "thresholds": [], 1346 | "timeFrom": null, 1347 | "timeRegions": [], 1348 | "timeShift": null, 1349 | "title": "Service Account Details", 1350 | "tooltip": { 1351 | "shared": true, 1352 | "sort": 0, 1353 | "value_type": "individual" 1354 | }, 1355 | "type": "graph", 1356 | "xaxis": { 1357 | "buckets": null, 1358 | "mode": "time", 1359 | "name": null, 1360 | "show": true, 1361 | "values": [] 1362 | }, 1363 | "yaxes": [ 1364 | { 1365 | "format": "short", 1366 | "label": null, 1367 | "logBase": 1, 1368 | "max": null, 1369 | "min": null, 1370 | "show": true 1371 | }, 1372 | { 1373 | "format": "short", 1374 | "label": null, 1375 | "logBase": 1, 1376 | "max": null, 1377 | "min": null, 1378 | "show": true 1379 | } 1380 | ], 1381 | "yaxis": { 1382 | "align": false, 1383 | "alignLevel": null 1384 | } 1385 | } 1386 | ], 1387 | "title": "Services Account", 1388 | "type": "row" 1389 | }, 1390 | { 1391 | "collapsed": true, 1392 | "datasource": null, 1393 | "gridPos": { 1394 | "h": 1, 1395 | "w": 24, 1396 | "x": 0, 1397 | "y": 36 1398 | }, 1399 | "id": 6, 1400 | "panels": [ 1401 | { 1402 | "aliasColors": {}, 1403 | "bars": false, 1404 | "dashLength": 10, 1405 | "dashes": false, 1406 | "datasource": null, 1407 | "fieldConfig": { 1408 | "defaults": { 1409 | "custom": {} 1410 | }, 1411 | "overrides": [] 1412 | }, 1413 | "fill": 1, 1414 | "fillGradient": 0, 1415 | "gridPos": { 1416 | "h": 8, 1417 | "w": 24, 1418 | "x": 0, 1419 | "y": 15 1420 | }, 1421 | "hiddenSeries": false, 1422 | "id": 4, 1423 | "legend": { 1424 | "avg": false, 1425 | "current": false, 1426 | "max": false, 1427 | "min": false, 1428 | "rightSide": true, 1429 | "show": true, 1430 | "total": false, 1431 | "values": false 1432 | }, 1433 | "lines": true, 1434 | "linewidth": 1, 1435 | "nullPointMode": "null", 1436 | "percentage": false, 1437 | "pluginVersion": "7.1.1", 1438 | "pointradius": 2, 1439 | "points": false, 1440 | "renderer": "flot", 1441 | "seriesOverrides": [], 1442 | "spaceLength": 10, 1443 | "stack": false, 1444 | "steppedLine": false, 1445 | "targets": [ 1446 | { 1447 | "expr": "k8s_unused_resources{type=\"Secrets\", namespaces=~\"$namespaces\"}", 1448 | "interval": "", 1449 | "legendFormat": "{{name}}", 1450 | "refId": "A" 1451 | } 1452 | ], 1453 | "thresholds": [], 1454 | "timeFrom": null, 1455 | "timeRegions": [], 1456 | "timeShift": null, 1457 | "title": "Secrets Details", 1458 | "tooltip": { 1459 | "shared": true, 1460 | "sort": 0, 1461 | "value_type": "individual" 1462 | }, 1463 | "type": "graph", 1464 | "xaxis": { 1465 | "buckets": null, 1466 | "mode": "time", 1467 | "name": null, 1468 | "show": true, 1469 | "values": [] 1470 | }, 1471 | "yaxes": [ 1472 | { 1473 | "format": "short", 1474 | "label": null, 1475 | "logBase": 1, 1476 | "max": null, 1477 | "min": null, 1478 | "show": true 1479 | }, 1480 | { 1481 | "format": "short", 1482 | "label": null, 1483 | "logBase": 1, 1484 | "max": null, 1485 | "min": null, 1486 | "show": true 1487 | } 1488 | ], 1489 | "yaxis": { 1490 | "align": false, 1491 | "alignLevel": null 1492 | } 1493 | } 1494 | ], 1495 | "title": "Secret", 1496 | "type": "row" 1497 | } 1498 | ], 1499 | "refresh": "10s", 1500 | "schemaVersion": 30, 1501 | "style": "dark", 1502 | "tags": [], 1503 | "templating": { 1504 | "list": [ 1505 | { 1506 | "allValue": null, 1507 | "current": { 1508 | "selected": false, 1509 | "text": "All", 1510 | "value": "$__all" 1511 | }, 1512 | "datasource": "Prometheus", 1513 | "definition": "label_values(namespace)", 1514 | "description": null, 1515 | "error": null, 1516 | "hide": 0, 1517 | "includeAll": true, 1518 | "label": "Namespaces", 1519 | "multi": false, 1520 | "name": "namespaces", 1521 | "options": [], 1522 | "query": { 1523 | "query": "label_values(namespace)", 1524 | "refId": "Prometheus-namespaces-Variable-Query" 1525 | }, 1526 | "refresh": 2, 1527 | "regex": "", 1528 | "skipUrlSync": false, 1529 | "sort": 0, 1530 | "tagValuesQuery": "", 1531 | "tagsQuery": "", 1532 | "type": "query", 1533 | "useTags": false 1534 | } 1535 | ] 1536 | }, 1537 | "time": { 1538 | "from": "now-5m", 1539 | "to": "now" 1540 | }, 1541 | "timepicker": { 1542 | "refresh_intervals": [ 1543 | "10s", 1544 | "30s", 1545 | "1m", 1546 | "5m", 1547 | "15m", 1548 | "30m", 1549 | "1h", 1550 | "2h", 1551 | "1d" 1552 | ] 1553 | }, 1554 | "timezone": "", 1555 | "title": "K8S-Purger - Hunt Unused Resources In Kubernetes", 1556 | "uid": "6a9x2wRnz", 1557 | "version": 6 1558 | } -------------------------------------------------------------------------------- /deploy/manifest.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | labels: 5 | app: k8spurger 6 | name: k8spurger 7 | spec: 8 | replicas: 1 9 | selector: 10 | matchLabels: 11 | app: k8spurger 12 | template: 13 | metadata: 14 | labels: 15 | app: k8spurger 16 | annotations: 17 | prometheus.io/scrape: "true" 18 | prometheus.io/port: "8000" 19 | prometheus.io/path: /metrics 20 | spec: 21 | serviceAccountName: k8spurger-sa 22 | containers: 23 | - name: k8spurger 24 | image: yogeshkunjir/k8spurger:v0.36 25 | imagePullPolicy: IfNotPresent 26 | resources: 27 | limits: 28 | cpu: "0.5" 29 | memory: 512Mi 30 | env: 31 | - name: REFRESH_INTERVAL 32 | value: "900" 33 | ports: 34 | - name: http 35 | containerPort: 8000 36 | protocol: TCP 37 | livenessProbe: 38 | failureThreshold: 3 39 | timeoutSeconds: 5 40 | httpGet: 41 | path: /metrics 42 | port: http 43 | readinessProbe: 44 | failureThreshold: 3 45 | timeoutSeconds: 5 46 | httpGet: 47 | path: /metrics 48 | port: http 49 | --- 50 | kind: Service 51 | apiVersion: v1 52 | metadata: 53 | name: k8spurger-svc 54 | labels: 55 | app: k8spurger 56 | spec: 57 | type: ClusterIP 58 | ports: 59 | - name: "http" 60 | protocol: TCP 61 | port: 80 62 | targetPort: "http" 63 | selector: 64 | app: k8spurger 65 | --- 66 | apiVersion: v1 67 | kind: ServiceAccount 68 | metadata: 69 | name: k8spurger-sa 70 | labels: 71 | app: k8spurger 72 | kubernetes.io/cluster-service: "true" 73 | --- 74 | kind: ClusterRole 75 | apiVersion: rbac.authorization.k8s.io/v1beta1 76 | metadata: 77 | name: k8spurger-cluster-role 78 | labels: 79 | app: k8spurger 80 | kubernetes.io/cluster-service: "true" 81 | rules: 82 | - apiGroups: 83 | - "" 84 | resources: 85 | - "*" 86 | verbs: 87 | - get 88 | - list 89 | - watch 90 | - apiGroups: 91 | - extensions 92 | resources: 93 | - deployments 94 | - replicasets 95 | - ingresses 96 | verbs: 97 | - get 98 | - list 99 | - watch 100 | - apiGroups: 101 | - apps 102 | resources: 103 | - statefulsets 104 | - deployments 105 | - replicasets 106 | verbs: 107 | - get 108 | - list 109 | - watch 110 | - apiGroups: 111 | - rbac.authorization.k8s.io 112 | resources: 113 | - rolebindings 114 | verbs: 115 | - get 116 | - list 117 | - watch 118 | --- 119 | kind: ClusterRoleBinding 120 | apiVersion: rbac.authorization.k8s.io/v1beta1 121 | metadata: 122 | name: k8spurger-rb 123 | labels: 124 | app: k8spurger 125 | kubernetes.io/cluster-service: "true" 126 | subjects: 127 | - kind: ServiceAccount 128 | namespace: default 129 | name: k8spurger-sa 130 | apiGroup: "" 131 | roleRef: 132 | kind: ClusterRole 133 | name: k8spurger-cluster-role 134 | apiGroup: "" 135 | --- 136 | -------------------------------------------------------------------------------- /deploy/prometheusrule.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: monitoring.coreos.com/v1 2 | kind: PrometheusRule 3 | metadata: 4 | labels: 5 | prometheus: k8s 6 | name: k8spurger-apps-rules 7 | spec: 8 | groups: 9 | - name: UnusedResources 10 | rules: 11 | - alert: UnusedResourceSecretWarning 12 | expr: sum(k8s_unused_resources{type="Secrets"}) > 0 13 | for: 1d 14 | annotations: 15 | message: "You have more than 1 unsed Secrets in your cluster" 16 | labels: 17 | severity: warning 18 | - alert: UnusedResourceConfigMapWarning 19 | expr: sum(k8s_unused_resources{type="ConfigMap"}) > 0 20 | for: 1d 21 | annotations: 22 | message: 'You have more than 1 unsed ConfigMap in your cluster' 23 | labels: 24 | severity: warning 25 | - alert: UnusedResourcePVClaimWarning 26 | expr: sum(k8s_unused_resources{type="PV Claim"}) > 0 27 | for: 1d 28 | annotations: 29 | message: 'You have more than 1 unsed PV Claim in your cluster' 30 | labels: 31 | severity: warning 32 | - alert: UnusedResourceServicesWarning 33 | expr: sum(k8s_unused_resources{type="Services"}) > 0 34 | for: 1d 35 | annotations: 36 | message: 'You have more than 1 unsed Services in your cluster' 37 | labels: 38 | severity: warning 39 | - alert: UnusedResourceIngressWarning 40 | expr: sum(k8s_unused_resources{type="Ingress"}) > 0 41 | for: 1d 42 | annotations: 43 | message: 'You have more than 1 unsed Ingress in your cluster' 44 | labels: 45 | severity: warning 46 | - alert: UnusedResourceServiceAccountWarning 47 | expr: sum(k8s_unused_resources{type="Service Account"}) > 0 48 | for: 1d 49 | annotations: 50 | message: 'You have more than 1 unsed Service Account in your cluster' 51 | labels: 52 | severity: warning 53 | - alert: UnusedResourceRoleBindingWarning 54 | expr: sum(k8s_unused_resources{type="Role Binding"}) > 0 55 | for: 1d 56 | annotations: 57 | message: 'You have more than 1 unsed Role Binding in your cluster' 58 | labels: 59 | severity: warning 60 | - alert: UnusedResourceDeploymentWarning 61 | expr: sum(k8s_unused_resources{type="Deployment"}) > 0 62 | for: 1d 63 | annotations: 64 | message: 'You have more than 1 unsed Deployment in your cluster' 65 | labels: 66 | severity: warning 67 | - alert: UnusedResourceStatefulSetsWarning 68 | expr: sum(k8s_unused_resources{type="Stateful Sets"}) > 0 69 | for: 1d 70 | annotations: 71 | message: 'You have more than 1 unsed Stateful Sets in your cluster' 72 | labels: 73 | severity: warning -------------------------------------------------------------------------------- /documentation/grafana_dashbaord.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yogeshkk/K8sPurger/ea07b23de1829881476eb51ede1f06d31a8f61a0/documentation/grafana_dashbaord.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Werkzeug==2.0.1 2 | gunicorn~=20.1.0 3 | kubernetes 4 | threaded 5 | prometheus_client 6 | --------------------------------------------------------------------------------