Skip to content

Add container_engine/list_clusters sample #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions container_engine/api-client/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.. This file is automatically generated. Do not edit this file directly.

Google Container Engine Python Samples
===============================================================================

This directory contains samples for Google Container Engine. `Google Container Engine`_ runs Docker containers on Google Cloud Platform, powered by Kubernetes.




.. _Google Container Engine: https://cloud.google.com/container-engine/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

Authentication is typically done through `Application Default Credentials`_,
which means you do not have to change the code to authenticate as long as
your environment has credentials. You have a few options for setting up
authentication:

#. When running locally, use the `Google Cloud SDK`_

.. code-block:: bash

gcloud auth application-default login


#. When running on App Engine or Compute Engine, credentials are already
set-up. However, you may need to configure your Compute Engine instance
with `additional scopes`_.

#. You can create a `Service Account key file`_. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
the path to the key file, for example:

.. code-block:: bash

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

Install Dependencies
++++++++++++++++++++

#. Install `pip`_ and `virtualenv`_ if you do not already have them.

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash

$ virtualenv env
$ source env/bin/activate

#. Install the dependencies needed to run the samples.

.. code-block:: bash

$ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Snippets
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash

$ python snippets.py

usage: snippets.py [-h] {list_clusters_and_nodepools} ...

positional arguments:
{list_clusters_and_nodepools}
list_clusters_and_nodepools
Lists all clusters and associated node pools.

optional arguments:
-h, --help show this help message and exit




.. _Google Cloud SDK: https://cloud.google.com/sdk/
18 changes: 18 additions & 0 deletions container_engine/api-client/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is used to generate README.rst

product:
name: Google Container Engine
short_name: Container Engine
url: https://cloud.google.com/container-engine/docs/
description: >
`Google Container Engine`_ runs Docker containers on Google Cloud Platform,
powered by Kubernetes.

setup:
- auth
- install_deps

samples:
- name: Snippets
file: snippets.py
show_help: true
3 changes: 3 additions & 0 deletions container_engine/api-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
google-api-python-client==1.6.2
google-auth==1.0.1
google-auth-httplib2==0.0.2
62 changes: 62 additions & 0 deletions container_engine/api-client/snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

import googleapiclient.discovery


def list_clusters_and_nodepools(project_id, zone):
"""Lists all clusters and associated node pools."""
service = googleapiclient.discovery.build('container', 'v1')
clusters_resource = service.projects().zones().clusters()

clusters_response = clusters_resource.list(
projectId=project_id, zone=zone).execute()

for cluster in clusters_response.get('clusters', []):
print('Cluster: {}, Status: {}, Current Master Version: {}'.format(
cluster['name'], cluster['status'],
cluster['currentMasterVersion']))

nodepools_response = clusters_resource.nodePools().list(
projectId=project_id, zone=zone,
clusterId=cluster['name']).execute()

for nodepool in nodepools_response['nodePools']:
print(
' -> Pool: {}, Status: {}, Machine Type: {}, '
'Autoscaling: {}'.format(
nodepool['name'], nodepool['status'],
nodepool['config']['machineType'],
nodepool.get('autoscaling', {}).get('enabled', False)))


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')
list_clusters_and_nodepools_parser = subparsers.add_parser(
'list_clusters_and_nodepools',
help=list_clusters_and_nodepools.__doc__)
list_clusters_and_nodepools_parser.add_argument('project_id')
list_clusters_and_nodepools_parser.add_argument('zone')

args = parser.parse_args()

if args.command == 'list_clusters_and_nodepools':
list_clusters_and_nodepools(args.project_id, args.zone)
else:
parser.print_help()
22 changes: 22 additions & 0 deletions container_engine/api-client/snippets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import snippets


def test_list_clusters_and_nodepools():
project_id = os.environ['GCLOUD_PROJECT']
snippets.list_clusters_and_nodepools(project_id, 'us-central1-f')