Skip to content

Commit e9c21d8

Browse files
committed
Refactor frontend VPC subnet lookup to require both vpc-name and subnet-name annotations. Change annotation check from OR to AND logic for vpc-name and subnet-name
1 parent e78a176 commit e9c21d8

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

cloud/linode/loadbalancers.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ func (l *loadbalancers) getVPCCreateOptions(ctx context.Context, service *v1.Ser
856856
func (l *loadbalancers) getFrontendVPCCreateOptions(ctx context.Context, service *v1.Service) ([]linodego.NodeBalancerVPCOptions, error) {
857857
frontendIPv4Range, hasIPv4Range := service.GetAnnotations()[annotations.NodeBalancerFrontendIPv4Range]
858858
frontendIPv6Range, hasIPv6Range := service.GetAnnotations()[annotations.NodeBalancerFrontendIPv6Range]
859-
_, hasVPCName := service.GetAnnotations()[annotations.NodeBalancerFrontendVPCName]
860-
_, hasSubnetName := service.GetAnnotations()[annotations.NodeBalancerFrontendSubnetName]
859+
vpcName, hasVPCName := service.GetAnnotations()[annotations.NodeBalancerFrontendVPCName]
860+
subnetName, hasSubnetName := service.GetAnnotations()[annotations.NodeBalancerFrontendSubnetName]
861861
frontendSubnetID, hasSubnetID := service.GetAnnotations()[annotations.NodeBalancerFrontendSubnetID]
862862

863863
// If no frontend VPC annotations are present, do not configure a frontend VPC.
@@ -881,8 +881,8 @@ func (l *loadbalancers) getFrontendVPCCreateOptions(ctx context.Context, service
881881
if err != nil {
882882
return nil, fmt.Errorf("invalid frontend subnet ID: %w", err)
883883
}
884-
case hasVPCName || hasSubnetName:
885-
subnetID, err = l.getFrontendSubnetIDForSVC(ctx, service)
884+
case hasVPCName && hasSubnetName:
885+
subnetID, err = l.getSubnetIDByVPCAndSubnetNames(ctx, vpcName, subnetName)
886886
if err != nil {
887887
return nil, err
888888
}
@@ -901,36 +901,19 @@ func (l *loadbalancers) getFrontendVPCCreateOptions(ctx context.Context, service
901901
return vpcCreateOpts, nil
902902
}
903903

904-
// getFrontendSubnetIDForSVC returns the subnet ID for the frontend VPC configuration.
905-
// Following precedence rules are applied:
906-
// 1. If the service has an annotation for FrontendSubnetID, use that.
907-
// 2. If the service has annotations specifying FrontendVPCName or FrontendSubnetName, use them.
908-
// 3. Return error if no VPC configuration is found.
909-
func (l *loadbalancers) getFrontendSubnetIDForSVC(ctx context.Context, service *v1.Service) (int, error) {
910-
// Check if the service has an annotation for FrontendSubnetID
911-
if specifiedSubnetID, ok := service.GetAnnotations()[annotations.NodeBalancerFrontendSubnetID]; ok {
912-
subnetID, err := strconv.Atoi(specifiedSubnetID)
913-
if err != nil {
914-
return 0, fmt.Errorf("invalid frontend subnet ID: %w", err)
915-
}
916-
return subnetID, nil
904+
// getSubnetIDByVPCAndSubnetNames returns the subnet ID for the given VPC name and subnet name.
905+
func (l *loadbalancers) getSubnetIDByVPCAndSubnetNames(ctx context.Context, vpcName, subnetName string) (int, error) {
906+
if vpcName == "" || subnetName == "" {
907+
return 0, fmt.Errorf("frontend VPC configuration requires either subnet-id annotation or both vpc-name and subnet-name annotations. No vpc-name or subnet-name annotation found")
917908
}
918909

919-
specifiedVPCName, vpcOk := service.GetAnnotations()[annotations.NodeBalancerFrontendVPCName]
920-
specifiedSubnetName, subnetOk := service.GetAnnotations()[annotations.NodeBalancerFrontendSubnetName]
921-
922-
// If no VPCName or SubnetName is specified and no subnet-id is provided, return error
923-
if !vpcOk || !subnetOk {
924-
return 0, fmt.Errorf("frontend VPC configuration requires either subnet-id annotation or both vpc-name and subnet-name annotations")
925-
}
926-
927-
vpcID, err := services.GetVPCID(ctx, l.client, specifiedVPCName)
910+
vpcID, err := services.GetVPCID(ctx, l.client, vpcName)
928911
if err != nil {
929-
return 0, fmt.Errorf("failed to get VPC ID for frontend VPC '%s': %w", specifiedVPCName, err)
912+
return 0, fmt.Errorf("failed to get VPC ID for frontend VPC '%s': %w", vpcName, err)
930913
}
931914

932915
// Use the VPC ID and Subnet Name to get the subnet ID
933-
return services.GetSubnetID(ctx, l.client, vpcID, specifiedSubnetName)
916+
return services.GetSubnetID(ctx, l.client, vpcID, subnetName)
934917
}
935918

936919
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {

0 commit comments

Comments
 (0)