diff --git a/pkg/fleetautoscalers/controller.go b/pkg/fleetautoscalers/controller.go index abc1e61945..a9a39a5443 100644 --- a/pkg/fleetautoscalers/controller.go +++ b/pkg/fleetautoscalers/controller.go @@ -313,7 +313,8 @@ func (c *Controller) syncFleetAutoscaler(ctx context.Context, key string) error } currentReplicas := fleet.Status.Replicas - desiredReplicas, scalingLimited, err := computeDesiredFleetSize(fas.Spec.Policy, fleet, c.gameServerLister, c.counter.Counts()) + gameServerNamespacedLister := c.gameServerLister.GameServers(fleet.Namespace) + desiredReplicas, scalingLimited, err := computeDesiredFleetSize(fas.Spec.Policy, fleet, gameServerNamespacedLister, c.counter.Counts()) // If there err is nil and not an inactive schedule error (ignorable in this case), then record the event if err != nil && !errors.Is(err, InactiveScheduleError{}) { c.recorder.Eventf(fas, corev1.EventTypeWarning, "FleetAutoscaler", diff --git a/pkg/fleetautoscalers/fleetautoscalers.go b/pkg/fleetautoscalers/fleetautoscalers.go index 0e7cc428ed..691166d5f6 100644 --- a/pkg/fleetautoscalers/fleetautoscalers.go +++ b/pkg/fleetautoscalers/fleetautoscalers.go @@ -61,20 +61,20 @@ func (InactiveScheduleError) Error() string { // computeDesiredFleetSize computes the new desired size of the given fleet func computeDesiredFleetSize(pol autoscalingv1.FleetAutoscalerPolicy, f *agonesv1.Fleet, - gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount) (int32, bool, error) { + gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount) (int32, bool, error) { switch pol.Type { case autoscalingv1.BufferPolicyType: return applyBufferPolicy(pol.Buffer, f) case autoscalingv1.WebhookPolicyType: return applyWebhookPolicy(pol.Webhook, f) case autoscalingv1.CounterPolicyType: - return applyCounterOrListPolicy(pol.Counter, nil, f, gameServerLister, nodeCounts) + return applyCounterOrListPolicy(pol.Counter, nil, f, gameServerNamespacedLister, nodeCounts) case autoscalingv1.ListPolicyType: - return applyCounterOrListPolicy(nil, pol.List, f, gameServerLister, nodeCounts) + return applyCounterOrListPolicy(nil, pol.List, f, gameServerNamespacedLister, nodeCounts) case autoscalingv1.SchedulePolicyType: - return applySchedulePolicy(pol.Schedule, f, gameServerLister, nodeCounts, time.Now()) + return applySchedulePolicy(pol.Schedule, f, gameServerNamespacedLister, nodeCounts, time.Now()) case autoscalingv1.ChainPolicyType: - return applyChainPolicy(pol.Chain, f, gameServerLister, nodeCounts, time.Now()) + return applyChainPolicy(pol.Chain, f, gameServerNamespacedLister, nodeCounts, time.Now()) } return 0, false, errors.New("wrong policy type, should be one of: Buffer, Webhook, Counter, List") @@ -252,7 +252,7 @@ func applyBufferPolicy(b *autoscalingv1.BufferPolicy, f *agonesv1.Fleet) (int32, } func applyCounterOrListPolicy(c *autoscalingv1.CounterPolicy, l *autoscalingv1.ListPolicy, - f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, + f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount) (int32, bool, error) { if !runtime.FeatureEnabled(runtime.FeatureCountsAndLists) { @@ -350,23 +350,23 @@ func applyCounterOrListPolicy(c *autoscalingv1.CounterPolicy, l *autoscalingv1.L switch availableCapacity := aggCapacity - aggCount; { case availableCapacity == buffer: if limited { - return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas, + return scaleLimited(scale, f, gameServerNamespacedLister, nodeCounts, key, isCounter, replicas, capacity, aggCapacity, minCapacity, maxCapacity) } return replicas, false, nil case availableCapacity < buffer: // Scale Up if limited { // Case where we want to scale up but we're already limited by MaxCapacity. - return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas, + return scaleLimited(scale, f, gameServerNamespacedLister, nodeCounts, key, isCounter, replicas, capacity, aggCapacity, minCapacity, maxCapacity) } return scaleUp(replicas, capacity, count, aggCapacity, availableCapacity, maxCapacity, minCapacity, buffer) case availableCapacity > buffer: // Scale Down if limited && scale == 1 { // Case where we want to scale down but we're already limited by MinCapacity - return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas, + return scaleLimited(scale, f, gameServerNamespacedLister, nodeCounts, key, isCounter, replicas, capacity, aggCapacity, minCapacity, maxCapacity) } - return scaleDown(f, gameServerLister, nodeCounts, key, isCounter, replicas, aggCount, + return scaleDown(f, gameServerNamespacedLister, nodeCounts, key, isCounter, replicas, aggCount, aggCapacity, minCapacity, buffer) } @@ -376,21 +376,21 @@ func applyCounterOrListPolicy(c *autoscalingv1.CounterPolicy, l *autoscalingv1.L return 0, false, errors.Errorf("unable to apply ListPolicy %v", l) } -func applySchedulePolicy(s *autoscalingv1.SchedulePolicy, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) { +func applySchedulePolicy(s *autoscalingv1.SchedulePolicy, f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) { // Ensure the scheduled autoscaler feature gate is enabled if !runtime.FeatureEnabled(runtime.FeatureScheduledAutoscaler) { return 0, false, errors.Errorf("cannot apply SchedulePolicy unless feature flag %s is enabled", runtime.FeatureScheduledAutoscaler) } if isScheduleActive(s, currentTime) { - return computeDesiredFleetSize(s.Policy, f, gameServerLister, nodeCounts) + return computeDesiredFleetSize(s.Policy, f, gameServerNamespacedLister, nodeCounts) } // If the schedule wasn't active then return the current replica amount of the fleet return f.Status.Replicas, false, &InactiveScheduleError{} } -func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) { +func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) { // Ensure the scheduled autoscaler feature gate is enabled if !runtime.FeatureEnabled(runtime.FeatureScheduledAutoscaler) { return 0, false, errors.Errorf("cannot apply ChainPolicy unless feature flag %s is enabled", runtime.FeatureScheduledAutoscaler) @@ -404,7 +404,7 @@ func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServer for _, entry := range c { switch entry.Type { case autoscalingv1.SchedulePolicyType: - replicas, limited, err = applySchedulePolicy(entry.Schedule, f, gameServerLister, nodeCounts, currentTime) + replicas, limited, err = applySchedulePolicy(entry.Schedule, f, gameServerNamespacedLister, nodeCounts, currentTime) // If no error was returned from the schedule policy (schedule is active and/or webhook policy within schedule was successful), then return the values given if err == nil { return replicas, limited, nil @@ -417,7 +417,7 @@ func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServer } default: // Every other policy type we just want to compute the desired fleet and return it - return computeDesiredFleetSize(entry.FleetAutoscalerPolicy, f, gameServerLister, nodeCounts) + return computeDesiredFleetSize(entry.FleetAutoscalerPolicy, f, gameServerNamespacedLister, nodeCounts) } } @@ -482,12 +482,13 @@ func isScheduleActive(s *autoscalingv1.SchedulePolicy, currentTime time.Time) bo // getSortedGameServers returns the list of Game Servers for the Fleet in the order in which the // Game Servers would be deleted. -func getSortedGameServers(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, +func getSortedGameServers(f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount) ([]*agonesv1.GameServer, error) { - gsList, err := fleets.ListGameServersByFleetOwner(gameServerLister, f) + gsList, err := fleets.ListGameServersByFleetOwner(gameServerNamespacedLister, f) if err != nil { return nil, err } + gameServers := gssets.SortGameServersByStrategy(f.Spec.Scheduling, gsList, nodeCounts, f.Spec.Priorities) return gameServers, nil } @@ -519,11 +520,11 @@ func scaleUpLimited(replicas int32, capacity, aggCapacity, minCapacity int64) (i } // scaleDownLimited scales down the fleet to meet the MaxCapacity -func scaleDownLimited(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, +func scaleDownLimited(f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32, aggCapacity, maxCapacity int64) (int32, bool, error) { // Game Servers in order of deletion on scale down - gameServers, err := getSortedGameServers(f, gameServerLister, nodeCounts) + gameServers, err := getSortedGameServers(f, gameServerNamespacedLister, nodeCounts) if err != nil { return 0, false, err } @@ -552,7 +553,7 @@ func scaleDownLimited(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameSer return replicas, true, nil } -func scaleLimited(scale int, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, +func scaleLimited(scale int, f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32, capacity, aggCapacity, minCapacity, maxCapacity int64) (int32, bool, error) { @@ -560,7 +561,7 @@ func scaleLimited(scale int, f *agonesv1.Fleet, gameServerLister listeragonesv1. case 1: // scale up return scaleUpLimited(replicas, capacity, aggCapacity, minCapacity) case -1: // scale down - return scaleDownLimited(f, gameServerLister, nodeCounts, key, isCounter, replicas, + return scaleDownLimited(f, gameServerNamespacedLister, nodeCounts, key, isCounter, replicas, aggCapacity, maxCapacity) case 0: return replicas, false, nil @@ -591,7 +592,7 @@ func scaleUp(replicas int32, capacity, count, aggCapacity, availableCapacity, ma } // scaleDown scales down for either Integer or Percentage Buffer. -func scaleDown(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, +func scaleDown(f *agonesv1.Fleet, gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32, aggCount, aggCapacity, minCapacity, buffer int64) (int32, bool, error) { // Exit early if we're already at MinCapacity to avoid calling getSortedGameServers if unnecessary @@ -601,7 +602,7 @@ func scaleDown(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerList // We first need to get the individual game servers in order of deletion on scale down, as any // game server may have a unique value for counts and / or capacity. - gameServers, err := getSortedGameServers(f, gameServerLister, nodeCounts) + gameServers, err := getSortedGameServers(f, gameServerNamespacedLister, nodeCounts) if err != nil { return 0, false, err } diff --git a/pkg/fleetautoscalers/fleetautoscalers_test.go b/pkg/fleetautoscalers/fleetautoscalers_test.go index 5a67717363..56a0ab6d94 100644 --- a/pkg/fleetautoscalers/fleetautoscalers_test.go +++ b/pkg/fleetautoscalers/fleetautoscalers_test.go @@ -189,7 +189,7 @@ func TestComputeDesiredFleetSize(t *testing.T) { _, cancel := agtesting.StartInformers(m, gameServers.Informer().HasSynced) defer cancel() - replicas, limited, err := computeDesiredFleetSize(fas.Spec.Policy, f, gameServers.Lister(), nc) + replicas, limited, err := computeDesiredFleetSize(fas.Spec.Policy, f, gameServers.Lister().GameServers(f.Namespace), nc) if tc.expected.err != "" && assert.NotNil(t, err) { assert.Equal(t, tc.expected.err, err.Error()) @@ -878,7 +878,8 @@ func TestApplyCounterPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", + Name: "gs1", + Namespace: "default", // We need the Label here so that the Lister can pick up that the gameserver is a part of // the fleet. If this was a real gameserver it would also have a label for // "agones.dev/gameserverset": "gameServerSetName". @@ -894,8 +895,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -904,8 +906,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -914,8 +917,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 7, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs4", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs4", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -924,8 +928,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 14, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs5", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs5", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -934,8 +939,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 13, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs6", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs6", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -944,8 +950,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 7, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs7", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs7", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -954,8 +961,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 7, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs8", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs8", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1102,8 +1110,9 @@ func TestApplyCounterPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1112,8 +1121,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1122,8 +1132,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1132,8 +1143,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 7, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs4", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs4", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1264,8 +1276,9 @@ func TestApplyCounterPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1274,8 +1287,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1284,8 +1298,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1413,8 +1428,9 @@ func TestApplyCounterPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1423,8 +1439,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1433,8 +1450,9 @@ func TestApplyCounterPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Counters: map[string]agonesv1.CounterStatus{ @@ -1469,7 +1487,7 @@ func TestApplyCounterPolicy(t *testing.T) { informer.GameServers().Informer().HasSynced) defer cancel() - replicas, limited, err := applyCounterOrListPolicy(tc.cp, nil, tc.fleet, informer.GameServers().Lister(), nc) + replicas, limited, err := applyCounterOrListPolicy(tc.cp, nil, tc.fleet, informer.GameServers().Lister().GameServers(tc.fleet.Namespace), nc) if tc.want.wantErr { assert.NotNil(t, err) @@ -1701,8 +1719,9 @@ func TestApplyListPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1711,8 +1730,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1721,8 +1741,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1731,8 +1752,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs4", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs4", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1741,8 +1763,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 8, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs5", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs5", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1751,8 +1774,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs6", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs6", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1761,8 +1785,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 4, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs7", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs7", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1771,8 +1796,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 8, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs8", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs8", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1840,8 +1866,9 @@ func TestApplyListPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1850,8 +1877,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1860,8 +1888,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -1870,8 +1899,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs4", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs4", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2035,8 +2065,9 @@ func TestApplyListPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2045,8 +2076,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 15, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2055,8 +2087,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2065,8 +2098,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs4", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs4", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n2", Lists: map[string]agonesv1.ListStatus{ @@ -2075,8 +2109,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 5, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs5", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs5", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n2", Lists: map[string]agonesv1.ListStatus{ @@ -2118,8 +2153,9 @@ func TestApplyListPolicy(t *testing.T) { }, gsList: []agonesv1.GameServer{ {ObjectMeta: metav1.ObjectMeta{ - Name: "gs1", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs1", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2128,8 +2164,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 15, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs2", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs2", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2138,8 +2175,9 @@ func TestApplyListPolicy(t *testing.T) { Capacity: 10, }}}}, {ObjectMeta: metav1.ObjectMeta{ - Name: "gs3", - Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, + Name: "gs3", + Namespace: "default", + Labels: map[string]string{"agones.dev/fleet": "fleet-1"}}, Status: agonesv1.GameServerStatus{ NodeName: "n1", Lists: map[string]agonesv1.ListStatus{ @@ -2174,7 +2212,7 @@ func TestApplyListPolicy(t *testing.T) { informer.GameServers().Informer().HasSynced) defer cancel() - replicas, limited, err := applyCounterOrListPolicy(nil, tc.lp, tc.fleet, informer.GameServers().Lister(), nc) + replicas, limited, err := applyCounterOrListPolicy(nil, tc.lp, tc.fleet, informer.GameServers().Lister().GameServers(tc.fleet.Namespace), nc) if tc.want.wantErr { assert.NotNil(t, err) diff --git a/pkg/fleets/fleets.go b/pkg/fleets/fleets.go index 28c1674430..b53521543b 100644 --- a/pkg/fleets/fleets.go +++ b/pkg/fleets/fleets.go @@ -44,10 +44,10 @@ func ListGameServerSetsByFleetOwner(gameServerSetLister listerv1.GameServerSetLi // ListGameServersByFleetOwner lists all GameServers that belong to a fleet through the // GameServer -> GameServerSet -> Fleet owner chain -func ListGameServersByFleetOwner(gameServerLister listerv1.GameServerLister, +func ListGameServersByFleetOwner(gameServerNamespacedLister listerv1.GameServerNamespaceLister, fleet *agonesv1.Fleet) ([]*agonesv1.GameServer, error) { - list, err := gameServerLister.List(labels.SelectorFromSet(labels.Set{agonesv1.FleetNameLabel: fleet.ObjectMeta.Name})) + list, err := gameServerNamespacedLister.List(labels.SelectorFromSet(labels.Set{agonesv1.FleetNameLabel: fleet.ObjectMeta.Name})) if err != nil { return list, errors.Wrapf(err, "error listing gameservers for fleets %s", fleet.ObjectMeta.Name) } diff --git a/pkg/fleets/fleets_test.go b/pkg/fleets/fleets_test.go index e933123649..13bbc8526f 100644 --- a/pkg/fleets/fleets_test.go +++ b/pkg/fleets/fleets_test.go @@ -100,7 +100,7 @@ func TestListGameServersByFleetOwner(t *testing.T) { informer.GameServers().Informer().HasSynced) defer cancel() - list, err := ListGameServersByFleetOwner(informer.GameServers().Lister(), f) + list, err := ListGameServersByFleetOwner(informer.GameServers().Lister().GameServers(f.Namespace), f) require.NoError(t, err) assert.Len(t, list, len(gsList), "Retrieved list should be same size as original")