Skip to content

Commit 87d70d4

Browse files
committed
add priority for annotations and add unittests
1 parent c06c3ae commit 87d70d4

File tree

4 files changed

+489
-50
lines changed

4 files changed

+489
-50
lines changed

cloud/linode/loadbalancers.go

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ func (l *loadbalancers) updateNodeBalancer(
401401
if err = validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil {
402402
return err
403403
}
404-
404+
}
405+
if Options.VPCNames != "" {
405406
var id int
406407
id, err = l.getSubnetIDForSVC(ctx, service)
407408
if err != nil {
@@ -669,6 +670,85 @@ func (l *loadbalancers) GetLinodeNBType(service *v1.Service) linodego.NodeBalanc
669670
return linodego.NodeBalancerPlanType(Options.DefaultNBType)
670671
}
671672

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+
672752
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
673753
connThrottle := getConnectionThrottle(service)
674754

@@ -684,27 +764,11 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
684764
Type: nbType,
685765
}
686766

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)
693769
if err != nil {
694770
return nil, err
695771
}
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-
}
708772
}
709773

710774
fwid, ok := service.GetAnnotations()[annotations.AnnLinodeCloudFirewallID]
@@ -829,6 +893,13 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
829893
if Options.VPCNames == "" {
830894
return 0, fmt.Errorf("CCM not configured with VPC, cannot create NodeBalancer with specified annotation")
831895
}
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+
}
832903
vpcName := strings.Split(Options.VPCNames, ",")[0]
833904
if specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]; ok {
834905
vpcName = specifiedVPCName
@@ -863,6 +934,8 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
863934
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil {
864935
return nil, err
865936
}
937+
}
938+
if Options.VPCNames != "" {
866939
id, err := l.getSubnetIDForSVC(ctx, service)
867940
if err != nil {
868941
return nil, err

0 commit comments

Comments
 (0)