Skip to content

Commit 9e00608

Browse files
Merge pull request #97 from qiangzii/master
support custom reserved ip count for vlan
2 parents ef8c7b8 + 8c536ec commit 9e00608

File tree

11 files changed

+77
-61
lines changed

11 files changed

+77
-61
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ deploy:
7373
kustomize build config/${TARGET} > ${DEPLOY}
7474

7575
publish: build tools
76-
hack/docker_build.sh ${TAG}
76+
hack/docker_build.sh ${REPO} ${TAG}
7777

7878
generate-prototype:
7979
protoc --go_out=. pkg/rpc/message.proto

cmd/tools/vxnet-client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func showVxNetInfo(vxnet string) *rpc.VxNet {
13-
if vxnets, err := qcclient.QClient.GetVxNets([]string{vxnet}); err != nil {
13+
if vxnets, err := qcclient.QClient.GetVxNets([]string{vxnet}, 0); err != nil {
1414
fmt.Printf("Get info for vxnet %s failed: %v\n", vxnet, err)
1515
} else {
1616
if len(vxnets) == 0 {

deploy/hostnic.yaml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ spec:
181181
description: The block size to use for IP address assignments from
182182
this pool. Defaults to 26 for IPv4 and 112 for IPv6.
183183
type: integer
184+
customReservedIPCount:
185+
description: CustomReservedIPCount used to specify user custom reserved ip count. Defaults to 0
186+
type: integer
187+
format: int64
184188
cidr:
185189
description: The pool CIDR.
186190
type: string
@@ -320,6 +324,9 @@ spec:
320324
description: The block size to use for IP address assignments from
321325
this pool. Defaults to 26 for IPv4 and 112 for IPv6.
322326
type: integer
327+
customReservedIPCount:
328+
description: CustomReservedIPCount used to specify user custom reserved ip count. Defaults to 0
329+
type: integer
323330
vxnets:
324331
description: vxnets in VxNetPool
325332
items:
@@ -474,7 +481,7 @@ spec:
474481
fieldRef:
475482
apiVersion: v1
476483
fieldPath: metadata.namespace
477-
image: qingcloud/hostnic-plus:v1.0.7
484+
image: qingcloud/hostnic-plus:v1.0.14
478485
imagePullPolicy: IfNotPresent
479486
name: hostnic-node
480487
ports:
@@ -513,7 +520,7 @@ spec:
513520
- /app/install_hostnic.sh
514521
command:
515522
- /bin/sh
516-
image: qingcloud/hostnic-plus:v1.0.7
523+
image: qingcloud/hostnic-plus:v1.0.14
517524
imagePullPolicy: IfNotPresent
518525
name: hostnic-init
519526
resources: {}
@@ -558,11 +565,11 @@ spec:
558565
name: hostnic-cfg-cm
559566
name: hostnic-cfg
560567
- name: apiaccesskey
561-
secret:
568+
configMap:
562569
items:
563-
- key: config.yaml
564-
path: config.yaml
565-
secretName: qcsecret
570+
- key: config.yaml
571+
path: config.yaml
572+
name: hostnic-qingcloud
566573
- hostPath:
567574
path: /etc/qingcloud
568575
name: qingcloud-cfg
@@ -588,7 +595,7 @@ spec:
588595
spec:
589596
containers:
590597
- name: hostnic-controller
591-
image: qingcloud/hostnic-plus:v1.0.7
598+
image: qingcloud/hostnic-plus:v1.0.14
592599
command:
593600
- /app/hostnic-controller
594601
- --v=5
@@ -608,12 +615,6 @@ spec:
608615
serviceAccountName: hostnic-node
609616
hostNetwork: true
610617
volumes:
611-
- name: apiaccesskey
612-
secret:
613-
items:
614-
- key: config.yaml
615-
path: config.yaml
616-
secretName: qcsecret
617618
- hostPath:
618619
path: /etc/qingcloud
619620
name: qingcloud-cfg
@@ -623,4 +624,10 @@ spec:
623624
- configMap:
624625
name: clusterconfig
625626
name: clusterconfig
627+
- configMap:
628+
items:
629+
- key: config.yaml
630+
path: config.yaml
631+
name: hostnic-qingcloud
632+
name: apiaccesskey
626633

hack/docker_build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ set -ex
44
set -o pipefail
55

66
# push to qingcloud with default latest tag
7+
REPO="$1"
78
REPO=${REPO:-qingcloud}
8-
TAG="$1"
9+
TAG="$2"
910

1011
# hostnic
1112
docker build -f build/hostnic/Dockerfile -t $REPO/hostnic-plus:$TAG .

pkg/allocator/allocator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (a *Allocator) getVxnets(vxnet string) (*rpc.VxNet, error) {
163163
}
164164
}
165165

166-
result, err := qcclient.QClient.GetVxNets([]string{vxnet})
166+
result, err := qcclient.QClient.GetVxNets([]string{vxnet}, 0)
167167
if err != nil {
168168
return nil, err
169169
}

pkg/apis/network/v1alpha1/ippool_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ type IPPoolSpec struct {
106106
// The block size to use for IP address assignments from this pool. Defaults to 26 for IPv4 and 112 for IPv6.
107107
BlockSize int `json:"blockSize,omitempty"`
108108

109+
// CustomReservedIPCount used to specify user custom reserved ip count. Defaults to 0
110+
// +optional
111+
CustomReservedIPCount int64 `json:"customReservedIPCount,omitempty"`
112+
109113
VLAN VLANConfig `json:"vlanConfig,omitempty"`
110114

111115
Gateway string `json:"gateway,omitempty"`

pkg/apis/network/v1alpha1/vxnetpool_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type VxNetPoolSpec struct {
4848

4949
// The block size to use for IP address assignments from this pool. Defaults to 26 for IPv4 and 112 for IPv6.
5050
BlockSize int `json:"blockSize"`
51+
52+
// CustomReservedIPCount used to specify user custom reserved ip count. Defaults to 0
53+
// +optional
54+
CustomReservedIPCount int64 `json:"customReservedIPCount,omitempty"`
5155
}
5256

5357
type PoolInfo struct {

pkg/controller/vxnet.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func (c *VxNetPoolController) handleObject(obj interface{}) {
439439
// newIPPool creates a new IPPool for a vxnet resource. It also sets
440440
// the appropriate OwnerReferences on the resource so handleObject can discover
441441
// the vxnet resource that 'owns' it.
442-
func (c *VxNetPoolController) createIPPool(name string, blockSize int, vxnet *rpc.VxNet) (*networkv1alpha1.IPPool, error) {
442+
func (c *VxNetPoolController) createIPPool(name string, blockSize int, customReservedIPCount int64, vxnet *rpc.VxNet) (*networkv1alpha1.IPPool, error) {
443443
ippool := &networkv1alpha1.IPPool{
444444
ObjectMeta: metav1.ObjectMeta{
445445
Name: name,
@@ -448,12 +448,13 @@ func (c *VxNetPoolController) createIPPool(name string, blockSize int, vxnet *rp
448448
},
449449
},
450450
Spec: networkv1alpha1.IPPoolSpec{
451-
Type: networkv1alpha1.IPPoolTypeLocal,
452-
CIDR: vxnet.Network,
453-
Gateway: vxnet.Gateway,
454-
BlockSize: blockSize,
455-
RangeStart: vxnet.IPStart,
456-
RangeEnd: vxnet.IPEnd,
451+
Type: networkv1alpha1.IPPoolTypeLocal,
452+
CIDR: vxnet.Network,
453+
Gateway: vxnet.Gateway,
454+
BlockSize: blockSize,
455+
CustomReservedIPCount: customReservedIPCount,
456+
RangeStart: vxnet.IPStart,
457+
RangeEnd: vxnet.IPEnd,
457458
},
458459
}
459460
return c.clientset.NetworkV1alpha1().IPPools().Create(context.TODO(), ippool, metav1.CreateOptions{})
@@ -535,7 +536,7 @@ func (c *VxNetPoolController) prepareK8SResource(pool *networkv1alpha1.VxNetPool
535536
if errors.IsNotFound(err) {
536537
// create ippool
537538
if v, ok := c.getVxNetInfo(vxnet.Name); ok {
538-
if _, err := c.createIPPool(vxnet.Name, pool.Spec.BlockSize, v); err != nil {
539+
if _, err := c.createIPPool(vxnet.Name, pool.Spec.BlockSize, pool.Spec.CustomReservedIPCount, v); err != nil {
539540
return false, err
540541
} else {
541542
// handle it's block at next event
@@ -623,7 +624,7 @@ func (c *VxNetPoolController) setJob(id, job string) {
623624
}
624625

625626
func (c *VxNetPoolController) deleteVIPsByVxnetID(vxnetID string) {
626-
vxnets, err := qcclient.QClient.GetVxNets([]string{vxnetID})
627+
vxnets, err := qcclient.QClient.GetVxNets([]string{vxnetID}, 0)
627628
if err != nil {
628629
klog.Errorf("Get info for vxnet %s failed: %v\n", vxnetID, err)
629630
return
@@ -681,7 +682,7 @@ func (c *VxNetPoolController) qingCloudSync() {
681682
}
682683
}
683684
if len(needUpdate) > 0 {
684-
if result, err := qcclient.QClient.GetVxNets(needUpdate); err != nil {
685+
if result, err := qcclient.QClient.GetVxNets(needUpdate, pool.Spec.CustomReservedIPCount); err != nil {
685686
klog.Errorf("Get vxnet %v from QingCloud failed: %v", needUpdate, err)
686687
return
687688
} else {

pkg/qcclient/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type QingCloudAPI interface {
1313
GetCreatedNicsByName(name string) ([]*rpc.HostNic, error)
1414

1515
//vxnet info
16-
GetVxNets([]string) (map[string]*rpc.VxNet, error)
16+
GetVxNets(ids []string, customReservedIPCount int64) (map[string]*rpc.VxNet, error)
1717

1818
//job info
1919
DescribeNicJobs(ids []string) ([]string, map[string]bool, error)

pkg/qcclient/wrapper.go

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"math/big"
7-
"net"
87
"os"
98
"time"
109

@@ -25,14 +24,14 @@ const (
2524
defaultOpTimeout = 300 * time.Second
2625
defaultWaitInterval = 5 * time.Second
2726

28-
reservedVIPCount = 12
29-
reservedVIPCountForVlan int64 = 7 //reserve 1/7 ip for hostnic br
27+
reservedVIPCountForVxlan int64 = 12
28+
reservedVIPCountForVlan int64 = 7 //reserve 1/7 ip for hostnic br
3029

3130
)
3231

33-
var (
34-
customReservedIPCount int64 = 0
35-
)
32+
// var (
33+
// customReservedIPCount int64 = 0
34+
// )
3635

3736
type Options struct {
3837
Tag string
@@ -177,7 +176,7 @@ func (q *qingcloudAPIWrapper) GetCreatedNicsByName(name string) ([]*rpc.HostNic,
177176

178177
if len(netIDs) > 0 {
179178
tmp := removeDupByMap(netIDs)
180-
vxnets, err := q.GetVxNets(tmp)
179+
vxnets, err := q.GetVxNets(tmp, 0)
181180
if err != nil {
182181
return nil, err
183182
}
@@ -217,7 +216,7 @@ func (q *qingcloudAPIWrapper) GetCreatedNicsByVxNet(vxnet string) ([]*rpc.HostNi
217216

218217
if len(netIDs) > 0 {
219218
tmp := removeDupByMap(netIDs)
220-
vxnets, err := q.GetVxNets(tmp)
219+
vxnets, err := q.GetVxNets(tmp, 0)
221220
if err != nil {
222221
return nil, err
223222
}
@@ -446,7 +445,7 @@ func (q *qingcloudAPIWrapper) DescribeNicJobs(ids []string) ([]string, map[strin
446445
return left, working, nil
447446
}
448447

449-
func (q *qingcloudAPIWrapper) getVxNets(ids []string, public bool) ([]*rpc.VxNet, error) {
448+
func (q *qingcloudAPIWrapper) getVxNets(ids []string, public bool, customReservedIPCount int64) ([]*rpc.VxNet, error) {
450449
input := &service.DescribeVxNetsInput{
451450
VxNets: service.StringSlice(ids),
452451
Limit: service.Int(constants.NicNumLimit),
@@ -477,21 +476,23 @@ func (q *qingcloudAPIWrapper) getVxNets(ids []string, public bool) ([]*rpc.VxNet
477476
if qcVxNet.TunnelType != nil {
478477
vxnetItem.TunnelType = *qcVxNet.TunnelType
479478
}
479+
var reservedIPCount int64
480480
if vxnetItem.TunnelType == constants.TunnelTypeVlan {
481-
// parse ip_network to get mask; if mask is 24, reserve more ip than specifc in config
482-
_, ipNet, err := net.ParseCIDR(vxnetItem.Network)
483-
if err != nil {
484-
log.Errorf("parse ip_network to get mask error: %v", err)
485-
}
486-
maskLength, _ := ipNet.Mask.Size()
487-
if maskLength == 24 {
488-
log.Infof("vxnet %s ip network %s mask is 24, reserve another 64 ip", *qcVxNet.VxNetID, vxnetItem.Network)
489-
customReservedIPCount = 64
481+
totalCount := IPRangeCount(*qcVxNet.Router.DYNIPStart, *qcVxNet.Router.DYNIPEnd)
482+
reservedIPCount = totalCount / reservedVIPCountForVlan
483+
maxCustomReservedCount := totalCount - reservedIPCount - 1
484+
485+
if (customReservedIPCount >= 0 && maxCustomReservedCount < customReservedIPCount) || customReservedIPCount < 0 {
486+
return nil, fmt.Errorf("invalid customReservedIPCount %d config in vxnetpool spec, should in range 0~%d", customReservedIPCount, maxCustomReservedCount)
490487
}
491-
vxnetItem.IPEnd = getIPEndAfterReservedForVlan(*qcVxNet.Router.DYNIPStart, *qcVxNet.Router.DYNIPEnd, reservedVIPCountForVlan, customReservedIPCount)
488+
log.Infof("vxnet %s custom reserve ip count: %d", *qcVxNet.VxNetID, customReservedIPCount)
489+
492490
} else {
493-
vxnetItem.IPEnd = getIPEndAfterReserved(*qcVxNet.Router.DYNIPEnd, reservedVIPCount, customReservedIPCount)
491+
reservedIPCount = reservedVIPCountForVxlan
492+
customReservedIPCount = 0
494493
}
494+
vxnetItem.IPEnd = getIPEndAfterReserved(*qcVxNet.Router.DYNIPEnd, reservedIPCount, customReservedIPCount)
495+
495496
} else {
496497
return nil, fmt.Errorf("vxnet %s should bind to vpc", *qcVxNet.VxNetID)
497498
}
@@ -502,12 +503,12 @@ func (q *qingcloudAPIWrapper) getVxNets(ids []string, public bool) ([]*rpc.VxNet
502503
return vxNets, nil
503504
}
504505

505-
func (q *qingcloudAPIWrapper) GetVxNets(ids []string) (map[string]*rpc.VxNet, error) {
506+
func (q *qingcloudAPIWrapper) GetVxNets(ids []string, customReservedIPCount int64) (map[string]*rpc.VxNet, error) {
506507
if len(ids) <= 0 {
507508
return nil, errors.WithStack(fmt.Errorf("GetVxNets should not have empty input"))
508509
}
509510

510-
vxnets, err := q.getVxNets(ids, false)
511+
vxnets, err := q.getVxNets(ids, false, customReservedIPCount)
511512
if err != nil {
512513
return nil, err
513514
}
@@ -523,7 +524,7 @@ func (q *qingcloudAPIWrapper) GetVxNets(ids []string) (map[string]*rpc.VxNet, er
523524
}
524525
}
525526
if len(left) > 0 {
526-
vxnets, err := q.getVxNets(left, true)
527+
vxnets, err := q.getVxNets(left, true, customReservedIPCount)
527528
if err != nil {
528529
return nil, err
529530
}
@@ -571,7 +572,7 @@ func (q *qingcloudAPIWrapper) attachNicTag(nics []string) {
571572
func (q *qingcloudAPIWrapper) CreateVIPs(vxnet *rpc.VxNet) (string, error) {
572573
vipName := constants.NicPrefix + vxnet.ID
573574
vipRange := fmt.Sprintf("%s-%s", vxnet.IPStart, vxnet.IPEnd)
574-
count := IPRangeCount(vxnet.IPStart, vxnet.IPEnd)
575+
count := int(IPRangeCount(vxnet.IPStart, vxnet.IPEnd))
575576
input := &service.CreateVIPsInput{
576577
Count: &count,
577578
VIPName: &vipName,
@@ -758,23 +759,16 @@ func (q *qingcloudAPIWrapper) DescribeClusterNodes(clusterID string) ([]*rpc.Nod
758759
return nodes, nil
759760
}
760761

761-
func IPRangeCount(from, to string) int {
762+
func IPRangeCount(from, to string) int64 {
762763
startIP := cnet.ParseIP(from)
763764
endIP := cnet.ParseIP(to)
764765
startInt := cnet.IPToBigInt(*startIP)
765766
endInt := cnet.IPToBigInt(*endIP)
766-
return int(big.NewInt(0).Sub(endInt, startInt).Int64() + 1)
767+
return big.NewInt(0).Sub(endInt, startInt).Int64() + 1
767768
}
768769

769770
func getIPEndAfterReserved(end string, reservedCount, customReservedCount int64) string {
770771
e := cnet.ParseIP(end)
771772
i := big.NewInt(0).Sub(cnet.IPToBigInt(*e), big.NewInt(reservedCount+customReservedCount))
772773
return cnet.BigIntToIP(i).String()
773774
}
774-
775-
func getIPEndAfterReservedForVlan(start, end string, reservedCount, customReservedCount int64) string {
776-
s := cnet.ParseIP(start)
777-
e := cnet.ParseIP(end)
778-
i := big.NewInt(0).Sub(cnet.IPToBigInt(*e), big.NewInt((cnet.IPToBigInt(*e).Int64()-cnet.IPToBigInt(*s).Int64()+1)/reservedCount+customReservedCount))
779-
return cnet.BigIntToIP(i).String()
780-
}

0 commit comments

Comments
 (0)