This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add more code samples for game servers (#32)
- Loading branch information
1 parent
a3dee35
commit 2d30003
Showing
29 changed files
with
2,103 additions
and
142 deletions.
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.