Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

docs: add more code samples for game servers #32

Merged
merged 5 commits into from Jul 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
521 changes: 516 additions & 5 deletions samples/snippets/README.rst

Large diffs are not rendered by default.

67 changes: 62 additions & 5 deletions samples/snippets/README.rst.in
Expand Up @@ -14,23 +14,80 @@ setup:
- install_deps

samples:
- name: Create deployment
- name: Create game server deployment
file: create_deployment.py
show_help: True
- name: Delete deployment
- name: Delete game server deployment
file: delete_deployment.py
show_help: True
- name: Get deployment
- name: Get game server deployment
file: get_deployment.py
show_help: True
- name: Update game server deployment
file: update_deployment.py
show_help: True
- name: Get rollout
file: get_rollout.py
show_help: True
- name: List deployments
- name: List game server deployments
file: list_deployments.py
show_help: True
- name: Update rollout by removing default config
- name: Update rollout to set default config
file: update_rollout_default.py
show_help: True
- name: Update rollout to set override config
file: update_rollout_override.py
show_help: True
- name: Update rollout to remove default config
file: update_rollout_remove_default.py
show_help: True
- name: Update rollout to remove override config
file: update_rollout_remove_override.py
show_help: True

- name: Create game server config
file: create_config.py
show_help: True
- name: Delete game server config
file: delete_config.py
show_help: True
- name: Get game server config
file: get_config.py
show_help: True
- name: List game server configs
file: list_configs.py
show_help: True

- name: Create realm
file: create_realm.py
show_help: True
- name: Delete realm
file: delete_realm.py
show_help: True
- name: Get realm
file: get_realm.py
show_help: True
- name: Update realm
file: update_realm.py
show_help: True
- name: List realms
file: list_realms.py
show_help: True

- name: Create game server cluster
file: create_cluster.py
show_help: True
- name: Delete game server cluster
file: delete_cluster.py
show_help: True
- name: Get game server cluster
file: get_cluster.py
show_help: True
- name: Update game server cluster
file: update_cluster.py
show_help: True
- name: List game server cluster
file: list_clusters.py
show_help: True

cloud_client_library: true
70 changes: 70 additions & 0 deletions samples/snippets/create_cluster.py
@@ -0,0 +1,70 @@
#!/usr/bin/env python

# Copyright 2020 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.

"""Google Cloud Game Servers sample for creating a game server cluster.

Example usage:
python create_cluster.py --project-id <project-id> --location <location>
--realm-id <realm-id> --cluster-id <cluster-id> --gke-cluster-name <gke-cluster-name>
"""

import argparse

from google.cloud import gaming
from google.cloud.gaming_v1.types import game_server_clusters


# [START cloud_game_servers_cluster_create]
def create_cluster(project_id, location, realm_id, cluster_id, gke_cluster_name):
"""Creates a game server cluster."""

client = gaming.GameServerClustersServiceClient()

gke_cluster_reference = game_server_clusters.GkeClusterReference(
cluster=gke_cluster_name,
)
request = game_server_clusters.CreateGameServerClusterRequest(
parent=f"projects/{project_id}/locations/{location}/realms/{realm_id}",
game_server_cluster_id=cluster_id,
game_server_cluster=game_server_clusters.GameServerCluster(
description="My Game Server Cluster",
connection_info=game_server_clusters.GameServerClusterConnectionInfo(
gke_cluster_reference=gke_cluster_reference,
namespace="default",
),
),
)

operation = client.create_game_server_cluster(request)
print(f"Create cluster operation: {operation.operation.name}")
operation.result(timeout=120)
# [END cloud_game_servers_cluster_create]


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--project-id', help='Your cloud project ID.', required=True)
parser.add_argument('--location', help='Your realm location.', required=True)
parser.add_argument('--realm-id', help='Your realm ID.', required=True)
parser.add_argument('--cluster-id', help='Your game server cluster ID.', required=True)
parser.add_argument(
'--gke-cluster-name',
help='The name of the GKE cluster which is managed by the game server cluster being created.',
required=True)

args = parser.parse_args()

create_cluster(args.project_id, args.location, args.realm_id, args.cluster_id, args.gke_cluster_name)
133 changes: 133 additions & 0 deletions samples/snippets/create_config.py
@@ -0,0 +1,133 @@
#!/usr/bin/env python

# Copyright 2020 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.

"""Google Cloud Game Servers sample for creating a game server config.

Example usage:
python create_config.py --project-id <project-id> --deployment-id <deployment-id> --config-id <config-id>
"""

import argparse

from google.cloud import gaming
from google.cloud.gaming_v1.types import game_server_configs

# [START cloud_game_servers_config_create]

