/
util.go
378 lines (331 loc) · 10.3 KB
/
util.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package util
import (
"errors"
"fmt"
"hash/fnv"
"net"
"regexp"
"strconv"
"strings"
"time"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/types"
"crypto/rand"
"github.com/urfave/cli/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
utilnet "k8s.io/utils/net"
)
// OvnConflictBackoff is the backoff used for pod annotation update conflict
var OvnConflictBackoff = wait.Backoff{
Steps: 2,
Duration: 10 * time.Millisecond,
Factor: 5.0,
Jitter: 0.1,
}
var (
rePciDeviceName = regexp.MustCompile(`^[0-9a-f]{4}:[0-9a-f]{2}:[01][0-9a-f]\.[0-7]$`)
reAuxDeviceName = regexp.MustCompile(`^\w+.\w+.\d+$`)
)
// IsPCIDeviceName check if passed device id is a PCI device name
func IsPCIDeviceName(deviceID string) bool {
return rePciDeviceName.MatchString(deviceID)
}
// IsAuxDeviceName check if passed device id is a Auxiliary device name
func IsAuxDeviceName(deviceID string) bool {
return reAuxDeviceName.MatchString(deviceID)
}
// StringArg gets the named command-line argument or returns an error if it is empty
func StringArg(context *cli.Context, name string) (string, error) {
val := context.String(name)
if val == "" {
return "", fmt.Errorf("argument --%s should be non-null", name)
}
return val, nil
}
// GetIPNetFullMask returns an IPNet object for IPV4 or IPV6 address with a full subnet mask
func GetIPNetFullMask(ipStr string) (*net.IPNet, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, fmt.Errorf("failed to parse IP %q", ipStr)
}
mask := GetIPFullMask(ip)
return &net.IPNet{
IP: ip,
Mask: mask,
}, nil
}
// GetIPFullMaskString returns /32 if ip is IPV4 family and /128 if ip is IPV6 family
func GetIPFullMaskString(ip string) string {
const (
// IPv4FullMask is the maximum prefix mask for an IPv4 address
IPv4FullMask = "/32"
// IPv6FullMask is the maxiumum prefix mask for an IPv6 address
IPv6FullMask = "/128"
)
if utilnet.IsIPv6(net.ParseIP(ip)) {
return IPv6FullMask
}
return IPv4FullMask
}
// GetIPFullMask returns a full IPv4 IPMask if ip is IPV4 family or a full IPv6
// IPMask otherwise
func GetIPFullMask(ip net.IP) net.IPMask {
if utilnet.IsIPv6(ip) {
return net.CIDRMask(128, 128)
}
return net.CIDRMask(32, 32)
}
// GetLegacyK8sMgmtIntfName returns legacy management ovs-port name
func GetLegacyK8sMgmtIntfName(nodeName string) string {
if len(nodeName) > 11 {
return types.K8sPrefix + (nodeName[:11])
}
return types.K8sPrefix + nodeName
}
// GetWorkerFromGatewayRouter determines a node's corresponding worker switch name from a gateway router name
func GetWorkerFromGatewayRouter(gr string) string {
return strings.TrimPrefix(gr, types.GWRouterPrefix)
}
// GetGatewayRouterFromNode determines a node's corresponding gateway router name
func GetGatewayRouterFromNode(node string) string {
return types.GWRouterPrefix + node
}
// GetNodeInternalAddrs returns the first IPv4 and/or IPv6 InternalIP defined
// for the node. On certain cloud providers (AWS) the egress IP will be added to
// the list of node IPs as an InternalIP address, we don't want to create the
// default allow logical router policies for that IP. Node IPs are ordered,
// meaning the egress IP will never be first in this list.
func GetNodeInternalAddrs(node *v1.Node) (net.IP, net.IP) {
var v4Addr, v6Addr net.IP
for _, nodeAddr := range node.Status.Addresses {
if nodeAddr.Type == v1.NodeInternalIP {
ip := utilnet.ParseIPSloppy(nodeAddr.Address)
if !utilnet.IsIPv6(ip) && v4Addr == nil {
v4Addr = ip
} else if utilnet.IsIPv6(ip) && v6Addr == nil {
v6Addr = ip
}
}
}
return v4Addr, v6Addr
}
// GetNodeAddresses returns all of the node's IPv4 and/or IPv6 annotated
// addresses as requested. Note that nodes not annotated will be ignored.
func GetNodeAddresses(ipv4, ipv6 bool, nodes ...*v1.Node) (ipsv4 []net.IP, ipsv6 []net.IP, err error) {
allCIDRs := sets.Set[string]{}
for _, node := range nodes {
ips, err := ParseNodeHostCIDRs(node)
if IsAnnotationNotSetError(err) {
continue
}
if err != nil {
return nil, nil, err
}
allCIDRs = allCIDRs.Insert(ips.UnsortedList()...)
}
for _, cidr := range allCIDRs.UnsortedList() {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return nil, nil, fmt.Errorf("failed to get parse CIDR %v: %w", cidr, err)
}
if ipv4 && utilnet.IsIPv4(ip) {
ipsv4 = append(ipsv4, ip)
} else if ipv6 && utilnet.IsIPv6(ip) {
ipsv6 = append(ipsv6, ip)
}
}
return
}
// GetNodeChassisID returns the machine's OVN chassis ID
func GetNodeChassisID() (string, error) {
chassisID, stderr, err := RunOVSVsctl("--if-exists", "get",
"Open_vSwitch", ".", "external_ids:system-id")
if err != nil {
klog.Errorf("No system-id configured in the local host, "+
"stderr: %q, error: %v", stderr, err)
return "", err
}
if chassisID == "" {
return "", fmt.Errorf("no system-id configured in the local host")
}
return chassisID, nil
}
// GetHybridOverlayPortName returns the name of the hybrid overlay switch port
// for a given node
func GetHybridOverlayPortName(nodeName string) string {
return "int-" + nodeName
}
type annotationNotSetError struct {
msg string
}
func (anse *annotationNotSetError) Error() string {
return anse.msg
}
// newAnnotationNotSetError returns an error for an annotation that is not set
func newAnnotationNotSetError(format string, args ...interface{}) error {
return &annotationNotSetError{msg: fmt.Sprintf(format, args...)}
}
// IsAnnotationNotSetError returns true if the error indicates that an annotation is not set
func IsAnnotationNotSetError(err error) bool {
var annotationNotSetError *annotationNotSetError
return errors.As(err, &annotationNotSetError)
}
type annotationAlreadySetError struct {
msg string
}
func (aase *annotationAlreadySetError) Error() string {
return aase.msg
}
// newAnnotationAlreadySetError returns an error for an annotation that is not set
func newAnnotationAlreadySetError(format string, args ...interface{}) error {
return &annotationAlreadySetError{msg: fmt.Sprintf(format, args...)}
}
// IsAnnotationAlreadySetError returns true if the error indicates that an annotation is already set
func IsAnnotationAlreadySetError(err error) bool {
var annotationAlreadySetError *annotationAlreadySetError
return errors.As(err, &annotationAlreadySetError)
}
// HashforOVN hashes the provided input to make it a valid addressSet or portGroup name.
func HashForOVN(s string) string {
h := fnv.New64a()
_, err := h.Write([]byte(s))
if err != nil {
klog.Errorf("Failed to hash %s", s)
return ""
}
hashString := strconv.FormatUint(h.Sum64(), 10)
return fmt.Sprintf("a%s", hashString)
}
// UpdateIPsSlice will search for values of oldIPs in the slice "s" and update it with newIPs values of same IP family
func UpdateIPsSlice(s, oldIPs, newIPs []string) ([]string, bool) {
n := make([]string, len(s))
copy(n, s)
updated := false
for i, entry := range s {
for _, oldIP := range oldIPs {
if entry == oldIP {
for _, newIP := range newIPs {
if utilnet.IsIPv6(net.ParseIP(oldIP)) {
if utilnet.IsIPv6(net.ParseIP(newIP)) {
n[i] = newIP
updated = true
break
}
} else {
if !utilnet.IsIPv6(net.ParseIP(newIP)) {
n[i] = newIP
updated = true
break
}
}
}
break
}
}
}
return n, updated
}
// FilterIPsSlice will filter a list of IPs by a list of CIDRs. By default,
// it will *remove* all IPs that match filter, unless keep is true.
//
// It is dual-stack aware.
func FilterIPsSlice(s []string, filter []net.IPNet, keep bool) []string {
out := make([]string, 0, len(s))
ipLoop:
for _, ipStr := range s {
ip := net.ParseIP(ipStr)
is4 := ip.To4() != nil
for _, cidr := range filter {
if is4 && cidr.IP.To4() != nil && cidr.Contains(ip) {
if keep {
out = append(out, ipStr)
continue ipLoop
} else {
continue ipLoop
}
}
if !is4 && cidr.IP.To4() == nil && cidr.Contains(ip) {
if keep {
out = append(out, ipStr)
continue ipLoop
} else {
continue ipLoop
}
}
}
if !keep { // discard mode, and nothing matched.
out = append(out, ipStr)
}
}
return out
}
// IsClusterIP checks if the provided IP is a clusterIP
func IsClusterIP(svcVIP string) bool {
ip := net.ParseIP(svcVIP)
is4 := ip.To4() != nil
for _, svcCIDR := range config.Kubernetes.ServiceCIDRs {
if is4 && svcCIDR.IP.To4() != nil && svcCIDR.Contains(ip) {
return true
}
if !is4 && svcCIDR.IP.To4() == nil && svcCIDR.Contains(ip) {
return true
}
}
return false
}
func GetSecondaryNetworkLogicalPortName(podNamespace, podName, nadName string) string {
return GetSecondaryNetworkPrefix(nadName) + composePortName(podNamespace, podName)
}
func GetLogicalPortName(podNamespace, podName string) string {
return composePortName(podNamespace, podName)
}
func GetSecondaryNetworkIfaceId(podNamespace, podName, nadName string) string {
return GetSecondaryNetworkPrefix(nadName) + composePortName(podNamespace, podName)
}
func GetIfaceId(podNamespace, podName string) string {
return composePortName(podNamespace, podName)
}
// composePortName should be called both for LogicalPortName and iface-id
// because ovn-nb man says:
// Logical_Switch_Port.name must match external_ids:iface-id
// in the Open_vSwitch database’s Interface table,
// because hypervisors use external_ids:iface-id as a lookup key to
// identify the network interface of that entity.
func composePortName(podNamespace, podName string) string {
return podNamespace + "_" + podName
}
func SliceHasStringItem(slice []string, item string) bool {
for _, i := range slice {
if i == item {
return true
}
}
return false
}
// StringSlice converts to a slice of the string representation of the input
// items
func StringSlice[T fmt.Stringer](items []T) []string {
s := make([]string, len(items))
for i := range items {
s[i] = items[i].String()
}
return s
}
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
// GenerateId returns a random id as a string with the requested length
func GenerateId(length int) string {
charsLength := len(chars)
b := make([]byte, length)
_, err := rand.Read(b) // generates len(b) random bytes
if err != nil {
klog.Errorf("Failed when generating a random ID: %v", err)
return ""
}
for i := 0; i < length; i++ {
b[i] = chars[int(b[i])%charsLength]
}
return string(b)
}