From 92c4a0078dab0bbcafe5cd9a93505f173d541714 Mon Sep 17 00:00:00 2001 From: Ivy Gooch Date: Wed, 15 Feb 2023 21:56:10 +0000 Subject: [PATCH] Pulls feature gate validation out of the validate func --- examples/gameserver.yaml | 8 +++--- pkg/apis/agones/v1/gameserver.go | 35 ++++++++++++++++++--------- pkg/apis/agones/v1/gameserver_test.go | 8 +++--- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/examples/gameserver.yaml b/examples/gameserver.yaml index bd81b9cea3..330edda047 100644 --- a/examples/gameserver.yaml +++ b/examples/gameserver.yaml @@ -91,16 +91,16 @@ spec: # Counts and Lists provides the configuration for generic (player, room, session, etc.) tracking features. # Commented out since Alpha, and disabled by default # counters: - # gameCounter: + # games: # count: 1 # capacity: 100 - # sessionCounter: + # sessions: # capacity: 999 # lists: - # playerList: + # players: # capacity: 1000 # values: - # roomList: + # rooms: # capacity: 333 # values: # - room1 diff --git a/pkg/apis/agones/v1/gameserver.go b/pkg/apis/agones/v1/gameserver.go index 9caf0581ed..fe8e8315d4 100644 --- a/pkg/apis/agones/v1/gameserver.go +++ b/pkg/apis/agones/v1/gameserver.go @@ -189,8 +189,8 @@ type GameServerSpec struct { Players *PlayersSpec `json:"players,omitempty"` // (Alpha, CountsAndLists feature flag) Counters and Lists provides the configuration for generic tracking features. // +optional - Counters *CountersSpec `json:"counters,omitempty"` - Lists *ListsSpec `json:"lists,omitempty"` + Counters map[string]*CounterSpec `json:"counters,omitempty"` + Lists map[string]*ListSpec `json:"lists,omitempty"` // (Alpha, SafeToEvict feature flag) Eviction specifies the eviction tolerance of the GameServer. Defaults to "Never". // +optional Eviction Eviction `json:"eviction,omitempty"` @@ -202,13 +202,16 @@ type PlayersSpec struct { InitialCapacity int64 `json:"initialCapacity,omitempty"` } -// CountersSpec tracks if counter specified (for giving error message if feature gate not set) -type CountersSpec struct { +// CounterSpec tracks if counter specified (for giving error message if feature gate not set) +type CounterSpec struct { + Count int64 `json:"count,omitempty"` + Capacity int64 `json:"capacity,omitempty"` } -// ListsSpec tracks the list capacity -type ListsSpec struct { - Capacity int64 `json:"capacity,omitempty"` +// ListSpec tracks the list capacity +type ListSpec struct { + Capacity int64 `json:"capacity,omitempty"` + Values []string `json:"values,omitempty"` } // Eviction specifies the eviction tolerance of the GameServer @@ -434,11 +437,8 @@ func (gs *GameServer) applyEvictionStatus() { } } -// Validate validates the GameServerSpec configuration. -// devAddress is a specific IP address used for local Gameservers, for fleets "" is used -// If a GameServer Spec is invalid there will be > 0 values in -// the returned array -func (gss *GameServerSpec) Validate(apiHooks APIHooks, devAddress string) ([]metav1.StatusCause, bool) { +// validateFeatureGates checks if fields are set when the associated feature gate is not set. +func (gss *GameServerSpec) validateFeatureGates() []metav1.StatusCause { var causes []metav1.StatusCause if !runtime.FeatureEnabled(runtime.FeaturePlayerTracking) { @@ -478,6 +478,17 @@ func (gss *GameServerSpec) Validate(apiHooks APIHooks, devAddress string) ([]met } } + return causes +} + +// Validate validates the GameServerSpec configuration. +// devAddress is a specific IP address used for local Gameservers, for fleets "" is used +// If a GameServer Spec is invalid there will be > 0 values in the returned array +func (gss *GameServerSpec) Validate(apiHooks APIHooks, devAddress string) ([]metav1.StatusCause, bool) { + var causes []metav1.StatusCause + + causes = append(causes, gss.validateFeatureGates()...) + if devAddress != "" { // verify that the value is a valid IP address. if net.ParseIP(devAddress) == nil { diff --git a/pkg/apis/agones/v1/gameserver_test.go b/pkg/apis/agones/v1/gameserver_test.go index 4f45e063d8..d8058c9e24 100644 --- a/pkg/apis/agones/v1/gameserver_test.go +++ b/pkg/apis/agones/v1/gameserver_test.go @@ -1118,7 +1118,7 @@ func TestGameServerValidateFeatures(t *testing.T) { gs: GameServer{ Spec: GameServerSpec{ Container: "testing", - Counters: &CountersSpec{}, + Counters: map[string]*CounterSpec{}, Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}}, }, @@ -1133,7 +1133,7 @@ func TestGameServerValidateFeatures(t *testing.T) { gs: GameServer{ Spec: GameServerSpec{ Container: "testing", - Lists: &ListsSpec{Capacity: 100}, + Lists: map[string]*ListSpec{}, Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}}, }, @@ -1148,7 +1148,7 @@ func TestGameServerValidateFeatures(t *testing.T) { gs: GameServer{ Spec: GameServerSpec{ Container: "testing", - Counters: &CountersSpec{}, + Counters: map[string]*CounterSpec{}, Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}}, }, @@ -1161,7 +1161,7 @@ func TestGameServerValidateFeatures(t *testing.T) { gs: GameServer{ Spec: GameServerSpec{ Container: "testing", - Lists: &ListsSpec{Capacity: 100}, + Lists: map[string]*ListSpec{}, Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}}, },