Skip to content

Commit 3a91c0c

Browse files
authored
Add Allocation Endpoint GCP solution for multi-cluster allocation to Agones examples (agones-dev#2499)
* Add Allocation Endpoint GCP solution for multi-cluster allocation to Agoens examples * Add label to the Allocation Endpoint Proxy * Addressed feedback on doc * Adding make files
1 parent 15bd88b commit 3a91c0c

23 files changed

Lines changed: 2183 additions & 2 deletions

‎build/boilerplate.go.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Google LLC All Rights Reserved.
1+
// Copyright 2022 Google LLC All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

‎build/boilerplate.yaml.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Google LLC All Rights Reserved.
1+
# Copyright 2022 Google LLC All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

‎build/includes/examples.mk‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# |___/
2525

2626
# test all example images exist on Google Cloud Registry
27+
test-examples-on-gcr: example-image-test.allocation-endpoint
2728
test-examples-on-gcr: example-image-test.autoscaler-webhook
2829
test-examples-on-gcr: example-image-test.cpp-simple
2930
test-examples-on-gcr: example-image-test.nodejs-simple
@@ -34,6 +35,7 @@ test-examples-on-gcr: example-image-test.crd-client
3435
test-examples-on-gcr: example-image-test.supertuxkart
3536
test-examples-on-gcr: example-image-test.simple-game-server
3637

38+
push-example-golang-images: example-image-push.allocation-endpoint
3739
push-example-golang-images: example-image-push.autoscaler-webhook
3840
push-example-golang-images: example-image-push.crd-client
3941
push-example-golang-images: example-image-push.supertuxkart

‎build/includes/release.mk‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ gen-changelog:
3737
--token $$TOKEN
3838

3939
# outputs the markdown for the example images section of the release template
40+
release-example-image-markdown: example-image-markdown.allocation-endpoint
4041
release-example-image-markdown: example-image-markdown.autoscaler-webhook
4142
release-example-image-markdown: example-image-markdown.cpp-simple
4243
release-example-image-markdown: example-image-markdown.crd-client
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2022 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# Makefile for building a multi-cluster allocation proxy
17+
#
18+
19+
# __ __ _ _ _
20+
# \ \ / /_ _ _ __(_) __ _| |__ | | ___ ___
21+
# \ \ / / _` | '__| |/ _` | '_ \| |/ _ \ __|
22+
# \ V / (_| | | | | (_| | |_) | | __\__ \
23+
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/
24+
#
25+
26+
REPOSITORY = gcr.io/agones-images
27+
28+
# Directory that this Makefile is in.
29+
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
30+
project_path := $(dir $(mkfile_path))
31+
server_tag = $(REPOSITORY)/allocation-endpoint-proxy:0.1
32+
root_path = $(realpath $(project_path)/../..)
33+
34+
# _____ _
35+
# |_ _|_ _ _ __ __ _ ___| |_ ___
36+
# | |/ _` | '__/ _` |/ _ \ __/ __|
37+
# | | (_| | | | (_| | __/ |_\__ \
38+
# |_|\__,_|_| \__, |\___|\__|___/
39+
# |___/
40+
41+
# build the allocation endpoint proxy binary
42+
build:
43+
cd $(root_path) && docker build -f $(project_path)/server/Dockerfile --tag=$(server_tag) .
44+
45+
run:
46+
docker run --network=host $(server_tag) $(args)
47+
48+
# check if hosted on Google Cloud Registry
49+
gcr-check:
50+
gcloud container images describe $(server_tag)
51+
52+
#output the tag for this image
53+
echo-image-tag:
54+
@echo $(server_tag)
55+
56+
# Push the docker image
57+
push: build
58+
docker push $(server_tag)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Allocation Endpoint
2+
3+
This is a sample solution to enable an Allocation Endpoint proxy in front of GKE Agones clusters.
4+
5+
In this folder you will find:
6+
7+
1. Terraform modules that create resources in GCP
8+
2. The Allocation Endpoint Proxy code that gets deployed to Cloud Run
9+
3. An [ESP container](https://cloud.google.com/endpoints/docs/grpc/get-started-cloud-run) that gets deployed as a sidecar alongside the `agones-allocator`
10+
4. A sample client code to send allocation requests to the proxy
11+
5. Dockerfile that builds the proxy and scripts to push the image to a docker repository
12+
6. Documentation on how to use the solution.
13+
14+
Here is the architecture of GCP resources created:
15+
16+
![architecture](https://github.com/googleforgames/agones/blob/main/examples/allocation-endpoint/architecture.png?raw=true)
17+
18+
19+
## GKE cluster
20+
First and foremost you need to create clusters and install Agones to experiment with this solution.
21+
The clusters can be in the same GCP project as your Allocation Endpoint proxy or they can be in a different project.
22+
23+
---
24+
**NOTE**
25+
26+
The solution has not been tested with non-GKE clusters.
27+
28+
---
29+
30+
When creating GKE Clusters, [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity) needs to be enabled. Please follow [GKE cluster setup](https://agones.dev/site/docs/installation/creating-cluster/gke/) and include workload-pool, e.g:
31+
32+
<pre>
33+
gcloud container clusters create [NAME] \
34+
...
35+
<b>--workload-pool=[GKE-PROJECT-ID].svc.id.goog \ </b>
36+
</pre>
37+
38+
Install Agones on your cluster. You need to disable mTLS because the `agones-allocator` container will be the backend for ESP container.
39+
40+
```
41+
helm upgrade my-release --install --namespace agones-system --create-namespace agones/agones \
42+
--set agones.allocator.disableMTLS=true \
43+
--set agones.allocator.disableTLS=true \
44+
--set agones.allocator.service.http.enabled=false
45+
```
46+
47+
After installing Agones, deploy [ESP](https://cloud.google.com/endpoints/docs/grpc/specify-esp-v2-startup-options) which is an envoy based proxy, deployed as a sidecar along side `agones-alloator` container. Run the following to patch the service deployement, change the service port to ESP and add annotation to `agones-allocator` service account to impersonate GCP service account.
48+
49+
Replace [GKE-PROJECT-ID] in `patch-agones-allocator.yaml` with your project ID before runing the scripts.
50+
51+
```
52+
kubectl patch deployment agones-allocator -n agones-system --patch-file patch-agones-allocator.yaml
53+
kubectl patch svc agones-allocator -n agones-system --type merge -p '{"spec":{"ports": [{"port": 443,"name":"https","targetPort":9443}]}}'
54+
kubectl annotate sa -n agones-system agones-allocator iam.gke.io/gcp-service-account=ae-esp-sa@[PROJECT-ID].iam.gserviceaccount.com
55+
```
56+
57+
## Terraform
58+
The terraform modules create resources in GCP:
59+
60+
```
61+
terraform apply \
62+
-var "project_id=[PROJECT-ID]" \
63+
-var "authorized_members=[\"serviceAccount:[SERVICE-ACCOUNT-EMAIL]\"]" \
64+
-var "clusters_info=[CLUSTERS-INFO]" \
65+
-var "workload-pool=[GKE-PROJECT-ID].svc.id.goog"
66+
```
67+
68+
`[CLUSTERS-INFO]` is in the form of `[{\"name\":\"cluster1\",\"endpoint\":\"34.83.14.82\",\"namespace\":\"default\",\"allocation_weight\":100},{...}]` deserializing to []ClusterInfo, defined in the `server/clusterselector.go`.
69+
70+
- The `name` is a unique randomly selected name for the cluster.
71+
- The `endpoint` is the `agones-allocator` external IP.
72+
- The `namespace` is the game server namespace.
73+
- The `allocation_weight` is a value between 0 and 100, which sets the relative allocation rate a cluster receives compared to other clusters. By setting weight to zero, a cluster stops receiving allocation requests.
74+
75+
`[SERVICE-ACCOUNT-EMAIL]` is the service account to be granted access the Allocation Endpoint. You need to have [the service account created](https://cloud.google.com/iam/docs/creating-managing-service-accounts) before running terraform.
76+
77+
## Server
78+
79+
The Allocation Endpoint proxy code is in `./server` folder. You can make changes and run the following to build and push the image to your own GCR repository:
80+
81+
```
82+
docker build --tag gcr.io/[PROJECT-ID]/allocation-endpoint-proxy:[VERSION] .
83+
docker push gcr.io/[PROJECT-ID]/allocation-endpoint-proxy:[VERSION]
84+
```
85+
86+
If you are building your own image, you can set `ae_proxy_image` terraform variable to your image.
87+
88+
## Client
89+
90+
The Allocation Endpoint client code is in `./client` folder. Get the Service Account Key for one of the Service Accounts in the list of `authorized_members` and put it under `sa_key.json`. Alternatively, you can leverage [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity) to retrieve the access token using default service account from metadata serer, when deploying your client in a GCP solution, e.g. GKE.
91+
92+
```
93+
go run *.go --url=[CLOUD-RUN-ENDPOINT]
94+
95+
```
96+
97+
`[CLOUD-RUN-ENDPOINT]` is the cloud run endpoint FQDN printed out after running the terraform. Leave out the scheme when setting the value e.g. `allocation-endpoint-proxy-<code>.a.run.app`.
98+
99+
## Future considerations
100+
- Requests using this example goes to public IP. For clusters in the same project you can instead leverage VPC with private IPs and remove dependency to the Service Account and Secret Manager to issue JWT in the proxy.
101+
- The proxy is skipping server cert validation. When you create a valid TLS cert, remove `InsecureSkipVerify: true``.
102+
- The solution should be compatible with non-GKE Agones clusters, but has not been tested.
103+
38 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module agones.dev/agones/examples/allocation-endpoint/client
2+
3+
go 1.18
4+
5+
require (
6+
agones.dev/agones v1.21.0
7+
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
8+
google.golang.org/api v0.69.0
9+
google.golang.org/grpc v1.44.0
10+
)
11+
12+
require (
13+
cloud.google.com/go/compute v1.2.0 // indirect
14+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
15+
github.com/golang/protobuf v1.5.2 // indirect
16+
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
17+
go.opencensus.io v0.23.0 // indirect
18+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
19+
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
20+
golang.org/x/text v0.3.7 // indirect
21+
google.golang.org/appengine v1.6.7 // indirect
22+
google.golang.org/genproto v0.0.0-20220211171837-173942840c17 // indirect
23+
google.golang.org/protobuf v1.27.1 // indirect
24+
)

0 commit comments

Comments
 (0)