-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
subnet.go
170 lines (145 loc) · 4.3 KB
/
subnet.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
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"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
)
func (c *openstackCloud) ListSubnets(opt subnets.ListOptsBuilder) ([]subnets.Subnet, error) {
return listSubnets(c, opt)
}
func listSubnets(c OpenstackCloud, opt subnets.ListOptsBuilder) ([]subnets.Subnet, error) {
var s []subnets.Subnet
done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
allPages, err := subnets.List(c.NetworkingClient(), opt).AllPages()
if err != nil {
return false, fmt.Errorf("error listing subnets: %v", err)
}
r, err := subnets.ExtractSubnets(allPages)
if err != nil {
return false, fmt.Errorf("error extracting subnets from pages: %v", err)
}
s = r
return true, nil
})
if err != nil {
return s, err
} else if done {
return s, nil
} else {
return s, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) GetSubnet(subnetID string) (*subnets.Subnet, error) {
return getSubnet(c, subnetID)
}
func getSubnet(c OpenstackCloud, subnetID string) (*subnets.Subnet, error) {
var subnet *subnets.Subnet
done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
sub, err := subnets.Get(c.NetworkingClient(), subnetID).Extract()
if err != nil {
return false, fmt.Errorf("error retrieving subnet: %v", err)
}
subnet = sub
return true, nil
})
if err != nil {
return nil, err
} else if done {
return subnet, nil
} else {
return nil, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) CreateSubnet(opt subnets.CreateOptsBuilder) (*subnets.Subnet, error) {
return createSubnet(c, opt)
}
func createSubnet(c OpenstackCloud, opt subnets.CreateOptsBuilder) (*subnets.Subnet, error) {
var s *subnets.Subnet
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
v, err := subnets.Create(c.NetworkingClient(), opt).Extract()
if err != nil {
return false, fmt.Errorf("error creating subnet: %v", err)
}
s = v
return true, nil
})
if err != nil {
return s, err
} else if done {
return s, nil
} else {
return s, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) DeleteSubnet(subnetID string) error {
return deleteSubnet(c, subnetID)
}
func deleteSubnet(c OpenstackCloud, subnetID string) error {
done, err := vfs.RetryWithBackoff(deleteBackoff, func() (bool, error) {
err := subnets.Delete(c.NetworkingClient(), subnetID).ExtractErr()
if err != nil && !isNotFound(err) {
return false, fmt.Errorf("error deleting subnet: %v", err)
}
if isNotFound(err) {
return true, nil
}
return false, nil
})
if err != nil {
return err
} else if done {
return nil
} else {
return wait.ErrWaitTimeout
}
}
func (c *openstackCloud) GetExternalSubnet() (subnet *subnets.Subnet, err error) {
return getExternalSubnet(c, c.extSubnetName)
}
func getExternalSubnet(c OpenstackCloud, subnetName *string) (subnet *subnets.Subnet, err error) {
if subnetName == nil {
return nil, nil
}
subnets, err := c.ListSubnets(subnets.ListOpts{
Name: fi.StringValue(subnetName),
})
if err != nil {
return nil, err
}
if len(subnets) == 1 {
return &subnets[0], nil
}
return nil, fmt.Errorf("did not find floatingsubnet for external router")
}
func (c *openstackCloud) GetLBFloatingSubnet() (subnet *subnets.Subnet, err error) {
return getLBFloatingSubnet(c, c.floatingSubnet)
}
func getLBFloatingSubnet(c OpenstackCloud, floatingSubnet *string) (subnet *subnets.Subnet, err error) {
if floatingSubnet == nil {
return nil, nil
}
subnets, err := c.ListSubnets(subnets.ListOpts{
Name: fi.StringValue(floatingSubnet),
})
if err != nil {
return nil, err
}
if len(subnets) == 1 {
return &subnets[0], nil
}
return nil, fmt.Errorf("did not find floatingsubnet for LB")
}