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

Adding Functionality table for go SDK #2081

Merged
merged 3 commits into from
Apr 29, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions sdks/go/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package sdk
import (
"context"

"agones.dev/agones/pkg/sdk/alpha"
"github.com/pkg/errors"

"google.golang.org/grpc"

"agones.dev/agones/pkg/sdk/alpha"
)

// Alpha is the struct for Alpha SDK functionality
// Alpha is the struct for Alpha SDK functionality.
type Alpha struct {
client alpha.SDKClient
}
Expand All @@ -41,29 +43,29 @@ func (a *Alpha) GetPlayerCapacity() (int64, error) {
return c.Count, errors.Wrap(err, "could not get player capacity")
}

// SetPlayerCapacity changes the player capacity to a new value
// SetPlayerCapacity changes the player capacity to a new value.
func (a *Alpha) SetPlayerCapacity(capacity int64) error {
_, err := a.client.SetPlayerCapacity(context.Background(), &alpha.Count{Count: capacity})
return errors.Wrap(err, "could not set player capacity")
}

// PlayerConnect increases the SDK’s stored player count by one, and appends this playerID to status.players.id.
// Returns true and adds the playerID to the list of playerIDs if the playerIDs was not already in the
// Will return true and add the playerID to the list of playerIDs if the playerIDs was not already in the
// list of connected playerIDs.
func (a *Alpha) PlayerConnect(id string) (bool, error) {
ok, err := a.client.PlayerConnect(context.Background(), &alpha.PlayerID{PlayerID: id})
return ok.Bool, errors.Wrap(err, "could not register connected player")
}

// PlayerDisconnect Decreases the SDK’s stored player count by one, and removes the playerID from status.players.id
// PlayerDisconnect Decreases the SDK’s stored player count by one, and removes the playerID from status.players.id.
// Will return true and remove the supplied playerID from the list of connected playerIDs if the
// playerID value exists within the list.
func (a *Alpha) PlayerDisconnect(id string) (bool, error) {
ok, err := a.client.PlayerDisconnect(context.Background(), &alpha.PlayerID{PlayerID: id})
return ok.Bool, errors.Wrap(err, "could not register disconnected player")
}

// GetPlayerCount returns the current player count
// GetPlayerCount returns the current player count.
func (a *Alpha) GetPlayerCount() (int64, error) {
count, err := a.client.GetPlayerCount(context.Background(), &alpha.Empty{})
return count.Count, errors.Wrap(err, "could not get player count")
Expand Down
6 changes: 4 additions & 2 deletions sdks/go/alpha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"testing"

"agones.dev/agones/pkg/sdk/alpha"
"github.com/stretchr/testify/assert"

"google.golang.org/grpc"

"agones.dev/agones/pkg/sdk/alpha"
)

