Skip to content

Commit 2209b29

Browse files
authored
[feat] : enable nodeipamcontroller within CCM (#366)
* enable nodeipamcontroller within CCM * fix linting errors * add documentation * update docs, add kube-controller-manager flag which should not be set * fix helm chart templates
1 parent 717a8f3 commit 2209b29

File tree

11 files changed

+576
-3
lines changed

11 files changed

+576
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ A Cloud Controller Manager (CCM) is a Kubernetes control plane component that em
4747
- Manages network policies
4848
- Configures network routes for optimal communication
4949

50+
#### NodeIPAM Controller
51+
- Manages and configures pod CIDRs to nodes
52+
5053
## Requirements
5154

5255
- Kubernetes 1.22+
@@ -75,6 +78,7 @@ A Cloud Controller Manager (CCM) is a Kubernetes control plane component that em
7578
- [Firewall Setup](docs/configuration/firewall.md)
7679
- [Route Configuration](docs/configuration/routes.md)
7780
- [Session Affinity](docs/configuration/session-affinity.md)
81+
- [NodeIPAM Configuration](docs/configuration/nodeipam.md)
7882

7983
### Examples and Development
8084
- [Examples](docs/examples/README.md) - Real-world usage examples

cloud/linode/cloud.go

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ var Options struct {
5151
NodeBalancerBackendIPv4Subnet string
5252
GlobalStopChannel chan<- struct{}
5353
EnableIPv6ForLoadBalancers bool
54+
AllocateNodeCIDRs bool
55+
ClusterCIDRIPv4 string
56+
NodeCIDRMaskSizeIPv4 int
57+
NodeCIDRMaskSizeIPv6 int
5458
}
5559

5660
type linodeCloud struct {
@@ -187,6 +191,10 @@ func (c *linodeCloud) Initialize(clientBuilder cloudprovider.ControllerClientBui
187191
serviceInformer := sharedInformer.Core().V1().Services()
188192
nodeInformer := sharedInformer.Core().V1().Nodes()
189193

194+
if err := startNodeIpamController(stopCh, c, nodeInformer, kubeclient); err != nil {
195+
klog.Fatal("starting of node ipam controller failed", err)
196+
}
197+
190198
if c.linodeTokenHealthChecker != nil {
191199
go c.linodeTokenHealthChecker.Run(stopCh)
192200
}

cloud/linode/nodeipamcontroller.go

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// This file holds the code related with the sample nodeipamcontroller
18+
// which demonstrates how cloud providers add external controllers to cloud-controller-manager
19+
20+
package linode
21+
22+
import (
23+
"fmt"
24+
"net"
25+
"strings"
26+
27+
"k8s.io/apimachinery/pkg/util/wait"
28+
v1 "k8s.io/client-go/informers/core/v1"
29+
"k8s.io/client-go/kubernetes"
30+
cloudprovider "k8s.io/cloud-provider"
31+
nodeipamcontroller "k8s.io/kubernetes/pkg/controller/nodeipam"
32+
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
33+
netutils "k8s.io/utils/net"
34+
)
35+
36+
const (
37+
maxAllowedNodeCIDRs = 2
38+
)
39+
40+
var (
41+
// defaultNodeMaskCIDRIPv4 is default mask size for IPv4 node cidr
42+
defaultNodeMaskCIDRIPv4 = 24
43+
// defaultNodeMaskCIDRIPv6 is default mask size for IPv6 node cidr
44+
defaultNodeMaskCIDRIPv6 = 64
45+
)
46+
47+
func startNodeIpamController(stopCh <-chan struct{}, cloud cloudprovider.Interface, nodeInformer v1.NodeInformer, kubeclient kubernetes.Interface) error {
48+
var serviceCIDR *net.IPNet
49+
var secondaryServiceCIDR *net.IPNet
50+
51+
// should we start nodeIPAM
52+
if !Options.AllocateNodeCIDRs {
53+
return nil
54+
}
55+
56+
// failure: bad cidrs in config
57+
clusterCIDRs, dualStack, err := processCIDRs(Options.ClusterCIDRIPv4)
58+
if err != nil {
59+
return fmt.Errorf("processCIDRs failed: %w", err)
60+
}
61+
62+
// failure: more than one cidr but they are not configured as dual stack
63+
if len(clusterCIDRs) > 1 && !dualStack {
64+
return fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily", len(clusterCIDRs))
65+
}
66+
67+
// failure: more than cidrs is not allowed even with dual stack
68+
if len(clusterCIDRs) > maxAllowedNodeCIDRs {
69+
return fmt.Errorf("len of clusters is:%v > more than max allowed of %d", len(clusterCIDRs), maxAllowedNodeCIDRs)
70+
}
71+
72+
/* TODO: uncomment and fix if we want to support service cidr overlap with nodecidr
73+
// service cidr processing
74+
if len(strings.TrimSpace(nodeIPAMConfig.ServiceCIDR)) != 0 {
75+
_, serviceCIDR, err = netutils.ParseCIDRSloppy(nodeIPAMConfig.ServiceCIDR)
76+
if err != nil {
77+
klog.ErrorS(err, "Unsuccessful parsing of service CIDR", "CIDR", nodeIPAMConfig.ServiceCIDR)
78+
}
79+
}
80+
81+
if len(strings.TrimSpace(nodeIPAMConfig.SecondaryServiceCIDR)) != 0 {
82+
_, secondaryServiceCIDR, err = netutils.ParseCIDRSloppy(nodeIPAMConfig.SecondaryServiceCIDR)
83+
if err != nil {
84+
klog.ErrorS(err, "Unsuccessful parsing of service CIDR", "CIDR", nodeIPAMConfig.SecondaryServiceCIDR)
85+
}
86+
}
87+
88+
// the following checks are triggered if both serviceCIDR and secondaryServiceCIDR are provided
89+
if serviceCIDR != nil && secondaryServiceCIDR != nil {
90+
// should be dual stack (from different IPFamilies)
91+
dualstackServiceCIDR, err := netutils.IsDualStackCIDRs([]*net.IPNet{serviceCIDR, secondaryServiceCIDR})
92+
if err != nil {
93+
return nil, false, fmt.Errorf("failed to perform dualstack check on serviceCIDR and secondaryServiceCIDR error:%v", err)
94+
}
95+
if !dualstackServiceCIDR {
96+
return nil, false, fmt.Errorf("serviceCIDR and secondaryServiceCIDR are not dualstack (from different IPfamiles)")
97+
}
98+
}
99+
*/
100+
101+
nodeCIDRMaskSizes := setNodeCIDRMaskSizes(clusterCIDRs)
102+
103+
ctx := wait.ContextForChannel(stopCh)
104+
105+
nodeIpamController, err := nodeipamcontroller.NewNodeIpamController(
106+
ctx,
107+
nodeInformer,
108+
cloud,
109+
kubeclient,
110+
clusterCIDRs,
111+
serviceCIDR,
112+
secondaryServiceCIDR,
113+
nodeCIDRMaskSizes,
114+
ipam.RangeAllocatorType,
115+
)
116+
if err != nil {
117+
return err
118+
}
119+
120+
go nodeIpamController.Run(ctx)
121+
return nil
122+
}
123+
124+
// processCIDRs is a helper function that works on a comma separated cidrs and returns
125+
// a list of typed cidrs
126+
// a flag if cidrs represents a dual stack
127+
// error if failed to parse any of the cidrs
128+
func processCIDRs(cidrsList string) ([]*net.IPNet, bool, error) {
129+
cidrsSplit := strings.Split(strings.TrimSpace(cidrsList), ",")
130+
131+
cidrs, err := netutils.ParseCIDRs(cidrsSplit)
132+
if err != nil {
133+
return nil, false, err
134+
}
135+
136+
// if cidrs has an error then the previous call will fail
137+
// safe to ignore error checking on next call
138+
dualstack, err := netutils.IsDualStackCIDRs(cidrs)
139+
if err != nil {
140+
return nil, false, fmt.Errorf("failed to perform dualstack check on cidrs: %w", err)
141+
}
142+
143+
return cidrs, dualstack, nil
144+
}
145+
146+
func setNodeCIDRMaskSizes(clusterCIDRs []*net.IPNet) []int {
147+
sortedSizes := func(maskSizeIPv4, maskSizeIPv6 int) []int {
148+
nodeMaskCIDRs := make([]int, len(clusterCIDRs))
149+
150+
for idx, clusterCIDR := range clusterCIDRs {
151+
if netutils.IsIPv6CIDR(clusterCIDR) {
152+
nodeMaskCIDRs[idx] = maskSizeIPv6
153+
} else {
154+
nodeMaskCIDRs[idx] = maskSizeIPv4
155+
}
156+
}
157+
return nodeMaskCIDRs
158+
}
159+
160+
if Options.NodeCIDRMaskSizeIPv4 != 0 {
161+
defaultNodeMaskCIDRIPv4 = Options.NodeCIDRMaskSizeIPv4
162+
}
163+
if Options.NodeCIDRMaskSizeIPv6 != 0 {
164+
defaultNodeMaskCIDRIPv6 = Options.NodeCIDRMaskSizeIPv6
165+
}
166+
return sortedSizes(defaultNodeMaskCIDRIPv4, defaultNodeMaskCIDRIPv6)
167+
}

0 commit comments

Comments
 (0)