@@ -5,95 +5,82 @@ import (
55 "github.com/pdok/mapserver-operator/internal/controller/constants"
66 "github.com/pdok/mapserver-operator/internal/controller/mapperutils"
77 smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
8+ appsv1 "k8s.io/api/apps/v1"
89 autoscalingv2 "k8s.io/api/autoscaling/v2"
910 corev1 "k8s.io/api/core/v1"
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112 ctrl "sigs.k8s.io/controller-runtime"
1213)
1314
1415func mutateHorizontalPodAutoscaler [R Reconciler , O pdoknlv3.WMSWFS ](r R , obj O , autoscaler * autoscalingv2.HorizontalPodAutoscaler ) error {
15- autoscalerPatch := obj .HorizontalPodAutoscalerPatch ()
16- var behaviourStabilizationWindowSeconds int32
17- if obj .Type () == pdoknlv3 .ServiceTypeWFS {
18- behaviourStabilizationWindowSeconds = 300
19- }
20-
16+ reconcilerClient := getReconcilerClient (r )
2117 labels := addCommonLabels (obj , smoothoperatorutils .CloneOrEmptyMap (obj .GetLabels ()))
22- if err := smoothoperatorutils .SetImmutableLabels (getReconcilerClient ( r ) , autoscaler , labels ); err != nil {
18+ if err := smoothoperatorutils .SetImmutableLabels (reconcilerClient , autoscaler , labels ); err != nil {
2319 return err
2420 }
2521
26- minReplicas := int32 (2 )
27- if autoscalerPatch != nil && autoscalerPatch .MinReplicas != nil {
28- minReplicas = * autoscalerPatch .MinReplicas
22+ autoscaler .Spec .MaxReplicas = 30
23+ autoscaler .Spec .MinReplicas = smoothoperatorutils .Pointer (int32 (2 ))
24+ autoscaler .Spec .ScaleTargetRef = autoscalingv2.CrossVersionObjectReference {
25+ APIVersion : appsv1 .SchemeGroupVersion .String (),
26+ Kind : "Deployment" ,
27+ Name : getSuffixedName (obj , constants .MapserverName ),
2928 }
3029
31- maxReplicas := int32 ( 30 )
32- if autoscalerPatch != nil && autoscalerPatch . MaxReplicas != 0 {
33- maxReplicas = autoscalerPatch . MaxReplicas
30+ var averageCPU int32 = 90
31+ if cpu := mapperutils . GetContainerResourceRequest ( obj , constants . MapserverName , corev1 . ResourceCPU ); cpu != nil {
32+ averageCPU = 80
3433 }
35-
36- var metrics []autoscalingv2.MetricSpec
37- if autoscalerPatch != nil {
38- metrics = autoscalerPatch .Metrics
39- }
40- if len (metrics ) == 0 {
41- var avgU int32 = 90
42- if cpu := mapperutils .GetContainerResourceRequest (obj , constants .MapserverName , corev1 .ResourceCPU ); cpu != nil {
43- avgU = 80
44- }
45- metrics = append (metrics , autoscalingv2.MetricSpec {
46- Type : autoscalingv2 .ResourceMetricSourceType ,
47- Resource : & autoscalingv2.ResourceMetricSource {
48- Name : corev1 .ResourceCPU ,
49- Target : autoscalingv2.MetricTarget {
50- Type : autoscalingv2 .UtilizationMetricType ,
51- AverageUtilization : smoothoperatorutils .Pointer (avgU ),
52- },
34+ autoscaler .Spec .Metrics = []autoscalingv2.MetricSpec {{
35+ Type : autoscalingv2 .ResourceMetricSourceType ,
36+ Resource : & autoscalingv2.ResourceMetricSource {
37+ Name : corev1 .ResourceCPU ,
38+ Target : autoscalingv2.MetricTarget {
39+ Type : autoscalingv2 .UtilizationMetricType ,
40+ AverageUtilization : & averageCPU ,
5341 },
54- })
42+ },
43+ }}
44+
45+ var behaviourStabilizationWindowSeconds int32
46+ if obj .Type () == pdoknlv3 .ServiceTypeWFS {
47+ behaviourStabilizationWindowSeconds = 300
5548 }
5649
57- autoscaler .Spec = autoscalingv2.HorizontalPodAutoscalerSpec {
58- ScaleTargetRef : autoscalingv2.CrossVersionObjectReference {
59- APIVersion : "apps/v1" ,
60- Kind : "Deployment" ,
61- Name : getSuffixedName (obj , constants .MapserverName ),
50+ autoscaler .Spec .Behavior = & autoscalingv2.HorizontalPodAutoscalerBehavior {
51+ ScaleUp : & autoscalingv2.HPAScalingRules {
52+ StabilizationWindowSeconds : & behaviourStabilizationWindowSeconds ,
53+ Policies : []autoscalingv2.HPAScalingPolicy {{
54+ Type : autoscalingv2 .PodsScalingPolicy ,
55+ Value : 20 ,
56+ PeriodSeconds : 60 ,
57+ }},
58+ SelectPolicy : smoothoperatorutils .Pointer (autoscalingv2 .MaxChangePolicySelect ),
6259 },
63- MinReplicas : & minReplicas ,
64- MaxReplicas : maxReplicas ,
65- Metrics : metrics ,
66- Behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
67- ScaleUp : & autoscalingv2.HPAScalingRules {
68- StabilizationWindowSeconds : & behaviourStabilizationWindowSeconds ,
69- SelectPolicy : smoothoperatorutils .Pointer (autoscalingv2 .MaxChangePolicySelect ),
70- Policies : []autoscalingv2.HPAScalingPolicy {
71- {
72- Type : autoscalingv2 .PodsScalingPolicy ,
73- Value : 20 ,
74- PeriodSeconds : 60 ,
75- },
60+ ScaleDown : & autoscalingv2.HPAScalingRules {
61+ StabilizationWindowSeconds : smoothoperatorutils .Pointer (int32 (3600 )),
62+ Policies : []autoscalingv2.HPAScalingPolicy {
63+ {
64+ Type : autoscalingv2 .PercentScalingPolicy ,
65+ Value : 10 ,
66+ PeriodSeconds : 600 ,
7667 },
77- },
78- ScaleDown : & autoscalingv2.HPAScalingRules {
79- StabilizationWindowSeconds : smoothoperatorutils .Pointer (int32 (3600 )),
80- SelectPolicy : smoothoperatorutils .Pointer (autoscalingv2 .MaxChangePolicySelect ),
81- Policies : []autoscalingv2.HPAScalingPolicy {
82- {
83- Type : autoscalingv2 .PercentScalingPolicy ,
84- Value : 10 ,
85- PeriodSeconds : 600 ,
86- },
87- {
88- Type : autoscalingv2 .PodsScalingPolicy ,
89- Value : 1 ,
90- PeriodSeconds : 600 ,
91- },
68+ {
69+ Type : autoscalingv2 .PodsScalingPolicy ,
70+ Value : 1 ,
71+ PeriodSeconds : 600 ,
9272 },
9373 },
74+ SelectPolicy : smoothoperatorutils .Pointer (autoscalingv2 .MaxChangePolicySelect ),
9475 },
9576 }
96-
77+ if obj .HorizontalPodAutoscalerPatch () != nil {
78+ patchedSpec , err := smoothoperatorutils .StrategicMergePatch (& autoscaler .Spec , obj .HorizontalPodAutoscalerPatch ())
79+ if err != nil {
80+ return err
81+ }
82+ autoscaler .Spec = * patchedSpec
83+ }
9784 if err := smoothoperatorutils .EnsureSetGVK (getReconcilerClient (r ), autoscaler , autoscaler ); err != nil {
9885 return err
9986 }
0 commit comments