Skip to content

Commit 36668fa

Browse files
authored
[occm] network-id and subnet-id in class settings (kubernetes#1979)
This patch make the `EnsureLoadbalancer` honor the settings for network-id and subnet-id made in the load balancer classes. Signed-off-by: Sven Haardiek <[email protected]> Signed-off-by: Sven Haardiek <[email protected]>
1 parent 03db687 commit 36668fa

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

docs/openstack-cloud-controller-manager/expose-applications-using-loadbalancer-type-service.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Request Body:
108108

109109
- `loadbalancer.openstack.org/class`
110110

111-
The name of a preconfigured class in the config file. If provided, this config options included in the class section take precedence over the annotations of floating-subnet-id and floating-network-id. See the section below for how it works.
111+
The name of a preconfigured class in the config file. If provided, this config options included in the class section take precedence over the annotations of floating-subnet-id, floating-network-id, network-id, subnet-id and member-subnet-id . See the section below for how it works.
112112

113113
- `loadbalancer.openstack.org/subnet-id`
114114

pkg/openstack/loadbalancer.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,62 @@ func (lbaas *LbaasV2) getMemberSubnetID(service *corev1.Service, svcConf *servic
15201520
return "", nil
15211521
}
15221522

1523+
// getSubnetID gets the configured subnet-id from the different possible sources.
1524+
func (lbaas *LbaasV2) getSubnetID(service *corev1.Service, svcConf *serviceConfig) (string, error) {
1525+
// Get subnet from service annotation
1526+
SubnetIDAnnotation := getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerSubnetID, "")
1527+
if SubnetIDAnnotation != "" {
1528+
return SubnetIDAnnotation, nil
1529+
}
1530+
1531+
// Get subnet from config class
1532+
configClassName := getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerClass, "")
1533+
if configClassName != "" {
1534+
lbClass := lbaas.opts.LBClasses[configClassName]
1535+
if lbClass == nil {
1536+
return "", fmt.Errorf("invalid loadbalancer class %q", configClassName)
1537+
}
1538+
if lbClass.SubnetID != "" {
1539+
return lbClass.SubnetID, nil
1540+
}
1541+
}
1542+
1543+
// Get subnet from Default Config
1544+
if lbaas.opts.SubnetID != "" {
1545+
return lbaas.opts.SubnetID, nil
1546+
}
1547+
1548+
return "", nil
1549+
}
1550+
1551+
// getNetworkID gets the configured network-id from the different possible sources.
1552+
func (lbaas *LbaasV2) getNetworkID(service *corev1.Service, svcConf *serviceConfig) (string, error) {
1553+
// Get subnet from service annotation
1554+
SubnetIDAnnotation := getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerNetworkID, "")
1555+
if SubnetIDAnnotation != "" {
1556+
return SubnetIDAnnotation, nil
1557+
}
1558+
1559+
// Get subnet from config class
1560+
configClassName := getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerClass, "")
1561+
if configClassName != "" {
1562+
lbClass := lbaas.opts.LBClasses[configClassName]
1563+
if lbClass == nil {
1564+
return "", fmt.Errorf("invalid loadbalancer class %q", configClassName)
1565+
}
1566+
if lbClass.NetworkID != "" {
1567+
return lbClass.NetworkID, nil
1568+
}
1569+
}
1570+
1571+
// Get subnet from Default Config
1572+
if lbaas.opts.NetworkID != "" {
1573+
return lbaas.opts.NetworkID, nil
1574+
}
1575+
1576+
return "", nil
1577+
}
1578+
15231579
func (lbaas *LbaasV2) checkServiceUpdate(service *corev1.Service, nodes []*corev1.Node, svcConf *serviceConfig) error {
15241580
if len(service.Spec.Ports) == 0 {
15251581
return fmt.Errorf("no ports provided to openstack load balancer")
@@ -1654,8 +1710,18 @@ func (lbaas *LbaasV2) checkService(service *corev1.Service, nodes []*corev1.Node
16541710

16551711
svcConf.connLimit = getIntFromServiceAnnotation(service, ServiceAnnotationLoadBalancerConnLimit, -1)
16561712

1657-
svcConf.lbNetworkID = getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerNetworkID, lbaas.opts.NetworkID)
1658-
svcConf.lbSubnetID = getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerSubnetID, lbaas.opts.SubnetID)
1713+
lbNetworkID, err := lbaas.getNetworkID(service, svcConf)
1714+
if err != nil {
1715+
return fmt.Errorf("failed to get network id to create load balancer for service %s: %v", serviceName, err)
1716+
}
1717+
svcConf.lbNetworkID = lbNetworkID
1718+
1719+
lbSubnetID, err := lbaas.getSubnetID(service, svcConf)
1720+
if err != nil {
1721+
return fmt.Errorf("failed to get subnet to create load balancer for service %s: %v", serviceName, err)
1722+
}
1723+
svcConf.lbSubnetID = lbSubnetID
1724+
16591725
if lbaas.opts.SubnetID != "" {
16601726
svcConf.lbMemberSubnetID = lbaas.opts.SubnetID
16611727
} else {

0 commit comments

Comments
 (0)