From 383bd224065f6d1e6ce3699b47b7e9822beed555 Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Sat, 25 Aug 2018 14:08:55 -0700 Subject: [PATCH] Ability to pass `GameServer` yaml/json to local sdk server To be able to work locally, you need to be able to specify your local `GameServer` configuration, as it likely will have application specific configuration in it -- or maybe you want to specify what state it's in. This commit allow you to specify the local resource as either yaml/json through a `-f` or `--file` flag. Closes #296 --- cmd/sdk-server/main.go | 63 ++++++++++++-- pkg/gameservers/localsdk.go | 37 ++++++-- pkg/gameservers/localsdk_test.go | 137 ++++++++++++++++++++---------- pkg/gameservers/sdk.go | 50 +++++++++++ pkg/gameservers/sdk_test.go | 87 +++++++++++++++++++ pkg/gameservers/sdkserver.go | 49 +---------- pkg/gameservers/sdkserver_test.go | 67 --------------- sdks/README.md | 24 +++++- 8 files changed, 343 insertions(+), 171 deletions(-) create mode 100644 pkg/gameservers/sdk_test.go diff --git a/cmd/sdk-server/main.go b/cmd/sdk-server/main.go index ea522fa744..1be008515e 100644 --- a/cmd/sdk-server/main.go +++ b/cmd/sdk-server/main.go @@ -19,19 +19,24 @@ import ( "fmt" "net" "net/http" + "os" + "path/filepath" "strings" "agones.dev/agones/pkg" + "agones.dev/agones/pkg/apis/stable/v1alpha1" "agones.dev/agones/pkg/client/clientset/versioned" "agones.dev/agones/pkg/gameservers" "agones.dev/agones/pkg/sdk" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/spf13/viper" "golang.org/x/net/context" "google.golang.org/grpc" + "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ) @@ -46,6 +51,7 @@ const ( // Flags (that can also be env vars) localFlag = "local" + fileFlag = "file" addressFlag = "address" ) @@ -81,7 +87,10 @@ func main() { defer cancel() if ctlConf.IsLocal { - sdk.RegisterSDKServer(grpcServer, gameservers.NewLocalSDKServer()) + err = registerLocal(grpcServer, ctlConf) + if err != nil { + logger.WithError(err).Fatal("Could not start local sdk server") + } } else { var config *rest.Config config, err = rest.InClusterConfig() @@ -124,6 +133,46 @@ func main() { logger.Info("shutting down sdk server") } +func registerLocal(grpcServer *grpc.Server, ctlConf config) error { + var local *gameservers.LocalSDKServer + if ctlConf.LocalFile != "" { + path, err := filepath.Abs(ctlConf.LocalFile) + if err != nil { + return err + } + + if _, err = os.Stat(path); os.IsNotExist(err) { + return errors.Errorf("Could not find file: %s", path) + } + + logger.WithField("path", path).Info("Reading GameServer configuration") + reader, err := os.Open(path) // nolint: gosec + if err != nil { + return err + } + + var gs v1alpha1.GameServer + // 4096 is the number of bytes the YAMLOrJSONDecoder goes looking + // into the file to determine if it's JSON or YAML + // (JSON == has whitespace followed by an open brace). + // The Kubernetes uses 4096 bytes as its default, so that's what we'll + // use as well. + // https://github.com/kubernetes/kubernetes/blob/master/plugin/pkg/admission/podnodeselector/admission.go#L86 + decoder := yaml.NewYAMLOrJSONDecoder(reader, 4096) + err = decoder.Decode(&gs) + if err != nil { + return err + } + local = gameservers.NewLocalSDKServer(&gs) + } else { + local = gameservers.NewLocalSDKServer(nil) + } + + sdk.RegisterSDKServer(grpcServer, local) + + return nil +} + // runGrpc runs the grpc service func runGrpc(grpcServer *grpc.Server, lis net.Listener) { logger.Info("Starting SDKServer grpc service...") @@ -157,9 +206,11 @@ func runGateway(ctx context.Context, grpcEndpoint string, mux *gwruntime.ServeMu // a configuration structure func parseEnvFlags() config { viper.SetDefault(localFlag, false) + viper.SetDefault(fileFlag, "") viper.SetDefault(addressFlag, "localhost") pflag.Bool(localFlag, viper.GetBool(localFlag), "Set this, or LOCAL env, to 'true' to run this binary in local development mode. Defaults to 'false'") + pflag.StringP(fileFlag, "f", viper.GetString(fileFlag), "Set this, or FILE env var to the path of a local yaml or json file that contains your GameServer resoure configuration") pflag.String(addressFlag, viper.GetString(addressFlag), "The Address to bind the server grpcPort to. Defaults to 'localhost") pflag.Parse() @@ -170,13 +221,15 @@ func parseEnvFlags() config { runtime.Must(viper.BindPFlags(pflag.CommandLine)) return config{ - IsLocal: viper.GetBool(localFlag), - Address: viper.GetString(addressFlag), + IsLocal: viper.GetBool(localFlag), + Address: viper.GetString(addressFlag), + LocalFile: viper.GetString(fileFlag), } } // config is all the configuration for this program type config struct { - Address string - IsLocal bool + Address string + IsLocal bool + LocalFile string } diff --git a/pkg/gameservers/localsdk.go b/pkg/gameservers/localsdk.go index 024241bd63..635b06c652 100644 --- a/pkg/gameservers/localsdk.go +++ b/pkg/gameservers/localsdk.go @@ -16,9 +16,10 @@ package gameservers import ( "io" - "time" "sync" + "time" + "agones.dev/agones/pkg/apis/stable/v1alpha1" "agones.dev/agones/pkg/sdk" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -28,7 +29,7 @@ import ( var ( _ sdk.SDKServer = &LocalSDKServer{} - fixture = &sdk.GameServer{ + defaultGs = &sdk.GameServer{ ObjectMeta: &sdk.GameServer_ObjectMeta{ Name: "local", Namespace: "default", @@ -51,19 +52,25 @@ var ( // is being run for local development, and doesn't connect to the // Kubernetes cluster type LocalSDKServer struct { + gs *sdk.GameServer watchPeriod time.Duration update chan struct{} updateObservers sync.Map } // NewLocalSDKServer returns the default LocalSDKServer -func NewLocalSDKServer() *LocalSDKServer { +func NewLocalSDKServer(gs *v1alpha1.GameServer) *LocalSDKServer { l := &LocalSDKServer{ + gs: defaultGs, watchPeriod: 5 * time.Second, update: make(chan struct{}), updateObservers: sync.Map{}, } + if gs != nil { + l.gs = convert(gs) + } + go func() { for value := range l.update { logrus.Info("gameserver update received") @@ -107,7 +114,15 @@ func (l *LocalSDKServer) Health(stream sdk.SDK_HealthServer) error { // SetLabel applies a Label to the backing GameServer metadata func (l *LocalSDKServer) SetLabel(_ context.Context, kv *sdk.KeyValue) (*sdk.Empty, error) { logrus.WithField("values", kv).Info("Setting label") - fixture.ObjectMeta.Labels[metadataPrefix+kv.Key] = kv.Value + + if l.gs.ObjectMeta == nil { + l.gs.ObjectMeta = &sdk.GameServer_ObjectMeta{} + } + if l.gs.ObjectMeta.Labels == nil { + l.gs.ObjectMeta.Labels = map[string]string{} + } + + l.gs.ObjectMeta.Labels[metadataPrefix+kv.Key] = kv.Value l.update <- struct{}{} return &sdk.Empty{}, nil } @@ -115,7 +130,15 @@ func (l *LocalSDKServer) SetLabel(_ context.Context, kv *sdk.KeyValue) (*sdk.Emp // SetAnnotation applies a Annotation to the backing GameServer metadata func (l *LocalSDKServer) SetAnnotation(_ context.Context, kv *sdk.KeyValue) (*sdk.Empty, error) { logrus.WithField("values", kv).Info("Setting annotation") - fixture.ObjectMeta.Annotations[metadataPrefix+kv.Key] = kv.Value + + if l.gs.ObjectMeta == nil { + l.gs.ObjectMeta = &sdk.GameServer_ObjectMeta{} + } + if l.gs.ObjectMeta.Annotations == nil { + l.gs.ObjectMeta.Annotations = map[string]string{} + } + + l.gs.ObjectMeta.Annotations[metadataPrefix+kv.Key] = kv.Value l.update <- struct{}{} return &sdk.Empty{}, nil } @@ -123,7 +146,7 @@ func (l *LocalSDKServer) SetAnnotation(_ context.Context, kv *sdk.KeyValue) (*sd // GetGameServer returns a dummy game server. func (l *LocalSDKServer) GetGameServer(context.Context, *sdk.Empty) (*sdk.GameServer, error) { logrus.Info("getting GameServer details") - return fixture, nil + return l.gs, nil } // WatchGameServer will return a dummy GameServer (with no changes), 3 times, every 5 seconds @@ -150,7 +173,7 @@ func (l *LocalSDKServer) WatchGameServer(_ *sdk.Empty, stream sdk.SDK_WatchGameS }() for range observer { - err := stream.Send(fixture) + err := stream.Send(l.gs) if err != nil { logrus.WithError(err).Error("error sending gameserver") return err diff --git a/pkg/gameservers/localsdk_test.go b/pkg/gameservers/localsdk_test.go index 3e3307e1c0..d4332fc962 100644 --- a/pkg/gameservers/localsdk_test.go +++ b/pkg/gameservers/localsdk_test.go @@ -19,15 +19,17 @@ import ( "testing" "time" + "agones.dev/agones/pkg/apis/stable/v1alpha1" "agones.dev/agones/pkg/sdk" "github.com/stretchr/testify/assert" "golang.org/x/net/context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestLocal(t *testing.T) { ctx := context.Background() e := &sdk.Empty{} - l := NewLocalSDKServer() + l := NewLocalSDKServer(nil) _, err := l.Ready(ctx, e) assert.Nil(t, err, "Ready should not error") @@ -53,60 +55,109 @@ func TestLocal(t *testing.T) { gs, err := l.GetGameServer(ctx, e) assert.Nil(t, err) - assert.Equal(t, fixture, gs) + assert.Equal(t, defaultGs, gs) } -func TestLocalSDKServerSetLabel(t *testing.T) { +func TestLocalSDKWithGameServer(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) + fixture := &v1alpha1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "stuff"}} + l := NewLocalSDKServer(fixture.DeepCopy()) 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") - } + assert.Equal(t, fixture.ObjectMeta.Name, gs.ObjectMeta.Name) } -func TestLocalSDKServerSetAnnotation(t *testing.T) { - ctx := context.Background() - e := &sdk.Empty{} - l := NewLocalSDKServer() - kv := &sdk.KeyValue{Key: "bar", Value: "foo"} +func TestLocalSDKServerSetLabel(t *testing.T) { + fixtures := map[string]struct { + gs *v1alpha1.GameServer + }{ + "default": { + gs: nil, + }, + "no annotation": { + gs: &v1alpha1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "empty"}}, + }, + "empty": { + gs: &v1alpha1.GameServer{}, + }, + } - stream := newGameServerMockStream() - go func() { - err := l.WatchGameServer(e, stream) - assert.Nil(t, err) - }() + for k,v := range fixtures { + t.Run(k, func(t *testing.T) { + ctx := context.Background() + e := &sdk.Empty{} + l := NewLocalSDKServer(v.gs) + 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") + } + }) + } +} - _, err := l.SetAnnotation(ctx, kv) - assert.Nil(t, err) +func TestLocalSDKServerSetAnnotation(t *testing.T) { - gs, err := l.GetGameServer(ctx, e) - assert.Nil(t, err) - assert.Equal(t, gs.ObjectMeta.Annotations[metadataPrefix+"bar"], "foo") + fixtures := map[string]struct { + gs *v1alpha1.GameServer + }{ + "default": { + gs: nil, + }, + "no annotation": { + gs: &v1alpha1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "empty"}}, + }, + "empty": { + gs: &v1alpha1.GameServer{}, + }, + } - 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") + for k, v := range fixtures { + t.Run(k, func(t *testing.T) { + ctx := context.Background() + e := &sdk.Empty{} + l := NewLocalSDKServer(v.gs) + 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") + } + }) } } @@ -114,7 +165,7 @@ func TestLocalSDKServerWatchGameServer(t *testing.T) { t.Parallel() e := &sdk.Empty{} - l := NewLocalSDKServer() + l := NewLocalSDKServer(nil) l.watchPeriod = time.Second stream := newGameServerMockStream() @@ -126,7 +177,7 @@ func TestLocalSDKServerWatchGameServer(t *testing.T) { for i := 0; i < 3; i++ { select { case msg := <-stream.msgs: - assert.Equal(t, fixture, msg) + assert.Equal(t, defaultGs, msg) case <-time.After(2 * l.watchPeriod): assert.FailNow(t, "timeout on receiving messages") } diff --git a/pkg/gameservers/sdk.go b/pkg/gameservers/sdk.go index 6b3132fbb7..af437a8529 100644 --- a/pkg/gameservers/sdk.go +++ b/pkg/gameservers/sdk.go @@ -14,7 +14,57 @@ package gameservers +import ( + "agones.dev/agones/pkg/apis/stable/v1alpha1" + "agones.dev/agones/pkg/sdk" +) + const ( // metadataPrefix prefix for labels and annotations metadataPrefix = "stable.agones.dev/sdk-" ) + +// convert converts a K8s GameServer object, into a gRPC SDK GameServer object +func convert(gs *v1alpha1.GameServer) *sdk.GameServer { + meta := gs.ObjectMeta + status := gs.Status + health := gs.Spec.Health + result := &sdk.GameServer{ + ObjectMeta: &sdk.GameServer_ObjectMeta{ + Name: meta.Name, + Namespace: meta.Namespace, + Uid: string(meta.UID), + ResourceVersion: meta.ResourceVersion, + Generation: meta.Generation, + CreationTimestamp: meta.CreationTimestamp.Unix(), + Annotations: meta.Annotations, + Labels: meta.Labels, + }, + Spec: &sdk.GameServer_Spec{ + Health: &sdk.GameServer_Spec_Health{ + Disabled: health.Disabled, + PeriodSeconds: health.PeriodSeconds, + FailureThreshold: health.FailureThreshold, + InitialDelaySeconds: health.InitialDelaySeconds, + }, + }, + Status: &sdk.GameServer_Status{ + State: string(status.State), + Address: status.Address, + }, + } + if meta.DeletionTimestamp != nil { + result.ObjectMeta.DeletionTimestamp = meta.DeletionTimestamp.Unix() + } + + // loop around and add all the ports + for _, p := range status.Ports { + grpcPort := &sdk.GameServer_Status_Port{ + Name: p.Name, + Port: p.Port, + } + result.Status.Ports = append(result.Status.Ports, grpcPort) + } + + return result +} diff --git a/pkg/gameservers/sdk_test.go b/pkg/gameservers/sdk_test.go new file mode 100644 index 0000000000..badb3bde4f --- /dev/null +++ b/pkg/gameservers/sdk_test.go @@ -0,0 +1,87 @@ +// Copyright 2018 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. + +package gameservers + +import ( + "testing" + + "agones.dev/agones/pkg/apis/stable/v1alpha1" + "agones.dev/agones/pkg/sdk" + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestConvert(t *testing.T) { + t.Parallel() + + fixture := &v1alpha1.GameServer{ + ObjectMeta: v1.ObjectMeta{ + CreationTimestamp: v1.Now(), + Namespace: "default", + Name: "test", + Labels: map[string]string{"foo": "bar"}, + Annotations: map[string]string{"stuff": "things"}, + UID: "1234", + }, + Spec: v1alpha1.GameServerSpec{ + Health: v1alpha1.Health{ + Disabled: false, + InitialDelaySeconds: 10, + FailureThreshold: 15, + PeriodSeconds: 20, + }, + }, + Status: v1alpha1.GameServerStatus{ + NodeName: "george", + Address: "127.0.0.1", + State: "Ready", + Ports: []v1alpha1.GameServerStatusPort{ + {Name: "default", Port: 12345}, + {Name: "beacon", Port: 123123}, + }, + }, + } + + eq := func(t *testing.T, fixture *v1alpha1.GameServer, sdkGs *sdk.GameServer) { + assert.Equal(t, fixture.ObjectMeta.Name, sdkGs.ObjectMeta.Name) + assert.Equal(t, fixture.ObjectMeta.Namespace, sdkGs.ObjectMeta.Namespace) + assert.Equal(t, fixture.ObjectMeta.CreationTimestamp.Unix(), sdkGs.ObjectMeta.CreationTimestamp) + assert.Equal(t, string(fixture.ObjectMeta.UID), sdkGs.ObjectMeta.Uid) + assert.Equal(t, fixture.ObjectMeta.Labels, sdkGs.ObjectMeta.Labels) + assert.Equal(t, fixture.ObjectMeta.Annotations, sdkGs.ObjectMeta.Annotations) + assert.Equal(t, fixture.Spec.Health.Disabled, sdkGs.Spec.Health.Disabled) + assert.Equal(t, fixture.Spec.Health.InitialDelaySeconds, sdkGs.Spec.Health.InitialDelaySeconds) + assert.Equal(t, fixture.Spec.Health.FailureThreshold, sdkGs.Spec.Health.FailureThreshold) + assert.Equal(t, fixture.Spec.Health.PeriodSeconds, sdkGs.Spec.Health.PeriodSeconds) + assert.Equal(t, fixture.Status.Address, sdkGs.Status.Address) + assert.Equal(t, string(fixture.Status.State), sdkGs.Status.State) + assert.Len(t, sdkGs.Status.Ports, len(fixture.Status.Ports)) + for i, fp := range fixture.Status.Ports { + p := sdkGs.Status.Ports[i] + assert.Equal(t, fp.Name, p.Name) + assert.Equal(t, fp.Port, p.Port) + } + } + + sdkGs := convert(fixture) + eq(t, fixture, sdkGs) + assert.Zero(t, sdkGs.ObjectMeta.DeletionTimestamp) + + now := v1.Now() + fixture.DeletionTimestamp = &now + sdkGs = convert(fixture) + eq(t, fixture, sdkGs) + assert.Equal(t, fixture.ObjectMeta.DeletionTimestamp.Unix(), sdkGs.ObjectMeta.DeletionTimestamp) +} diff --git a/pkg/gameservers/sdkserver.go b/pkg/gameservers/sdkserver.go index 23ea094bbf..edac4f0a24 100644 --- a/pkg/gameservers/sdkserver.go +++ b/pkg/gameservers/sdkserver.go @@ -385,7 +385,7 @@ func (s *SDKServer) GetGameServer(context.Context, *sdk.Empty) (*sdk.GameServer, return nil, err } - return s.convert(gs), nil + return convert(gs), nil } // WatchGameServer sends events through the stream when changes occur to the @@ -408,7 +408,7 @@ func (s *SDKServer) sendGameServerUpdate(gs *stablev1alpha1.GameServer) { defer s.streamMutex.RUnlock() for _, stream := range s.connectedStreams { - err := stream.Send(s.convert(gs)) + err := stream.Send(convert(gs)) // We essentially ignoring any disconnected streams. // I think this is fine, as disconnections shouldn't actually happen. // but we should log them, just in case they do happen, and we can track it @@ -419,51 +419,6 @@ func (s *SDKServer) sendGameServerUpdate(gs *stablev1alpha1.GameServer) { } } -// convert converts a K8s GameServer object, into a gRPC SDK GameServer object -func (s *SDKServer) convert(gs *stablev1alpha1.GameServer) *sdk.GameServer { - meta := gs.ObjectMeta - status := gs.Status - health := gs.Spec.Health - result := &sdk.GameServer{ - ObjectMeta: &sdk.GameServer_ObjectMeta{ - Name: meta.Name, - Namespace: meta.Namespace, - Uid: string(meta.UID), - ResourceVersion: meta.ResourceVersion, - Generation: meta.Generation, - CreationTimestamp: meta.CreationTimestamp.Unix(), - Annotations: meta.Annotations, - Labels: meta.Labels, - }, - Spec: &sdk.GameServer_Spec{ - Health: &sdk.GameServer_Spec_Health{ - Disabled: health.Disabled, - PeriodSeconds: health.PeriodSeconds, - FailureThreshold: health.FailureThreshold, - InitialDelaySeconds: health.InitialDelaySeconds, - }, - }, - Status: &sdk.GameServer_Status{ - State: string(status.State), - Address: status.Address, - }, - } - if meta.DeletionTimestamp != nil { - result.ObjectMeta.DeletionTimestamp = meta.DeletionTimestamp.Unix() - } - - // loop around and add all the ports - for _, p := range status.Ports { - grpcPort := &sdk.GameServer_Status_Port{ - Name: p.Name, - Port: p.Port, - } - result.Status.Ports = append(result.Status.Ports, grpcPort) - } - - return result -} - // runHealth actively checks the health, and if not // healthy will push the Unhealthy state into the queue so // it can be updated diff --git a/pkg/gameservers/sdkserver_test.go b/pkg/gameservers/sdkserver_test.go index 83b44627e8..5008b9c8a8 100644 --- a/pkg/gameservers/sdkserver_test.go +++ b/pkg/gameservers/sdkserver_test.go @@ -479,73 +479,6 @@ func TestSidecarHTTPHealthCheck(t *testing.T) { wg.Wait() // wait for go routine test results. } -func TestSDKServerConvert(t *testing.T) { - t.Parallel() - - fixture := &v1alpha1.GameServer{ - ObjectMeta: metav1.ObjectMeta{ - CreationTimestamp: metav1.Now(), - Namespace: "default", - Name: "test", - Labels: map[string]string{"foo": "bar"}, - Annotations: map[string]string{"stuff": "things"}, - UID: "1234", - }, - Spec: v1alpha1.GameServerSpec{ - Health: v1alpha1.Health{ - Disabled: false, - InitialDelaySeconds: 10, - FailureThreshold: 15, - PeriodSeconds: 20, - }, - }, - Status: v1alpha1.GameServerStatus{ - NodeName: "george", - Address: "127.0.0.1", - State: "Ready", - Ports: []v1alpha1.GameServerStatusPort{ - {Name: "default", Port: 12345}, - {Name: "beacon", Port: 123123}, - }, - }, - } - - m := agtesting.NewMocks() - sc, err := defaultSidecar(m) - assert.Nil(t, err) - - eq := func(t *testing.T, fixture *v1alpha1.GameServer, sdkGs *sdk.GameServer) { - assert.Equal(t, fixture.ObjectMeta.Name, sdkGs.ObjectMeta.Name) - assert.Equal(t, fixture.ObjectMeta.Namespace, sdkGs.ObjectMeta.Namespace) - assert.Equal(t, fixture.ObjectMeta.CreationTimestamp.Unix(), sdkGs.ObjectMeta.CreationTimestamp) - assert.Equal(t, string(fixture.ObjectMeta.UID), sdkGs.ObjectMeta.Uid) - assert.Equal(t, fixture.ObjectMeta.Labels, sdkGs.ObjectMeta.Labels) - assert.Equal(t, fixture.ObjectMeta.Annotations, sdkGs.ObjectMeta.Annotations) - assert.Equal(t, fixture.Spec.Health.Disabled, sdkGs.Spec.Health.Disabled) - assert.Equal(t, fixture.Spec.Health.InitialDelaySeconds, sdkGs.Spec.Health.InitialDelaySeconds) - assert.Equal(t, fixture.Spec.Health.FailureThreshold, sdkGs.Spec.Health.FailureThreshold) - assert.Equal(t, fixture.Spec.Health.PeriodSeconds, sdkGs.Spec.Health.PeriodSeconds) - assert.Equal(t, fixture.Status.Address, sdkGs.Status.Address) - assert.Equal(t, string(fixture.Status.State), sdkGs.Status.State) - assert.Len(t, sdkGs.Status.Ports, len(fixture.Status.Ports)) - for i, fp := range fixture.Status.Ports { - p := sdkGs.Status.Ports[i] - assert.Equal(t, fp.Name, p.Name) - assert.Equal(t, fp.Port, p.Port) - } - } - - sdkGs := sc.convert(fixture) - eq(t, fixture, sdkGs) - assert.Zero(t, sdkGs.ObjectMeta.DeletionTimestamp) - - now := metav1.Now() - fixture.DeletionTimestamp = &now - sdkGs = sc.convert(fixture) - eq(t, fixture, sdkGs) - assert.Equal(t, fixture.ObjectMeta.DeletionTimestamp.Unix(), sdkGs.ObjectMeta.DeletionTimestamp) -} - func TestSDKServerGetGameServer(t *testing.T) { t.Parallel() diff --git a/sdks/README.md b/sdks/README.md index 94f9db4821..3fe14f2fa2 100644 --- a/sdks/README.md +++ b/sdks/README.md @@ -126,13 +126,33 @@ To run in local mode, pass the flag `--local` to the executable. For example: -```bash +```console $ ./sidecar.linux.amd64 --local -{"level":"info","local":true,"msg":"Starting sdk sidecar","port":59357,"time":"2017-12-22T16:09:03-08:00","version":"0.1-5217b21"} +{"ctlConf":{"Address":"localhost","IsLocal":true,"LocalFile":""},"grpcPort":59357,"httpPort":59358,"level":"info","msg":"Starting sdk sidecar","source":"main","time":"2018-08-25T18:01:58-07:00","version":"0.4.0-b44960a8"} +{"level":"info","msg":"Starting SDKServer grpc service...","source":"main","time":"2018-08-25T18:01:58-07:00"} +{"level":"info","msg":"Starting SDKServer grpc-gateway...","source":"main","time":"2018-08-25T18:01:58-07:00"} {"level":"info","msg":"Ready request has been received!","time":"2017-12-22T16:09:19-08:00"} {"level":"info","msg":"Shutdown request has been received!","time":"2017-12-22T16:10:19-08:00"} ``` +### Providing your own `GameServer` configuration for local development + +⚠️⚠️⚠️ **Providing your own `GameServer` is currently a development feature and has not been released** ⚠️⚠️⚠️ + +By default, the local sdk-server will create a dummy `GameServer` configuration that is used for `GameServer()` +and `WatchGameServer()` SDK calls. If you wish to provide your own configuration, as either yaml or json, this +can be passed through as either `--file` or `-f` along with the `--local` flag. + +For example: + +```console +$ ./sdk-server.linux.amd64 --local -f ../../../examples/simple-udp/gameserver.yaml +{"ctlConf":{"Address":"localhost","IsLocal":true,"LocalFile":"../../../examples/simple-udp/gameserver.yaml"},"grpcPort":59357,"httpPort":59358,"level":"info","msg":"Starting sdk sidecar","source":"main","time":"2018-08-25T17:56:39-07:00","version":"0.4.0-b44960a8"} +{"level":"info","msg":"Reading GameServer configuration","path":"/home/user/workspace/agones/src/agones.dev/agones/examples/simple-udp/gameserver.yaml","source":"main","time":"2018-08-25T17:56:39-07:00"} +{"level":"info","msg":"Starting SDKServer grpc service...","source":"main","time":"2018-08-25T17:56:39-07:00"} +{"level":"info","msg":"Starting SDKServer grpc-gateway...","source":"main","time":"2018-08-25T17:56:39-07:00"} +``` + ### Writing your own SDK If there isn't a SDK for the language and platform you are looking for, you have several options: