|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + v1 "k8s.io/api/apps/v1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/resource" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/utils/ptr" |
| 16 | + |
| 17 | + "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1" |
| 18 | +) |
| 19 | + |
| 20 | +var _ = Describe("HTTPScalingSetController", func() { |
| 21 | + Describe("functional tests", func() { |
| 22 | + It("HTTPScalingSet generates the interceptor and scaler using default values", func() { |
| 23 | + name := "default-values" |
| 24 | + namesapce := "http-ss-defaults" |
| 25 | + createNamespace(namesapce) |
| 26 | + interceptorName := fmt.Sprintf("%s-interceptor", name) |
| 27 | + interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name) |
| 28 | + |
| 29 | + scalerName := fmt.Sprintf("%s-external-scaler", name) |
| 30 | + scalerServiceName := fmt.Sprintf("%s-external-scaler", name) |
| 31 | + |
| 32 | + css := &v1alpha1.HTTPScalingSet{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{ |
| 34 | + Name: name, |
| 35 | + Namespace: namesapce, |
| 36 | + }, |
| 37 | + Spec: v1alpha1.HTTPScalingSetSpec{ |
| 38 | + Interceptor: v1alpha1.HTTPInterceptorSepc{}, |
| 39 | + Scaler: v1alpha1.HTTPScalerSepc{}, |
| 40 | + }, |
| 41 | + } |
| 42 | + err := k8sClient.Create(context.Background(), css) |
| 43 | + Expect(err).ToNot(HaveOccurred()) |
| 44 | + |
| 45 | + // Validate interceptor proxy service |
| 46 | + interceptorProxyService := &corev1.Service{} |
| 47 | + Eventually(func() error { |
| 48 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService) |
| 49 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 50 | + |
| 51 | + Expect(interceptorProxyService).ShouldNot(BeNil()) |
| 52 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 53 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 54 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 55 | + Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy")) |
| 56 | + Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(int32(8080))) |
| 57 | + |
| 58 | + // Validate interceptor admin service |
| 59 | + interceptorAdminService := &corev1.Service{} |
| 60 | + Eventually(func() error { |
| 61 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService) |
| 62 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 63 | + |
| 64 | + Expect(interceptorAdminService).ShouldNot(BeNil()) |
| 65 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 66 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 67 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 68 | + Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin")) |
| 69 | + Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(int32(9090))) |
| 70 | + |
| 71 | + // Validate interceptor deployment |
| 72 | + interceptorDeploy := &v1.Deployment{} |
| 73 | + Eventually(func() error { |
| 74 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy) |
| 75 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 76 | + |
| 77 | + Expect(interceptorDeploy).ShouldNot(BeNil()) |
| 78 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 79 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 80 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 81 | + Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(int32(1))) |
| 82 | + Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil()) |
| 83 | + Expect(interceptorDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil()) |
| 84 | + |
| 85 | + // Validate scaler service |
| 86 | + scalerService := &corev1.Service{} |
| 87 | + Eventually(func() error { |
| 88 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService) |
| 89 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 90 | + |
| 91 | + Expect(scalerService).ShouldNot(BeNil()) |
| 92 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 93 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler")) |
| 94 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 95 | + Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc")) |
| 96 | + Expect(scalerService.Spec.Ports[0].Port).Should(Equal(int32(9090))) |
| 97 | + |
| 98 | + // Validate scaler deployment |
| 99 | + scalerDeploy := &v1.Deployment{} |
| 100 | + Eventually(func() error { |
| 101 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy) |
| 102 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 103 | + |
| 104 | + Expect(scalerDeploy).ShouldNot(BeNil()) |
| 105 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 106 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler")) |
| 107 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 108 | + Expect(*scalerDeploy.Spec.Replicas).Should(Equal(int32(1))) |
| 109 | + Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Requests).Should(BeNil()) |
| 110 | + Expect(scalerDeploy.Spec.Template.Spec.Containers[0].Resources.Limits).Should(BeNil()) |
| 111 | + }) |
| 112 | + |
| 113 | + It("HTTPScalingSet generates the interceptor and scaler using custom values", func() { |
| 114 | + name := "custom-values" |
| 115 | + namesapce := "http-ss-custom" |
| 116 | + createNamespace(namesapce) |
| 117 | + interceptorName := fmt.Sprintf("%s-interceptor", name) |
| 118 | + interceptorProxyServiceName, interceptorAdminServiceName := getInterceptorServiceNames(name) |
| 119 | + var interceptorReplicas int32 = 3 |
| 120 | + interceptorResouces := corev1.ResourceRequirements{ |
| 121 | + Requests: corev1.ResourceList{ |
| 122 | + "cpu": *resource.NewQuantity(10, resource.DecimalSI), |
| 123 | + "memory": *resource.NewQuantity(11, resource.DecimalSI), |
| 124 | + }, |
| 125 | + Limits: corev1.ResourceList{ |
| 126 | + "cpu": *resource.NewQuantity(20, resource.DecimalSI), |
| 127 | + "memory": *resource.NewQuantity(21, resource.DecimalSI), |
| 128 | + }, |
| 129 | + } |
| 130 | + var interceptorProxyPort int32 = 6666 |
| 131 | + var interceptorAdminPort int32 = 7777 |
| 132 | + |
| 133 | + scalerName := fmt.Sprintf("%s-external-scaler", name) |
| 134 | + scalerServiceName := fmt.Sprintf("%s-external-scaler", name) |
| 135 | + var scalerReplicas int32 = 2 |
| 136 | + scalerResouces := corev1.ResourceRequirements{ |
| 137 | + Requests: corev1.ResourceList{ |
| 138 | + "cpu": *resource.NewQuantity(30, resource.DecimalSI), |
| 139 | + "memory": *resource.NewQuantity(31, resource.DecimalSI), |
| 140 | + }, |
| 141 | + Limits: corev1.ResourceList{ |
| 142 | + "cpu": *resource.NewQuantity(40, resource.DecimalSI), |
| 143 | + "memory": *resource.NewQuantity(41, resource.DecimalSI), |
| 144 | + }, |
| 145 | + } |
| 146 | + var scalerPort int32 = 8888 |
| 147 | + |
| 148 | + css := &v1alpha1.HTTPScalingSet{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{ |
| 150 | + Name: name, |
| 151 | + Namespace: namesapce, |
| 152 | + }, |
| 153 | + Spec: v1alpha1.HTTPScalingSetSpec{ |
| 154 | + Interceptor: v1alpha1.HTTPInterceptorSepc{ |
| 155 | + Replicas: ptr.To(interceptorReplicas), |
| 156 | + Resources: interceptorResouces, |
| 157 | + Config: &v1alpha1.HTTPInterceptorConfigurationSepc{ |
| 158 | + ProxyPort: ptr.To(interceptorProxyPort), |
| 159 | + AdminPort: ptr.To(interceptorAdminPort), |
| 160 | + }, |
| 161 | + Labels: map[string]string{ |
| 162 | + "interceptor-label-1": "value-1", |
| 163 | + }, |
| 164 | + Annotations: map[string]string{ |
| 165 | + "interceptor-annotation-1": "value-2", |
| 166 | + }, |
| 167 | + }, |
| 168 | + Scaler: v1alpha1.HTTPScalerSepc{ |
| 169 | + Replicas: ptr.To(scalerReplicas), |
| 170 | + Resources: scalerResouces, |
| 171 | + Config: v1alpha1.HTTPScalerConfigurationSepc{ |
| 172 | + Port: ptr.To(scalerPort), |
| 173 | + }, |
| 174 | + Labels: map[string]string{ |
| 175 | + "scaler-label-1": "value-3", |
| 176 | + }, |
| 177 | + Annotations: map[string]string{ |
| 178 | + "scaler-annotation-1": "value-4", |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + } |
| 183 | + err := k8sClient.Create(context.Background(), css) |
| 184 | + Expect(err).ToNot(HaveOccurred()) |
| 185 | + |
| 186 | + // Validate interceptor proxy service |
| 187 | + interceptorProxyService := &corev1.Service{} |
| 188 | + Eventually(func() error { |
| 189 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorProxyServiceName, Namespace: namesapce}, interceptorProxyService) |
| 190 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 191 | + |
| 192 | + Expect(interceptorProxyService).ShouldNot(BeNil()) |
| 193 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 194 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 195 | + Expect(interceptorProxyService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 196 | + Expect(interceptorProxyService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("proxy")) |
| 197 | + Expect(interceptorProxyService.Spec.Ports[0].Port).Should(Equal(interceptorProxyPort)) |
| 198 | + |
| 199 | + // Validate interceptor admin service |
| 200 | + interceptorAdminService := &corev1.Service{} |
| 201 | + Eventually(func() error { |
| 202 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorAdminServiceName, Namespace: namesapce}, interceptorAdminService) |
| 203 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 204 | + |
| 205 | + Expect(interceptorAdminService).ShouldNot(BeNil()) |
| 206 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 207 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 208 | + Expect(interceptorAdminService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 209 | + Expect(interceptorAdminService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("admin")) |
| 210 | + Expect(interceptorAdminService.Spec.Ports[0].Port).Should(Equal(interceptorAdminPort)) |
| 211 | + |
| 212 | + // Validate interceptor deployment |
| 213 | + interceptorDeploy := &v1.Deployment{} |
| 214 | + Eventually(func() error { |
| 215 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: interceptorName, Namespace: namesapce}, interceptorDeploy) |
| 216 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 217 | + |
| 218 | + Expect(interceptorDeploy).ShouldNot(BeNil()) |
| 219 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 220 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "interceptor")) |
| 221 | + Expect(interceptorDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 222 | + Expect(*interceptorDeploy.Spec.Replicas).Should(Equal(interceptorReplicas)) |
| 223 | + Expect(interceptorDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("interceptor-label-1", "value-1")) |
| 224 | + Expect(interceptorDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("interceptor-annotation-1", "value-2")) |
| 225 | + validateResources(interceptorResouces, interceptorDeploy.Spec.Template.Spec.Containers[0].Resources) |
| 226 | + |
| 227 | + // Validate scaler service |
| 228 | + scalerService := &corev1.Service{} |
| 229 | + Eventually(func() error { |
| 230 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerServiceName, Namespace: namesapce}, scalerService) |
| 231 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 232 | + |
| 233 | + Expect(scalerService).ShouldNot(BeNil()) |
| 234 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 235 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler")) |
| 236 | + Expect(scalerService.Spec.Selector).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 237 | + Expect(scalerService.Spec.Ports[0].TargetPort.StrVal).Should(Equal("grpc")) |
| 238 | + Expect(scalerService.Spec.Ports[0].Port).Should(Equal(scalerPort)) |
| 239 | + |
| 240 | + // Validate scaler deployment |
| 241 | + scalerDeploy := &v1.Deployment{} |
| 242 | + Eventually(func() error { |
| 243 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: scalerName, Namespace: namesapce}, scalerDeploy) |
| 244 | + }).WithTimeout(30 * time.Second).WithPolling(5 * time.Second).ShouldNot(HaveOccurred()) |
| 245 | + |
| 246 | + Expect(scalerDeploy).ShouldNot(BeNil()) |
| 247 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set", name)) |
| 248 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-component", "external-scaler")) |
| 249 | + Expect(scalerDeploy.Spec.Selector.MatchLabels).Should(HaveKeyWithValue("http.keda.sh/scaling-set-kind", "HTTPScalingSet")) |
| 250 | + Expect(*scalerDeploy.Spec.Replicas).Should(Equal(scalerReplicas)) |
| 251 | + Expect(scalerDeploy.ObjectMeta.Labels).Should(HaveKeyWithValue("scaler-label-1", "value-3")) |
| 252 | + Expect(scalerDeploy.ObjectMeta.Annotations).Should(HaveKeyWithValue("scaler-annotation-1", "value-4")) |
| 253 | + validateResources(scalerResouces, scalerDeploy.Spec.Template.Spec.Containers[0].Resources) |
| 254 | + }) |
| 255 | + |
| 256 | + }) |
| 257 | +}) |
0 commit comments