Skip to content

Commit e256084

Browse files
0xaravindhigooch
andauthored
Enhance logging and error handling in computeDesiredFleetSize, including Chain policies (#4179)
* Add logging for policy type, error handling in computeDesiredFleetSize, and log errors in Chain policies * test-fix --------- Co-authored-by: igooch <igooch@google.com>
1 parent 503009b commit e256084

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

pkg/fleetautoscalers/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ func TestControllerSyncFleetAutoscaler(t *testing.T) {
691691

692692
err := c.syncFleetAutoscaler(ctx, "default/fas-1")
693693
if assert.NotNil(t, err) {
694-
assert.Equal(t, "error calculating autoscaling fleet: fleet-1: wrong policy type, should be one of: Buffer, Webhook, Counter, List", err.Error())
694+
assert.Equal(t, "error calculating autoscaling fleet: fleet-1: wrong policy type, should be one of: Buffer, Webhook, Counter, List, Schedule, Chain", err.Error())
695695
}
696696
})
697697

pkg/fleetautoscalers/fleetautoscalers.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,36 @@ func (InactiveScheduleError) Error() string {
6363
// computeDesiredFleetSize computes the new desired size of the given fleet
6464
func computeDesiredFleetSize(pol autoscalingv1.FleetAutoscalerPolicy, f *agonesv1.Fleet,
6565
gameServerNamespacedLister listeragonesv1.GameServerNamespaceLister, nodeCounts map[string]gameservers.NodeCount, fasLog *FasLogger) (int32, bool, error) {
66+
67+
var (
68+
replicas int32
69+
limited bool
70+
err error
71+
)
72+
6673
switch pol.Type {
6774
case autoscalingv1.BufferPolicyType:
68-
return applyBufferPolicy(pol.Buffer, f, fasLog)
75+
replicas, limited, err = applyBufferPolicy(pol.Buffer, f, fasLog)
6976
case autoscalingv1.WebhookPolicyType:
70-
return applyWebhookPolicy(pol.Webhook, f, fasLog)
77+
replicas, limited, err = applyWebhookPolicy(pol.Webhook, f, fasLog)
7178
case autoscalingv1.CounterPolicyType:
72-
return applyCounterOrListPolicyWrapper(pol.Counter, nil, f, gameServerNamespacedLister, nodeCounts, fasLog)
79+
replicas, limited, err = applyCounterOrListPolicyWrapper(pol.Counter, nil, f, gameServerNamespacedLister, nodeCounts, fasLog)
7380
case autoscalingv1.ListPolicyType:
74-
return applyCounterOrListPolicyWrapper(nil, pol.List, f, gameServerNamespacedLister, nodeCounts, fasLog)
81+
replicas, limited, err = applyCounterOrListPolicyWrapper(nil, pol.List, f, gameServerNamespacedLister, nodeCounts, fasLog)
7582
case autoscalingv1.SchedulePolicyType:
76-
return applySchedulePolicy(pol.Schedule, f, gameServerNamespacedLister, nodeCounts, time.Now(), fasLog)
83+
replicas, limited, err = applySchedulePolicy(pol.Schedule, f, gameServerNamespacedLister, nodeCounts, time.Now(), fasLog)
7784
case autoscalingv1.ChainPolicyType:
78-
return applyChainPolicy(pol.Chain, f, gameServerNamespacedLister, nodeCounts, time.Now(), fasLog)
85+
replicas, limited, err = applyChainPolicy(pol.Chain, f, gameServerNamespacedLister, nodeCounts, time.Now(), fasLog)
86+
default:
87+
err = errors.New("wrong policy type, should be one of: Buffer, Webhook, Counter, List, Schedule, Chain")
88+
}
89+
90+
if err != nil {
91+
loggerForFleetAutoscalerKey(fasLog.fas.ObjectMeta.Name, fasLog.baseLogger).
92+
Debugf("Failed to apply policy type %q: %v", pol.Type, err)
7993
}
8094

81-
return 0, false, errors.New("wrong policy type, should be one of: Buffer, Webhook, Counter, List")
95+
return replicas, limited, err
8296
}
8397

8498
// buildURLFromWebhookPolicy - build URL for Webhook and set CARoot for client Transport
@@ -451,11 +465,26 @@ func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServer
451465
switch entry.Type {
452466
case autoscalingv1.SchedulePolicyType:
453467
replicas, limited, err = applySchedulePolicy(entry.Schedule, f, gameServerNamespacedLister, nodeCounts, currentTime, fasLog)
468+
469+
if err != nil {
470+
loggerForFleetAutoscalerKey(fasLog.fas.ObjectMeta.Name, fasLog.baseLogger).Debugf(
471+
"Failed to apply SchedulePolicy ID=%s in ChainPolicy: %v", entry.ID, err)
472+
}
454473
case autoscalingv1.WebhookPolicyType:
455474
replicas, limited, err = applyWebhookPolicy(entry.Webhook, f, fasLog)
475+
476+
if err != nil {
477+
loggerForFleetAutoscalerKey(fasLog.fas.ObjectMeta.Name, fasLog.baseLogger).Debugf(
478+
"Failed to apply WebhookPolicy ID=%s in ChainPolicy: %v", entry.ID, err)
479+
}
456480
default:
457481
// Every other policy type we just want to compute the desired fleet and return it
458482
replicas, limited, err = computeDesiredFleetSize(entry.FleetAutoscalerPolicy, f, gameServerNamespacedLister, nodeCounts, fasLog)
483+
484+
if err != nil {
485+
loggerForFleetAutoscalerKey(fasLog.fas.ObjectMeta.Name, fasLog.baseLogger).Debugf(
486+
"Failed to apply %s ID=%s in ChainPolicy: %v", entry.Type, entry.ID, err)
487+
}
459488
}
460489

461490
// If no error occurred, exit the loop early

pkg/fleetautoscalers/fleetautoscalers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestComputeDesiredFleetSize(t *testing.T) {
178178
expected: expected{
179179
replicas: 0,
180180
limited: false,
181-
err: "wrong policy type, should be one of: Buffer, Webhook, Counter, List",
181+
err: "wrong policy type, should be one of: Buffer, Webhook, Counter, List, Schedule, Chain",
182182
},
183183
},
184184
}

0 commit comments

Comments
 (0)