Skip to content
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

[occm] add tag on floating ip create #2577

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
39 changes: 39 additions & 0 deletions pkg/openstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/gophercloud/gophercloud/v2/openstack/loadbalancer/v2/loadbalancers"
v2monitors "github.com/gophercloud/gophercloud/v2/openstack/loadbalancer/v2/monitors"
v2pools "github.com/gophercloud/gophercloud/v2/openstack/loadbalancer/v2/pools"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -603,6 +604,42 @@ func (lbaas *LbaasV2) updateFloatingIP(floatingip *floatingips.FloatingIP, portI
return floatingip, nil
}

func (lbaas *LbaasV2) updateFloatingIPTag(floatingip *floatingips.FloatingIP, Tag string, delete bool) error {
lidongshengxdayu marked this conversation as resolved.
Show resolved Hide resolved
if Tag == "" {
return fmt.Errorf("Error input tag argument ")
}
tags, err := attributestags.List(context.TODO(), lbaas.network, "floatingips", floatingip.ID).Extract()
if err != nil {
klog.V(3).Infof("Cannot get floatIP tags for floating %s", floatingip.ID)
kayrus marked this conversation as resolved.
Show resolved Hide resolved
return err
}
found := false
for _, tag := range tags {
if tag == Tag {
found = true
}
}
if delete {
if !found {
return nil
}
err = attributestags.Delete(context.TODO(), lbaas.network, "floatingips", floatingip.ID, Tag).ExtractErr()
if err != nil {
klog.V(3).Infof("Cannot update floatIP tag %s for floating %s", Tag, floatingip.ID)
}
return err
}

if !found {
err = attributestags.Add(context.TODO(), lbaas.network, "floatingips", floatingip.ID, Tag).ExtractErr()
if err != nil {
klog.V(3).Infof("Cannot update floatIP tag %s for floating %s", Tag, floatingip.ID)
}
return err
}
return nil
}

// ensureFloatingIP manages a FIP for a Service and returns the address that should be advertised in the
// .Status.LoadBalancer. In particular it will:
// 1. Lookup if any FIP is already attached to the VIP port of the LB.
Expand Down Expand Up @@ -648,6 +685,7 @@ func (lbaas *LbaasV2) ensureFloatingIP(clusterName string, service *corev1.Servi
if err != nil {
return "", err
}
_ = lbaas.updateFloatingIPTag(floatIP, svcConf.lbName, true)
}
}
return lb.VipAddress, nil
Expand Down Expand Up @@ -747,6 +785,7 @@ func (lbaas *LbaasV2) ensureFloatingIP(clusterName string, service *corev1.Servi
}

if floatIP != nil {
_ = lbaas.updateFloatingIPTag(floatIP, svcConf.lbName, false)
return floatIP.FloatingIP, nil
}

Expand Down