-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkprovide.go
More file actions
72 lines (63 loc) · 1.96 KB
/
linkprovide.go
File metadata and controls
72 lines (63 loc) · 1.96 KB
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
package neslink
import (
"errors"
"github.com/vishvananda/netlink"
)
type LinkProvider struct {
name string
f func() (netlink.Link, error)
}
var (
ErrNoLink error = errors.New("failed to obtain link from provider")
)
// Provide determines the network namespace path based on the provider's
// conditions. Since some conditions are collected at the time of the provider's
// creation and others when this function is called, repeat calls are not always
// expected to produce the same result. Also note, the path is only returned,
// not opened.
func (lp LinkProvider) Provide() (netlink.Link, error) {
return lp.f()
}
// LPGeneric provides the means to create custom providers.
func LPGeneric(providerName string, function func() (netlink.Link, error)) LinkProvider {
if providerName == "" {
providerName = "unnamed-link-provider"
}
return LinkProvider{
name: providerName,
f: function,
}
}
// LPName creates a link provider that when called, will provide the
// pre-existing link with the given name (in the namespace this is called in).
// If no matches are found, an error is returned.
func LPName(name string) LinkProvider {
return LinkProvider{
name: "name",
f: func() (netlink.Link, error) {
return netlink.LinkByName(name)
},
}
}
// LPAlias creates a link provider that when called, will provide the
// pre-existing link with the given alias (in the namespace this is called in).
// If no matches are found, an error is returned.
func LPAlias(alias string) LinkProvider {
return LinkProvider{
name: "alias",
f: func() (netlink.Link, error) {
return netlink.LinkByAlias(alias)
},
}
}
// LPIndex creates a link provider that when called, will provide the
// pre-existing link with the given index (in the namespace this is called in).
// If no matches are found, an error is returned.
func LPIndex(index int) LinkProvider {
return LinkProvider{
name: "index",
f: func() (netlink.Link, error) {
return netlink.LinkByIndex(index)
},
}
}