-
Notifications
You must be signed in to change notification settings - Fork 68
[improvement][breaking] : allow auto-allocation of NB backend ips #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
c532126
1c013e4
2e37923
43adf9a
0559ea3
814247e
be69ff5
50a1c55
427bd32
828e2d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -444,12 +444,16 @@ | |
// Add all of the Nodes to the config | ||
newNBNodes := make([]linodego.NodeBalancerConfigRebuildNodeOptions, 0, len(nodes)) | ||
subnetID := 0 | ||
if Options.NodeBalancerBackendIPv4SubnetID != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for resolving the subnetID appears in multiple places (e.g. in updateNodeBalancer and buildLoadBalancerRequest). Consider refactoring this repeated code into a shared helper function to ensure consistency and simplify future maintenance. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
subnetID = Options.NodeBalancerBackendIPv4SubnetID | ||
} | ||
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range] | ||
if ok { | ||
if err = validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
rahulait marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends { | ||
var id int | ||
id, err = l.getSubnetIDForSVC(ctx, service) | ||
if err != nil { | ||
|
@@ -717,6 +721,86 @@ | |
return linodego.NodeBalancerPlanType(Options.DefaultNBType) | ||
} | ||
|
||
// getVPCCreateOptions returns the VPC options for the NodeBalancer creation. | ||
// Order of precedence: | ||
// 1. NodeBalancerBackendIPv4Range annotation | ||
// 2. NodeBalancerBackendVPCName and NodeBalancerBackendSubnetName annotation | ||
// 3. NodeBalancerBackendIPv4SubnetID/NodeBalancerBackendIPv4SubnetName flag | ||
// 4. NodeBalancerBackendIPv4Subnet flag | ||
// 5. Default to using the subnet ID of the service's VPC | ||
func (l *loadbalancers) getVPCCreateOptions(ctx context.Context, service *v1.Service) ([]linodego.NodeBalancerVPCOptions, error) { | ||
rahulait marked this conversation as resolved.
Show resolved
Hide resolved
rahulait marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Evaluate subnetID based on annotations or flags | ||
subnetID, err := l.getSubnetIDForSVC(ctx, service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Precedence 1: If the user has specified a NodeBalancerBackendIPv4Range, use that | ||
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range] | ||
if ok { | ||
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil { | ||
return nil, err | ||
} | ||
// If the user has specified a NodeBalancerBackendIPv4Range, use that | ||
// for the NodeBalancer backend ipv4 range | ||
if backendIPv4Range != "" { | ||
vpcCreateOpts := []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: subnetID, | ||
IPv4Range: backendIPv4Range, | ||
}, | ||
} | ||
return vpcCreateOpts, nil | ||
} | ||
} | ||
|
||
// Precedence 2: If the user wants to overwrite the default VPC name or subnet name | ||
// and have specified it in the annotations, use it to set subnetID | ||
// and auto-allocate subnets from it for the NodeBalancer | ||
_, vpcInAnnotation := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName] | ||
_, subnetInAnnotation := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetName] | ||
if vpcInAnnotation || subnetInAnnotation { | ||
vpcCreateOpts := []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: subnetID, | ||
}, | ||
} | ||
return vpcCreateOpts, nil | ||
} | ||
|
||
// Precedence 3: If the user has specified a NodeBalancerBackendIPv4SubnetID, use that | ||
// and auto-allocate subnets from it for the NodeBalancer | ||
if Options.NodeBalancerBackendIPv4SubnetID != 0 { | ||
vpcCreateOpts := []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: Options.NodeBalancerBackendIPv4SubnetID, | ||
}, | ||
} | ||
return vpcCreateOpts, nil | ||
} | ||
|
||
// Precedence 4: If the user has specified a NodeBalancerBackendIPv4Subnet, use that | ||
// and auto-allocate subnets from it for the NodeBalancer | ||
if Options.NodeBalancerBackendIPv4Subnet != "" { | ||
vpcCreateOpts := []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: subnetID, | ||
IPv4Range: Options.NodeBalancerBackendIPv4Subnet, | ||
IPv4RangeAutoAssign: true, | ||
}, | ||
} | ||
return vpcCreateOpts, nil | ||
} | ||
|
||
// Default to using the subnet ID of the service's VPC | ||
vpcCreateOpts := []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: subnetID, | ||
}, | ||
} | ||
return vpcCreateOpts, nil | ||
} | ||
|
||
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) { | ||
connThrottle := getConnectionThrottle(service) | ||
|
||
|
@@ -732,21 +816,11 @@ | |
Type: nbType, | ||
} | ||
|
||
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range] | ||
if ok { | ||
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil { | ||
return nil, err | ||
} | ||
subnetID, err := l.getSubnetIDForSVC(ctx, service) | ||
if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends { | ||
createOpts.VPCs, err = l.getVPCCreateOptions(ctx, service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
createOpts.VPCs = []linodego.NodeBalancerVPCOptions{ | ||
{ | ||
SubnetID: subnetID, | ||
IPv4Range: backendIPv4Range, | ||
}, | ||
} | ||
} | ||
|
||
fwid, ok := service.GetAnnotations()[annotations.AnnLinodeCloudFirewallID] | ||
|
@@ -882,6 +956,13 @@ | |
if Options.VPCNames == "" { | ||
return 0, fmt.Errorf("CCM not configured with VPC, cannot create NodeBalancer with specified annotation") | ||
} | ||
if specifiedSubnetID, ok := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetID]; ok { | ||
subnetID, err := strconv.Atoi(specifiedSubnetID) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return subnetID, nil | ||
} | ||
vpcName := strings.Split(Options.VPCNames, ",")[0] | ||
if specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]; ok { | ||
vpcName = specifiedVPCName | ||
|
@@ -907,11 +988,17 @@ | |
configs := make([]*linodego.NodeBalancerConfigCreateOptions, 0, len(ports)) | ||
|
||
subnetID := 0 | ||
if Options.NodeBalancerBackendIPv4SubnetID != 0 { | ||
subnetID = Options.NodeBalancerBackendIPv4SubnetID | ||
} | ||
// Check for the NodeBalancerBackendIPv4Range annotation | ||
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range] | ||
if ok { | ||
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends { | ||
id, err := l.getSubnetIDForSVC(ctx, service) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -1088,9 +1175,9 @@ | |
} | ||
|
||
// getNodePrivateIP provides the Linode Backend IP the NodeBalancer will communicate with. | ||
// If a service specifies NodeBalancerBackendIPv4Range annotation, it will | ||
// If CCM runs within VPC and DisableNodeBalancerVPCBackends is set to false, it will | ||
// use NodeInternalIP of node. | ||
// For services which don't have NodeBalancerBackendIPv4Range annotation, | ||
// For services outside of VPC, it will use linode specific private IP address | ||
// Backend IP can be overwritten to the one specified using AnnLinodeNodePrivateIP | ||
// annotation over the NodeInternalIP. | ||
func getNodePrivateIP(node *v1.Node, subnetID int) string { | ||
|
Uh oh!
There was an error while loading. Please reload this page.