-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
209 lines (181 loc) · 6.05 KB
/
client.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"time"
"github.com/yunify/hostnic-cni/pkg/signals"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
networkv1alpha1 "github.com/yunify/hostnic-cni/pkg/apis/network/v1alpha1"
clientset "github.com/yunify/hostnic-cni/pkg/client/clientset/versioned"
informers "github.com/yunify/hostnic-cni/pkg/client/informers/externalversions"
"github.com/yunify/hostnic-cni/pkg/constants"
"github.com/yunify/hostnic-cni/pkg/simple/client/network/ippool/ipam"
)
var handleID, pool, masterURL, kubeconfig string
var listBrokenBlocks, fixBrokenBlocks, debug bool
func main() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&handleID, "handleID", "", "handleID")
flag.StringVar(&pool, "pool", "", "pool name")
flag.BoolVar(&listBrokenBlocks, "lb", false, "whether to list broken ipamblocks")
flag.BoolVar(&fixBrokenBlocks, "fb", false, "whether to fix broken ipamblocks")
flag.BoolVar(&debug, "d", false, "show debug info for broken ipamblocks")
flag.Parse()
// set up signals so we handle the first shutdown signals gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("Error building kubeconfig: %v", err)
return
}
k8sClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("Error building kubernetes clientset: %v", err)
return
}
client, err := clientset.NewForConfig(cfg)
if err != nil {
fmt.Printf("Error building example clientset: %v", err)
return
}
k8sInformerFactory := k8sinformers.NewSharedInformerFactory(k8sClient, time.Second*10)
informerFactory := informers.NewSharedInformerFactory(client, time.Second*30)
ipamClient := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory, k8sInformerFactory)
k8sInformerFactory.Start(stopCh)
informerFactory.Start(stopCh)
if err = ipamClient.Sync(stopCh); err != nil {
fmt.Printf("ipamclient sync error: %v", err)
return
}
// c := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory)
if handleID != "" {
if err := ipamClient.ReleaseByHandle(handleID); err != nil {
fmt.Printf("Release %s failed: %v\n", handleID, err)
} else {
fmt.Printf("Release %s OK\n", handleID)
}
}
// Get all ippool and ipamblocks
// Get and fix broken ipamblock here
var args ipam.GetUtilizationArgs
var utils []*ipam.PoolBlocksUtilization
if pool != "" {
args.Pools = []string{pool}
}
utils, err = ipamClient.GetPoolBlocksUtilization(args)
if err != nil {
fmt.Printf("GetUtilization failed: %v\n", err)
return
} else {
fmt.Printf("GetUtilization:\n")
for _, util := range utils {
fmt.Printf("\t%s: Capacity %3d, Unallocated %3d, Allocate %3d, Reserved %3d\n",
util.Name, util.Capacity, util.Unallocated, util.Allocate, util.Reserved)
for _, block := range util.Blocks {
fmt.Printf("\t%24s: Capacity %3d, Unallocated %3d, Allocate %3d, Reserved %3d\n",
block.Name, block.Capacity, block.Unallocated, block.Allocate, block.Reserved)
}
}
}
// Get configmap
cm, err := k8sClient.CoreV1().ConfigMaps(constants.IPAMConfigNamespace).Get(context.TODO(), constants.IPAMConfigName, metav1.GetOptions{})
if err != nil {
fmt.Printf("GetSubnets failed: %v\n", err)
return
}
var apps map[string][]string
if err := json.Unmarshal([]byte(cm.Data[constants.IPAMConfigDate]), &apps); err != nil {
fmt.Printf("GetSubnets failed: %v\n", err)
return
}
// GetSubnets
autoSign := "off"
if cm.Data[constants.IPAMAutoAssignForNamespace] == "on" {
autoSign = "on"
}
fmt.Printf("GetSubnets: autoSign[%s]\n", autoSign)
for ns, subnets := range apps {
if ns != constants.IPAMDefaultPoolKey || autoSign == "off" {
fmt.Printf("\t%s: %v\n", ns, subnets)
}
}
fmt.Printf("FreeSubnets: %v\n", getFreeSubnets(autoSign, apps, utils))
// broken blocks
if listBrokenBlocks || fixBrokenBlocks {
utils, err = ipamClient.GetAndFixBrokenBlocks(args, fixBrokenBlocks)
if err != nil {
fmt.Printf("GetAndFixBrokenBlocks failed: %v\n", err)
return
}
fmt.Printf("\nBroken blocks for each ippool:\n")
for _, ippool := range utils {
fmt.Printf("\t%s: %v\n", ippool.Name, ippool.BrokenBlockNames)
if debug {
// print debug msg
for _, block := range ippool.BrokenBlocks {
for ipStr, podNames := range block.IpToPods {
if len(podNames) > 1 {
fmt.Printf("\t\tip %s was allocated more than noce to pods %v\n", ipStr, podNames)
}
}
for ipStr, podNames := range block.IpWithoutRecord {
fmt.Printf("\t\tip %s was allocated to pods %v, but not record in ipamblock\n", ipStr, podNames)
}
// for ipStr, podNames := range block.IpToPodsWithWrongRecord {
// if len(podNames) == 1 {
// fmt.Printf("\t\tip %s was recorded to wrong pods %v\n", ipStr, podNames)
// }
// }
}
}
if fixBrokenBlocks {
fmt.Printf("\tfix success: %v\n", ippool.BrokenBlockFixSucceed)
}
fmt.Println()
}
}
}
func getFreeSubnets(autoSign string, apps map[string][]string, ippool []*ipam.PoolBlocksUtilization) []string {
all := make(map[string]struct{})
for _, pool := range ippool {
for _, block := range pool.Blocks {
all[block.Name] = struct{}{}
}
}
for ns, subnets := range apps {
if ns == constants.IPAMDefaultPoolKey {
if autoSign != "on" {
for _, pool := range ippool {
if contains(subnets, pool.Name) {
for _, block := range pool.Blocks {
delete(all, block.Name)
}
}
}
}
} else {
for _, subnet := range subnets {
delete(all, subnet)
}
}
}
free := []string{}
for subnet := range all {
free = append(free, subnet)
}
return free
}
func contains(items []string, item string) bool {
for _, v := range items {
if v == item {
return true
}
}
return false
}