@@ -392,78 +392,156 @@ func TestBuildNginxResourceObjects_NginxProxyConfig(t *testing.T) {
392392
393393func TestBuildNginxResourceObjects_DeploymentReplicasFromHPA (t * testing.T ) {
394394 t .Parallel ()
395- g := NewWithT (t )
396395
397- // Create a fake HPA with status.desiredReplicas set
398- hpa := & autoscalingv2.HorizontalPodAutoscaler {
399- ObjectMeta : metav1.ObjectMeta {
400- Name : "gw-nginx" ,
401- Namespace : "default" ,
396+ tests := []struct {
397+ currentReplicas * int32
398+ configReplicas * int32
399+ name string
400+ description string
401+ expectedValue int32
402+ hpaExists bool
403+ deploymentExists bool
404+ expectedNil bool
405+ }{
406+ {
407+ name : "HPA exists - use current deployment replicas" ,
408+ hpaExists : true ,
409+ deploymentExists : true ,
410+ currentReplicas : helpers .GetPointer (int32 (8 )),
411+ configReplicas : helpers .GetPointer (int32 (5 )),
412+ expectedNil : false ,
413+ expectedValue : 8 ,
414+ description : "When HPA exists, read current deployment replicas (set by HPA)" ,
402415 },
403- Status : autoscalingv2.HorizontalPodAutoscalerStatus {
404- DesiredReplicas : 7 ,
416+ {
417+ name : "HPA does not exist - use configured replicas" ,
418+ hpaExists : false ,
419+ deploymentExists : false ,
420+ configReplicas : helpers .GetPointer (int32 (3 )),
421+ expectedNil : false ,
422+ expectedValue : 3 ,
423+ description : "When HPA doesn't exist yet (initial creation), use configured replicas" ,
405424 },
406- }
407-
408- agentTLSSecret := & corev1.Secret {
409- ObjectMeta : metav1.ObjectMeta {
410- Name : agentTLSTestSecretName ,
411- Namespace : ngfNamespace ,
425+ {
426+ name : "HPA enabled but doesn't exist, no configured replicas" ,
427+ hpaExists : false ,
428+ deploymentExists : false ,
429+ configReplicas : nil ,
430+ expectedNil : true ,
431+ description : "When HPA enabled but doesn't exist and no replicas configured, don't set replicas" ,
412432 },
413- Data : map [string ][]byte {"tls.crt" : []byte ("tls" )},
414433 }
415434
416- fakeClient := fake .NewFakeClient (agentTLSSecret , hpa )
435+ for _ , tc := range tests {
436+ t .Run (tc .name , func (t * testing.T ) {
437+ t .Parallel ()
438+ g := NewWithT (t )
417439
418- provisioner := & NginxProvisioner {
419- cfg : Config {
420- GatewayPodConfig : & config.GatewayPodConfig {
421- Namespace : ngfNamespace ,
422- Version : "1.0.0" ,
423- Image : "ngf-image" ,
424- },
425- AgentTLSSecretName : agentTLSTestSecretName ,
426- },
427- baseLabelSelector : metav1.LabelSelector {
428- MatchLabels : map [string ]string {"app" : "nginx" },
429- },
430- k8sClient : fakeClient ,
431- }
440+ agentTLSSecret := & corev1.Secret {
441+ ObjectMeta : metav1.ObjectMeta {
442+ Name : agentTLSTestSecretName ,
443+ Namespace : ngfNamespace ,
444+ },
445+ Data : map [string ][]byte {"tls.crt" : []byte ("tls" )},
446+ }
432447
433- gateway := & gatewayv1.Gateway {
434- ObjectMeta : metav1.ObjectMeta {
435- Name : "gw" ,
436- Namespace : "default" ,
437- },
438- Spec : gatewayv1.GatewaySpec {
439- Listeners : []gatewayv1.Listener {{Port : 80 }},
440- },
441- }
448+ var fakeClient client.Client
449+ switch {
450+ case tc .hpaExists && tc .deploymentExists :
451+ // Create a fake HPA and existing deployment
452+ hpa := & autoscalingv2.HorizontalPodAutoscaler {
453+ ObjectMeta : metav1.ObjectMeta {
454+ Name : "gw-nginx" ,
455+ Namespace : "default" ,
456+ },
457+ Status : autoscalingv2.HorizontalPodAutoscalerStatus {
458+ DesiredReplicas : 7 ,
459+ },
460+ }
461+ existingDeployment := & appsv1.Deployment {
462+ ObjectMeta : metav1.ObjectMeta {
463+ Name : "gw-nginx" ,
464+ Namespace : "default" ,
465+ },
466+ Spec : appsv1.DeploymentSpec {
467+ Replicas : tc .currentReplicas ,
468+ Selector : & metav1.LabelSelector {
469+ MatchLabels : map [string ]string {"app" : "nginx" },
470+ },
471+ },
472+ }
473+ fakeClient = fake .NewFakeClient (agentTLSSecret , hpa , existingDeployment )
474+ case tc .hpaExists :
475+ hpa := & autoscalingv2.HorizontalPodAutoscaler {
476+ ObjectMeta : metav1.ObjectMeta {
477+ Name : "gw-nginx" ,
478+ Namespace : "default" ,
479+ },
480+ Status : autoscalingv2.HorizontalPodAutoscalerStatus {
481+ DesiredReplicas : 7 ,
482+ },
483+ }
484+ fakeClient = fake .NewFakeClient (agentTLSSecret , hpa )
485+ default :
486+ fakeClient = fake .NewFakeClient (agentTLSSecret )
487+ }
442488
443- resourceName := "gw-nginx"
444- nProxyCfg := & graph.EffectiveNginxProxy {
445- Kubernetes : & ngfAPIv1alpha2.KubernetesSpec {
446- Deployment : & ngfAPIv1alpha2.DeploymentSpec {
447- Replicas : nil , // Should be overridden by HPA
448- Autoscaling : & ngfAPIv1alpha2.AutoscalingSpec {Enable : true },
449- },
450- },
451- }
489+ provisioner := & NginxProvisioner {
490+ cfg : Config {
491+ GatewayPodConfig : & config.GatewayPodConfig {
492+ Namespace : ngfNamespace ,
493+ Version : "1.0.0" ,
494+ Image : "ngf-image" ,
495+ },
496+ AgentTLSSecretName : agentTLSTestSecretName ,
497+ },
498+ baseLabelSelector : metav1.LabelSelector {
499+ MatchLabels : map [string ]string {"app" : "nginx" },
500+ },
501+ k8sClient : fakeClient ,
502+ }
452503
453- objects , err := provisioner .buildNginxResourceObjects (resourceName , gateway , nProxyCfg )
454- g .Expect (err ).ToNot (HaveOccurred ())
504+ gateway := & gatewayv1.Gateway {
505+ ObjectMeta : metav1.ObjectMeta {
506+ Name : "gw" ,
507+ Namespace : "default" ,
508+ },
509+ Spec : gatewayv1.GatewaySpec {
510+ Listeners : []gatewayv1.Listener {{Port : 80 }},
511+ },
512+ }
455513
456- // Find the deployment object
457- var deployment * appsv1.Deployment
458- for _ , obj := range objects {
459- if d , ok := obj .(* appsv1.Deployment ); ok {
460- deployment = d
461- break
462- }
514+ resourceName := "gw-nginx"
515+ nProxyCfg := & graph.EffectiveNginxProxy {
516+ Kubernetes : & ngfAPIv1alpha2.KubernetesSpec {
517+ Deployment : & ngfAPIv1alpha2.DeploymentSpec {
518+ Replicas : tc .configReplicas ,
519+ Autoscaling : & ngfAPIv1alpha2.AutoscalingSpec {Enable : true },
520+ },
521+ },
522+ }
523+
524+ objects , err := provisioner .buildNginxResourceObjects (resourceName , gateway , nProxyCfg )
525+ g .Expect (err ).ToNot (HaveOccurred ())
526+
527+ // Find the deployment object
528+ var deployment * appsv1.Deployment
529+ for _ , obj := range objects {
530+ if d , ok := obj .(* appsv1.Deployment ); ok {
531+ deployment = d
532+ break
533+ }
534+ }
535+ g .Expect (deployment ).ToNot (BeNil ())
536+
537+ if tc .expectedNil {
538+ g .Expect (deployment .Spec .Replicas ).To (BeNil (), tc .description )
539+ } else {
540+ g .Expect (deployment .Spec .Replicas ).ToNot (BeNil (), tc .description )
541+ g .Expect (* deployment .Spec .Replicas ).To (Equal (tc .expectedValue ), tc .description )
542+ }
543+ })
463544 }
464- g .Expect (deployment ).ToNot (BeNil ())
465- g .Expect (deployment .Spec .Replicas ).ToNot (BeNil ())
466- g .Expect (* deployment .Spec .Replicas ).To (Equal (int32 (7 )))
467545}
468546
469547func TestBuildNginxResourceObjects_Plus (t * testing.T ) {
0 commit comments