-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_loadBalancerIP.go
301 lines (259 loc) · 8.37 KB
/
get_loadBalancerIP.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// ANSI color codes for terminal output
const (
ColorReset = "\033[0m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorPurple = "\033[35m"
ColorCyan = "\033[36m"
ColorWhite = "\033[37m"
Bold = "\033[1m"
)
// Spinner animation characters
var (
chars = []string{"|", "/", "-", "\\"}
)
func main() {
// Get current user
currentUser, err := user.Current()
if err != nil {
fmt.Printf("%sError getting current user: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Path to the kubeconfig file
var kubeconfig string
flag.StringVar(&kubeconfig, "kubeconfig", filepath.Join(currentUser.HomeDir, ".kube", "config"), "path to the kubeconfig file")
flag.Parse()
// Load kubeconfig file
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("%sError loading kubeconfig: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Create Kubernetes clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("%sError creating Kubernetes client: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Print welcome message
printWelcomeMessage(currentUser)
// Prompt user for Ansible username
reader := bufio.NewReader(os.Stdin)
fmt.Print(ColorBlue, "\nEnter the Ansible username to run ARP command (Ex: johndoe or johndoe-adm): ", ColorReset)
ansibleUsername, _ := reader.ReadString('\n')
ansibleUsername = strings.TrimSpace(ansibleUsername)
// Get all nodes in the cluster
nodes, err := getAllNodes(clientset)
if err != nil {
fmt.Printf("%sError fetching nodes: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Create inventory file
err = createInventoryFile(nodes, ansibleUsername)
if err != nil {
fmt.Printf("%sError creating inventory file: %v%s\n", ColorRed, err, ColorReset)
os.Exit(1)
}
// Get interface name starting with '7' using Ansible
arpInterface := getInterfaceNameStartingWithSeven()
if arpInterface == "" {
fmt.Println(ColorRed, "Failed to retrieve network interface starting with '7'. Please check your setup.", ColorReset)
os.Exit(1)
}
// Prompt user for LB IPs
fmt.Print(ColorBlue, "\nDo you want to get all LoadBalancer IPs ? (yes/no): ", ColorReset)
option, _ := reader.ReadString('\n')
option = strings.TrimSpace(option)
// Get LB IPs based on user's choice
var lbIPs []string
if option == "yes" {
lbIPs = getLoadBalancerIPsStartingWithSeven(clientset)
} else if option == "no" {
lbIPs = getSpecificLoadBalancerIPs(reader)
} else {
fmt.Println(ColorRed, "Invalid option. Please choose 'yes' or 'no'.", ColorReset)
os.Exit(1)
}
// Run ARP command on all nodes
stopSpinner := loadingAnimation()
defer stopSpinner() // Ensure spinner stops at the end
runARPCommandOnAllNodes(nodes, arpInterface, lbIPs, ansibleUsername)
// Print the interface used for ARP command
fmt.Printf("\nInterface Used to run ARP command: %s%s%s\n\n\n", ColorGreen, arpInterface, ColorReset)
fmt.Printf("%s****%s\n\n", ColorPurple, ColorReset)
// Remove the inventory file after displaying the final output
err = removeInventoryFile()
if err != nil {
fmt.Printf("%sError removing inventory file: %v%s\n", ColorRed, err, ColorReset)
}
}
func printWelcomeMessage(currentUser *user.User) {
fmt.Println("\n*******************************************")
fmt.Printf("%s*** Welcome, %s! ***%s\n", ColorGreen, currentUser.Username, ColorReset)
fmt.Println("*******************************************")
fmt.Printf("%sThis tool helps you find the node name associated with LoadBalancer IPs in your Kubernetes cluster.%s\n", ColorCyan, ColorReset) // Italics
}
func getLoadBalancerIPsStartingWithSeven(clientset *kubernetes.Clientset) []string {
var lbIPs []string
// Get LoadBalancer services
services, err := clientset.CoreV1().Services("").List(context.TODO(), v1.ListOptions{})
if err != nil {
fmt.Printf("%sError fetching services: %v%s\n", ColorRed, err, ColorReset)
return lbIPs
}
// Collect LoadBalancer IPs
for _, service := range services.Items {
if service.Spec.Type == "LoadBalancer" {
for _, ingress := range service.Status.LoadBalancer.Ingress {
if strings.HasPrefix(ingress.IP, "7") {
lbIPs = append(lbIPs, ingress.IP)
}
}
}
}
return lbIPs
}
func getSpecificLoadBalancerIPs(reader *bufio.Reader) []string {
fmt.Print("\nEnter LB IP(s) separated by comma: ")
lbIPsStr, _ := reader.ReadString('\n')
lbIPsStr = strings.TrimSpace(lbIPsStr)
lbIPs := strings.Split(lbIPsStr, ",")
return lbIPs
}
func getAllNodes(clientset *kubernetes.Clientset) ([]string, error) {
var nodes []string
// Get all nodes in the cluster
nodeList, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
if err != nil {
return nil, err
}
// Collect node names
for _, node := range nodeList.Items {
nodes = append(nodes, node.Name)
}
return nodes, nil
}
func createInventoryFile(nodes []string, ansibleUsername string) error {
// Create or overwrite k8s.inventory file
file, err := os.Create("k8s.inventory")
if err != nil {
return err
}
defer file.Close()
// Write inventory header
_, err = file.WriteString("[k8s]\n")
if err != nil {
return err
}
// Write nodes to inventory file
for _, node := range nodes {
_, err := file.WriteString(fmt.Sprintf("%s ansible_user=%s\n", node, ansibleUsername))
if err != nil {
return err
}
}
return nil
}
func loadingAnimation() func() {
stop := make(chan struct{})
done := make(chan struct{})
go func() {
fmt.Println("\n*******************************************")
fmt.Println("*** Please wait... I am working on it ***")
fmt.Println("*******************************************")
for {
select {
case <-stop:
done <- struct{}{}
return
default:
for _, char := range chars {
fmt.Printf("\r%sWorking %s%s", ColorPurple, char, ColorReset)
time.Sleep(100 * time.Millisecond)
}
}
}
}()
return func() {
close(stop)
<-done
fmt.Print("\r \r") // Clear spinner line
}
}
func runARPCommandOnAllNodes(nodes []string, arpInterface string, lbIPs []string, ansibleUsername string) {
var hostingNodes [][]string
for _, node := range nodes {
for _, ip := range lbIPs {
cmd := exec.Command("ansible", "-i", "k8s.inventory", node, "-u", ansibleUsername, "-m", "shell", "-a", fmt.Sprintf("arping -q -I %s %s -c 1", arpInterface, ip))
out, err := cmd.CombinedOutput()
if err != nil {
// If the output contains "FAILED", add the node to the list of LoadBalancer IP hosting nodes
if strings.Contains(string(out), "FAILED") {
hostingNodes = append(hostingNodes, []string{node, ip})
}
continue
}
}
}
// Print table with color
fmt.Println("\nHere is your result:")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Node Name", "LoadBalancer IP"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgRedColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgRedColor},
)
table.SetColumnColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgYellowColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgYellowColor},
)
for _, row := range hostingNodes {
table.Append(row)
}
table.Render() // Render the table with color settings
}
func removeInventoryFile() error {
// Check if the file exists
if _, err := os.Stat("k8s.inventory"); err == nil {
// Remove the file
if err := os.Remove("k8s.inventory"); err != nil {
return err
}
}
return nil
}
func getInterfaceNameStartingWithSeven() string {
// Run a command using Ansible to get the interface name whose IP starts with '7'
cmd := exec.Command("ansible", "-i", "k8s.inventory", "k8s[1]", "-m", "shell", "-a", "ip route | awk '/7/ {print $3}' | head -2")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%sError executing Ansible command: %v%s\n", ColorRed, err, ColorReset)
return "" // Return empty string or handle error appropriately
}
//fmt.Println("Command output:", string(out))
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) >= 3 {
interfaceName := strings.TrimSpace(lines[2]) // Third line
return interfaceName
}
return ""
}