@@ -42,21 +42,6 @@ const (
4242
4343var emptyDirVolumeSource = corev1.VolumeSource {EmptyDir : & corev1.EmptyDirVolumeSource {}}
4444
45- func autoscalingEnabled (dep * ngfAPIv1alpha2.DeploymentSpec ) bool {
46- return dep != nil && dep .Autoscaling .Enabled
47- }
48-
49- func cloneHPAAnnotationMap (src map [string ]string ) map [string ]string {
50- if src == nil {
51- return nil
52- }
53- annotations := make (map [string ]string , len (src ))
54- for k , v := range src {
55- annotations [k ] = v
56- }
57- return annotations
58- }
59-
6045func (p * NginxProvisioner ) buildNginxResourceObjects (
6146 resourceName string ,
6247 gateway * gatewayv1.Gateway ,
@@ -179,23 +164,48 @@ func (p *NginxProvisioner) buildNginxResourceObjects(
179164 objects = append (objects , openshiftObjs ... )
180165 }
181166
182- if nProxyCfg != nil && nProxyCfg .Kubernetes != nil {
183- if autoscalingEnabled (nProxyCfg .Kubernetes .Deployment ) {
184- objectMeta .Annotations = cloneHPAAnnotationMap (nProxyCfg .Kubernetes .Deployment .Autoscaling .HPAAnnotations )
185- hpa := buildNginxDeploymentHPA (
186- objectMeta ,
187- nProxyCfg ,
188- )
189- objects = append (objects , service , deployment , hpa )
190- return objects , nil
191- }
192- }
193-
194167 objects = append (objects , service , deployment )
195168
169+ if hpa := p .buildHPAIfEnabled (objectMeta , nProxyCfg ); hpa != nil {
170+ objects = append (objects , hpa )
171+ }
172+
196173 return objects , err
197174}
198175
176+ func autoscalingEnabled (dep * ngfAPIv1alpha2.DeploymentSpec ) bool {
177+ return dep != nil && dep .Autoscaling .Enabled
178+ }
179+
180+ func cloneHPAAnnotationMap (src map [string ]string ) map [string ]string {
181+ if src == nil {
182+ return nil
183+ }
184+ annotations := make (map [string ]string , len (src ))
185+ for k , v := range src {
186+ annotations [k ] = v
187+ }
188+ return annotations
189+ }
190+
191+ func (p * NginxProvisioner ) buildHPAIfEnabled (
192+ objectMeta metav1.ObjectMeta ,
193+ nProxyCfg * graph.EffectiveNginxProxy ,
194+ ) client.Object {
195+ if nProxyCfg == nil || nProxyCfg .Kubernetes == nil {
196+ return nil
197+ }
198+
199+ if ! autoscalingEnabled (nProxyCfg .Kubernetes .Deployment ) {
200+ return nil
201+ }
202+
203+ hpaAnnotations := cloneHPAAnnotationMap (nProxyCfg .Kubernetes .Deployment .Autoscaling .HPAAnnotations )
204+ objectMeta .Annotations = hpaAnnotations
205+
206+ return buildNginxDeploymentHPA (objectMeta , nProxyCfg )
207+ }
208+
199209func (p * NginxProvisioner ) buildNginxSecrets (
200210 objectMeta metav1.ObjectMeta ,
201211 agentTLSSecretName string ,
@@ -926,22 +936,18 @@ func (p *NginxProvisioner) buildImage(nProxyCfg *graph.EffectiveNginxProxy) (str
926936 return fmt .Sprintf ("%s:%s" , image , tag ), pullPolicy
927937}
928938
929- func getMetricTargetByType (
930- target autoscalingv2.MetricTarget ,
931- ) autoscalingv2.MetricTarget {
939+ func getMetricTargetByType (target autoscalingv2.MetricTarget ) autoscalingv2.MetricTarget {
932940 switch target .Type {
933941 case autoscalingv2 .UtilizationMetricType :
934942 return autoscalingv2.MetricTarget {
935943 Type : autoscalingv2 .UtilizationMetricType ,
936944 AverageUtilization : target .AverageUtilization ,
937945 }
938-
939946 case autoscalingv2 .AverageValueMetricType :
940947 return autoscalingv2.MetricTarget {
941948 Type : autoscalingv2 .AverageValueMetricType ,
942949 AverageValue : target .AverageValue ,
943950 }
944-
945951 case autoscalingv2 .ValueMetricType :
946952 return autoscalingv2.MetricTarget {
947953 Type : autoscalingv2 .ValueMetricType ,
@@ -994,59 +1000,9 @@ func buildNginxDeploymentHPA(
9941000
9951001 if autoscalingTemplate != nil {
9961002 for _ , additionalAutoscaling := range * autoscalingTemplate {
997- switch additionalAutoscaling .Type {
998- case autoscalingv2 .ResourceMetricSourceType :
999- metrics = append (metrics , autoscalingv2.MetricSpec {
1000- Type : additionalAutoscaling .Type ,
1001- Resource : & autoscalingv2.ResourceMetricSource {
1002- Name : additionalAutoscaling .Resource .Name ,
1003- Target : getMetricTargetByType (additionalAutoscaling .Resource .Target ),
1004- },
1005- })
1006-
1007- case autoscalingv2 .PodsMetricSourceType :
1008- metrics = append (metrics , autoscalingv2.MetricSpec {
1009- Type : additionalAutoscaling .Type ,
1010- Pods : & autoscalingv2.PodsMetricSource {
1011- Metric : additionalAutoscaling .Pods .Metric ,
1012- Target : getMetricTargetByType (additionalAutoscaling .Pods .Target ),
1013- },
1014- })
1015-
1016- case autoscalingv2 .ContainerResourceMetricSourceType :
1017- metrics = append (metrics , autoscalingv2.MetricSpec {
1018- Type : additionalAutoscaling .Type ,
1019- ContainerResource : & autoscalingv2.ContainerResourceMetricSource {
1020- Name : additionalAutoscaling .ContainerResource .Name ,
1021- Target : getMetricTargetByType (additionalAutoscaling .ContainerResource .Target ),
1022- Container : additionalAutoscaling .ContainerResource .Container ,
1023- },
1024- })
1025-
1026- case autoscalingv2 .ObjectMetricSourceType :
1027- metrics = append (metrics , autoscalingv2.MetricSpec {
1028- Type : additionalAutoscaling .Type ,
1029- Object : & autoscalingv2.ObjectMetricSource {
1030- DescribedObject : additionalAutoscaling .Object .DescribedObject ,
1031- Target : getMetricTargetByType (additionalAutoscaling .Object .Target ),
1032- Metric : autoscalingv2.MetricIdentifier {
1033- Name : additionalAutoscaling .Object .Metric .Name ,
1034- Selector : additionalAutoscaling .Object .Metric .Selector ,
1035- },
1036- },
1037- })
1038-
1039- case autoscalingv2 .ExternalMetricSourceType :
1040- metrics = append (metrics , autoscalingv2.MetricSpec {
1041- Type : additionalAutoscaling .Type ,
1042- External : & autoscalingv2.ExternalMetricSource {
1043- Metric : autoscalingv2.MetricIdentifier {
1044- Name : additionalAutoscaling .External .Metric .Name ,
1045- Selector : additionalAutoscaling .External .Metric .Selector ,
1046- },
1047- Target : getMetricTargetByType (additionalAutoscaling .External .Target ),
1048- },
1049- })
1003+ metric := buildAdditionalMetric (additionalAutoscaling )
1004+ if metric != nil {
1005+ metrics = append (metrics , * metric )
10501006 }
10511007 }
10521008 }
@@ -1056,7 +1012,7 @@ func buildNginxDeploymentHPA(
10561012 return nil
10571013 }
10581014
1059- hpa := & autoscalingv2.HorizontalPodAutoscaler {
1015+ return & autoscalingv2.HorizontalPodAutoscaler {
10601016 ObjectMeta : objectMeta ,
10611017 Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
10621018 ScaleTargetRef : autoscalingv2.CrossVersionObjectReference {
@@ -1070,8 +1026,61 @@ func buildNginxDeploymentHPA(
10701026 Behavior : nProxyCfg .Kubernetes .Deployment .Autoscaling .Behavior ,
10711027 },
10721028 }
1029+ }
10731030
1074- return hpa
1031+ func buildAdditionalMetric (additionalAutoscaling autoscalingv2.MetricSpec ) * autoscalingv2.MetricSpec {
1032+ switch additionalAutoscaling .Type {
1033+ case autoscalingv2 .ResourceMetricSourceType :
1034+ return & autoscalingv2.MetricSpec {
1035+ Type : additionalAutoscaling .Type ,
1036+ Resource : & autoscalingv2.ResourceMetricSource {
1037+ Name : additionalAutoscaling .Resource .Name ,
1038+ Target : getMetricTargetByType (additionalAutoscaling .Resource .Target ),
1039+ },
1040+ }
1041+ case autoscalingv2 .PodsMetricSourceType :
1042+ return & autoscalingv2.MetricSpec {
1043+ Type : additionalAutoscaling .Type ,
1044+ Pods : & autoscalingv2.PodsMetricSource {
1045+ Metric : additionalAutoscaling .Pods .Metric ,
1046+ Target : getMetricTargetByType (additionalAutoscaling .Pods .Target ),
1047+ },
1048+ }
1049+ case autoscalingv2 .ContainerResourceMetricSourceType :
1050+ return & autoscalingv2.MetricSpec {
1051+ Type : additionalAutoscaling .Type ,
1052+ ContainerResource : & autoscalingv2.ContainerResourceMetricSource {
1053+ Name : additionalAutoscaling .ContainerResource .Name ,
1054+ Target : getMetricTargetByType (additionalAutoscaling .ContainerResource .Target ),
1055+ Container : additionalAutoscaling .ContainerResource .Container ,
1056+ },
1057+ }
1058+ case autoscalingv2 .ObjectMetricSourceType :
1059+ return & autoscalingv2.MetricSpec {
1060+ Type : additionalAutoscaling .Type ,
1061+ Object : & autoscalingv2.ObjectMetricSource {
1062+ DescribedObject : additionalAutoscaling .Object .DescribedObject ,
1063+ Target : getMetricTargetByType (additionalAutoscaling .Object .Target ),
1064+ Metric : autoscalingv2.MetricIdentifier {
1065+ Name : additionalAutoscaling .Object .Metric .Name ,
1066+ Selector : additionalAutoscaling .Object .Metric .Selector ,
1067+ },
1068+ },
1069+ }
1070+ case autoscalingv2 .ExternalMetricSourceType :
1071+ return & autoscalingv2.MetricSpec {
1072+ Type : additionalAutoscaling .Type ,
1073+ External : & autoscalingv2.ExternalMetricSource {
1074+ Metric : autoscalingv2.MetricIdentifier {
1075+ Name : additionalAutoscaling .External .Metric .Name ,
1076+ Selector : additionalAutoscaling .External .Metric .Selector ,
1077+ },
1078+ Target : getMetricTargetByType (additionalAutoscaling .External .Target ),
1079+ },
1080+ }
1081+ default :
1082+ return nil
1083+ }
10751084}
10761085
10771086// TODO(sberman): see about how this can be made more elegant. Maybe create some sort of Object factory
0 commit comments