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

Add events to SDK state change operations #866

Merged
merged 2 commits into from
Jul 4, 2019
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
2 changes: 1 addition & 1 deletion pkg/gameservers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func (c *Controller) syncGameServerRequestReadyState(gs *v1alpha1.GameServer) (*
if addressPopulated {
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Address and port populated")
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "SDK.Ready() executed")
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "SDK.Ready() complete")
return gs, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gameservers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ func TestControllerSyncGameServerRequestReadyState(t *testing.T) {
assert.Nil(t, err, "should not error")
assert.True(t, gsUpdated, "GameServer wasn't updated")
assert.Equal(t, v1alpha1.GameServerStateReady, gs.Status.State)
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "SDK.Ready() executed")
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "SDK.Ready() complete")
})

t.Run("GameServer without an Address, but RequestReady State", func(t *testing.T) {
Expand Down Expand Up @@ -995,7 +995,7 @@ func TestControllerSyncGameServerRequestReadyState(t *testing.T) {
assert.Equal(t, gs.Status.Address, ipFixture)

agtesting.AssertEventContains(t, m.FakeRecorder.Events, "Address and port populated")
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "SDK.Ready() executed")
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "SDK.Ready() complete")
})

for _, s := range []v1alpha1.GameServerState{"Unknown", v1alpha1.GameServerStateUnhealthy} {
Expand Down
16 changes: 12 additions & 4 deletions pkg/sdkserver/sdkserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,22 @@ func (s *SDKServer) updateState() error {
s.gsUpdateMutex.RLock()
gs.Status.State = s.gsState
s.gsUpdateMutex.RUnlock()

_, err = gameServers.Update(gs)
if err != nil {
return errors.Wrapf(err, "could not update GameServer %s/%s to state %s", s.namespace, s.gameServerName, gs.Status.State)
}

// state specific work here
if gs.Status.State == stablev1alpha1.GameServerStateUnhealthy {
s.recorder.Event(gs, corev1.EventTypeWarning, string(gs.Status.State), "No longer healthy")
level := corev1.EventTypeNormal
// post state specific work here
switch gs.Status.State {
case stablev1alpha1.GameServerStateUnhealthy:
level = corev1.EventTypeWarning
}

return errors.Wrapf(err, "could not update GameServer %s/%s to state %s", s.namespace, s.gameServerName, gs.Status.State)
s.recorder.Event(gs, level, string(gs.Status.State), "SDK state change")

return nil
}

func (s *SDKServer) gameServer() (*stablev1alpha1.GameServer, error) {
Expand Down
16 changes: 12 additions & 4 deletions pkg/sdkserver/sdkserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ func TestSidecarRun(t *testing.T) {
sc.Ready(ctx, &sdk.Empty{}) // nolint: errcheck
},
expected: expected{
state: v1alpha1.GameServerStateRequestReady,
state: v1alpha1.GameServerStateRequestReady,
recordings: []string{"Normal " + string(v1alpha1.GameServerStateRequestReady)},
},
},
"shutdown": {
f: func(sc *SDKServer, ctx context.Context) {
sc.Shutdown(ctx, &sdk.Empty{}) // nolint: errcheck
},
expected: expected{
state: v1alpha1.GameServerStateShutdown,
state: v1alpha1.GameServerStateShutdown,
recordings: []string{"Normal " + string(v1alpha1.GameServerStateShutdown)},
},
},
"unhealthy": {
Expand All @@ -75,7 +77,7 @@ func TestSidecarRun(t *testing.T) {
},
expected: expected{
state: v1alpha1.GameServerStateUnhealthy,
recordings: []string{string(v1alpha1.GameServerStateUnhealthy)},
recordings: []string{"Warning " + string(v1alpha1.GameServerStateUnhealthy)},
},
},
"label": {
Expand Down Expand Up @@ -844,7 +846,13 @@ func TestSDKServerAllocate(t *testing.T) {
}

func defaultSidecar(m agtesting.Mocks) (*SDKServer, error) {
return NewSDKServer("test", "default", m.KubeClient, m.AgonesClient)
server, err := NewSDKServer("test", "default", m.KubeClient, m.AgonesClient)
if err != nil {
return server, err
}

server.recorder = m.FakeRecorder
return server, err
}

func waitForMessage(sc *SDKServer) error {
Expand Down