Skip to content

Commit 0d9a469

Browse files
author
Rahul Sharma
committed
enable nodeipamcontroller within CCM
1 parent 021ba42 commit 0d9a469

File tree

6 files changed

+491
-0
lines changed

6 files changed

+491
-0
lines changed

cloud/linode/cloud.go

Lines changed: 8 additions & 0 deletions
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

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
var (
37+
// defaultNodeMaskCIDRIPv4 is default mask size for IPv4 node cidr
38+
defaultNodeMaskCIDRIPv4 = 24
39+
// defaultNodeMaskCIDRIPv6 is default mask size for IPv6 node cidr
40+
defaultNodeMaskCIDRIPv6 = 64
41+
)
42+
43+
func startNodeIpamController(stopCh <-chan struct{}, cloud cloudprovider.Interface, nodeInformer v1.NodeInformer, kubeclient kubernetes.Interface) error {
44+
var serviceCIDR *net.IPNet
45+
var secondaryServiceCIDR *net.IPNet
46+
47+
// should we start nodeIPAM
48+
if !Options.AllocateNodeCIDRs {
49+
return nil
50+
}
51+
52+
// failure: bad cidrs in config
53+
clusterCIDRs, dualStack, err := processCIDRs(Options.ClusterCIDRIPv4)
54+
if err != nil {
55+
return fmt.Errorf("processCIDRs failed: %v", err)
56+
}
57+
58+
// failure: more than one cidr but they are not configured as dual stack
59+
if len(clusterCIDRs) > 1 && !dualStack {
60+
return fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily", len(clusterCIDRs))
61+
}
62+
63+
// failure: more than cidrs is not allowed even with dual stack
64+
if len(clusterCIDRs) > 2 {
65+
return fmt.Errorf("len of clusters is:%v > more than max allowed of 2", len(clusterCIDRs))
66+
}
67+
68+
/* TODO: uncomment and fix if we want to support service cidr overlap with nodecidr
69+
// service cidr processing
70+
if len(strings.TrimSpace(nodeIPAMConfig.ServiceCIDR)) != 0 {
71+
_, serviceCIDR, err = netutils.ParseCIDRSloppy(nodeIPAMConfig.ServiceCIDR)
72+
if err != nil {
73+
klog.ErrorS(err, "Unsuccessful parsing of service CIDR", "CIDR", nodeIPAMConfig.ServiceCIDR)
74+
}
75+
}
76+
77+
if len(strings.TrimSpace(nodeIPAMConfig.SecondaryServiceCIDR)) != 0 {
78+
_, secondaryServiceCIDR, err = netutils.ParseCIDRSloppy(nodeIPAMConfig.SecondaryServiceCIDR)
79+
if err != nil {
80+
klog.ErrorS(err, "Unsuccessful parsing of service CIDR", "CIDR", nodeIPAMConfig.SecondaryServiceCIDR)
81+
}
82+
}
83+
84+
// the following checks are triggered if both serviceCIDR and secondaryServiceCIDR are provided
85+
if serviceCIDR != nil && secondaryServiceCIDR != nil {
86+
// should be dual stack (from different IPFamilies)
87+
dualstackServiceCIDR, err := netutils.IsDualStackCIDRs([]*net.IPNet{serviceCIDR, secondaryServiceCIDR})
88+
if err != nil {
89+
return nil, false, fmt.Errorf("failed to perform dualstack check on serviceCIDR and secondaryServiceCIDR error:%v", err)
90+
}
91+
if !dualstackServiceCIDR {
92+
return nil, false, fmt.Errorf("serviceCIDR and secondaryServiceCIDR are not dualstack (from different IPfamiles)")
93+
}
94+
}
95+
*/
96+
97+
nodeCIDRMaskSizes, err := setNodeCIDRMaskSizes(clusterCIDRs)
98+
if err != nil {
99+
return fmt.Errorf("setNodeCIDRMaskSizes failed: %v", err)
100+
}
101+
102+
ctx := wait.ContextForChannel(stopCh)
103+
104+
nodeIpamController, err := nodeipamcontroller.NewNodeIpamController(
105+
ctx,
106+
nodeInformer,
107+
cloud,
108+
kubeclient,
109+
clusterCIDRs,
110+
serviceCIDR,
111+
secondaryServiceCIDR,
112+
nodeCIDRMaskSizes,
113+
ipam.CIDRAllocatorType(ipam.RangeAllocatorType),
114+
)
115+
if err != nil {
116+
return err
117+
}
118+
119+
go nodeIpamController.Run(ctx)
120+
return nil
121+
}
122+
123+
// processCIDRs is a helper function that works on a comma separated cidrs and returns
124+
// a list of typed cidrs
125+
// a flag if cidrs represents a dual stack
126+
// error if failed to parse any of the cidrs
127+
func processCIDRs(cidrsList string) ([]*net.IPNet, bool, error) {
128+
cidrsSplit := strings.Split(strings.TrimSpace(cidrsList), ",")
129+
130+
cidrs, err := netutils.ParseCIDRs(cidrsSplit)
131+
if err != nil {
132+
return nil, false, err
133+
}
134+
135+
// if cidrs has an error then the previous call will fail
136+
// safe to ignore error checking on next call
137+
dualstack, _ := netutils.IsDualStackCIDRs(cidrs)
138+
139+
return cidrs, dualstack, nil
140+
}
141+
142+
func setNodeCIDRMaskSizes(clusterCIDRs []*net.IPNet) ([]int, error) {
143+
sortedSizes := func(maskSizeIPv4, maskSizeIPv6 int) []int {
144+
nodeMaskCIDRs := make([]int, len(clusterCIDRs))
145+
146+
for idx, clusterCIDR := range clusterCIDRs {
147+
if netutils.IsIPv6CIDR(clusterCIDR) {
148+
nodeMaskCIDRs[idx] = maskSizeIPv6
149+
} else {
150+
nodeMaskCIDRs[idx] = maskSizeIPv4
151+
}
152+
}
153+
return nodeMaskCIDRs
154+
}
155+
156+
if Options.NodeCIDRMaskSizeIPv4 != 0 {
157+
defaultNodeMaskCIDRIPv4 = Options.NodeCIDRMaskSizeIPv4
158+
}
159+
if Options.NodeCIDRMaskSizeIPv6 != 0 {
160+
defaultNodeMaskCIDRIPv6 = Options.NodeCIDRMaskSizeIPv6
161+
}
162+
return sortedSizes(defaultNodeMaskCIDRIPv4, defaultNodeMaskCIDRIPv6), nil
163+
}

0 commit comments

Comments
 (0)