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 committed Aug 21, 2018
1 parent 45bf008 commit 988c23b
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 190 deletions.
38 changes: 38 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.

# project specific
.*
.idea
*.zip
/release
bin
/docs
*.md
*.amd64

# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
!.gitignore
!.helmignore
!.gitattributes
!.dockerignore
*.iml
bin
*.o
Expand Down
2 changes: 1 addition & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ GCP_CLUSTER_ZONE ?= us-west1-c
MINIKUBE_PROFILE ?= agones

# Game Server image to use while doing end-to-end tests
GS_TEST_IMAGE ?= gcr.io/agones-images/udp-server:0.1
GS_TEST_IMAGE ?= gcr.io/agones-images/udp-server:0.3

# Directory that this Makefile is in.
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
Expand Down
6 changes: 4 additions & 2 deletions docs/sdk_rest_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ $ curl -d "{}" -H "Content-Type: application/json" -X POST http://localhost:5935

### Set Label

TODO: write stuff here.
Apply a Label with the prefix "stable.agones.dev/sdk-" to the backing `GameServer` metadata.

See the SDK [SetLabel](../sdks/README.md#setlabelkey-value) documentation for restrictions.

#### Example

Expand All @@ -74,7 +76,7 @@ $ curl -d '{"key": "foo", "value": "bar"}' -H "Content-Type: application/json" -

### Set Annotation

TODO: write stuff here.
Apply a Annotation with the prefix "stable.agones.dev/sdk-" to the backing `GameServer` metadata

#### Example

Expand Down
17 changes: 15 additions & 2 deletions examples/cpp-simple/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void watchUpdates(agones::SDK *sdk) {
});
}

