Skip to content

Commit

Permalink
Implement SDK SetLabel and SetAnnotation functionality
Browse files Browse the repository at this point in the history
This implements new functions in the SDK:

- SetLabel(key, value) - that lets you set a label on the backing `GameServer`
- SetAnnotation(key, value) - that lets you set an annotation on the backing
`GameServer`

All keys are prefixed with "stable.agones.dev/sdk-" to maintain isolation.

Closes googleforgames#279
  • Loading branch information
markmandel authored and Victor Prodan committed Sep 3, 2018
1 parent d159a6e commit b4cb67c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/gameservers/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"sync"
"time"
"sync"

"agones.dev/agones/pkg/apis/stable/v1alpha1"
"agones.dev/agones/pkg/sdk"
Expand Down
54 changes: 54 additions & 0 deletions pkg/gameservers/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,60 @@ func TestLocalSDKServerSetAnnotation(t *testing.T) {
}
}

func TestLocalSDKServerSetLabel(t *testing.T) {
ctx := context.Background()
e := &sdk.Empty{}
l := NewLocalSDKServer()
kv := &sdk.KeyValue{Key: "foo", Value: "bar"}

stream := newGameServerMockStream()
go func() {
err := l.WatchGameServer(e, stream)
assert.Nil(t, err)
}()

_, err := l.SetLabel(ctx, kv)
assert.Nil(t, err)

gs, err := l.GetGameServer(ctx, e)
assert.Nil(t, err)
assert.Equal(t, gs.ObjectMeta.Labels[metadataPrefix+"foo"], "bar")

select {
case msg := <-stream.msgs:
assert.Equal(t, msg.ObjectMeta.Labels[metadataPrefix+"foo"], "bar")
case <-time.After(2 * l.watchPeriod):
assert.FailNow(t, "timeout on receiving messages")
}
}

func TestLocalSDKServerSetAnnotation(t *testing.T) {
ctx := context.Background()
e := &sdk.Empty{}
l := NewLocalSDKServer()
kv := &sdk.KeyValue{Key: "bar", Value: "foo"}

stream := newGameServerMockStream()
go func() {
err := l.WatchGameServer(e, stream)
assert.Nil(t, err)
}()

_, err := l.SetAnnotation(ctx, kv)
assert.Nil(t, err)

gs, err := l.GetGameServer(ctx, e)
assert.Nil(t, err)
assert.Equal(t, gs.ObjectMeta.Annotations[metadataPrefix+"bar"], "foo")

select {
case msg := <-stream.msgs:
assert.Equal(t, msg.ObjectMeta.Annotations[metadataPrefix+"bar"], "foo")
case <-time.After(2 * l.watchPeriod):
assert.FailNow(t, "timeout on receiving messages")
}
}

func TestLocalSDKServerWatchGameServer(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 2 additions & 3 deletions sdks/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ func (s *SDK) WatchGameServer(f GameServerCallback) error {
var gs *sdk.GameServer
gs, err = stream.Recv()
if err != nil {
if err != io.EOF {
fmt.Printf("error watching GameServer: %s\n", err.Error())
} else {
if err == io.EOF {
fmt.Println("gameserver event stream EOF received")
return
}
fmt.Printf("error watching GameServer: %s\n", err.Error())
// This is to wait for the reconnection, and not peg the CPU at 100%
time.Sleep(time.Second)
continue
Expand Down

0 comments on commit b4cb67c

Please sign in to comment.