Skip to content

Commit 7eb27b2

Browse files
committed
fix: improve error handling in GetAutoScalingReplicaCount and ManifestCreationService
1 parent a8c3fdf commit 7eb27b2

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

pkg/deployment/manifest/ManifestCreationService.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ func (impl *ManifestCreationServiceImpl) autoscalingCheckBeforeTrigger(ctx conte
982982

983983
hpaResourceRequest := helper.GetAutoScalingReplicaCount(templateMap, appName)
984984
impl.logger.Debugw("autoscalingCheckBeforeTrigger", "pipelineId", pipelineId, "hpaResourceRequest", hpaResourceRequest)
985-
if hpaResourceRequest.IsEnable {
985+
if hpaResourceRequest != nil && hpaResourceRequest.IsEnable {
986986
var resourceManifest map[string]interface{}
987987

988988
resourceManifest, err = impl.getK8sHPAResourceManifest(newCtx, clusterId, namespace, hpaResourceRequest)
@@ -991,7 +991,11 @@ func (impl *ManifestCreationServiceImpl) autoscalingCheckBeforeTrigger(ctx conte
991991
}
992992

993993
if len(resourceManifest) > 0 {
994-
statusMap := resourceManifest["status"].(map[string]interface{})
994+
statusMap, ok := resourceManifest["status"].(map[string]interface{})
995+
if !ok {
996+
impl.logger.Warnw("error occurred while parsing hpa resource manifest, status is not a map", "resourceManifestStatus", resourceManifest["status"])
997+
return merged, nil
998+
}
995999
currentReplicaVal := statusMap["currentReplicas"]
9961000
// currentReplicas key might not be available in manifest while k8s is calculating replica count
9971001
// it's a valid case so, we are not throwing error
@@ -1016,7 +1020,7 @@ func (impl *ManifestCreationServiceImpl) autoscalingCheckBeforeTrigger(ctx conte
10161020
impl.logger.Debugw("autoscaling is not enabled", "pipelineId", pipelineId)
10171021
}
10181022

1019-
//check for custom chart support
1023+
// check for custom chart support
10201024
if autoscalingEnabledPath, ok := templateMap[appBean.CustomAutoScalingEnabledPathKey]; ok {
10211025
if deploymentType == models.DEPLOYMENTTYPE_STOP {
10221026
merged, err = helper.SetScalingValues(templateMap, appBean.CustomAutoScalingEnabledPathKey, merged, false)

pkg/deployment/manifest/helper/helper.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func FetchRequiredReplicaCount(currentReplicaCount float64, reqMaxReplicas float
9797
}
9898

9999
func GetAutoScalingReplicaCount(templateMap map[string]interface{}, appName string) *util4.HpaResourceRequest {
100+
hpaResourceRequest := &util4.HpaResourceRequest{}
100101
hasOverride := false
101102
if _, ok := templateMap[bean3.FullnameOverride]; ok {
102103
appNameOverride := templateMap[bean3.FullnameOverride].(string)
@@ -113,37 +114,66 @@ func GetAutoScalingReplicaCount(templateMap map[string]interface{}, appName stri
113114
}
114115
}
115116
}
116-
hpaResourceRequest := &util4.HpaResourceRequest{}
117117
hpaResourceRequest.Version = ""
118118
hpaResourceRequest.Group = autoscaling.ServiceName
119119
hpaResourceRequest.Kind = bean3.HorizontalPodAutoscaler
120120
if _, ok := templateMap[bean3.KedaAutoscaling]; ok {
121121
as := templateMap[bean3.KedaAutoscaling]
122-
asd := as.(map[string]interface{})
122+
asd, ok := as.(map[string]interface{})
123+
if !ok {
124+
return hpaResourceRequest
125+
}
123126
if _, ok := asd[bean3.Enabled]; ok {
124-
enable := asd[bean3.Enabled].(bool)
127+
enable, ok := asd[bean3.Enabled].(bool)
128+
if !ok {
129+
return hpaResourceRequest
130+
}
125131
if enable {
126-
hpaResourceRequest.IsEnable = enable
127-
hpaResourceRequest.ReqReplicaCount = templateMap[bean3.ReplicaCount].(float64)
128-
hpaResourceRequest.ReqMaxReplicas = asd["maxReplicaCount"].(float64)
129-
hpaResourceRequest.ReqMinReplicas = asd["minReplicaCount"].(float64)
130132
hpaResourceRequest.ResourceName = fmt.Sprintf("%s-%s-%s", "keda-hpa", appName, "keda")
133+
hpaResourceRequest.ReqReplicaCount, ok = templateMap[bean3.ReplicaCount].(float64)
134+
if !ok {
135+
return hpaResourceRequest
136+
}
137+
hpaResourceRequest.ReqMaxReplicas, ok = asd["maxReplicaCount"].(float64)
138+
if !ok {
139+
return hpaResourceRequest
140+
}
141+
hpaResourceRequest.ReqMinReplicas, ok = asd["minReplicaCount"].(float64)
142+
if !ok {
143+
return hpaResourceRequest
144+
}
145+
hpaResourceRequest.IsEnable = true
131146
return hpaResourceRequest
132147
}
133148
}
134149
}
135150

136151
if _, ok := templateMap[autoscaling.ServiceName]; ok {
137152
as := templateMap[autoscaling.ServiceName]
138-
asd := as.(map[string]interface{})
153+
asd, ok := as.(map[string]interface{})
154+
if !ok {
155+
return hpaResourceRequest
156+
}
139157
if _, ok := asd[bean3.Enabled]; ok {
140-
enable := asd[bean3.Enabled].(bool)
158+
enable, ok := asd[bean3.Enabled].(bool)
159+
if !ok {
160+
return hpaResourceRequest
161+
}
141162
if enable {
142-
hpaResourceRequest.IsEnable = asd[bean3.Enabled].(bool)
143-
hpaResourceRequest.ReqReplicaCount = templateMap[bean3.ReplicaCount].(float64)
144-
hpaResourceRequest.ReqMaxReplicas = asd["MaxReplicas"].(float64)
145-
hpaResourceRequest.ReqMinReplicas = asd["MinReplicas"].(float64)
146163
hpaResourceRequest.ResourceName = fmt.Sprintf("%s-%s", appName, "hpa")
164+
hpaResourceRequest.ReqReplicaCount, ok = templateMap[bean3.ReplicaCount].(float64)
165+
if !ok {
166+
return hpaResourceRequest
167+
}
168+
hpaResourceRequest.ReqMaxReplicas, ok = asd["MaxReplicas"].(float64)
169+
if !ok {
170+
return hpaResourceRequest
171+
}
172+
hpaResourceRequest.ReqMinReplicas, ok = asd["MinReplicas"].(float64)
173+
if !ok {
174+
return hpaResourceRequest
175+
}
176+
hpaResourceRequest.IsEnable = true
147177
return hpaResourceRequest
148178
}
149179
}

0 commit comments

Comments
 (0)