@@ -9,23 +9,21 @@ import (
99 . "github.com/onsi/gomega"
1010 v1 "k8s.io/api/apps/v1"
1111 corev1 "k8s.io/api/core/v1"
12+ "k8s.io/apimachinery/pkg/api/resource"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 "k8s.io/apimachinery/pkg/types"
15+ "k8s.io/utils/ptr"
1416
1517 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
1618)
1719
1820var _ = Describe ("ClusterHTTPScalingSetController" , func () {
19-
20- // var (
21- // testLogger = zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))
22- // )
23-
2421 Describe ("functional tests" , func () {
25- It ("ClusterHTTPScalingSet generates the interceptor and scaler" , func () {
26- name := "testing-name "
22+ It ("ClusterHTTPScalingSet generates the interceptor and scaler using default values " , func () {
23+ name := "default-values "
2724 interceptorName := fmt .Sprintf ("%s-interceptor" , name )
2825 interceptorProxyServiceName , interceptorAdminServiceName := getInterceptorServiceNames (name )
26+
2927 scalerName := fmt .Sprintf ("%s-external-scaler" , name )
3028 scalerServiceName := fmt .Sprintf ("%s-external-scaler" , name )
3129
@@ -41,6 +39,145 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
4139 err := k8sClient .Create (context .Background (), css )
4240 Expect (err ).ToNot (HaveOccurred ())
4341
42+ // Validate interceptor proxy service
43+ interceptorProxyService := & corev1.Service {}
44+ Eventually (func () error {
45+ return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorProxyServiceName , Namespace : "keda" }, interceptorProxyService )
46+ }).WithTimeout (30 * time .Second ).WithPolling (5 * time .Second ).ShouldNot (HaveOccurred ())
47+
48+ Expect (interceptorProxyService ).ShouldNot (BeNil ())
49+ Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
50+ Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
51+ Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
52+ Expect (interceptorProxyService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("proxy" ))
53+ Expect (interceptorProxyService .Spec .Ports [0 ].Port ).Should (Equal (int32 (8080 )))
54+
55+ // Validate interceptor admin service
56+ interceptorAdminService := & corev1.Service {}
57+ Eventually (func () error {
58+ return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorAdminServiceName , Namespace : "keda" }, interceptorAdminService )
59+ }).WithTimeout (30 * time .Second ).WithPolling (5 * time .Second ).ShouldNot (HaveOccurred ())
60+
61+ Expect (interceptorAdminService ).ShouldNot (BeNil ())
62+ Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
63+ Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
64+ Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
65+ Expect (interceptorAdminService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("admin" ))
66+ Expect (interceptorAdminService .Spec .Ports [0 ].Port ).Should (Equal (int32 (9090 )))
67+
68+ // Validate interceptor deployment
69+ interceptorDeploy := & v1.Deployment {}
70+ Eventually (func () error {
71+ return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorName , Namespace : "keda" }, interceptorDeploy )
72+ }).WithTimeout (30 * time .Second ).WithPolling (5 * time .Second ).ShouldNot (HaveOccurred ())
73+
74+ Expect (interceptorDeploy ).ShouldNot (BeNil ())
75+ Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
76+ Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
77+ Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
78+ Expect (* interceptorDeploy .Spec .Replicas ).Should (Equal (int32 (1 )))
79+ Expect (interceptorDeploy .Spec .Template .Spec .Containers [0 ].Resources .Requests ).Should (BeNil ())
80+ Expect (interceptorDeploy .Spec .Template .Spec .Containers [0 ].Resources .Limits ).Should (BeNil ())
81+
82+ // Validate scaler service
83+ scalerService := & corev1.Service {}
84+ Eventually (func () error {
85+ return k8sClient .Get (context .Background (), types.NamespacedName {Name : scalerServiceName , Namespace : "keda" }, scalerService )
86+ }).WithTimeout (30 * time .Second ).WithPolling (5 * time .Second ).ShouldNot (HaveOccurred ())
87+
88+ Expect (scalerService ).ShouldNot (BeNil ())
89+ Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
90+ Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "external-scaler" ))
91+ Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
92+ Expect (scalerService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("grpc" ))
93+ Expect (scalerService .Spec .Ports [0 ].Port ).Should (Equal (int32 (9090 )))
94+
95+ // Validate scaler deployment
96+ scalerDeploy := & v1.Deployment {}
97+ Eventually (func () error {
98+ return k8sClient .Get (context .Background (), types.NamespacedName {Name : scalerName , Namespace : "keda" }, scalerDeploy )
99+ }).WithTimeout (30 * time .Second ).WithPolling (5 * time .Second ).ShouldNot (HaveOccurred ())
100+
101+ Expect (scalerDeploy ).ShouldNot (BeNil ())
102+ Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
103+ Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "external-scaler" ))
104+ Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
105+ Expect (* scalerDeploy .Spec .Replicas ).Should (Equal (int32 (1 )))
106+ Expect (scalerDeploy .Spec .Template .Spec .Containers [0 ].Resources .Requests ).Should (BeNil ())
107+ Expect (scalerDeploy .Spec .Template .Spec .Containers [0 ].Resources .Limits ).Should (BeNil ())
108+ })
109+
110+ It ("ClusterHTTPScalingSet generates the interceptor and scaler using custom values" , func () {
111+ name := "custom-values"
112+ interceptorName := fmt .Sprintf ("%s-interceptor" , name )
113+ interceptorProxyServiceName , interceptorAdminServiceName := getInterceptorServiceNames (name )
114+ var interceptorReplicas int32 = 3
115+ interceptorResouces := corev1.ResourceRequirements {
116+ Requests : corev1.ResourceList {
117+ "cpu" : * resource .NewQuantity (10 , resource .DecimalSI ),
118+ "memory" : * resource .NewQuantity (11 , resource .DecimalSI ),
119+ },
120+ Limits : corev1.ResourceList {
121+ "cpu" : * resource .NewQuantity (20 , resource .DecimalSI ),
122+ "memory" : * resource .NewQuantity (21 , resource .DecimalSI ),
123+ },
124+ }
125+ var interceptorProxyPort int32 = 6666
126+ var interceptorAdminPort int32 = 7777
127+
128+ scalerName := fmt .Sprintf ("%s-external-scaler" , name )
129+ scalerServiceName := fmt .Sprintf ("%s-external-scaler" , name )
130+ var scalerReplicas int32 = 2
131+ scalerResouces := corev1.ResourceRequirements {
132+ Requests : corev1.ResourceList {
133+ "cpu" : * resource .NewQuantity (30 , resource .DecimalSI ),
134+ "memory" : * resource .NewQuantity (31 , resource .DecimalSI ),
135+ },
136+ Limits : corev1.ResourceList {
137+ "cpu" : * resource .NewQuantity (40 , resource .DecimalSI ),
138+ "memory" : * resource .NewQuantity (41 , resource .DecimalSI ),
139+ },
140+ }
141+ var scalerPort int32 = 8888
142+
143+ css := & v1alpha1.ClusterHTTPScalingSet {
144+ ObjectMeta : metav1.ObjectMeta {
145+ Name : name ,
146+ },
147+ Spec : v1alpha1.HTTPScalingSetSpec {
148+ Interceptor : v1alpha1.HTTPInterceptorSepc {
149+ Replicas : ptr .To (int32 (interceptorReplicas )),
150+ Resources : interceptorResouces ,
151+ Config : & v1alpha1.HTTPInterceptorConfigurationSepc {
152+ ProxyPort : ptr .To (interceptorProxyPort ),
153+ AdminPort : ptr .To (interceptorAdminPort ),
154+ },
155+ Labels : map [string ]string {
156+ "interceptor-label-1" : "value-1" ,
157+ },
158+ Annotations : map [string ]string {
159+ "interceptor-annotation-1" : "value-2" ,
160+ },
161+ },
162+ Scaler : v1alpha1.HTTPScalerSepc {
163+ Replicas : ptr .To (int32 (scalerReplicas )),
164+ Resources : scalerResouces ,
165+ Config : v1alpha1.HTTPScalerConfigurationSepc {
166+ Port : ptr .To (scalerPort ),
167+ },
168+ Labels : map [string ]string {
169+ "scaler-label-1" : "value-3" ,
170+ },
171+ Annotations : map [string ]string {
172+ "scaler-annotation-1" : "value-4" ,
173+ },
174+ },
175+ },
176+ }
177+ err := k8sClient .Create (context .Background (), css )
178+ Expect (err ).ToNot (HaveOccurred ())
179+
180+ // Validate interceptor proxy service
44181 interceptorProxyService := & corev1.Service {}
45182 Eventually (func () error {
46183 return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorProxyServiceName , Namespace : "keda" }, interceptorProxyService )
@@ -50,7 +187,10 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
50187 Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
51188 Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
52189 Expect (interceptorProxyService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
190+ Expect (interceptorProxyService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("proxy" ))
191+ Expect (interceptorProxyService .Spec .Ports [0 ].Port ).Should (Equal (interceptorProxyPort ))
53192
193+ // Validate interceptor admin service
54194 interceptorAdminService := & corev1.Service {}
55195 Eventually (func () error {
56196 return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorAdminServiceName , Namespace : "keda" }, interceptorAdminService )
@@ -60,7 +200,10 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
60200 Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
61201 Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
62202 Expect (interceptorAdminService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
203+ Expect (interceptorAdminService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("admin" ))
204+ Expect (interceptorAdminService .Spec .Ports [0 ].Port ).Should (Equal (interceptorAdminPort ))
63205
206+ // Validate interceptor deployment
64207 interceptorDeploy := & v1.Deployment {}
65208 Eventually (func () error {
66209 return k8sClient .Get (context .Background (), types.NamespacedName {Name : interceptorName , Namespace : "keda" }, interceptorDeploy )
@@ -70,7 +213,12 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
70213 Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
71214 Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "interceptor" ))
72215 Expect (interceptorDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
216+ Expect (* interceptorDeploy .Spec .Replicas ).Should (Equal (interceptorReplicas ))
217+ Expect (interceptorDeploy .ObjectMeta .Labels ).Should (HaveKeyWithValue ("interceptor-label-1" , "value-1" ))
218+ Expect (interceptorDeploy .ObjectMeta .Annotations ).Should (HaveKeyWithValue ("interceptor-annotation-1" , "value-2" ))
219+ validateResources (interceptorResouces , interceptorDeploy .Spec .Template .Spec .Containers [0 ].Resources )
73220
221+ // Validate scaler service
74222 scalerService := & corev1.Service {}
75223 Eventually (func () error {
76224 return k8sClient .Get (context .Background (), types.NamespacedName {Name : scalerServiceName , Namespace : "keda" }, scalerService )
@@ -80,7 +228,10 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
80228 Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
81229 Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "external-scaler" ))
82230 Expect (scalerService .Spec .Selector ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
231+ Expect (scalerService .Spec .Ports [0 ].TargetPort .StrVal ).Should (Equal ("grpc" ))
232+ Expect (scalerService .Spec .Ports [0 ].Port ).Should (Equal (scalerPort ))
83233
234+ // Validate scaler deployment
84235 scalerDeploy := & v1.Deployment {}
85236 Eventually (func () error {
86237 return k8sClient .Get (context .Background (), types.NamespacedName {Name : scalerName , Namespace : "keda" }, scalerDeploy )
@@ -90,6 +241,18 @@ var _ = Describe("ClusterHTTPScalingSetController", func() {
90241 Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set" , name ))
91242 Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-component" , "external-scaler" ))
92243 Expect (scalerDeploy .Spec .Selector .MatchLabels ).Should (HaveKeyWithValue ("http.keda.sh/scaling-set-kind" , "ClusterHTTPScalingSet" ))
244+ Expect (* scalerDeploy .Spec .Replicas ).Should (Equal (scalerReplicas ))
245+ Expect (scalerDeploy .ObjectMeta .Labels ).Should (HaveKeyWithValue ("scaler-label-1" , "value-3" ))
246+ Expect (scalerDeploy .ObjectMeta .Annotations ).Should (HaveKeyWithValue ("scaler-annotation-1" , "value-4" ))
247+ validateResources (scalerResouces , scalerDeploy .Spec .Template .Spec .Containers [0 ].Resources )
93248 })
249+
94250 })
95251})
252+
253+ func validateResources (expected , value corev1.ResourceRequirements ) {
254+ Expect (expected .Requests .Cpu ().AsApproximateFloat64 ()).Should (BeEquivalentTo (value .Requests .Cpu ().AsApproximateFloat64 ()))
255+ Expect (expected .Requests .Memory ().AsApproximateFloat64 ()).Should (BeEquivalentTo (value .Requests .Memory ().AsApproximateFloat64 ()))
256+ Expect (expected .Limits .Cpu ().AsApproximateFloat64 ()).Should (BeEquivalentTo (value .Limits .Cpu ().AsApproximateFloat64 ()))
257+ Expect (expected .Limits .Memory ().AsApproximateFloat64 ()).Should (BeEquivalentTo (value .Limits .Memory ().AsApproximateFloat64 ()))
258+ }
0 commit comments