-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
131 lines (116 loc) · 3.67 KB
/
main.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
// K8sToolbox Golang Utility - Enhanced Implementation
// This utility provides Kubernetes-specific diagnostics, automated health checks, and connectivity tests.
package main
import (
"context"
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"log"
"os"
)
var clientset *kubernetes.Clientset
var config *rest.Config
func main() {
var err error
config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// Define flags for CLI commands
healthCheckCmd := flag.NewFlagSet("healthcheck", flag.ExitOnError)
namespace := healthCheckCmd.String("namespace", "default", "Namespace to check pod health")
connectivityCheckCmd := flag.NewFlagSet("connectivity", flag.ExitOnError)
namespaceConn := connectivityCheckCmd.String("namespace", "default", "Namespace of the pod")
podName := connectivityCheckCmd.String("pod", "", "Name of the pod to test connectivity from")
target := connectivityCheckCmd.String("target", "", "Target service or IP to check connectivity to")
if len(os.Args) < 2 {
fmt.Println("Expected 'healthcheck' or 'connectivity' command")
os.Exit(1)
}
switch os.Args[1] {
case "healthcheck":
err := healthCheckCmd.Parse(os.Args[2:])
if err != nil {
return
}
performHealthCheck(*namespace)
case "connectivity":
err := connectivityCheckCmd.Parse(os.Args[2:])
if err != nil {
return
}
if *podName == "" || *target == "" {
fmt.Println("Please specify both pod name and target for connectivity check")
os.Exit(1)
}
testPodConnectivity(*namespaceConn, *podName, *target)
default:
fmt.Printf("Unknown command: %s\n", os.Args[1])
os.Exit(1)
}
}
// performHealthCheck performs a health check on all pods in the specified namespace
func performHealthCheck(namespace string) {
// Get all pods in the specified namespace
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Printf("Error listing pods: %v\n", err)
os.Exit(1)
}
// Perform health checks on each pod
fmt.Printf("Performing health checks on namespace '%s'\n", namespace)
for _, pod := range pods.Items {
fmt.Printf("Checking pod: %s\n", pod.Name)
if pod.Status.Phase == "Running" {
fmt.Printf("Pod %s is healthy\n", pod.Name)
} else {
fmt.Printf("Pod %s is not healthy (Status: %s)\n", pod.Name, pod.Status.Phase)
}
}
}
// testPodConnectivity tests network connectivity from a pod to a specified target
func testPodConnectivity(namespace, podName, target string) {
if clientset == nil || config == nil {
log.Fatalf("Error: Kubernetes clientset or config is not initialized")
}
// Set up the exec request
req := clientset.CoreV1().RESTClient().
Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
Param("container", podName).
Param("command", "ping").
Param("command", "-c").
Param("command", "3").
Param("command", target).
Param("stdin", "true").
Param("stderr", "true").
Param("stdout", "true").
Param("tty", "false")
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
log.Fatalf("Could not initialize command: %v", err)
}
// Create a context that can be used for managing cancellation and timeout
ctx := context.Background()
// Call StreamWithContext instead of the deprecated Stream
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
log.Fatalf("Could not execute command: %v", err)
}
}