Skip to content

Commit

Permalink
Add status subresource for Fleet and Gameserverset
Browse files Browse the repository at this point in the history
Switch to using UpdateStatus function when it is necessary, add RBAC
permissions for "fleets/status" and "gameserversets/status".
  • Loading branch information
aLekSer committed Feb 21, 2019
1 parent 861771b commit b146278
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions install/helm/agones/templates/crds/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ spec:
template:
{{- include "gameserver.validation" . | indent 14 }}
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
Expand Down
2 changes: 2 additions & 0 deletions install/helm/agones/templates/crds/gameserverset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ spec:
template:
{{- include "gameserver.validation" . | indent 14 }}
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
Expand Down
6 changes: 6 additions & 0 deletions install/helm/agones/templates/serviceaccounts/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ rules:
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers", "gameserversets"]
verbs: ["create", "delete", "get", "list", "update", "watch"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameserversets/status"]
verbs: ["update"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers"]
verbs: ["patch"]
- apiGroups: ["stable.agones.dev"]
resources: ["fleets", "fleetallocations", "fleetautoscalers"]
verbs: ["get", "list", "update", "watch"]
- apiGroups: ["stable.agones.dev"]
resources: ["fleets/status"]
verbs: ["update"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameserverallocations"]
verbs: ["list", "watch", "delete"]
Expand Down
10 changes: 10 additions & 0 deletions install/yaml/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ rules:
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers", "gameserversets"]
verbs: ["create", "delete", "get", "list", "update", "watch"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameserversets/status"]
verbs: ["update"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameservers"]
verbs: ["patch"]
- apiGroups: ["stable.agones.dev"]
resources: ["fleets", "fleetallocations", "fleetautoscalers"]
verbs: ["get", "list", "update", "watch"]
- apiGroups: ["stable.agones.dev"]
resources: ["fleets/status"]
verbs: ["update"]
- apiGroups: ["stable.agones.dev"]
resources: ["gameserverallocations"]
verbs: ["list", "watch", "delete"]
Expand Down Expand Up @@ -328,6 +334,8 @@ spec:
minimum: 1
maximum: 2147483648
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
Expand Down Expand Up @@ -936,6 +944,8 @@ spec:
minimum: 1
maximum: 2147483648
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
Expand Down
17 changes: 15 additions & 2 deletions pkg/fleets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,24 @@ func (c *Controller) syncFleet(key string) error {
func (c *Controller) upsertGameServerSet(fleet *stablev1alpha1.Fleet, active *stablev1alpha1.GameServerSet, replicas int32) error {
if active.ObjectMeta.UID == "" {
active.Spec.Replicas = replicas
gsSet, err := c.gameServerSetGetter.GameServerSets(active.ObjectMeta.Namespace).Create(active)
gsSets := c.gameServerSetGetter.GameServerSets(active.ObjectMeta.Namespace)
gsSet, err := gsSets.Create(active)
if err != nil {
return errors.Wrapf(err, "error creating gameserverset for fleet %s", fleet.ObjectMeta.Name)
}

// extra step which is needed to set
// default values for GameServerSet Status Subresource
gsSetCopy := gsSet.DeepCopy()
gsSetCopy.Status.ReadyReplicas = 0
gsSetCopy.Status.Replicas = 0
gsSetCopy.Status.AllocatedReplicas = 0
_, err = gsSets.UpdateStatus(gsSetCopy)
if err != nil {
return errors.Wrapf(err, "error updating status of gameserverset for fleet %s",
fleet.ObjectMeta.Name)
}

c.recorder.Eventf(fleet, corev1.EventTypeNormal, "CreatingGameServerSet",
"Created GameServerSet %s", gsSet.ObjectMeta.Name)
return nil
Expand Down Expand Up @@ -492,7 +505,7 @@ func (c *Controller) updateFleetStatus(fleet *stablev1alpha1.Fleet) error {
fCopy.Status.AllocatedReplicas += gsSet.Status.AllocatedReplicas
}

_, err = c.fleetGetter.Fleets(fCopy.Namespace).Update(fCopy)
_, err = c.fleetGetter.Fleets(fCopy.Namespace).UpdateStatus(fCopy)
return errors.Wrapf(err, "error updating status of fleet %s", fCopy.ObjectMeta.Name)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/fleets/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ func TestControllerSyncFleet(t *testing.T) {
m.AgonesClient.AddReactor("update", "gameserversets", func(action k8stesting.Action) (bool, runtime.Object, error) {
updated = true
ua := action.(k8stesting.UpdateAction)
// separate update of status subresource
if ua.GetSubresource() != "" {
assert.Equal(t, ua.GetSubresource(), "status")
return true, nil, nil
}
// update main resource
gsSet := ua.GetObject().(*v1alpha1.GameServerSet)
assert.Equal(t, int32(3), gsSet.Spec.Replicas)
assert.Equal(t, "gsSet1", gsSet.ObjectMeta.Name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gameserversets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (c *Controller) updateStatusIfChanged(gsSet *v1alpha1.GameServerSet, status
if gsSet.Status != status {
gsSetCopy := gsSet.DeepCopy()
gsSetCopy.Status = status
_, err := c.gameServerSetGetter.GameServerSets(gsSet.ObjectMeta.Namespace).Update(gsSetCopy)
_, err := c.gameServerSetGetter.GameServerSets(gsSet.ObjectMeta.Namespace).UpdateStatus(gsSetCopy)
if err != nil {
return errors.Wrapf(err, "error updating status on GameServerSet %s", gsSet.ObjectMeta.Name)
}
Expand Down

0 comments on commit b146278

Please sign in to comment.