Skip to content

Instantly share code, notes, and snippets.

@aLekSer
Created August 25, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aLekSer/872a17259bac8d3f358016b615f53757 to your computer and use it in GitHub Desktop.
Save aLekSer/872a17259bac8d3f358016b615f53757 to your computer and use it in GitHub Desktop.
Example of using Python library to list Agones GameServers
from pprint import pprint
from kubernetes import client, config
import kubernetes.client
from kubernetes.client.rest import ApiException
def main():
print("Hi")
config.load_kube_config()
for api in client.ApisApi().get_api_versions().groups:
versions = []
for v in api.versions:
name = ""
if v.version == api.preferred_version.version and len(
api.versions) > 1:
name += "*"
name += v.version
versions.append(name)
print("%-40s %s" % (api.name, ",".join(versions)))
api = client.CustomObjectsApi()
with kubernetes.client.ApiClient(config.load_kube_config()) as api_client:
# Create an instance of the API class
api_instance = kubernetes.client.CustomObjectsApi(api_client)
try:
body = None # object | The JSON schema of the Resource to create.
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
dry_run = 'dry_run_example' # str | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed (optional)
limit = 56 # int | limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and kubernetes.clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, kubernetes.clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a kubernetes.client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. (optional)
timeout_seconds = 56 # int | Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity. (optional)
api_response = api_instance.list_namespaced_custom_object("agones.dev", "v1", "default", "gameservers", pretty=pretty, limit=limit, timeout_seconds=timeout_seconds)
pprint(api_response)
except ApiException as e:
print("Exception when calling CustomObjectsApi->list_namespaced_custom_object: %s\n" % e)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment