Skip to content

Commit

Permalink
Add annotation to specify floating ip to use on LB when creating ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleouf66 committed Mar 15, 2023
1 parent abb3f2b commit 66ee273
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
36 changes: 30 additions & 6 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ const (
// Default to false.
IngressAnnotationLoadBalancerKeepFloatingIP = "octavia.ingress.kubernetes.io/keep-floatingip"

// IngressAnnotationFloatingIp is the key of the annotation on an ingress to set floating IP that will be associated to LoadBalancers.
// If the floatingIP is not available, a new one will be created.
IngressAnnotationFloatingIp = "octavia.ingress.kubernetes.io/floatingip"

// IngressAnnotationSourceRangesKey is the key of the annotation on an ingress to set allowed IP ranges on their LoadBalancers.
// It should be a comma-separated list of CIDRs.
IngressAnnotationSourceRangesKey = "octavia.ingress.kubernetes.io/whitelist-source-range"
Expand Down Expand Up @@ -599,7 +603,7 @@ func (c *Controller) deleteIngress(ing *nwv1.Ingress) error {
// any floating IPs associated with the load balancer VIP port.
logger.Debug("deleting floating IP")

if _, err = c.osClient.EnsureFloatingIP(true, loadbalancer.VipPortID, "", ""); err != nil {
if _, err = c.osClient.EnsureFloatingIP(true, loadbalancer.VipPortID, "", "", ""); err != nil {
return fmt.Errorf("failed to delete floating IP: %v", err)
}

Expand Down Expand Up @@ -926,15 +930,35 @@ func (c *Controller) ensureIngress(ing *nwv1.Ingress) error {
address := lb.VipAddress
// Allocate floating ip for loadbalancer vip if the external network is configured and the Ingress is not internal.
if !isInternal && c.config.Octavia.FloatingIPNetwork != "" {
logger.Info("creating floating IP")

description := fmt.Sprintf("Floating IP for Kubernetes ingress %s in namespace %s from cluster %s", ingName, ingNamespace, clusterName)
address, err = c.osClient.EnsureFloatingIP(false, lb.VipPortID, c.config.Octavia.FloatingIPNetwork, description)
floatingIPSetting := getStringFromIngressAnnotation(ing, IngressAnnotationFloatingIp, "")
if err != nil {
return fmt.Errorf("failed to create floating IP: %v", err)
return fmt.Errorf("unknown annotation %s: %v", IngressAnnotationFloatingIp, err)
}

description := fmt.Sprintf("Floating IP for Kubernetes ingress %s in namespace %s from cluster %s", ingName, ingNamespace, clusterName)
successReUseFIPS := false

if floatingIPSetting != "" {
logger.Info("try to use floating IP : ", floatingIPSetting)
address, err = c.osClient.EnsureFloatingIP(false, lb.VipPortID, floatingIPSetting, c.config.Octavia.FloatingIPNetwork, description)
if err != nil {
logger.Info("failed to use provided floating IP ", floatingIPSetting, " : ", err)
} else {
successReUseFIPS = true
}
}

logger.WithFields(log.Fields{"fip": address}).Info("floating IP created")
if !successReUseFIPS {
logger.Info("creating floating IP")

address, err = c.osClient.EnsureFloatingIP(false, lb.VipPortID, "", c.config.Octavia.FloatingIPNetwork, description)
if err != nil {
return fmt.Errorf("failed to create floating IP: %v", err)
}

logger.WithFields(log.Fields{"fip": address}).Info("floating IP created")
}
}

// Update ingress status
Expand Down
40 changes: 36 additions & 4 deletions pkg/ingress/controller/openstack/neutron.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (os *OpenStack) getPorts(listOpts ports.ListOpts) ([]ports.Port, error) {
}

// EnsureFloatingIP makes sure a floating IP is allocated for the port
func (os *OpenStack) EnsureFloatingIP(needDelete bool, portID string, floatingIPNetwork string, description string) (string, error) {
func (os *OpenStack) EnsureFloatingIP(needDelete bool, portID string, existingfloatingIP string, floatingIPNetwork string, description string) (string, error) {
listOpts := floatingips.ListOpts{PortID: portID}
fips, err := os.getFloatingIPs(listOpts)
if err != nil {
Expand Down Expand Up @@ -100,9 +100,41 @@ func (os *OpenStack) EnsureFloatingIP(needDelete bool, portID string, floatingIP
FloatingNetworkID: floatingIPNetwork,
Description: description,
}
fip, err = floatingips.Create(os.neutron, floatIPOpts).Extract()
if err != nil {
return "", err
if existingfloatingIP != "" {
// try to find fip
opts := &floatingips.ListOpts{
FloatingIP: existingfloatingIP,
FloatingNetworkID: floatingIPNetwork,
}
allPages, err := floatingips.List(os.neutron, opts).AllPages()
if err != nil {
return "", err
}
osFips, err := floatingips.ExtractFloatingIPs(allPages)
if err != nil {
return "", err
}
if len(osFips) != 1 {
return "", err
}
// check if fip is used
if osFips[0].PortID != "" {
return "", fmt.Errorf("floating IP %s already used by port %s", osFips[0].FloatingIP, osFips[0].PortID)
}
updateOpts := floatingips.UpdateOpts{
PortID: &portID,
Description: &description,
}
// attach fip to lb vip
fip, err = floatingips.Update(os.neutron, osFips[0].ID, updateOpts).Extract()
if err != nil {
return "", err
}
} else {
fip, err = floatingips.Create(os.neutron, floatIPOpts).Extract()
if err != nil {
return "", err
}
}
} else {
fip = &fips[0]
Expand Down

0 comments on commit 66ee273

Please sign in to comment.