func TestAlphaGetAndSetPlayerCapacity(t *testing.T) {
Expand Down Expand Up @@ -52,7 +54,7 @@ func TestAlphaGetAndSetPlayerCapacity(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, playerID, mock.playerDisconnected)

// put the player back in
// Put the player back in.
ok, err = a.PlayerConnect(playerID)
assert.NoError(t, err)
assert.True(t, ok)
Expand Down
38 changes: 18 additions & 20 deletions sdks/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package sdk is the Go game server sdk
// Package sdk is the Go game server sdk.
package sdk

import (
Expand All @@ -22,25 +22,28 @@ import (
"os"
"time"

"agones.dev/agones/pkg/sdk"
"github.com/pkg/errors"

"google.golang.org/grpc"

"agones.dev/agones/pkg/sdk"
)

// GameServerCallback is a function definition to be called
// when a GameServer CRD has been changed
// when a GameServer CRD has been changed.
type GameServerCallback func(gs *sdk.GameServer)

// SDK is an instance of the Agones SDK
// SDK is an instance of the Agones SDK.
type SDK struct {
client sdk.SDKClient
ctx context.Context
health sdk.SDK_HealthClient
alpha *Alpha
}

// NewSDK starts a new SDK instance, and connects to
// localhost on port 9357. Blocks until connection and handshake are made.
// NewSDK starts a new SDK instance, and connects to localhost
// on port "AGONES_SDK_GRPC_PORT" which by default is 9357.
// Blocks until connection and handshake are made.
// Times out after 30 seconds.
func NewSDK() (*SDK, error) {
p := os.Getenv("AGONES_SDK_GRPC_PORT")
Expand All @@ -51,7 +54,7 @@ func NewSDK() (*SDK, error) {
s := &SDK{
ctx: context.Background(),
}
// block for at least 30 seconds
// Block for at least 30 seconds.
ctx, cancel := context.WithTimeout(s.ctx, 30*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, addr, grpc.WithBlock(), grpc.WithInsecure())
Expand All @@ -64,13 +67,12 @@ func NewSDK() (*SDK, error) {
return s, errors.Wrap(err, "could not set up health check")
}

// Alpha returns the Alpha SDK
// Alpha returns the Alpha SDK.
func (s *SDK) Alpha() *Alpha {
return s.alpha
}

// Ready marks the Game Server as ready to
// receive connections
// Ready marks the Game Server as ready to receive connections.
func (s *SDK) Ready() error {
_, err := s.client.Ready(s.ctx, &sdk.Empty{})
return errors.Wrap(err, "could not send Ready message")
Expand All @@ -82,8 +84,7 @@ func (s *SDK) Allocate() error {
return errors.Wrap(err, "could not mark self as Allocated")
}

// Shutdown marks the Game Server as ready to
// shutdown
// Shutdown marks the Game Server as ready to shutdown.
func (s *SDK) Shutdown() error {
_, err := s.client.Shutdown(s.ctx, &sdk.Empty{})
return errors.Wrapf(err, "could not send Shutdown message")
Expand All @@ -97,29 +98,26 @@ func (s *SDK) Reserve(d time.Duration) error {
return errors.Wrap(err, "could not send Reserve message")
}

// Health sends a ping to the health
// check to indicate that this server is healthy
// Health sends a ping to the sidecar health check to indicate that this Game Server is healthy.
func (s *SDK) Health() error {
return errors.Wrap(s.health.Send(&sdk.Empty{}), "could not send Health ping")
}

// SetLabel sets a metadata label on the `GameServer` with the prefix
// agones.dev/sdk-
// SetLabel sets a metadata label on the `GameServer` with the prefix "agones.dev/sdk-".
func (s *SDK) SetLabel(key, value string) error {
kv := &sdk.KeyValue{Key: key, Value: value}
_, err := s.client.SetLabel(s.ctx, kv)
return errors.Wrap(err, "could not set label")
}

// SetAnnotation sets a metadata annotation on the `GameServer` with the prefix
// agones.dev/sdk-
// SetAnnotation sets a metadata annotation on the `GameServer` with the prefix "agones.dev/sdk-".
func (s *SDK) SetAnnotation(key, value string) error {
kv := &sdk.KeyValue{Key: key, Value: value}
_, err := s.client.SetAnnotation(s.ctx, kv)
return errors.Wrap(err, "could not set annotation")
}

// GameServer retrieve the GameServer details
// GameServer retrieve the GameServer details.
func (s *SDK) GameServer() (*sdk.GameServer, error) {
gs, err := s.client.GetGameServer(s.ctx, &sdk.Empty{})
return gs, errors.Wrap(err, "could not retrieve gameserver")
Expand All @@ -144,7 +142,7 @@ func (s *SDK) WatchGameServer(f GameServerCallback) error {
return
}
_, _ = fmt.Fprintf(os.Stderr, "error watching GameServer: %s\n", err.Error())
// This is to wait for the reconnection, and not peg the CPU at 100%
// This is to wait for the reconnection, and not peg the CPU at 100%.
time.Sleep(time.Second)
continue
}
Expand Down
5 changes: 3 additions & 2 deletions sdks/go/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"testing"
"time"

"agones.dev/agones/pkg/sdk"
"github.com/stretchr/testify/assert"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"agones.dev/agones/pkg/sdk"
)

func TestSDK(t *testing.T) {
Expand All @@ -35,7 +37,6 @@ func TestSDK(t *testing.T) {
health: sm.hm,
}

// gate
assert.False(t, sm.ready)
assert.False(t, sm.shutdown)
assert.False(t, sm.hm.healthy)
Expand Down
21 changes: 21 additions & 0 deletions site/content/en/docs/Guides/Client SDKs/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ description: "This is the Go version of the Agones Game Server Client SDK. "

Check the [Client SDK Documentation]({{< relref "_index.md" >}}) for more details on each of the SDK functions and how to run the SDK locally.

## SDK Functionality

| Area | Action | Implemented |
|-----------------|---------------------|-------------|
| Lifecycle | Ready | ✔️ |
| Lifecycle | Health | ✔️ |
| Lifecycle | Reserve | ✔️ |
| Lifecycle | Allocate | ✔️ |
| Lifecycle | Shutdown | ✔️ |
| Configuration | GameServer | ✔️ |
| Configuration | Watch | ✔️ |
| Metadata | SetAnnotation | ✔️ |
| Metadata | SetLabel | ✔️ |
| Player Tracking | GetConnectedPlayers | ✔️ |
| Player Tracking | GetPlayerCapacity | ✔️ |
| Player Tracking | GetPlayerCount | ✔️ |
| Player Tracking | IsPlayerConnected | ✔️ |
| Player Tracking | PlayerConnect | ✔️ |
| Player Tracking | PlayerDisconnect | ✔️ |
| Player Tracking | SetPlayerCapacity | ✔️ |

## Installation

`go get` the source, {{< ghlink href="sdks/go" >}}directly from GitHub{{< /ghlink >}}
Expand Down