-
Notifications
You must be signed in to change notification settings - Fork 4
/
clusterpool.go
153 lines (131 loc) · 5.3 KB
/
clusterpool.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
/*
* Copyright (c) 2023 Baidu, Inc. All Rights Reserved.
*
* 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 clusterpool
import (
"context"
"fmt"
"net"
"github.com/cilium/ipam/cidrset"
"github.com/sirupsen/logrus"
operatorMetrics "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/operator/metrics"
operatorOption "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/operator/option"
ipPkg "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/ip"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/ipam"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/ipam/allocator"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/ipam/allocator/podcidr"
ipamMetrics "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/ipam/metrics"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/logging"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/logging/logfields"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/option"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/trigger"
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "ipam-allocator-clusterpool")
type ErrCIDRColision struct {
cidr string
allocator podcidr.CIDRAllocator
}
func (e ErrCIDRColision) Error() string {
return fmt.Sprintf("requested CIDR %s colides with %s", e.cidr, e.allocator)
}
func (e *ErrCIDRColision) Is(target error) bool {
t, ok := target.(*ErrCIDRColision)
if !ok {
return false
}
return t.cidr == e.cidr
}
// AllocatorOperator is an implementation of IPAM allocator interface for CCE
// IPAM.
type AllocatorOperator struct {
v4CIDRSet, v6CIDRSet []podcidr.CIDRAllocator
}
// Init sets up CCE allocator based on given options
func (a *AllocatorOperator) Init(ctx context.Context) error {
if option.Config.EnableIPv4 {
if len(operatorOption.Config.ClusterPoolIPv4CIDR) == 0 {
return fmt.Errorf("%s must be provided when using ClusterPool", operatorOption.ClusterPoolIPv4CIDR)
}
v4Allocators, err := newCIDRSets(false, operatorOption.Config.ClusterPoolIPv4CIDR, operatorOption.Config.NodeCIDRMaskSizeIPv4)
if err != nil {
return fmt.Errorf("unable to initialize IPv4 allocator %w", err)
}
a.v4CIDRSet = v4Allocators
} else if len(operatorOption.Config.ClusterPoolIPv4CIDR) != 0 {
return fmt.Errorf("%s must not be set if IPv4 is disabled", operatorOption.ClusterPoolIPv4CIDR)
}
if option.Config.EnableIPv6 {
if len(operatorOption.Config.ClusterPoolIPv6CIDR) == 0 {
return fmt.Errorf("%s must be provided when using ClusterPool", operatorOption.ClusterPoolIPv6CIDR)
}
v6Allocators, err := newCIDRSets(true, operatorOption.Config.ClusterPoolIPv6CIDR, operatorOption.Config.NodeCIDRMaskSizeIPv6)
if err != nil {
return fmt.Errorf("unable to initialize IPv6 allocator %w", err)
}
a.v6CIDRSet = v6Allocators
} else if len(operatorOption.Config.ClusterPoolIPv6CIDR) != 0 {
return fmt.Errorf("%s must not be set if IPv6 is disabled", operatorOption.ClusterPoolIPv6CIDR)
}
return nil
}
// Start kicks of Operator allocation.
func (a *AllocatorOperator) Start(ctx context.Context, updater ipam.NetResourceSetGetterUpdater) (allocator.NetResourceSetEventHandler, error) {
log.WithFields(logrus.Fields{
logfields.IPv4CIDRs: operatorOption.Config.ClusterPoolIPv4CIDR,
logfields.IPv6CIDRs: operatorOption.Config.ClusterPoolIPv6CIDR,
}).Info("Starting ClusterPool IP allocator")
var (
iMetrics trigger.MetricsObserver
)
if operatorOption.Config.EnableMetrics {
iMetrics = ipamMetrics.NewTriggerMetrics(operatorMetrics.Namespace, "k8s_sync")
} else {
iMetrics = &ipamMetrics.NoOpMetricsObserver{}
}
nodeManager := podcidr.NewNodesPodCIDRManager(a.v4CIDRSet, a.v6CIDRSet, updater, iMetrics)
return nodeManager, nil
}
func newCIDRSets(isV6 bool, strCIDRs []string, maskSize int) ([]podcidr.CIDRAllocator, error) {
cidrAllocators := make([]podcidr.CIDRAllocator, 0, len(strCIDRs))
for _, strCIDR := range strCIDRs {
addr, cidr, err := net.ParseCIDR(strCIDR)
if err != nil {
return nil, err
}
// Check if CIDRs collide with each other.
for _, cidrAllocator := range cidrAllocators {
if cidrAllocator.InRange(cidr) {
return nil, &ErrCIDRColision{
cidr: strCIDR,
allocator: cidrAllocator,
}
}
}
cidrSet, err := newCIDRSet(isV6, addr, cidr, maskSize)
if err != nil {
return nil, err
}
cidrAllocators = append(cidrAllocators, cidrSet)
}
return cidrAllocators, nil
}
func newCIDRSet(isV6 bool, addr net.IP, cidr *net.IPNet, maskSize int) (podcidr.CIDRAllocator, error) {
switch {
case isV6 && ipPkg.IsIPv4(addr):
return nil, fmt.Errorf("CIDR is not v6 family: %s", cidr)
case !isV6 && !ipPkg.IsIPv4(addr):
return nil, fmt.Errorf("CIDR is not v4 family: %s", cidr)
}
return cidrset.NewCIDRSet(cidr, maskSize)
}