-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
router.go
150 lines (130 loc) · 4.07 KB
/
router.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
/*
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/extensions/layer3/routers"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kops/util/pkg/vfs"
)
func (c *openstackCloud) ListRouters(opt routers.ListOpts) ([]routers.Router, error) {
return listRouters(c, opt)
}
func listRouters(c OpenstackCloud, opt routers.ListOpts) ([]routers.Router, error) {
var rs []routers.Router
done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
allPages, err := routers.List(c.NetworkingClient(), opt).AllPages()
if err != nil {
return false, fmt.Errorf("error listing routers: %v", err)
}
r, err := routers.ExtractRouters(allPages)
if err != nil {
return false, fmt.Errorf("error extracting routers from pages: %v", err)
}
rs = r
return true, nil
})
if err != nil {
return rs, err
} else if done {
return rs, nil
} else {
return rs, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) CreateRouter(opt routers.CreateOptsBuilder) (*routers.Router, error) {
return createRouter(c, opt)
}
func createRouter(c OpenstackCloud, opt routers.CreateOptsBuilder) (*routers.Router, error) {
var r *routers.Router
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
v, err := routers.Create(c.NetworkingClient(), opt).Extract()
if err != nil {
return false, fmt.Errorf("error creating router: %v", err)
}
r = v
return true, nil
})
if err != nil {
return r, err
} else if done {
return r, nil
} else {
return r, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) CreateRouterInterface(routerID string, opt routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) {
return createRouterInterface(c, routerID, opt)
}
func createRouterInterface(c OpenstackCloud, routerID string, opt routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error) {
var i *routers.InterfaceInfo
done, err := vfs.RetryWithBackoff(writeBackoff, func() (bool, error) {
v, err := routers.AddInterface(c.NetworkingClient(), routerID, opt).Extract()
if err != nil {
return false, fmt.Errorf("error creating router interface: %v", err)
}
i = v
return true, nil
})
if err != nil {
return i, err
} else if done {
return i, nil
} else {
return i, wait.ErrWaitTimeout
}
}
func (c *openstackCloud) DeleteRouterInterface(routerID string, opt routers.RemoveInterfaceOptsBuilder) error {
return deleteRouterInterface(c, routerID, opt)
}
func deleteRouterInterface(c OpenstackCloud, routerID string, opt routers.RemoveInterfaceOptsBuilder) error {
done, err := vfs.RetryWithBackoff(deleteBackoff, func() (bool, error) {
_, err := routers.RemoveInterface(c.NetworkingClient(), routerID, opt).Extract()
if err != nil && !isNotFound(err) {
return false, fmt.Errorf("error deleting router interface: %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) DeleteRouter(routerID string) error {
return deleteRouter(c, routerID)
}
func deleteRouter(c OpenstackCloud, routerID string) error {
done, err := vfs.RetryWithBackoff(deleteBackoff, func() (bool, error) {
err := routers.Delete(c.NetworkingClient(), routerID).ExtractErr()
if err != nil && !isNotFound(err) {
return false, fmt.Errorf("error deleting router: %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
}
}