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

Openstack delete dynamic floating ip in delete cluster #7045

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/resources/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ go_library(
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/servers:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/dns/v2/zones:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups:go_default_library",
Expand Down
77 changes: 35 additions & 42 deletions pkg/resources/openstack/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ limitations under the License.
package openstack

import (
"strings"

"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
v2pools "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"k8s.io/klog"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
)

const (
typeLB = "LoadBalancer"
typeLBL = "LBListener"
typeLBP = "LBPool"
typeLB = "LoadBalancer"
typeLBL = "LBListener"
typeLBP = "LBPool"
typeLBPM = "LBPoolMonitor"
)

func (os *clusterDiscoveryOS) ListLB() ([]*resources.Resource, error) {
func (os *clusterDiscoveryOS) DeleteSubnetLBs(subnet subnets.Subnet) ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource

opts := loadbalancers.ListOpts{
Name: "api." + os.clusterName,
VipSubnetID: subnet.ID,
}
lbs, err := os.osCloud.ListLBs(opts)
if err != nil {
Expand All @@ -57,25 +57,33 @@ func (os *clusterDiscoveryOS) ListLB() ([]*resources.Resource, error) {
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
return resourceTrackers, nil
}

func (os *clusterDiscoveryOS) ListLBPools() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
if os.osCloud.UseOctavia() {
klog.V(2).Info("skipping LB Children because using Octavia")
continue
}

if os.osCloud.UseOctavia() {
klog.V(2).Info("skipping ListLBPools because using Octavia")
return nil, nil
}
//Identify pools associated to this LB
drekle marked this conversation as resolved.
Show resolved Hide resolved
for _, pool := range lb.Pools {

pools, err := os.osCloud.ListPools(v2pools.ListOpts{})
if err != nil {
return nil, err
}
monitorList, err := os.cloud.(openstack.OpenstackCloud).ListMonitors(monitors.ListOpts{
PoolID: pool.ID,
})
if err != nil {
return nil, err
}
for _, monitor := range monitorList {
resourceTracker := &resources.Resource{
Name: monitor.Name,
ID: monitor.ID,
Type: typeLBPM,
Deleter: func(cloud fi.Cloud, r *resources.Resource) error {
return cloud.(openstack.OpenstackCloud).DeleteMonitor(r.ID)
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}

for _, pool := range pools {
if strings.Contains(pool.Name, os.clusterName) {
resourceTracker := &resources.Resource{
Name: pool.Name,
ID: pool.ID,
Expand All @@ -86,25 +94,9 @@ func (os *clusterDiscoveryOS) ListLBPools() ([]*resources.Resource, error) {
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
}
return resourceTrackers, nil
}

func (os *clusterDiscoveryOS) ListLBListener() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource

if os.osCloud.UseOctavia() {
klog.V(2).Info("skipping ListLBListener because using Octavia")
return nil, nil
}

listeners, err := os.osCloud.ListListeners(listeners.ListOpts{})
if err != nil {
return nil, err
}

for _, listener := range listeners {
if strings.Contains(listener.Name, os.clusterName) {
//Identify listeners associated to this LB
for _, listener := range lb.Listeners {
resourceTracker := &resources.Resource{
Name: listener.Name,
ID: listener.ID,
Expand All @@ -116,5 +108,6 @@ func (os *clusterDiscoveryOS) ListLBListener() ([]*resources.Resource, error) {
resourceTrackers = append(resourceTrackers, resourceTracker)
}
}

return resourceTrackers, nil
}
8 changes: 8 additions & 0 deletions pkg/resources/openstack/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (os *clusterDiscoveryOS) ListNetwork() ([]*resources.Resource, error) {
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
//associated load balancers
lbTrackers, err := os.DeleteSubnetLBs(subnet)
if err != nil {
return resourceTrackers, err
}
resourceTrackers = append(resourceTrackers, lbTrackers...)

resourceTracker := &resources.Resource{
Name: subnet.Name,
ID: subnet.ID,
Expand All @@ -106,6 +113,7 @@ func (os *clusterDiscoveryOS) ListNetwork() ([]*resources.Resource, error) {
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)

}
resourceTracker := &resources.Resource{
Name: network.Name,
Expand Down
3 changes: 0 additions & 3 deletions pkg/resources/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ func ListResources(cloud openstack.OpenstackCloud, clusterName string) (map[stri
os.ListInstances,
os.ListServerGroups,
os.ListVolumes,
os.ListLBListener,
os.ListLBPools,
os.ListLB,
os.ListPorts,
os.ListSecurityGroups,
os.ListNetwork,
Expand Down
1 change: 1 addition & 0 deletions upup/pkg/fi/cloudup/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ go_library(
"//vendor/github.com/gophercloud/gophercloud/openstack/dns/v2/zones:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external:go_default_library",
"//vendor/github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips:go_default_library",
Expand Down
7 changes: 7 additions & 0 deletions upup/pkg/fi/cloudup/openstack/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
v2pools "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
l3floatingip "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
Expand Down Expand Up @@ -250,6 +251,12 @@ type OpenstackCloud interface {

ListPools(v2pools.ListOpts) ([]v2pools.Pool, error)

// ListMonitors will list HealthMonitors matching the provided options
ListMonitors(monitors.ListOpts) ([]monitors.Monitor, error)

// DeleteMonitor will delete a Pool resources Health Monitor
DeleteMonitor(monitorID string) error

// DeletePool will delete loadbalancer pool
DeletePool(poolID string) error

Expand Down
40 changes: 40 additions & 0 deletions upup/pkg/fi/cloudup/openstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,51 @@ import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
v2pools "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kops/util/pkg/vfs"
)

func (c *openstackCloud) ListMonitors(opts monitors.ListOpts) (monitorList []monitors.Monitor, err error) {

done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
allPages, err := monitors.List(c.LoadBalancerClient(), opts).AllPages()
if err != nil {
return false, fmt.Errorf("failed to list monitors: %s", err)
}
monitorList, err = monitors.ExtractMonitors(allPages)
if err != nil {
return false, fmt.Errorf("failed to extract monitor pages: %s", err)
}
return true, nil
})
if !done {
if err == nil {
err = wait.ErrWaitTimeout
}
return monitorList, err
}
return monitorList, nil
}

func (c *openstackCloud) DeleteMonitor(monitorID string) error {
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
err := monitors.Delete(c.LoadBalancerClient(), monitorID).ExtractErr()
if err != nil && !isNotFound(err) {
return false, fmt.Errorf("error deleting pool: %v", err)
}
return true, nil
})
if err != nil {
return err
} else if done {
return nil
} else {
return wait.ErrWaitTimeout
}
}

func (c *openstackCloud) DeletePool(poolID string) error {
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
err := v2pools.Delete(c.LoadBalancerClient(), poolID).ExtractErr()
Expand Down