Skip to content

Commit 51109fd

Browse files
committed
include functional tests for cluster scoper resource
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
1 parent 1034a8f commit 51109fd

File tree

4 files changed

+202
-11
lines changed

4 files changed

+202
-11
lines changed

operator/apis/http/v1alpha1/httpscalingset_defaults.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ func (c *HTTPInterceptorSepc) GetImage() string {
108108
return *c.Image
109109
}
110110

111+
func (c *HTTPInterceptorSepc) GetLabels() map[string]string {
112+
if c.Labels == nil {
113+
return map[string]string{}
114+
}
115+
return c.Labels
116+
}
117+
118+
func (c *HTTPInterceptorSepc) GetAnnotations() map[string]string {
119+
if c.Annotations == nil {
120+
return map[string]string{}
121+
}
122+
return c.Annotations
123+
}
124+
111125
func (c *HTTPScalerSepc) GetPort() int32 {
112126
if c.Config.Port == nil {
113127
return 9090
@@ -121,3 +135,17 @@ func (c *HTTPScalerSepc) GetImage() string {
121135
}
122136
return *c.Image
123137
}
138+
139+
func (c *HTTPScalerSepc) GetLabels() map[string]string {
140+
if c.Labels == nil {
141+
return map[string]string{}
142+
}
143+
return c.Labels
144+
}
145+
146+
func (c *HTTPScalerSepc) GetAnnotations() map[string]string {
147+
if c.Annotations == nil {
148+
return map[string]string{}
149+
}
150+
return c.Annotations
151+
}

operator/controllers/http/clusterhttpscalingset_controller_test.go

Lines changed: 170 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1820
var _ = 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+
}

operator/controllers/http/interceptor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ func createOrUpdateInterceptorResources(
158158
envs,
159159
httpSpec.Interceptor.Replicas,
160160
selector,
161-
httpss.GetLabels(),
162-
httpss.GetAnnotations(),
161+
httpSpec.Interceptor.GetLabels(),
162+
httpSpec.Interceptor.GetAnnotations(),
163163
httpSpec.Interceptor.Resources,
164164
)
165165
// Set HTTPScaledObject instance as the owner and controller

operator/controllers/http/scaler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func createOrUpdateExternalScalerResources(
9696
envs,
9797
httpSpec.Scaler.Replicas,
9898
selector,
99-
httpss.GetLabels(),
100-
httpss.GetAnnotations(),
99+
httpSpec.Scaler.GetLabels(),
100+
httpSpec.Scaler.GetAnnotations(),
101101
httpSpec.Scaler.Resources,
102102
)
103103
// Set HTTPScaledObject instance as the owner and controller

0 commit comments

Comments
 (0)