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 22, 2019
1 parent 4c0736a commit d9987ac
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 983 deletions.
11 changes: 11 additions & 0 deletions install/helm/agones/templates/crds/_gameserverspecvalidation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ properties:
type: integer
minimum: 1
maximum: 65535
loglevel:
type: string
enum:
- panic
- fatal
- error
- warn
- warning
- info
- debug
- trace
scheduling:
type: string
enum:
Expand Down
33 changes: 33 additions & 0 deletions install/yaml/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
loglevel:
type: string
enum:
- panic
- fatal
- error
- warn
- warning
- info
- debug
- trace
scheduling:
type: string
enum:
Expand Down Expand Up @@ -594,6 +605,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
loglevel:
type: string
enum:
- panic
- fatal
- error
- warn
- warning
- info
- debug
- trace
scheduling:
type: string
enum:
Expand Down Expand Up @@ -860,6 +882,17 @@ spec:
type: integer
minimum: 1
maximum: 65535
loglevel:
type: string
enum:
- panic
- fatal
- error
- warn
- warning
- info
- debug
- trace
scheduling:
type: string
enum:
Expand Down
12 changes: 11 additions & 1 deletion pkg/apis/agones/v1/gameserver.go
Original file line number Diff line number Diff line change
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:"sdk-server-loglevel,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
32 changes: 21 additions & 11 deletions pkg/apis/agones/v1/gameserver_test.go
Original file line number Diff line number Diff line change
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,6 +155,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 11,
PeriodSeconds: 12,
},
sdkServerLogLevel: "info",
},
},
"set basic defaults on static gameserver": {
Expand All @@ -173,6 +177,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
InitialDelaySeconds: 5,
PeriodSeconds: 5,
},
sdkServerLogLevel: "info",
},
},
"health is disabled": {
Expand All @@ -192,6 +197,7 @@ func TestGameServerApplyDefaults(t *testing.T) {
health: Health{
Disabled: true,
},
sdkServerLogLevel: "info",
},
},
"convert from legacy single port to multiple": {
Expand All @@ -205,17 +211,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 +241,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 +490,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
Original file line number Diff line number Diff line change
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 = 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)
}

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
Loading

0 comments on commit d9987ac

Please sign in to comment.