@@ -401,7 +401,8 @@ func (l *loadbalancers) updateNodeBalancer(
401
401
if err = validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
402
402
return err
403
403
}
404
-
404
+ }
405
+ if Options .VPCNames != "" {
405
406
var id int
406
407
id , err = l .getSubnetIDForSVC (ctx , service )
407
408
if err != nil {
@@ -669,6 +670,85 @@ func (l *loadbalancers) GetLinodeNBType(service *v1.Service) linodego.NodeBalanc
669
670
return linodego .NodeBalancerPlanType (Options .DefaultNBType )
670
671
}
671
672
673
+ // getVPCCreateOptions returns the VPC options for the NodeBalancer creation.
674
+ // Order of precedence:
675
+ // 1. NodeBalancerBackendIPv4Range annotation
676
+ // 2. NodeBalancerBackendVPCName and NodeBalancerBackendSubnetName annotation
677
+ // 3. NodeBalancerBackendIPv4SubnetID/NodeBalancerBackendIPv4SubnetName flag
678
+ // 4. NodeBalancerBackendIPv4Subnet flag
679
+ // 5. Default to using the subnet ID of the service's VPC
680
+ func (l * loadbalancers ) getVPCCreateOptions (ctx context.Context , service * v1.Service ) ([]linodego.NodeBalancerVPCOptions , error ) {
681
+ subnetID , err := l .getSubnetIDForSVC (ctx , service )
682
+ if err != nil {
683
+ return nil , err
684
+ }
685
+
686
+ backendIPv4Range , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendIPv4Range ]
687
+ if ok {
688
+ if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
689
+ return nil , err
690
+ }
691
+ }
692
+
693
+ // If the user has specified NodeBalancerBackendIPv4Range annotation for service,
694
+ // use it for the NodeBalancer backend ipv4 range
695
+ if backendIPv4Range != "" {
696
+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
697
+ {
698
+ SubnetID : subnetID ,
699
+ IPv4Range : backendIPv4Range ,
700
+ },
701
+ }
702
+ return vpcCreateOpts , nil
703
+ }
704
+
705
+ // If the user wants to overwrite the default VPC name or subnet name
706
+ // and have specified it in the annotations, use it for the NodeBalancer
707
+ // backend ipv4 range
708
+ _ , vpcInAnnotation := service .GetAnnotations ()[annotations .NodeBalancerBackendVPCName ]
709
+ _ , subnetInAnnotation := service .GetAnnotations ()[annotations .NodeBalancerBackendSubnetName ]
710
+ if vpcInAnnotation || subnetInAnnotation {
711
+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
712
+ {
713
+ SubnetID : subnetID ,
714
+ },
715
+ }
716
+ return vpcCreateOpts , nil
717
+ }
718
+
719
+ // If the user has specified a NodeBalancerBackendIPv4SubnetID, use that
720
+ // and auto-allocate subnets from it for the NodeBalancer
721
+ if Options .NodeBalancerBackendIPv4SubnetID != 0 {
722
+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
723
+ {
724
+ SubnetID : Options .NodeBalancerBackendIPv4SubnetID ,
725
+ },
726
+ }
727
+ return vpcCreateOpts , nil
728
+ }
729
+
730
+ // If the user has specified a NodeBalancerBackendIPv4Subnet, use that
731
+ // and auto-allocate subnets from it for the NodeBalancer
732
+ if Options .NodeBalancerBackendIPv4Subnet != "" {
733
+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
734
+ {
735
+ SubnetID : subnetID ,
736
+ IPv4Range : Options .NodeBalancerBackendIPv4Subnet ,
737
+ IPv4RangeAutoAssign : true ,
738
+ },
739
+ }
740
+ return vpcCreateOpts , nil
741
+ }
742
+
743
+ // Default to using the subnet ID of the service's VPC
744
+ vpcCreateOpts := []linodego.NodeBalancerVPCOptions {
745
+ {
746
+ SubnetID : subnetID ,
747
+ },
748
+ }
749
+ return vpcCreateOpts , nil
750
+ }
751
+
672
752
func (l * loadbalancers ) createNodeBalancer (ctx context.Context , clusterName string , service * v1.Service , configs []* linodego.NodeBalancerConfigCreateOptions ) (lb * linodego.NodeBalancer , err error ) {
673
753
connThrottle := getConnectionThrottle (service )
674
754
@@ -684,27 +764,11 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
684
764
Type : nbType ,
685
765
}
686
766
687
- backendIPv4Range , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendIPv4Range ]
688
- if ok {
689
- if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
690
- return nil , err
691
- }
692
- subnetID , err := l .getSubnetIDForSVC (ctx , service )
767
+ if Options .VPCNames != "" {
768
+ createOpts .VPCs , err = l .getVPCCreateOptions (ctx , service )
693
769
if err != nil {
694
770
return nil , err
695
771
}
696
- createOpts .VPCs = []linodego.NodeBalancerVPCOptions {
697
- {
698
- SubnetID : subnetID ,
699
- IPv4Range : backendIPv4Range ,
700
- },
701
- }
702
- } else if Options .NodeBalancerBackendIPv4SubnetID != 0 {
703
- createOpts .VPCs = []linodego.NodeBalancerVPCOptions {
704
- {
705
- SubnetID : Options .NodeBalancerBackendIPv4SubnetID ,
706
- },
707
- }
708
772
}
709
773
710
774
fwid , ok := service .GetAnnotations ()[annotations .AnnLinodeCloudFirewallID ]
@@ -829,6 +893,13 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
829
893
if Options .VPCNames == "" {
830
894
return 0 , fmt .Errorf ("CCM not configured with VPC, cannot create NodeBalancer with specified annotation" )
831
895
}
896
+ if specifiedSubnetID , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendSubnetID ]; ok {
897
+ subnetID , err := strconv .Atoi (specifiedSubnetID )
898
+ if err != nil {
899
+ return 0 , err
900
+ }
901
+ return subnetID , nil
902
+ }
832
903
vpcName := strings .Split (Options .VPCNames , "," )[0 ]
833
904
if specifiedVPCName , ok := service .GetAnnotations ()[annotations .NodeBalancerBackendVPCName ]; ok {
834
905
vpcName = specifiedVPCName
@@ -863,6 +934,8 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
863
934
if err := validateNodeBalancerBackendIPv4Range (backendIPv4Range ); err != nil {
864
935
return nil , err
865
936
}
937
+ }
938
+ if Options .VPCNames != "" {
866
939
id , err := l .getSubnetIDForSVC (ctx , service )
867
940
if err != nil {
868
941
return nil , err
0 commit comments