Skip to content

Commit

Permalink
SDK GameServer() function for retrieving backing GameServer configu…
Browse files Browse the repository at this point in the history
…ration.

Extended the gRPC SDK to include a `GameServer()` function for both Go, C++
and REST by extending the backing `sdk.proto` and implementing the functionality
in the sdk sidecar.

This is the start of work needed for googleforgames#277.
  • Loading branch information
markmandel committed Jul 17, 2018
1 parent 2cbd855 commit f25990b
Show file tree
Hide file tree
Showing 37 changed files with 7,583 additions and 115 deletions.
2 changes: 2 additions & 0 deletions docs/governance/templates/release_issue.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and copy it into a release issue. Fill in relevent values, found inside {}
- [ ] Run `make gen-changelog` to generate the CHANGELOG.md (if release candidate `make gen-changelog RELEASE_VERSION={version}.rc`)
- [ ] Ensure the [helm `tag` value][values] is correct (should be the {version} if a full release, {version}.rc if release candidate)
- [ ] Run `make gen-install`
- [ ] Ensure all example images exist on gcr.io/agones-images
- [ ] If full release, update documentation with updated example images tags
- [ ] If RC release, update all ⚠️⚠️⚠️ warnings to: "**This is currently a release candidate feature**"
- [ ] If full release, remove all instances of "⚠️⚠️⚠️ **This is currently a development feature and has not been released** ⚠️⚠️⚠️"
- [ ] If full release, update install docs with the new release version
Expand Down
45 changes: 44 additions & 1 deletion docs/sdk_rest_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,47 @@ Call when the GameServer session is over and it's time to shut down