// TODO: set a LABEL and ANNOTATION on startup
int main() {
std::cout << "C++ Game Server has started!" << std::endl;

Expand All @@ -60,8 +59,22 @@ int main() {
std::thread health (doHealth, sdk);
std::thread watch (watchUpdates, sdk);

std::cout << "Setting a label" << std::endl;
grpc::Status status = sdk->SetLabel("test-label", "test-value");
if (!status.ok()) {
std::cout << "Could not run SetLabel(): "+ status.error_message() + ". Exiting!" << std::endl;
return -1;
}

std::cout << "Setting an annotation" << std::endl;
status = sdk->SetAnnotation("test-annotation", "test value");
if (!status.ok()) {
std::cout << "Could not run SetAnnotation(): "+ status.error_message() + ". Exiting!" << std::endl;
return -1;
}

std::cout << "Marking server as ready..." << std::endl;
grpc::Status status = sdk->Ready();
status = sdk->Ready();
if (!status.ok()) {
std::cout << "Could not run Ready(): "+ status.error_message() + ". Exiting!" << std::endl;
return -1;
Expand Down
7 changes: 6 additions & 1 deletion examples/simple-udp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"
"time"

"strconv"

coresdk "agones.dev/agones/pkg/sdk"
"agones.dev/agones/sdks/go"
)
Expand Down Expand Up @@ -96,12 +98,15 @@ func readWriteLoop(conn net.PacketConn, stop chan struct{}, s *sdk.SDK) {
watchGameServerEvents(s)

case "LABEL":
err := s.SetLabel("timestamp", time.Now().UTC().String())
log.Print("Setting label")
// label values can only be alpha, - and .
err := s.SetLabel("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
if err != nil {
log.Fatalf("could not set label: %v", err)
}

case "ANNOTATION":
log.Print("Setting annotation")
err := s.SetAnnotation("timestamp", time.Now().UTC().String())
if err != nil {
log.Fatalf("could not set annotation: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/gameservers/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,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
l.update <- struct{}{}
return &sdk.Empty{}, nil
}

// 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
l.update <- struct{}{}
return &sdk.Empty{}, nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/gameservers/sdkserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ func (s *SDKServer) updateLabel(key, value string) error {
}

gsCopy := gs.DeepCopy()
if gsCopy.ObjectMeta.Labels == nil {
gsCopy.ObjectMeta.Labels = map[string]string{}
}
gsCopy.ObjectMeta.Labels[metadataPrefix+key] = value

_, err = s.gameServerGetter.GameServers(s.namespace).Update(gsCopy)
Expand All @@ -307,6 +310,9 @@ func (s *SDKServer) updateAnnotation(key, value string) error {
}

gsCopy := gs.DeepCopy()
if gsCopy.ObjectMeta.Annotations == nil {
gsCopy.ObjectMeta.Annotations = map[string]string{}
}
gsCopy.ObjectMeta.Annotations[metadataPrefix+key] = value

_, err = s.gameServerGetter.GameServers(s.namespace).Update(gsCopy)
Expand Down
2 changes: 0 additions & 2 deletions pkg/gameservers/sdkserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func TestSidecarRun(t *testing.T) {
gs := v1alpha1.GameServer{
ObjectMeta: metav1.ObjectMeta{
Name: "test", Namespace: "default",
Labels: map[string]string{},
Annotations: map[string]string{},
},
Spec: v1alpha1.GameServerSpec{
Health: v1alpha1.Health{Disabled: false, FailureThreshold: 1, PeriodSeconds: 1, InitialDelaySeconds: 0},
Expand Down
24 changes: 10 additions & 14 deletions pkg/sdk/sdk.pb.go

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

4 changes: 0 additions & 4 deletions sdk.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ service SDK {
}

// Apply a Label to the backing GameServer metadata
// TODO: document
// TODO: e2e test
rpc SetLabel(KeyValue) returns (Empty) {
option (google.api.http) = {
put: "/metadata/label"
Expand All @@ -66,8 +64,6 @@ service SDK {
}

// Apply a Annotation to the backing GameServer metadata
// TODO: document
// TODO: e2e test
rpc SetAnnotation(KeyValue) returns (Empty) {
option (google.api.http) = {
put: "/metadata/annotation"
Expand Down
4 changes: 2 additions & 2 deletions sdk.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"/metadata/annotation": {
"put": {
"summary": "Apply a Annotation to the backing GameServer metadata\nTODO: document",
"summary": "Apply a Annotation to the backing GameServer metadata",
"operationId": "SetAnnotation",
"responses": {
"200": {
Expand All @@ -88,7 +88,7 @@
},
"/metadata/label": {
"put": {
"summary": "Apply a Label to the backing GameServer metadata\nTODO: document",
"summary": "Apply a Label to the backing GameServer metadata",
"operationId": "SetLabel",
"responses": {
"200": {
Expand Down
20 changes: 19 additions & 1 deletion sdks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ This tells Agones to shut down the currently running game server.
The GameServer state will be set `Shutdown` and the
backing Pod will be deleted, if they have not shut themselves down already.

### SetLabel(key, value)
⚠️⚠️⚠️ **`SetLabel` is currently a development feature and has not been released** ⚠️⚠️⚠️

This will set a [Label](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) value on the backing `GameServer`
record that is stored in Kubernetes. To maintain isolation, the `key` value is automatically prefixed with "stable.agones.dev/sdk-"

> Note: There are limits on the characters that be used for label keys and values. Details are [here](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set).
This can be useful if you want to information from your running game server process to be observable or searchable through the Kubernetes API.

### SetAnnotation(key, value)
⚠️⚠️⚠️ **`SetAnnotation` is currently a development feature and has not been released** ⚠️⚠️⚠️

This will set a [Annotation](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/) value on the backing
`Gameserver` record that is stored in Kubernetes. To maintain isolation, the `key` value is automatically prefixed with "stable.agones.dev/sdk-"

This can be useful if you want to information from your running game server process to be observable through the Kubernetes API.

### GameServer()
This returns most of the backing GameServer configuration and Status. This can be useful
for instances where you may want to know Health check configuration, or the IP and Port
Expand All @@ -63,7 +81,7 @@ and the [examples](../examples).

### WatchGameServer(function(gameserver){...})

⚠️⚠️⚠️ **`WatchGameServer` is currently a development feature and has not been released** ⚠️⚠️⚠
⚠️⚠️⚠️ **`WatchGameServer` is currently a development feature and has not been released** ⚠️⚠️⚠

This executes the passed in callback with the current `GameServer` details whenever the underlying `GameServer` configuration is updated.
This can be useful to track `GameServer > Status > State` changes, `metadata` changes, such as labels and annotations, and more.
Expand Down
Loading

0 comments on commit 988c23b

Please sign in to comment.