-
Notifications
You must be signed in to change notification settings - Fork 4
/
list-gke-nodes.py
68 lines (54 loc) · 2.68 KB
/
list-gke-nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# List GKE nodes
# Official GCP SDK (Python) Documentation: https://googleapis.github.io/google-api-python-client/docs/dyn/
import json
import sys
import argparse
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.cloud import resource_manager
client = resource_manager.Client()
credentials = GoogleCredentials.get_application_default()
service = discovery.build('container', 'v1', credentials=credentials)
# Filter of Projects that will be scanned
parser_args = argparse.ArgumentParser(description='Define the projetc_id filter.'
'if empity will looking for all the active project_id that the credential have access')
parser_args.add_argument('--project')
project_Filter = parser_args.parse_args()
if project_Filter.project is None:
env_filter = {'lifecycleState': 'ACTIVE' }
else:
env_filter = {'projectId': project_Filter.project ,'lifecycleState': 'ACTIVE' }
# Print csv Header
print ('project_id; project_name;cluster_name;node_name;node_version;status;autopilot;image_type;',
'preemptible;machineType;diskSizeGb;diskType;autoscaling;minNodeCount;maxNodeCount;autoUpgrade;autoRepair;',
'maxPodsPerNode;podIpv4CidrSize;locations')
zone='-'
for project in client.list_projects(env_filter):
req = service.projects().zones().clusters().list(projectId=project.project_id, zone=zone)
resp = req.execute()
try:
for cluster in resp['clusters']:
for node in cluster['nodePools']:
print(
project.project_id, ';',
project.name, ';',
cluster.get('name'),';',
node.get('name'),';',
node.get('version'),';',
node.get('status'),';',
node.get('autopilot'),';',
node.get('config').get('imageType'),';',
node.get('config').get('preemptible'),';',
node.get('config').get('machineType'),';',
node.get('config').get('diskSizeGb'),';',
node.get('config').get('diskType'),';',
node.get('autoscaling',{}).get('enabled',{}),';',
node.get('autoscaling',{}).get('minNodeCount',{}),';',
node.get('autoscaling',{}).get('maxNodeCount',{}),';',
node.get('management',{}).get('autoUpgrade',{}),';',
node.get('management',{}).get('autoRepair',{}),';',
node.get('maxPodsConstraint',{}).get('maxPodsPerNode',{}),';',
node.get('podIpv4CidrSize'),';',
node.get('locations')
)
except KeyError: pass