Skip to content

Commit

Permalink
Add Sidecar log level through GS specification
Browse files Browse the repository at this point in the history
Add ability to specify logrus log level in yaml configuration and
apply this log level on creating the GameServer.
  • Loading branch information
aLekSer committed Aug 27, 2019
1 parent b2e85c8 commit 1d3ef31
Show file tree
Hide file tree
Showing 8 changed files with 908 additions and 984 deletions.
11 changes: 11 additions & 0 deletions install/helm/agones/templates/crds/_gameserverspecvalidation.yaml
Expand Up @@ -92,6 +92,17 @@ properties:
type: integer
minimum: 1
maximum: 65535
sdkServerLoglevel:
type: string
description: |
sdkServerLoglevel has three options:
- "Info" (default) output SDK server messages of all log levels except debug messages
- "Error" return log messages with level Error and above
- "Debug" output all log messages of the game server sidecar
enum:
- Error
- Info
- Debug
scheduling:
type: string
enum:
Expand Down
33 changes: 33 additions & 0 deletions install/yaml/install.yaml
Expand Up @@ -339,6 +339,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
sdkServerLoglevel:
type: string
description: |
sdkServerLoglevel has three options:
- "Info" (default) output SDK server messages of all log levels except debug messages
- "Error" return log messages with level Error and above
- "Debug" output all log messages of the game server sidecar
enum:
- Error
- Info
- Debug
scheduling:
type: string
enum:
Expand Down Expand Up @@ -594,6 +605,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
sdkServerLoglevel:
type: string
description: |
sdkServerLoglevel has three options:
- "Info" (default) output SDK server messages of all log levels except debug messages
- "Error" return log messages with level Error and above
- "Debug" output all log messages of the game server sidecar
enum:
- Error
- Info
- Debug
scheduling:
type: string
enum:
Expand Down Expand Up @@ -860,6 +882,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
sdkServerLoglevel:
type: string
description: |
sdkServerLoglevel has three options:
- "Info" (default) output SDK server messages of all log levels except debug messages
- "Error" return log messages with level Error and above
- "Debug" output all log messages of the game server sidecar
enum:
- Error
- Info
- Debug
scheduling:
type: string
enum:
Expand Down
12 changes: 11 additions & 1 deletion pkg/apis/agones/v1/gameserver.go
Expand Up @@ -135,6 +135,8 @@ type GameServerSpec struct {
Health Health `json:"health,omitempty"`
// Scheduling strategy. Defaults to "Packed".
Scheduling apis.SchedulingStrategy `json:"scheduling,omitempty"`
// SdkServerLogLevel specifies the log level for gameserver sidecar.
SdkServerLogLevel string `json:"sdkServerLogLevel,omitempty"`
// Template describes the Pod that will be created for the GameServer
Template corev1.PodTemplateSpec `json:"template"`
}
Expand Down Expand Up @@ -213,9 +215,17 @@ func (gss *GameServerSpec) ApplyDefaults() {
gss.applyPortDefaults()
gss.applyHealthDefaults()
gss.applySchedulingDefaults()
gss.applyLogLevelDefaults()
}

// applyContainerDefaults applues the container defaults
// applyLogLevelDefaults applies the log level default - "Info"
func (gss *GameServerSpec) applyLogLevelDefaults() {
if gss.SdkServerLogLevel == "" {
gss.SdkServerLogLevel = "Info"
}
}

// applyContainerDefaults applies the container defaults
func (gss *GameServerSpec) applyContainerDefaults() {
if len(gss.Template.Spec.Containers) == 1 {
gss.Container = gss.Template.Spec.Containers[0].Name
Expand Down
35 changes: 23 additions & 12 deletions pkg/apis/agones/v1/gameserver_test.go
Expand Up @@ -61,11 +61,12 @@ func TestGameServerApplyDefaults(t *testing.T) {
t.Parallel()

type expected struct {
protocol corev1.Protocol
state GameServerState
policy PortPolicy
health Health
scheduling apis.SchedulingStrategy
protocol corev1.Protocol
state GameServerState
policy PortPolicy
health Health
scheduling apis.SchedulingStrategy
sdkServerLogLevel string
}
data := map[string]struct {
gameServer GameServer
Expand Down Expand Up @@ -93,6 +94,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 5,
PeriodSeconds: 5,
},
sdkServerLogLevel: "Info",
},
},
"defaults on passthrough": {
Expand All @@ -116,6 +118,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 5,
PeriodSeconds: 5,
},
sdkServerLogLevel: "Info",
},
},
"defaults are already set": {
Expand Down Expand Up @@ -152,12 +155,14 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 11,
PeriodSeconds: 12,
},
sdkServerLogLevel: "Info",
},
},
"set basic defaults on static gameserver": {
gameServer: GameServer{
Spec: GameServerSpec{
Ports: []GameServerPort{{PortPolicy: Static}},
Ports: []GameServerPort{{PortPolicy: Static}},
SdkServerLogLevel: "Error",
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}},
},
Expand All @@ -173,6 +178,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 5,
PeriodSeconds: 5,
},
sdkServerLogLevel: "Error",
},
},
"health is disabled": {
Expand All @@ -192,6 +198,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
health: Health{
Disabled: true,
},
sdkServerLogLevel: "Info",
},
},
"convert from legacy single port to multiple": {
Expand All @@ -205,17 +212,19 @@ func TestGameServerApplyDefaults(t *testing.T) {
Protocol: corev1.ProtocolTCP,
},
},
Health: Health{Disabled: true},
Health: Health{Disabled: true},
SdkServerLogLevel: "Debug",
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}},
},
container: "testing",
expected: expected{
protocol: corev1.ProtocolTCP,
state: GameServerStateCreating,
policy: Static,
scheduling: apis.Packed,
health: Health{Disabled: true},
protocol: corev1.ProtocolTCP,
state: GameServerStateCreating,
policy: Static,
scheduling: apis.Packed,
health: Health{Disabled: true},
sdkServerLogLevel: "Debug",
},
},
}
Expand All @@ -233,6 +242,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
assert.Equal(t, test.expected.state, test.gameServer.Status.State)
assert.Equal(t, test.expected.health, test.gameServer.Spec.Health)
assert.Equal(t, test.expected.scheduling, test.gameServer.Spec.Scheduling)
assert.Equal(t, test.expected.sdkServerLogLevel, test.gameServer.Spec.SdkServerLogLevel)
})
}
}
Expand Down Expand Up @@ -481,6 +491,7 @@ func TestGameServerPatch(t *testing.T) {

assert.Contains(t, string(patch), `{"op":"replace","path":"/spec/container","value":"bear"}`)
}

