Skip to content

Commit 7d6d621

Browse files
Merge pull request #91 from qiangzii/master
update ipamclient tool to fix borken ipamblocks;
2 parents c11a1be + 590d720 commit 7d6d621

File tree

3 files changed

+361
-5
lines changed

3 files changed

+361
-5
lines changed

cmd/tools/ipam-client/client.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import (
2121
)
2222

2323
var handleID, pool, masterURL, kubeconfig string
24+
var listBrokenBlocks, fixBrokenBlocks, debug bool
2425

2526
func main() {
2627
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
2728
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
2829
flag.StringVar(&handleID, "handleID", "", "handleID")
2930
flag.StringVar(&pool, "pool", "", "pool name")
31+
flag.BoolVar(&listBrokenBlocks, "lb", false, "whether to list broken ipamblocks")
32+
flag.BoolVar(&fixBrokenBlocks, "fb", false, "whether to fix broken ipamblocks")
33+
flag.BoolVar(&debug, "d", false, "show debug info for broken ipamblocks")
3034
flag.Parse()
3135

3236
// set up signals so we handle the first shutdown signals gracefully
@@ -73,11 +77,15 @@ func main() {
7377
}
7478
}
7579

80+
// Get all ippool and ipamblocks
81+
// Get and fix broken ipamblock here
7682
var args ipam.GetUtilizationArgs
83+
var utils []*ipam.PoolBlocksUtilization
84+
7785
if pool != "" {
7886
args.Pools = []string{pool}
7987
}
80-
utils, err := ipamClient.GetPoolBlocksUtilization(args)
88+
utils, err = ipamClient.GetPoolBlocksUtilization(args)
8189
if err != nil {
8290
fmt.Printf("GetUtilization failed: %v\n", err)
8391
return
@@ -93,7 +101,7 @@ func main() {
93101
}
94102
}
95103

96-
// GetSubnets
104+
// Get configmap
97105
cm, err := k8sClient.CoreV1().ConfigMaps(constants.IPAMConfigNamespace).Get(context.TODO(), constants.IPAMConfigName, metav1.GetOptions{})
98106
if err != nil {
99107
fmt.Printf("GetSubnets failed: %v\n", err)
@@ -106,18 +114,56 @@ func main() {
106114
return
107115
}
108116

117+
// GetSubnets
109118
autoSign := "off"
110119
if cm.Data[constants.IPAMAutoAssignForNamespace] == "on" {
111120
autoSign = "on"
112121
}
113-
114122
fmt.Printf("GetSubnets: autoSign[%s]\n", autoSign)
115123
for ns, subnets := range apps {
116124
if ns != constants.IPAMDefaultPoolKey || autoSign == "off" {
117125
fmt.Printf("\t%s: %v\n", ns, subnets)
118126
}
119127
}
120128
fmt.Printf("FreeSubnets: %v\n", getFreeSubnets(autoSign, apps, utils))
129+
130+
// broken blocks
131+
if listBrokenBlocks || fixBrokenBlocks {
132+
utils, err = ipamClient.GetAndFixBrokenBlocks(args, fixBrokenBlocks)
133+
if err != nil {
134+
fmt.Printf("GetAndFixBrokenBlocks failed: %v\n", err)
135+
return
136+
}
137+
fmt.Printf("\nBroken blocks for each ippool:\n")
138+
for _, ippool := range utils {
139+
fmt.Printf("\t%s: %v\n", ippool.Name, ippool.BrokenBlockNames)
140+
if debug {
141+
// print debug msg
142+
for _, block := range ippool.BrokenBlocks {
143+
for ipStr, podNames := range block.IpToPods {
144+
if len(podNames) > 1 {
145+
fmt.Printf("\t\tip %s was allocated more than noce to pods %v\n", ipStr, podNames)
146+
}
147+
}
148+
149+
for ipStr, podNames := range block.IpWithoutRecord {
150+
fmt.Printf("\t\tip %s was allocated to pods %v, but not record in ipamblock\n", ipStr, podNames)
151+
}
152+
153+
// for ipStr, podNames := range block.IpToPodsWithWrongRecord {
154+
// if len(podNames) == 1 {
155+
// fmt.Printf("\t\tip %s was recorded to wrong pods %v\n", ipStr, podNames)
156+
// }
157+
// }
158+
}
159+
}
160+
if fixBrokenBlocks {
161+
fmt.Printf("\tfix success: %v\n", ippool.BrokenBlockFixSucceed)
162+
}
163+
fmt.Println()
164+
}
165+
166+
}
121167
}
122168

123169
func getFreeSubnets(autoSign string, apps map[string][]string, ippool []*ipam.PoolBlocksUtilization) []string {

0 commit comments

Comments
 (0)