|
| 1 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | + |
| 17 | +import googleapiclient.discovery |
| 18 | + |
| 19 | + |
| 20 | +def list_clusters_and_nodepools(project_id, zone): |
| 21 | + """Lists all clusters and associated node pools.""" |
| 22 | + service = googleapiclient.discovery.build('container', 'v1') |
| 23 | + clusters_resource = service.projects().zones().clusters() |
| 24 | + |
| 25 | + clusters_response = clusters_resource.list( |
| 26 | + projectId=project_id, zone=zone).execute() |
| 27 | + |
| 28 | + for cluster in clusters_response.get('clusters', []): |
| 29 | + print('Cluster: {}, Status: {}, Current Master Version: {}'.format( |
| 30 | + cluster['name'], cluster['status'], |
| 31 | + cluster['currentMasterVersion'])) |
| 32 | + |
| 33 | + nodepools_response = clusters_resource.nodePools().list( |
| 34 | + projectId=project_id, zone=zone, |
| 35 | + clusterId=cluster['name']).execute() |
| 36 | + |
| 37 | + for nodepool in nodepools_response['nodePools']: |
| 38 | + print( |
| 39 | + ' -> Pool: {}, Status: {}, Machine Type: {}, ' |
| 40 | + 'Autoscaling: {}'.format( |
| 41 | + nodepool['name'], nodepool['status'], |
| 42 | + nodepool['config']['machineType'], |
| 43 | + nodepool.get('autoscaling', {}).get('enabled', False))) |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + parser = argparse.ArgumentParser( |
| 48 | + description=__doc__, |
| 49 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 50 | + subparsers = parser.add_subparsers(dest='command') |
| 51 | + list_clusters_and_nodepools_parser = subparsers.add_parser( |
| 52 | + 'list_clusters_and_nodepools', |
| 53 | + help=list_clusters_and_nodepools.__doc__) |
| 54 | + list_clusters_and_nodepools_parser.add_argument('project_id') |
| 55 | + list_clusters_and_nodepools_parser.add_argument('zone') |
| 56 | + |
| 57 | + args = parser.parse_args() |
| 58 | + |
| 59 | + if args.command == 'list_clusters_and_nodepools': |
| 60 | + list_clusters_and_nodepools(args.project_id, args.zone) |
| 61 | + else: |
| 62 | + parser.print_help() |
0 commit comments