# FLEET_SPEC is the spec portion of an agones Fleet. It must be in JSON format.
# See https://agones.dev/site/docs/reference/fleet/ for more on fleets.
FLEET_SPEC = """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there guidance somewhere on how to construct this JSON?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the protos mention https://agones.dev/site/docs/reference/fleet/, could you add that as a comment here as well?

Attributes:
fleet_spec (str):
Agones fleet spec. Example spec:
``https://agones.dev/site/docs/reference/fleet/``.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Apologies for triple posting) I see the link provides a YAML but no JSON equivalent. If the Agones docs are within in your control it may be nice to provide a JSON version.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the go canonical example, https://github.com/GoogleCloudPlatform/golang-samples/blob/master/gaming/servers/create_config.go#L28

This fleet spec should be included in the sample range, and have the same comment as the Go fleet has.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the same comment as the Go sample has. Thanks for point this out.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the sample's # [START cloud_game_servers_config_create] to above the start of the fleet spec definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Sorry I missed what you meant in the previous comment.

{
"replicas": 10,
"scheduling": "Packed",
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxSurge": "25%",
"maxUnavailable": "25%"
}
},
"template": {
"metadata": {
"labels": {
"gameName": "udp-server"
}
},
"spec": {
"ports": [
{
"name": "default",
"portPolicy": "Dynamic",
"containerPort": 2156,
"protocol": "TCP"
}
],
"health": {
"initialDelaySeconds": 30,
"periodSeconds": 60
},
"sdkServer": {
"logLevel": "Info",
"grpcPort": 9357,
"httpPort": 9358
},
"template": {
"spec": {
"containers": [
{
"name": "dedicated",
"image": "gcr.io/agones-images/udp-server:0.17",
"imagePullPolicy": "Always",
"resources": {
"requests": {
"memory": "200Mi",
"cpu": "500m"
},
"limits": {
"memory": "200Mi",
"cpu": "500m"
}
}
}
]
}
}
}
}
}
"""


def create_config(project_id, deployment_id, config_id):
"""Creates a game server config."""

client = gaming.GameServerConfigsServiceClient()

fleet_config = game_server_configs.FleetConfig(
name="my-fleet-spec", fleet_spec=FLEET_SPEC,
)

# Location is hard coded as global, as game server configs can
# only be created in global. This is done for all operations on
# game server configs.
request = game_server_configs.CreateGameServerConfigRequest(
parent=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}",
config_id=config_id,
game_server_config=game_server_configs.GameServerConfig(
description="My Game Server Config", fleet_configs=[fleet_config],
),
)

operation = client.create_game_server_config(request)
print(f"Create config operation: {operation.operation.name}")
operation.result(timeout=120)


# [END cloud_game_servers_config_create]

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--project-id", help="Your cloud project ID.", required=True)
parser.add_argument(
"--deployment-id", help="Your game server deployment ID.", required=True
)
parser.add_argument(
"--config-id", help="Your game server config ID.", required=True
)

args = parser.parse_args()

create_config(args.project_id, args.deployment_id, args.config_id)
10 changes: 5 additions & 5 deletions samples/snippets/create_deployment.py
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Game Servers sample for creating a Game Server Deployment.
"""Google Cloud Game Servers sample for creating a game server deployment.

Example usage:
python create_deployment.py --project-id <project-id> --deployment-id <deployment-id>
Expand All @@ -26,15 +26,15 @@
from google.cloud.gaming_v1.types import game_server_deployments


# [START cloud_game_servers_create_deployment]
# [START cloud_game_servers_deployment_create]
def create_deployment(project_id, deployment_id):
"""Creates a game server deployment."""

client = gaming.GameServerDeploymentsServiceClient()

# Location is hard coded as global, as Game Server Deployments can
# Location is hard coded as global, as game server deployments can
# only be created in global. This is done for all operations on
# Game Server Deployments, as well as for its child resource types.
# game server deployments, as well as for its child resource types.
request = game_server_deployments.CreateGameServerDeploymentRequest(
parent=f"projects/{project_id}/locations/global",
deployment_id=deployment_id,
Expand All @@ -46,7 +46,7 @@ def create_deployment(project_id, deployment_id):
operation = client.create_game_server_deployment(request)
print(f"Create deployment operation: {operation.operation.name}")
operation.result(timeout=120)
# [END cloud_game_servers_create_deployment]
# [END cloud_game_servers_deployment_create]


if __name__ == "__main__":
Expand Down
58 changes: 58 additions & 0 deletions samples/snippets/create_realm.py
@@ -0,0 +1,58 @@
#!/usr/bin/env python

# Copyright 2020 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.

"""Google Cloud Game Servers sample for creating a realm.

Example usage:
python create_realm.py --project-id <project-id> --location <location> --realm-id <realm-id>
"""

import argparse

from google.cloud import gaming
from google.cloud.gaming_v1.types import realms


# [START cloud_game_servers_realm_create]
def create_realm(project_id, location, realm_id):
"""Creates a realm."""

client = gaming.RealmsServiceClient()

request = realms.CreateRealmRequest(
parent=f"projects/{project_id}/locations/{location}",
realm_id=realm_id,
realm=realms.Realm(
description="My Realm",
time_zone="US/Pacific",
),
)

operation = client.create_realm(request)
print(f"Create realm operation: {operation.operation.name}")
operation.result(timeout=120)
# [END cloud_game_servers_realm_create]


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--project-id', help='Your cloud project ID.', required=True)
parser.add_argument('--location', help='Your realm location.', required=True)
parser.add_argument('--realm-id', help='Your realm ID.', required=True)

args = parser.parse_args()

create_realm(args.project_id, args.location, args.realm_id)