-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ipam.go
222 lines (186 loc) · 6.59 KB
/
ipam.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipam
import (
"net"
"github.com/sirupsen/logrus"
agentK8s "github.com/cilium/cilium/daemon/k8s"
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/datapath/types"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/k8s/client"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "ipam")
)
type ErrAllocation error
// Family is the type describing all address families support by the IP
// allocation manager
type Family string
const (
IPv6 Family = "ipv6"
IPv4 Family = "ipv4"
)
// DeriveFamily derives the address family of an IP
func DeriveFamily(ip net.IP) Family {
if ip.To4() == nil {
return IPv6
}
return IPv4
}
// Configuration is the configuration passed into the IPAM subsystem
type Configuration interface {
// IPv4Enabled must return true when IPv4 is enabled
IPv4Enabled() bool
// IPv6 must return true when IPv6 is enabled
IPv6Enabled() bool
// IPAMMode returns the IPAM mode
IPAMMode() string
// HealthCheckingEnabled must return true when health-checking is
// enabled
HealthCheckingEnabled() bool
// UnreachableRoutesEnabled returns true when unreachable-routes is
// enabled
UnreachableRoutesEnabled() bool
// SetIPv4NativeRoutingCIDR is called by the IPAM module to announce
// the native IPv4 routing CIDR if it exists
SetIPv4NativeRoutingCIDR(cidr *cidr.CIDR)
// IPv4NativeRoutingCIDR is called by the IPAM module retrieve
// the native IPv4 routing CIDR if it exists
GetIPv4NativeRoutingCIDR() *cidr.CIDR
}
// Owner is the interface the owner of an IPAM allocator has to implement
type Owner interface {
// UpdateCiliumNodeResource is called to create/update the CiliumNode
// resource. The function must block until the custom resource has been
// created.
UpdateCiliumNodeResource()
}
// K8sEventRegister is used to register and handle events as they are processed
// by K8s controllers.
type K8sEventRegister interface {
// K8sEventReceived is called to do metrics accounting for received
// Kubernetes events, as well as calculating timeouts for k8s watcher
// cache sync.
K8sEventReceived(apiGroupResourceName string, scope string, action string, valid, equal bool)
// K8sEventProcessed is called to do metrics accounting for each processed
// Kubernetes event.
K8sEventProcessed(scope string, action string, status bool)
}
type MtuConfiguration interface {
GetDeviceMTU() int
}
type Metadata interface {
GetIPPoolForPod(owner string, family Family) (pool string, err error)
}
// NewIPAM returns a new IP address manager
func NewIPAM(nodeAddressing types.NodeAddressing, c Configuration, owner Owner, k8sEventReg K8sEventRegister, node agentK8s.LocalCiliumNodeResource, mtuConfig MtuConfiguration, clientset client.Clientset) *IPAM {
ipam := &IPAM{
nodeAddressing: nodeAddressing,
config: c,
owner: map[Pool]map[string]string{},
expirationTimers: map[timerKey]expirationTimer{},
excludedIPs: map[string]string{},
}
switch c.IPAMMode() {
case ipamOption.IPAMKubernetes, ipamOption.IPAMClusterPool:
log.WithFields(logrus.Fields{
logfields.V4Prefix: nodeAddressing.IPv4().AllocationCIDR(),
logfields.V6Prefix: nodeAddressing.IPv6().AllocationCIDR(),
}).Infof("Initializing %s IPAM", c.IPAMMode())
if c.IPv6Enabled() {
ipam.IPv6Allocator = newHostScopeAllocator(nodeAddressing.IPv6().AllocationCIDR().IPNet)
}
if c.IPv4Enabled() {
ipam.IPv4Allocator = newHostScopeAllocator(nodeAddressing.IPv4().AllocationCIDR().IPNet)
}
case ipamOption.IPAMMultiPool:
log.Info("Initializing MultiPool IPAM")
manager := newMultiPoolManager(c, node, owner, clientset.CiliumV2().CiliumNodes())
if c.IPv6Enabled() {
ipam.IPv6Allocator = manager.Allocator(IPv6)
}
if c.IPv4Enabled() {
ipam.IPv4Allocator = manager.Allocator(IPv4)
}
case ipamOption.IPAMCRD, ipamOption.IPAMENI, ipamOption.IPAMAzure, ipamOption.IPAMAlibabaCloud:
log.Info("Initializing CRD-based IPAM")
if c.IPv6Enabled() {
ipam.IPv6Allocator = newCRDAllocator(IPv6, c, owner, clientset, k8sEventReg, mtuConfig)
}
if c.IPv4Enabled() {
ipam.IPv4Allocator = newCRDAllocator(IPv4, c, owner, clientset, k8sEventReg, mtuConfig)
}
case ipamOption.IPAMDelegatedPlugin:
log.Info("Initializing no-op IPAM since we're using a CNI delegated plugin")
if c.IPv6Enabled() {
ipam.IPv6Allocator = &noOpAllocator{}
}
if c.IPv4Enabled() {
ipam.IPv4Allocator = &noOpAllocator{}
}
default:
log.Fatalf("Unknown IPAM backend %s", c.IPAMMode())
}
return ipam
}
// WithMetadata sets an optional Metadata provider, which IPAM will use to
// determine what IPAM pool an IP owner should allocate its IP from
func (ipam *IPAM) WithMetadata(m Metadata) {
ipam.metadata = m
}
// getIPOwner returns the owner for an IP in a particular pool or the empty
// string in case the pool or IP is not registered.
func (ipam *IPAM) getIPOwner(ip string, pool Pool) string {
if p, ok := ipam.owner[pool]; ok {
return p[ip]
}
return ""
}
// registerIPOwner registers a new owner for an IP in a particular pool.
func (ipam *IPAM) registerIPOwner(ip net.IP, owner string, pool Pool) {
if _, ok := ipam.owner[pool]; !ok {
ipam.owner[pool] = make(map[string]string)
}
ipam.owner[pool][ip.String()] = owner
}
// releaseIPOwner releases ip from pool and returns the previous owner.
func (ipam *IPAM) releaseIPOwner(ip net.IP, pool Pool) string {
var owner string
if m, ok := ipam.owner[pool]; ok {
ipStr := ip.String()
owner = m[ipStr]
delete(m, ipStr)
if len(m) == 0 {
delete(ipam.owner, pool)
}
}
return owner
}
// ExcludeIP ensures that a certain IP is never allocated. It is preferred to
// use this method instead of allocating the IP as the allocation block can
// change and suddenly cover the IP to be excluded.
func (ipam *IPAM) ExcludeIP(ip net.IP, owner string, pool Pool) {
ipam.allocatorMutex.Lock()
ipam.excludedIPs[pool.String()+":"+ip.String()] = owner
ipam.allocatorMutex.Unlock()
}
// isIPExcluded is used to check if a particular IP is excluded from being allocated.
func (ipam *IPAM) isIPExcluded(ip net.IP, pool Pool) (string, bool) {
owner, ok := ipam.excludedIPs[pool.String()+":"+ip.String()]
return owner, ok
}
// PoolOrDefault returns the default pool if no pool is specified.
func PoolOrDefault(pool string) Pool {
if pool == "" {
return PoolDefault()
}
return Pool(pool)
}
// PoolDefault returns the default pool
func PoolDefault() Pool {
return Pool(option.Config.IPAMDefaultIPPool)
}