@@ -27,6 +27,107 @@ import (
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828)
2929
30+ func TestLBPNNameSelectorGate (t * testing.T ) {
31+ vpcAPI := & fakeVPCAPI {
32+ vpcs : []* scwvpc.VPC {
33+ {ID : "vpc-default" , Name : "default" , Region : scw .RegionFrPar , ProjectID : "proj-1" },
34+ },
35+ privateNetworks : []* scwvpc.PrivateNetwork {
36+ {ID : "pn-resolved-1" , Name : "my-cluster" , VpcID : "vpc-default" },
37+ },
38+ }
39+
40+ testLB := & scwlb.LB {
41+ ID : "lb-1" ,
42+ Zone : scw .ZoneFrPar2 ,
43+ }
44+
45+ t .Run ("pn-names ignored when gate is disabled" , func (t * testing.T ) {
46+ // Do NOT set SCW_ENABLE_LB_PN_NAME_SELECTOR (gate off by default)
47+ t .Setenv (enableLBPNNameSelectorEnv , "" )
48+
49+ lbAPI := & fakeLBAPI {}
50+ lb := newTestLB (lbAPI , vpcAPI , "pn-from-env" )
51+
52+ service := & v1.Service {
53+ ObjectMeta : metav1.ObjectMeta {
54+ Annotations : map [string ]string {
55+ "service.beta.kubernetes.io/scw-loadbalancer-pn-names" : "default/my-cluster" ,
56+ },
57+ },
58+ }
59+
60+ gotIDs , err := lb .attachPrivateNetworks (testLB , service , false , scw .RegionFrPar , "proj-1" )
61+ if err != nil {
62+ t .Fatalf ("unexpected error: %v" , err )
63+ }
64+
65+ // pn-names should be ignored; should fall back to env var PN_ID
66+ if len (gotIDs ) != 1 || gotIDs [0 ] != "pn-from-env" {
67+ t .Errorf ("expected fallback to env var PN_ID [pn-from-env], got %v" , gotIDs )
68+ }
69+ if len (lbAPI .attachCalls ) != 1 || lbAPI .attachCalls [0 ] != "pn-from-env" {
70+ t .Errorf ("expected attach call for pn-from-env, got %v" , lbAPI .attachCalls )
71+ }
72+ })
73+
74+ t .Run ("pn-names used when gate is enabled" , func (t * testing.T ) {
75+ t .Setenv (enableLBPNNameSelectorEnv , "true" )
76+
77+ lbAPI := & fakeLBAPI {}
78+ lb := newTestLB (lbAPI , vpcAPI , "pn-from-env" )
79+
80+ service := & v1.Service {
81+ ObjectMeta : metav1.ObjectMeta {
82+ Annotations : map [string ]string {
83+ "service.beta.kubernetes.io/scw-loadbalancer-pn-names" : "default/my-cluster" ,
84+ },
85+ },
86+ }
87+
88+ gotIDs , err := lb .attachPrivateNetworks (testLB , service , false , scw .RegionFrPar , "proj-1" )
89+ if err != nil {
90+ t .Fatalf ("unexpected error: %v" , err )
91+ }
92+
93+ // pn-names should resolve, taking precedence over env var
94+ if len (gotIDs ) != 1 || gotIDs [0 ] != "pn-resolved-1" {
95+ t .Errorf ("expected resolved PN [pn-resolved-1], got %v" , gotIDs )
96+ }
97+ if len (lbAPI .attachCalls ) != 1 || lbAPI .attachCalls [0 ] != "pn-resolved-1" {
98+ t .Errorf ("expected attach call for pn-resolved-1, got %v" , lbAPI .attachCalls )
99+ }
100+ })
101+
102+ t .Run ("pn-names only with no env var returns nil when gate disabled" , func (t * testing.T ) {
103+ t .Setenv (enableLBPNNameSelectorEnv , "" )
104+
105+ lbAPI := & fakeLBAPI {}
106+ lb := newTestLB (lbAPI , vpcAPI , "" ) // no env var PN_ID
107+
108+ service := & v1.Service {
109+ ObjectMeta : metav1.ObjectMeta {
110+ Annotations : map [string ]string {
111+ "service.beta.kubernetes.io/scw-loadbalancer-pn-names" : "default/my-cluster" ,
112+ },
113+ },
114+ }
115+
116+ gotIDs , err := lb .attachPrivateNetworks (testLB , service , false , scw .RegionFrPar , "proj-1" )
117+ if err != nil {
118+ t .Fatalf ("unexpected error: %v" , err )
119+ }
120+
121+ // pn-names ignored + no env var = no PNs configured
122+ if gotIDs != nil {
123+ t .Errorf ("expected nil configured IDs when gate disabled and no env var, got %v" , gotIDs )
124+ }
125+ if len (lbAPI .attachCalls ) != 0 {
126+ t .Errorf ("expected no attach calls, got %v" , lbAPI .attachCalls )
127+ }
128+ })
129+ }
130+
30131// fakeLBAPI implements the subset of LoadBalancerAPI needed for attachPrivateNetworks tests.
31132type fakeLBAPI struct {
32133 privateNetworks []* scwlb.PrivateNetwork
@@ -391,6 +492,9 @@ func TestAttachPrivateNetworks(t *testing.T) {
391492
392493 for _ , tt := range tests {
393494 t .Run (tt .name , func (t * testing.T ) {
495+ // Enable the PN name selector feature for all tests in this suite
496+ t .Setenv (enableLBPNNameSelectorEnv , "true" )
497+
394498 lbAPI := & fakeLBAPI {
395499 privateNetworks : tt .existingPNs ,
396500 }
0 commit comments