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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = """ | ||
{ | ||
"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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
python-game-servers/google/cloud/gaming_v1/types/game_server_configs.py
Lines 199 to 202 in 952f47e
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.