forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_creator.go
102 lines (88 loc) · 2.71 KB
/
config_creator.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
package kawasaki
import (
"encoding/hex"
"fmt"
"net"
"code.cloudfoundry.org/guardian/kawasaki/subnets"
"code.cloudfoundry.org/lager"
)
const (
maxInterfacePrefixLen = 2
maxChainPrefixLen = 16
maxAllowedMtuSize = 1500
)
//go:generate counterfeiter . IDGenerator
type IDGenerator interface {
Generate() string
}
type NetworkConfig struct {
ContainerHandle string
HostIntf string
ContainerIntf string
IPTablePrefix string
IPTableInstance string
BridgeName string
BridgeIP net.IP
ContainerIP net.IP
ExternalIP net.IP
Subnet *net.IPNet
Mtu int
PluginNameservers []net.IP
OperatorNameservers []net.IP
AdditionalNameservers []net.IP
AdditionalHostEntries []string
PluginSearchDomains []string
}
type Creator struct {
idGenerator IDGenerator
interfacePrefix string
chainPrefix string
externalIP net.IP
operatorNameservers []net.IP
additionalNameservers []net.IP
additionalHostEntries []string
mtu int
}
func NewConfigCreator(idGenerator IDGenerator, interfacePrefix, chainPrefix string, externalIP net.IP, operatorNameservers, additionalNameservers []net.IP, additionalHostEntries []string, mtu int) *Creator {
if len(interfacePrefix) > maxInterfacePrefixLen {
panic("interface prefix is too long")
}
if len(chainPrefix) > maxChainPrefixLen {
panic("chain prefix is too long")
}
return &Creator{
idGenerator: idGenerator,
interfacePrefix: interfacePrefix,
chainPrefix: chainPrefix,
externalIP: externalIP,
operatorNameservers: operatorNameservers,
additionalNameservers: additionalNameservers,
additionalHostEntries: additionalHostEntries,
mtu: min(mtu, maxAllowedMtuSize),
}
}
func (c *Creator) Create(log lager.Logger, handle string, subnet *net.IPNet, ip net.IP) (NetworkConfig, error) {
id := c.idGenerator.Generate()
return NetworkConfig{
ContainerHandle: handle,
HostIntf: fmt.Sprintf("%s%s-0", c.interfacePrefix, id),
ContainerIntf: fmt.Sprintf("%s%s-1", c.interfacePrefix, id),
BridgeName: fmt.Sprintf("%s%s%s", c.interfacePrefix, "brdg-", hex.EncodeToString(subnet.IP)),
IPTablePrefix: c.chainPrefix,
IPTableInstance: id,
ContainerIP: ip,
BridgeIP: subnets.GatewayIP(subnet),
ExternalIP: c.externalIP,
Subnet: subnet,
Mtu: c.mtu,
OperatorNameservers: c.operatorNameservers,
AdditionalNameservers: c.additionalNameservers,
AdditionalHostEntries: c.additionalHostEntries,
}, nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}