Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go SDK for integration with Game Servers. #20

Merged
merged 1 commit into from
Dec 13, 2017
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.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
72 changes: 69 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"

[[constraint]]
name = "google.golang.org/grpc"
version = "1.8.0"
34 changes: 25 additions & 9 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
#

NAME = agon-build
VERSION = 0.1
VERSION ?= 0.1
TAG = $(NAME):$(VERSION)
CLUSTER_NAME = test-cluster
KUBECONFIG = $(build_path)/.kube
REPOSITORY = gcr.io/agon-images
CLUSTER_NAME ?= test-cluster
KUBECONFIG ?= $(build_path)/.kube
REPOSITORY ?= gcr.io/agon-images

# Directory that this Makefile is in.
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
Expand All @@ -42,6 +42,7 @@ common_mounts = -v $(build_path)/.config/gcloud:/root/.config/gcloud \
-v $(agon_path):$(mount_path)

gameserver_tag = $(REPOSITORY)/gameservers-controller:$(VERSION)
sidecar_tag = $(REPOSITORY)/gameservers-sidecar:$(VERSION)

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
Expand All @@ -50,12 +51,14 @@ gameserver_tag = $(REPOSITORY)/gameservers-controller:$(VERSION)
# |_|\__,_|_| \__, |\___|\__|___/
# |___/

# build all
build: build-gameservers-controller-image build-gameservers-sidecar-image

# Run all tests
test: ensure-image
docker run --rm $(common_mounts) --entrypoint=go $(TAG) test -race $(agon_package)/...

# Build a static binary for the gameserver operator
# CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Build a static binary for the gameserver controller
build-gameservers-controller-binary: ensure-image
docker run --rm -e "CGO_ENABLED=0" $(common_mounts) --entrypoint=go $(TAG) build \
-o $(mount_path)/gameservers/controller/bin/controller -a -installsuffix cgo $(agon_package)/gameservers/controller
Expand All @@ -64,9 +67,22 @@ build-gameservers-controller-binary: ensure-image
build-gameservers-controller-image: ensure-image build-gameservers-controller-binary
docker build $(agon_path)/gameservers/controller/ --tag=$(gameserver_tag)

# Generate the CRD client
gen-client: ensure-image
docker run --rm $(common_mounts) --entrypoint="/root/gen-client.sh" $(TAG)
# build the static binary for the gamesever sidecar
build-gameservers-sidecar-binary: ensure-image
docker run --rm -e "CGO_ENABLED=0" $(common_mounts) --entrypoint=go $(TAG) build \
-o $(mount_path)/gameservers/sidecar/bin/sidecar -a -installsuffix cgo $(agon_package)/gameservers/sidecar

# Build the image for the gameserver sidecar
build-gameservers-sidecar-image: ensure-image build-gameservers-sidecar-binary
docker build $(agon_path)/gameservers/sidecar/ --tag=$(sidecar_tag)

# Generate the sidecar gRPC code
gen-gameservers-sidecar-grpc: ensure-image
docker run --rm $(common_mounts) --entrypoint="/root/gen-grpc-go.sh" $(TAG)

# Generate the client for our CustomResourceDefinition
gen-crd-client: ensure-image
docker run --rm $(common_mounts) --entrypoint="/root/gen-crd-client.sh" $(TAG)
docker run --rm $(common_mounts) --entrypoint=goimports $(TAG) -w $(mount_path)/pkg

# Run a bash shell with the developer tools in it. (Creates the image if it doesn't exist)
Expand Down
5 changes: 4 additions & 1 deletion build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ tasks you may wish to accomplish.

Targets for developing with the build image

### `make build-gameserver-image`
### `make build-gameservers-controller-image`
Compile the gameserver controller and then build the docker image

### `make build-gameservers-sidecar-image`
Compile the gameserver sidecar and then build the docker image

### `make test`
Run all tests

Expand Down
File renamed without changes.
18 changes: 16 additions & 2 deletions build/build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,31 @@ ENV PATH /google-cloud-sdk/bin:$PATH
RUN gcloud components update && gcloud components install kubectl
RUN echo "source <(kubectl completion bash)" >> /root/.bashrc

