@@ -42,21 +42,6 @@ const (
42
42
43
43
var emptyDirVolumeSource = corev1.VolumeSource {EmptyDir : & corev1.EmptyDirVolumeSource {}}
44
44
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
-
60
45
func (p * NginxProvisioner ) buildNginxResourceObjects (
61
46
resourceName string ,
62
47
gateway * gatewayv1.Gateway ,
@@ -179,23 +164,48 @@ func (p *NginxProvisioner) buildNginxResourceObjects(
179
164
objects = append (objects , openshiftObjs ... )
180
165
}
181
166
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
-
194
167
objects = append (objects , service , deployment )
195
168
169
+ if hpa := p .buildHPAIfEnabled (objectMeta , nProxyCfg ); hpa != nil {
170
+ objects = append (objects , hpa )
171
+ }
172
+
196
173
return objects , err
197
174
}
198
175
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
+
199
209
func (p * NginxProvisioner ) buildNginxSecrets (
200
210
objectMeta metav1.ObjectMeta ,
201
211
agentTLSSecretName string ,
@@ -926,22 +936,18 @@ func (p *NginxProvisioner) buildImage(nProxyCfg *graph.EffectiveNginxProxy) (str
926
936
return fmt .Sprintf ("%s:%s" , image , tag ), pullPolicy
927
937
}
928
938
929
- func getMetricTargetByType (
930
- target autoscalingv2.MetricTarget ,
931
- ) autoscalingv2.MetricTarget {
939
+ func getMetricTargetByType (target autoscalingv2.MetricTarget ) autoscalingv2.MetricTarget {
932
940
switch target .Type {
933
941
case autoscalingv2 .UtilizationMetricType :
934
942
return autoscalingv2.MetricTarget {
935
943
Type : autoscalingv2 .UtilizationMetricType ,
936
944
AverageUtilization : target .AverageUtilization ,
937
945
}
938
-
939
946
case autoscalingv2 .AverageValueMetricType :
940
947
return autoscalingv2.MetricTarget {
941
948
Type : autoscalingv2 .AverageValueMetricType ,
942
949
AverageValue : target .AverageValue ,
943
950
}
944
-
945
951
case autoscalingv2 .ValueMetricType :
946
952
return autoscalingv2.MetricTarget {
947
953
Type : autoscalingv2 .ValueMetricType ,
@@ -994,59 +1000,9 @@ func buildNginxDeploymentHPA(
994
1000
995
1001
if autoscalingTemplate != nil {
996
1002
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 )
1050
1006
}
1051
1007
}
1052
1008
}
@@ -1056,7 +1012,7 @@ func buildNginxDeploymentHPA(
1056
1012
return nil
1057
1013
}
1058
1014
1059
- hpa := & autoscalingv2.HorizontalPodAutoscaler {
1015
+ return & autoscalingv2.HorizontalPodAutoscaler {
1060
1016
ObjectMeta : objectMeta ,
1061
1017
Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
1062
1018
ScaleTargetRef : autoscalingv2.CrossVersionObjectReference {
@@ -1070,8 +1026,61 @@ func buildNginxDeploymentHPA(
1070
1026
Behavior : nProxyCfg .Kubernetes .Deployment .Autoscaling .Behavior ,
1071
1027
},
1072
1028
}
1029
+ }
1073
1030
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
+ }
1075
1084
}
1076
1085
1077
1086
// TODO(sberman): see about how this can be made more elegant. Maybe create some sort of Object factory
0 commit comments