```bash
$ curl -d "{}" -H "Content-Type: application/json" -X POST http://localhost:59358/shutdown
```
```

### GameServer

⚠️⚠️⚠️ **/gameserver is currently a development feature and has not been released** ⚠️⚠️⚠️

Call when you want to retrieve the backing `GameServer` configuration details

- Path: `/gameserver`
- Method: `GET`

```bash
$ curl -H "Content-Type: application/json" -X GET http://localhost:59358/gameserver
```

Response:
```json
{
"object_meta": {
"name": "local",
"namespace": "default",
"uid": "1234",
"resource_version": "v1",
"generation": "1",
"creation_timestamp": "1531795395",
"annotations": {
"annotation": "true"
},
"labels": {
"islocal": "true"
}
},
"status": {
"state": "Ready",
"address": "127.0.0.1",
"ports": [
{
"name": "default",
"port": 7777
}
]
}
}
```
2 changes: 1 addition & 1 deletion examples/cpp-simple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ REPOSITORY = gcr.io/agones-images
# Directory that this Makefile is in.
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
server_tag = $(REPOSITORY)/cpp-simple-server:0.1
server_tag = $(REPOSITORY)/cpp-simple-server:0.2

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp-simple/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ spec:
spec:
containers:
- name: cpp-simple
image: gcr.io/agones-images/cpp-simple-server:0.1
image: gcr.io/agones-images/cpp-simple-server:0.2
# imagePullPolicy: Always # add for development
2 changes: 1 addition & 1 deletion examples/cpp-simple/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ spec:
spec:
containers:
- name: cpp-simple
image: gcr.io/agones-images/cpp-simple-server:0.1
image: gcr.io/agones-images/cpp-simple-server:0.2
# imagePullPolicy: Always # add for development
12 changes: 12 additions & 0 deletions examples/cpp-simple/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,20 @@ int main() {
std::cout << "Could not run Ready(): "+ status.error_message() + ". Exiting!" << std::endl;
return -1;
}

std::cout << "...marked Ready" << std::endl;

std::cout << "Getting GameServer details..." << std::endl;
stable::agones::dev::sdk::GameServer gameserver;
status = sdk->GameServer(&gameserver);

if (!status.ok()) {
std::cout << "Could not run GameServer(): "+ status.error_message() + ". Exiting!" << std::endl;
return -1;
}

std::cout << "GameServer name: " << gameserver.object_meta().name() << std::endl;

for (int i = 0; i < 10; i++) {
int time = i*10;
std::cout << "Running for " + std::to_string(time) + " seconds !" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-udp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ REPOSITORY = gcr.io/agones-images

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
server_tag = $(REPOSITORY)/udp-server:0.1
server_tag = $(REPOSITORY)/udp-server:0.2
package = agones.dev/agones/examples/simple-udp

# _____ _
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-udp/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

FROM alpine:3.7

RUN adduser -D server
RUN adduser -D server

COPY ./bin/server /home/server/server

Expand Down
2 changes: 1 addition & 1 deletion examples/simple-udp/server/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ spec:
spec:
containers:
- name: simple-udp
image: gcr.io/agones-images/udp-server:0.1
image: gcr.io/agones-images/udp-server:0.2
2 changes: 1 addition & 1 deletion examples/simple-udp/server/gameserver-legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ spec:
spec:
containers:
- name: simple-udp
image: gcr.io/agones-images/udp-server:0.1
image: gcr.io/agones-images/udp-server:0.2
2 changes: 1 addition & 1 deletion examples/simple-udp/server/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ spec:
spec:
containers:
- name: simple-udp
image: gcr.io/agones-images/udp-server:0.1
image: gcr.io/agones-images/udp-server:0.2
2 changes: 1 addition & 1 deletion examples/simple-udp/server/gameserverset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ spec:
spec:
containers:
- name: simple-udp
image: gcr.io/agones-images/udp-server:0.1
image: gcr.io/agones-images/udp-server:0.2
24 changes: 24 additions & 0 deletions examples/simple-udp/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"time"

"agones.dev/agones/sdks/go"
"encoding/json"
coresdk "agones.dev/agones/pkg/sdk"
)

// main starts a UDP server that received 1024 byte sized packets at at time
Expand Down Expand Up @@ -86,6 +88,9 @@ func readWriteLoop(conn net.PacketConn, stop chan struct{}, s *sdk.SDK) {
// turns off the health pings
case "UNHEALTHY":
close(stop)

case "GAMESERVER":
writeGameServerName(s, conn, sender)
}

// echo it back
Expand All @@ -96,6 +101,25 @@ func readWriteLoop(conn net.PacketConn, stop chan struct{}, s *sdk.SDK) {
}
}

// writes the GameServer name to the connection UDP stream
func writeGameServerName(s *sdk.SDK, conn net.PacketConn, sender net.Addr) {
var gs *coresdk.GameServer
gs, err := s.GameServer()
if err != nil {
log.Fatalf("Could not retrieve GameServer: %v", err)
}
var j []byte
j, err = json.Marshal(gs)
if err != nil {
log.Fatalf("error mashalling GameServer to JSON: %v", err)
}
log.Printf("GameServer: %s \n", string(j))
msg := "NAME: " + gs.ObjectMeta.Name + "\n"
if _, err = conn.WriteTo([]byte(msg), sender); err != nil {
log.Fatalf("Could not write to udp stream: %v", err)
}
}

// doHealth sends the regular Health Pings
func doHealth(sdk *sdk.SDK, stop <-chan struct{}) {
tick := time.Tick(2 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion install/helm/agones/templates/serviceaccounts/sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ metadata:
rules:
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers"]
verbs: ["get", "update"]
verbs: ["list", "update"]
---
{{- range .Values.gameservers.namespaces }}
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
2 changes: 1 addition & 1 deletion install/yaml/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ metadata:
rules:
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers"]
verbs: ["get", "update"]
verbs: ["list", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down
26 changes: 26 additions & 0 deletions pkg/gameservers/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package gameservers
import (
"io"

"time"

"agones.dev/agones/pkg/sdk"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -57,3 +59,27 @@ func (l *LocalSDKServer) Health(stream sdk.SDK_HealthServer) error {
logrus.Info("Health Ping Received!")
}
}

// GetGameServer returns a dummy game server.
func (l *LocalSDKServer) GetGameServer(context.Context, *sdk.Empty) (*sdk.GameServer, error) {
logrus.Info("getting GameServer details")
gs := &sdk.GameServer{
ObjectMeta: &sdk.GameServer_ObjectMeta{
Name: "local",
Namespace: "default",
Uid: "1234",
Generation: 1,
ResourceVersion: "v1",
CreationTimestamp: time.Now().Unix(),
Labels: map[string]string{"islocal": "true"},
Annotations: map[string]string{"annotation": "true"},
},
Status: &sdk.GameServer_Status{
State: "Ready",
Address: "127.0.0.1",
Ports: []*sdk.GameServer_Status_Port{{Name: "default", Port: 7777}},
},
}

return gs, nil
}
27 changes: 26 additions & 1 deletion pkg/gameservers/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"sync"
"testing"

"time"

"agones.dev/agones/pkg/sdk"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
Expand All @@ -39,7 +41,7 @@ func TestLocal(t *testing.T) {
stream := newMockStream()

go func() {
err := l.Health(stream)
err = l.Health(stream)
assert.Nil(t, err)
wg.Done()
}()
Expand All @@ -48,4 +50,27 @@ func TestLocal(t *testing.T) {
close(stream.msgs)

wg.Wait()

gs, err := l.GetGameServer(ctx, e)
assert.Nil(t, err)

expected := &sdk.GameServer{
ObjectMeta: &sdk.GameServer_ObjectMeta{
Name: "local",
Namespace: "default",
Uid: "1234",
Generation: 1,
ResourceVersion: "v1",
CreationTimestamp: time.Now().Unix(),
Labels: map[string]string{"islocal": "true"},
Annotations: map[string]string{"annotation": "true"},
},
Status: &sdk.GameServer_Status{
State: "Ready",
Address: "127.0.0.1",
Ports: []*sdk.GameServer_Status_Port{{Name: "default", Port: 7777}},
},
}

assert.Equal(t, expected, gs)
}
2 changes: 1 addition & 1 deletion pkg/gameservers/portallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package gameservers

import (
"fmt"
"strconv"
"sync"
"testing"
"time"
"strconv"

"agones.dev/agones/pkg/apis/stable/v1alpha1"
agtesting "agones.dev/agones/pkg/testing"
Expand Down
Loading

0 comments on commit f25990b

Please sign in to comment.