# install protoc for grpc
ENV PB_VER 3.5.0
ENV PB_URL https://github.com/google/protobuf/releases/download/v${PB_VER}/protoc-${PB_VER}-linux-x86_64.zip
RUN mkdir -p /tmp/protoc && \
curl -L ${PB_URL} > /tmp/protoc/protoc.zip && \
cd /tmp/protoc && \
unzip protoc.zip && \
cp /tmp/protoc/bin/protoc /usr/local/bin && \
cp -R /tmp/protoc/include/* /usr/local/include && \
chmod go+rx /usr/local/bin/protoc && \
cd /tmp && \
rm -r /tmp/protoc

# install go tooling for building and testing
RUN go get -u github.com/golang/dep/cmd/dep && \
go get -u github.com/alecthomas/gometalinter && \
go get -u k8s.io/code-generator/... && \
go get -u github.com/golang/protobuf/protoc-gen-go && \
/go/bin/gometalinter --install

# make sure we keep the path to go
RUN echo "export PATH=/usr/local/go/bin:/go/bin/:\$PATH" >> /root/.bashrc

# script for generating the CRD scripts
COPY gen-client.sh /root/gen-client.sh
RUN chmod +x /root/gen-client.sh
COPY *.sh /root/
RUN chmod +x /root/*.sh

ENTRYPOINT kubectl
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ rsync -r /go/src/github.com/agonio/agon/vendor/k8s.io/ /go/src/k8s.io/
/go/src/k8s.io/code-generator/generate-groups.sh "all" \
github.com/agonio/agon/pkg/client \
github.com/agonio/agon/pkg/apis stable:v1alpha1 \
--go-header-file=/go/src/github.com/agonio/agon/build/crd.boilerplate.go.txt
--go-header-file=/go/src/github.com/agonio/agon/build/boilerplate.go.txt
22 changes: 22 additions & 0 deletions build/build-image/gen-grpc-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

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

cd /go/src/github.com/agonio/agon
protoc -I . sdk.proto --go_out=plugins=grpc:sdks/go
cat ./build/boilerplate.go.txt ./sdks/go/sdk.pb.go >> ./sdk.pb.go
goimports -w ./sdk.pb.go
cp ./sdk.pb.go ./gameservers/sidecar/sdk
mv ./sdk.pb.go ./sdks/go
4 changes: 2 additions & 2 deletions examples/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ metadata:
name: "gds-example"
spec:
# if there is more than one container, specify which one is the game server
container: exampleServer
container: example-server
# `static` is the only current option. Dynamic port allocated will come in future releases.
# When `static` is the policy specified, `hostPort` is required, to specify the port that game clients will connect to
portPolicy: "static"
Expand All @@ -48,6 +48,6 @@ spec:
# Pod Specification
spec:
containers:
- name: exampleServer
- name: example-server
image: gcr.io/agon/test-server:0.1
imagePullPolicy: Always
19 changes: 19 additions & 0 deletions examples/simple-udp/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"log"
"net"
"os"

"github.com/agonio/agon/sdks/go"
)

const exit = "EXIT"
Expand All @@ -40,6 +42,18 @@ func main() {
}
defer conn.Close()

log.Print("Creating SDK instance")
s, err := sdk.NewSDK()
if err != nil {
log.Fatalf("Could not connect to sdk: %v", err)
}
log.Print("Marking this server as ready")
// This tells Agon that the server is ready to receive connections.
err = s.Ready()
if err != nil {
log.Fatalf("Could not send ready message")
}

b := make([]byte, 1024)
for {
n, sender, err := conn.ReadFrom(b)
Expand All @@ -51,6 +65,11 @@ func main() {
log.Printf("Received packet from %v: %v", sender.String(), txt)
if txt == exit {
log.Printf("Received EXIT command. Exiting.")
// This tells Agon to shutdown this Game Server
err := s.Shutdown()
if err != nil {
log.Printf("Could not shutdown")
}
os.Exit(0)
}

Expand Down
Loading