func TestGameServerGetDevAddress(t *testing.T) {
devGs := &GameServer{
ObjectMeta: metav1.ObjectMeta{
Expand Down
24 changes: 19 additions & 5 deletions pkg/sdkserver/sdkserver.go
Expand Up @@ -63,6 +63,7 @@ var _ sdk.SDKServer = &SDKServer{}
// nolint: maligned
type SDKServer struct {
logger *logrus.Entry
logLevel string
gameServerName string
namespace string
informerFactory externalversions.SharedInformerFactory
Expand Down Expand Up @@ -165,7 +166,7 @@ func NewSDKServer(gameServerName, namespace string, kubeClient kubernetes.Interf
logfields.GameServerKey,
strings.Join([]string{agones.GroupName, s.namespace, s.gameServerName}, "."))

s.logger.Info("created GameServer sidecar")
s.logger.Info("Created GameServer sidecar")

return s, nil
}
Expand All @@ -192,8 +193,21 @@ func (s *SDKServer) Run(stop <-chan struct{}) error {
}

// grab configuration details
if gs.Spec.SdkServerLogLevel == "" {
s.logLevel = "info"
} else {
s.logLevel = strings.ToLower(gs.Spec.SdkServerLogLevel)
}
s.logger.WithField("logLevel", s.logLevel).Info("Setting LogLevel configuration")
level, err := logrus.ParseLevel(s.logLevel)
if err == nil {
s.logger.Logger.SetLevel(level)
} else {
s.logger.WithError(err).Info("Specified wrong sdkServerLogLevel")
}

s.health = gs.Spec.Health
s.logger.WithField("health", s.health).Info("setting health configuration")
s.logger.WithField("health", s.health).Info("Setting health configuration")
s.healthTimeout = time.Duration(gs.Spec.Health.PeriodSeconds) * time.Second
s.initHealthLastUpdated(time.Duration(gs.Spec.Health.InitialDelaySeconds) * time.Second)

Expand All @@ -214,7 +228,7 @@ func (s *SDKServer) Run(stop <-chan struct{}) error {
go func() {
if err := s.server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
s.logger.WithError(err).Info("health check: http server closed")
s.logger.WithError(err).Info("Health check: http server closed")
} else {
err = errors.Wrap(err, "Could not listen on :8080")
runtime.HandleError(s.logger.WithError(err), err)
Expand Down Expand Up @@ -319,7 +333,7 @@ func (s *SDKServer) gameServer() (*agonesv1.GameServer, error) {
// updateLabels updates the labels on this GameServer to the ones persisted in SDKServer,
// i.e. SDKServer.gsLabels, with the prefix of "agones.dev/sdk-"
func (s *SDKServer) updateLabels() error {
s.logger.WithField("labels", s.gsLabels).Info("updating label")
s.logger.WithField("labels", s.gsLabels).Info("Updating label")
gs, err := s.gameServer()
if err != nil {
return err
Expand Down Expand Up @@ -534,7 +548,7 @@ func (s *SDKServer) sendGameServerUpdate(gs *agonesv1.GameServer) {
func (s *SDKServer) runHealth() {
s.checkHealth()
if !s.healthy() {
s.logger.WithField("gameServerName", s.gameServerName).Info("has failed health check")
s.logger.WithField("gameServerName", s.gameServerName).Info("GameServer has failed health check")
s.enqueueState(agonesv1.GameServerStateUnhealthy)
}
}
Expand Down

0 comments on commit 1d3ef31

Please sign in to comment.