Skip to content

Commit 5f8a46a

Browse files
authored
Merge pull request agones-dev#35 from googleprivate/feature/cpp-sdk
C++ SDK implementation, example and doc
2 parents dd27d28 + cb8dc9f commit 5f8a46a

21 files changed

Lines changed: 1462 additions & 60 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
!.gitignore
1717
*.iml
1818
bin
19+
*.o

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
Agon is a library for running dedicated game servers on [Kubernetes](https://kubernetes.io).
44

55
## Disclaimer
6-
This software is currenty alpha, and subject to change. Not to be used in production systems.
6+
This software is currently alpha, and subject to change. Not to be used in production systems.
77

8-
## Roadmap for 0.1 release
9-
- Develop a [Custom Resource Defintion](https://kubernetes.io/docs/concepts/api-extension/custom-resources/#customresourcedefinitions) for dedicated game server
10-
- Sidecar for managing the DGS lifecycle and recorded status, e.g. registering the port the server has started on
11-
- A Kubernetes operator that registers the CRD, and creates a Pod with the DGS in it, with the accompanying sidecar for system registration.
12-
- A basic client library for integration with a DGS
13-
- Simple example code
14-
- Documentation of the above
8+
## Major Features
9+
- Be able to define a `GameServer` within Kubernetes - either through yaml or the via API
10+
- Manage GameServer lifecycles - including health checking and connection information.
11+
- Client SDKs for integration with dedicated game servers to work with Agon.
1512

1613
## Requirements
1714
- Requires a Kubernetes cluster of version 1.8+
1815
- Open the firewall access for the range of ports that Game Servers can be connected to in the cluster.
16+
- Game Servers must have the [project SDK](sdks) integrated, to manage Game Server state, health checking, etc.
1917

2018
## Installation
2119
`kubectl apply -f install.yaml`
@@ -27,9 +25,11 @@ _Note:_ There has yet to be a release of Agon, so you will need to edit the `ins
2725
development release or [build from source](build/README.md)
2826

2927
## Usage
30-
See the [examples](./examples) directory
28+
See the [sdks](sdks) and [examples](examples) directories.
29+
30+
More documentation forthcoming.
3131

32-
## Development
32+
## Development and Contribution
3333
See the tools in the [build](build/README.md) directory for testing and building Agon from source.
3434

3535
## Licence

build/Makefile

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ go_version_flags = -ldflags "-X github.com/agonio/agon/pkg.Version=$(VERSION)"
6868
# |___/
6969

7070
# build all
71-
build: build-gameservers-controller-image build-gameservers-sidecar-image
71+
build: build-images build-sdks
72+
73+
# build the docker images
74+
build-images: build-gameservers-controller-image build-gameservers-sidecar-image
75+
76+
#build all the sdks
77+
build-sdks: build-sdk-cpp
7278

7379
# Run all tests
7480
test: ensure-image
@@ -105,13 +111,18 @@ build-gameservers-sidecar-binary: ensure-image
105111
build-gameservers-sidecar-image: ensure-image build-gameservers-sidecar-binary
106112
docker build $(agon_path)/gameservers/sidecar/ --tag=$(sidecar_tag)
107113

114+
# Build the cpp sdk linux archive
115+
build-sdk-cpp: ensure-image
116+
docker run --rm $(common_mounts) -w $(mount_path)/sdks/cpp --entrypoint make $(build_tag) build install archive VERSION=$(VERSION)
117+
108118
# push the gameservers sidecar image
109119
push-gameservers-sidecar-image: ensure-image
110120
docker push $(sidecar_tag)
111121

112-
# Generate the sidecar gRPC code
113-
gen-gameservers-sidecar-grpc: ensure-image
122+
# Generate the SDK gRPC server and client code
123+
gen-gameservers-sdk-grpc: ensure-image
114124
docker run --rm $(common_mounts) --entrypoint="/root/gen-grpc-go.sh" $(build_tag)
125+
docker run --rm $(common_mounts) --entrypoint="/root/gen-grpc-cpp.sh" $(build_tag)
115126

116127
# Generate the client for our CustomResourceDefinition
117128
gen-crd-client: ensure-image
@@ -137,14 +148,12 @@ godoc:
137148
docker run -p 8888:8888 --rm $(common_mounts) -v $(build_path)/.index:/root/.index \
138149
--entrypoint=godoc $(build_tag) -http=":8888" -index=true -index_files=/root/.index
139150

140-
.PHONY: build-image
141-
142151
# Creates the build docker image
143-
build-image:
152+
build-build-image:
144153
docker build --tag=$(build_tag) $(build_path)/build-image
145154

146155
# Deletes the local build docker image
147-
clean-image:
156+
clean-build-image:
148157
docker rmi $(build_tag)
149158

150159
ensure-config:
@@ -154,7 +163,7 @@ ensure-config:
154163
ensure-image: ensure-config
155164
@if [ -z $$(docker images -q $(build_tag)) ]; then\
156165
echo "Could not find $(build_tag) image. Building...";\
157-
$(MAKE) build-image;\
166+
$(MAKE) build-build-image;\
158167
fi
159168

160169
# Initialise the gcloud login and project configuration, if you are working with GCP
@@ -164,23 +173,23 @@ gcloud-init: ensure-config
164173
--entrypoint="gcloud" $(build_tag) init
165174

166175
# Creates and authenticates a small, 3 node GKE cluster to work against
167-
gcloud-test-cluster:
176+
gcloud-test-cluster: ensure-image
168177
docker run --rm -it $(common_mounts) \
169178
--entrypoint="gcloud" $(build_tag) \
170179
deployment-manager deployments create test-cluster --config=$(mount_path)/build/gke-test-cluster/deployment.yml
171180
$(MAKE) gcloud-auth-cluster
172181

173182
# Pulls down authentication information for kubectl against a cluster, name can be specified through CLUSTER_NAME
174183
# (defaults to 'test-cluster')
175-
gcloud-auth-cluster:
184+
gcloud-auth-cluster: ensure-image
176185
docker run --rm $(common_mounts) --entrypoint="gcloud" $(build_tag) config set container/cluster $(CLUSTER_NAME)
177186
docker run --rm $(common_mounts) --entrypoint="gcloud" $(build_tag) config set compute/zone \
178187
`grep zone: $(build_path)/gke-test-cluster/deployment.yml | sed 's/zone: //'`
179188
docker run --rm $(common_mounts) --entrypoint="gcloud" $(build_tag) container clusters get-credentials $(CLUSTER_NAME)
180189

181190
# authenticate our docker configuration so that you can do a docker push directly
182191
# to the gcr.io repository
183-
gcloud-auth-docker:
192+
gcloud-auth-docker: ensure-image
184193
-sudo rm -rf /tmp/gcloud-auth-docker
185194
mkdir -p /tmp/gcloud-auth-docker
186195
-cp ~/.dockercfg /tmp/gcloud-auth-docker

build/README.md

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Build
1+
# Developing, Testing and Building Agon
22

33
Tooling for building and developing against Agon, with only dependencies being
44
[Make](https://www.gnu.org/software/make/) and [Docker](https://www.docker.com)
@@ -7,25 +7,48 @@ Rather than installing all the dependencies locally, you can test and build Agon
77
built from the Dockerfile in this directory. There is an accompanying Makefile for all the common
88
tasks you may wish to accomplish.
99

10+
<!-- ToC start -->
11+
## Table of Contents
12+
13+
1. [GOPATH](#gopath)
14+
1. [Testing and Building](#testing-and-building)
15+
1. [Running a Test Google Kubernetes Engine Cluster](#running-a-test-google-kubernetes-engine-cluster)
16+
1. [Running a Test Minikube cluster](#running-a-test-minikube-cluster)
17+
1. [Next Steps](#next-steps)
18+
1. [Make Variable Reference](#make-variable-reference)
19+
1. [VERSION](#version)
20+
1. [REGISTRY](#registry)
21+
1. [KUBECONFIG](#kubeconfig)
22+
1. [CLUSTER_NAME](#cluster_name)
23+
1. [Make Target Reference](#make-target-reference)
24+
1. [Development Targets](#development-targets)
25+
1. [Build Image Targets](#build-image-targets)
26+
1. [Google Cloud Platform](#google-cloud-platform)
27+
<!-- ToC end -->
28+
1029
## GOPATH
1130

1231
This project should be cloned to the directory `$GOPATH/src/github.com/agonio/agon`
1332
for when you are developing locally, and require package resolution in your IDE.
1433

1534
This is not required if you are simply building using the `make` targets
1635

17-
### Testing and Building
36+
## Testing and Building
1837
Make sure you are in the `build` directory to start.
1938

2039
First, let's test all the code. To do this, run `make test`, which will execute all the unit tests for the codebase.
40+
2141
If you haven't run any of the `build` make targets before then this will also create the build image, and then run the tests.
42+
Building the `build-image` may take a few minutes to download all the dependencies, so feel
43+
free to make cup of tea or coffee at this point. ☕️
2244

2345
The build image is only created the first time one of the make targets is executed, and will only rebuild if the build
2446
Dockerfile has changed.
2547

2648
Assuming that the tests all pass, let's go ahead an compile the code and build the Docker images that Agon consists of.
2749

28-
To compile the code and create the Docker images run `make build`. This will compile the code and create the docker image.
50+
To compile the code, create the Docker images, and compile and archive the sdks,
51+
run `make build`. This will compile the code and create the docker image.
2952
You may note that the docker image is tagged with a concatenation of the upcoming release number and short git hash
3053
for the current commit. This has also been set in the code itself, so that it can be seen in log statements.
3154

@@ -108,18 +131,27 @@ All targets will create the build image if it is not present.
108131
Targets for developing with the build image
109132

110133
#### `make build`
134+
Build all the images required for Agon, as well as the SDKs
135+
136+
#### `make build-images`
111137
Build all the images required for Agon
112138

139+
#### `make build-sdks`
140+
Build all the sdks required for Agon
141+
142+
#### `make build-sdk-cpp`
143+
Build the cpp sdk static and dynamic libraries (linux libraries only)
144+
113145
#### `make test`
114146
Run all tests
115147

116-
### `make push`
148+
#### `make push`
117149
Pushes all built images up to the `$(REGISTRY)`
118150

119-
### `make shell`
151+
#### `make shell`
120152
Run a bash shell with the developer tools (go tooling, kubectl, etc) and source code in it.
121153

122-
### `make godoc`
154+
#### `make godoc`
123155
Run a container with godoc (search index enabled)
124156

125157
#### `make build-gameservers-controller-image`
@@ -131,37 +163,37 @@ Compile the gameserver sidecar and then build the docker image
131163
#### `make gen-crd-client`
132164
Generate the Custom Resource Definition client(s)
133165

134-
#### `make gen-gameservers-sidecar-grpc`
135-
Generate the gRPC sidecar Server and Client
166+
#### `make gen-gameservers-sdk-grpc`
167+
Generate the SDK gRPC server and client code
136168

137169
### Build Image Targets
138170

139171
Targets for building the build image
140172

141-
### `make clean-config`
173+
#### `make clean-config`
142174
Cleans the kubernetes and gcloud configurations
143175

144-
### `make clean-image`
176+
#### `make clean-build-image`
145177
Deletes the local build docker image
146178

147-
### `make build-image`
179+
#### `make build-build-image`
148180
Creates the build docker image
149181

150-
## Google Cloud Platform
182+
### Google Cloud Platform
151183

152184
A set of utilities for setting up a Container Engine cluster on Google Cloud Platform,
153185
since it's an easy way to get a test cluster working with Kubernetes.
154186

155-
### `make gcloud-init`
187+
#### `make gcloud-init`
156188
Initialise the gcloud login and project configuration, if you are working with GCP
157189

158-
### `make gcloud-test-cluster`
190+
#### `make gcloud-test-cluster`
159191
Creates and authenticates a small, 3 node GKE cluster to work against
160192

161-
### `make gcloud-auth-cluster`
193+
#### `make gcloud-auth-cluster`
162194
Pulls down authentication information for kubectl against a cluster, name can be specified through CLUSTER_NAME
163195
(defaults to 'test-cluster')
164196

165-
### `make gcloud-auth-docker`
197+
#### `make gcloud-auth-docker`
166198
Creates a short lived access to Google Cloud container repositories, so that you are able to call
167-
`docker push` directly. Useful when used in combination with `make push` command.
199+
`docker push` directly. Useful when used in combination with `make push` command.

build/build-image/Dockerfile

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,42 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.9.2
15+
# compiling proto + grpc takes an exceptionally long time
16+
# so we'll use that as the base.
17+
FROM grpc/cxx:1.8
1618

17-
RUN apt-get update && apt-get install -y wget unzip bash-completion rsync
19+
RUN apt-get update && \
20+
apt-get install -y wget rsync make python bash-completion && \
21+
apt-get clean
1822

19-
WORKDIR /
23+
# install go
24+
WORKDIR /usr/local
25+
ENV GO_VERSION=1.9.2
26+
ENV GOPATH /go
27+
RUN wget -q https://redirector.gvt1.com/edgedl/go/go${GO_VERSION}.linux-amd64.tar.gz && \
28+
tar -xzf go${GO_VERSION}.linux-amd64.tar.gz && rm go${GO_VERSION}.linux-amd64.tar.gz && mkdir ${GOPATH}
2029

2130
# install gcloud + kubectl, because it's an easy way to test/dev against kubernetes.
22-
RUN wget -q https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.zip && unzip -q google-cloud-sdk.zip && rm google-cloud-sdk.zip
23-
RUN /google-cloud-sdk/install.sh --usage-reporting=true --path-update=true --bash-completion=true --rc-path=/root/.bashrc
24-
ENV PATH /google-cloud-sdk/bin:$PATH
31+
WORKDIR /opt
32+
RUN wget -q https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.zip && unzip -q google-cloud-sdk.zip && \
33+
rm google-cloud-sdk.zip && \
34+
/opt/google-cloud-sdk/install.sh --usage-reporting=true --path-update=true --bash-completion=true --rc-path=/root/.bashrc
2535

36+
# update the path for both go and gcloud
37+
ENV PATH /usr/local/go/bin:/opt/google-cloud-sdk/bin:$PATH
38+
39+
# RUN gcloud components update
2640
RUN gcloud components update && gcloud components install kubectl
2741
RUN echo "source <(kubectl completion bash)" >> /root/.bashrc
2842

29-
# install protoc for grpc
30-
ENV PB_VER 3.5.0
31-
ENV PB_URL https://github.com/google/protobuf/releases/download/v${PB_VER}/protoc-${PB_VER}-linux-x86_64.zip
32-
RUN mkdir -p /tmp/protoc && \
33-
curl -L ${PB_URL} > /tmp/protoc/protoc.zip && \
34-
cd /tmp/protoc && \
35-
unzip protoc.zip && \
36-
cp /tmp/protoc/bin/protoc /usr/local/bin && \
37-
cp -R /tmp/protoc/include/* /usr/local/include && \
38-
chmod go+rx /usr/local/bin/protoc && \
39-
cd /tmp && \
40-
rm -r /tmp/protoc
41-
42-
# install go tooling for building and testing
43+
# install go tooling for development, building and testing
4344
RUN go get -u github.com/golang/dep/cmd/dep && \
4445
go get -u github.com/alecthomas/gometalinter && \
4546
go get -u github.com/golang/protobuf/protoc-gen-go && \
4647
/go/bin/gometalinter --install
4748

4849
# install the release branch of the code generator tools
49-
RUN cd /go/src && mkdir -p k8s.io && cd k8s.io && \
50+
RUN mkdir -p /go/src && cd /go/src && mkdir -p k8s.io && cd k8s.io && \
5051
git clone -b release-1.8 --depth=3 https://github.com/kubernetes/code-generator.git
5152

5253
# make sure we keep the path to go
@@ -56,4 +57,4 @@ RUN echo "export PATH=/usr/local/go/bin:/go/bin/:\$PATH" >> /root/.bashrc
5657
COPY *.sh /root/
5758
RUN chmod +x /root/*.sh
5859

59-
ENTRYPOINT kubectl
60+
WORKDIR /go

build/build-image/gen-grpc-cpp.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
cd /go/src/github.com/agonio/agon
18+
protoc -I . --grpc_out=./sdks/cpp --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` sdk.proto
19+
protoc -I . --cpp_out=./sdks/cpp sdk.proto
20+
mkdir /tmp/cpp
21+
ls ./sdks/cpp | xargs -I@ bash -c "cat ./build/boilerplate.go.txt ./sdks/cpp/@ >> /tmp/cpp/@"
22+
# already has a header, so we'll remove it
23+
rm /tmp/cpp/sdk.grpc.pb.h
24+
mv /tmp/cpp/* ./sdks/cpp/
25+

examples/cpp-simple/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2017 Google Inc. 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+
FROM debian:stretch
16+
RUN useradd -m server
17+
18+
COPY ./bin/server-static /home/server/server-static
19+
RUN chown -R server /home/server && \
20+
chmod o+x /home/server/server-static
21+
22+
USER server
23+
ENTRYPOINT /home/server/server-static

0 commit comments

Comments
 (0)