-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
lb.go
133 lines (115 loc) · 3.58 KB
/
lb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openstack
import (
"fmt"
"strings"
"github.com/gophercloud/gophercloud/v2/openstack/loadbalancer/v2/loadbalancers"
"github.com/gophercloud/gophercloud/v2/openstack/loadbalancer/v2/monitors"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
"k8s.io/klog/v2"
"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"
typeLBPM = "LBPoolMonitor"
)
func (os *clusterDiscoveryOS) DeleteSubnetLBs(subnet subnets.Subnet) ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
preExistingSubnet := false
if !strings.HasSuffix(subnet.Name, os.clusterName) {
preExistingSubnet = true
}
opts := loadbalancers.ListOpts{
VipSubnetID: subnet.ID,
}
lbs, err := os.osCloud.ListLBs(opts)
if err != nil {
return nil, err
}
filteredLBs := []loadbalancers.LoadBalancer{}
if preExistingSubnet {
// if we have preExistingSubnet, we cannot delete others than api LB
for _, lb := range lbs {
if lb.Name == fmt.Sprintf("api.%s", os.clusterName) {
filteredLBs = append(filteredLBs, lb)
}
}
} else {
filteredLBs = lbs
}
for _, lb := range filteredLBs {
resourceTracker := &resources.Resource{
Name: lb.Name,
ID: lb.ID,
Type: typeLB,
Deleter: func(cloud fi.Cloud, r *resources.Resource) error {
opts := loadbalancers.DeleteOpts{
Cascade: true,
}
return cloud.(openstack.OpenstackCloud).DeleteLB(r.ID, opts)
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)
if os.osCloud.UseOctavia() {
klog.V(2).Info("skipping LB Children because using Octavia")
continue
}
// Identify pools associated to this LB
for _, pool := range lb.Pools {
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)
}
resourceTracker := &resources.Resource{
Name: pool.Name,
ID: pool.ID,
Type: typeLBP,
Deleter: func(cloud fi.Cloud, r *resources.Resource) error {
return cloud.(openstack.OpenstackCloud).DeletePool(r.ID)
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
// Identify listeners associated to this LB
for _, listener := range lb.Listeners {
resourceTracker := &resources.Resource{
Name: listener.Name,
ID: listener.ID,
Type: typeLBL,
Deleter: func(cloud fi.Cloud, r *resources.Resource) error {
return cloud.(openstack.OpenstackCloud).DeleteListener(r.ID)
},
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
}
return resourceTrackers, nil
}