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
36 changes: 36 additions & 0 deletions pkg/openstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -618,6 +619,39 @@ func (lbaas *LbaasV2) updateFloatingIP(ctx context.Context, floatingip *floating
return floatingip, nil
}

func (lbaas *LbaasV2) updateFloatingIPTag(ctx context.Context, floatingip *floatingips.FloatingIP, Tag string, delete bool) error {
kayrus marked this conversation as resolved.
Show resolved Hide resolved
if Tag == "" {
return fmt.Errorf("Error input tag argument ")
}
tags, err := attributestags.List(ctx, 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 := slices.Contains(tags, Tag)

if delete {
if !found {
return nil
}
err = attributestags.Delete(ctx, 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(ctx, 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 @@ -663,6 +697,7 @@ func (lbaas *LbaasV2) ensureFloatingIP(ctx context.Context, clusterName string,
if err != nil {
return "", err
}
_ = lbaas.updateFloatingIPTag(ctx, floatIP, svcConf.lbName, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why do you skip err?

Copy link
Author

@lidongshengxdayu lidongshengxdayu Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether returning an error will cause a wider range of logic errors, and I'm not very familiar with the code base.
And tag is optional. I think it should not affect the normal logic of floating ip. Even if an error occurs, it can be checked through the log.
At this position, even if an error occurs and return, the next loop cannot be updated either

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think this and the add below need to return err, tell me and I will add it.

}
}
return lb.VipAddress, nil
Expand Down Expand Up @@ -762,6 +797,7 @@ func (lbaas *LbaasV2) ensureFloatingIP(ctx context.Context, clusterName string,
}

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

Expand Down