@@ -452,7 +452,8 @@ func (l *loadbalancers) updateNodeBalancer(
452452 if err = validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
453453 return err
454454 }
455-
455+ }
456+ if Options .VPCNames != "" {
456457 var id int
457458 id , err = l .getSubnetIDForSVC (ctx , service )
458459 if err != nil {
@@ -720,6 +721,85 @@ func (l *loadbalancers) GetLinodeNBType(service *v1.Service) linodego.NodeBalanc
720721 return linodego .NodeBalancerPlanType (Options .DefaultNBType )
721722}
722723
724+ // getVPCCreateOptions returns the VPC options for the NodeBalancer creation.
725+ // Order of precedence:
726+ // 1. NodeBalancerBackendIPv4Range annotation
727+ // 2. NodeBalancerBackendVPCName and NodeBalancerBackendSubnetName annotation
728+ // 3. NodeBalancerBackendIPv4SubnetID/NodeBalancerBackendIPv4SubnetName flag
729+ // 4. NodeBalancerBackendIPv4Subnet flag
730+ // 5. Default to using the subnet ID of the service's VPC
731+ func (l * loadbalancers ) getVPCCreateOptions (ctx context.Context , service * v1.Service ) ([]linodego.NodeBalancerVPCOptions , error ) {
732+ subnetID , err := l .getSubnetIDForSVC (ctx , service )
733+ if err != nil {
734+ return nil , err
735+ }
736+
737+ backendIPv4Range , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendIPv4Range ]
738+ if ok {
739+ if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
740+ return nil , err
741+ }
742+ }
743+
744+ // If the user has specified NodeBalancerBackendIPv4Range annotation for service,
745+ // use it for the NodeBalancer backend ipv4 range
746+ if backendIPv4Range != "" {
747+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
748+ {
749+ SubnetID : subnetID ,
750+ IPv4Range : backendIPv4Range ,
751+ },
752+ }
753+ return vpcCreateOpts , nil
754+ }
755+
756+ // If the user wants to overwrite the default VPC name or subnet name
757+ // and have specified it in the annotations, use it for the NodeBalancer
758+ // backend ipv4 range
759+ _ , vpcInAnnotation := service .GetAnnotations ()[annotations .NodeBalancerBackendVPCName ]
760+ _ , subnetInAnnotation := service .GetAnnotations ()[annotations .NodeBalancerBackendSubnetName ]
761+ if vpcInAnnotation || subnetInAnnotation {
762+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
763+ {
764+ SubnetID : subnetID ,
765+ },
766+ }
767+ return vpcCreateOpts , nil
768+ }
769+
770+ // If the user has specified a NodeBalancerBackendIPv4SubnetID, use that
771+ // and auto-allocate subnets from it for the NodeBalancer
772+ if Options .NodeBalancerBackendIPv4SubnetID != 0 {
773+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
774+ {
775+ SubnetID : Options .NodeBalancerBackendIPv4SubnetID ,
776+ },
777+ }
778+ return vpcCreateOpts , nil
779+ }
780+
781+ // If the user has specified a NodeBalancerBackendIPv4Subnet, use that
782+ // and auto-allocate subnets from it for the NodeBalancer
783+ if Options .NodeBalancerBackendIPv4Subnet != "" {
784+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
785+ {
786+ SubnetID : subnetID ,
787+ IPv4Range : Options .NodeBalancerBackendIPv4Subnet ,
788+ IPv4RangeAutoAssign : true ,
789+ },
790+ }
791+ return vpcCreateOpts , nil
792+ }
793+
794+ // Default to using the subnet ID of the service's VPC
795+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
796+ {
797+ SubnetID : subnetID ,
798+ },
799+ }
800+ return vpcCreateOpts , nil
801+ }
802+
723803func (l * loadbalancers ) createNodeBalancer (ctx context.Context , clusterName string , service * v1.Service , configs []* linodego.NodeBalancerConfigCreateOptions ) (lb * linodego.NodeBalancer , err error ) {
724804 connThrottle := getConnectionThrottle (service )
725805
@@ -735,27 +815,11 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
735815 Type : nbType ,
736816 }
737817
738- backendIPv4Range , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendIPv4Range ]
739- if ok {
740- if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
741- return nil , err
742- }
743- subnetID , err := l .getSubnetIDForSVC (ctx , service )
818+ if Options .VPCNames != "" {
819+ createOpts .VPCs , err = l .getVPCCreateOptions (ctx , service )
744820 if err != nil {
745821 return nil , err
746822 }
747- createOpts .VPCs = []linodego.NodeBalancerVPCOptions {
748- {
749- SubnetID : subnetID ,
750- IPv4Range : backendIPv4Range ,
751- },
752- }
753- } else if Options .NodeBalancerBackendIPv4SubnetID != 0 {
754- createOpts .VPCs = []linodego.NodeBalancerVPCOptions {
755- {
756- SubnetID : Options .NodeBalancerBackendIPv4SubnetID ,
757- },
758- }
759823 }
760824
761825 fwid , ok := service .GetAnnotations ()[annotations .AnnLinodeCloudFirewallID ]
@@ -891,6 +955,13 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
891955 if Options .VPCNames == "" {
892956 return 0 , fmt .Errorf ("CCM not configured with VPC, cannot create NodeBalancer with specified annotation" )
893957 }
958+ if specifiedSubnetID , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendSubnetID ]; ok {
959+ subnetID , err := strconv .Atoi (specifiedSubnetID )
960+ if err != nil {
961+ return 0 , err
962+ }
963+ return subnetID , nil
964+ }
894965 vpcName := strings .Split (Options .VPCNames , "," )[0 ]
895966 if specifiedVPCName , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendVPCName ]; ok {
896967 vpcName = specifiedVPCName
@@ -925,6 +996,8 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
925996 if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
926997 return nil , err
927998 }
999+ }
1000+ if Options .VPCNames != "" {
9281001 id , err := l .getSubnetIDForSVC (ctx , service )
9291002 if err != nil {
9301003 return nil , err
0 commit comments