Last active
February 8, 2026 17:02
-
-
Save dipankardas011/25b40c6b2fd99d823e84122e31a88a77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "log" | |
| "net" | |
| "os" | |
| "path/filepath" | |
| "time" | |
| "golang.org/x/crypto/ssh" | |
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
| "k8s.io/client-go/kubernetes" | |
| "k8s.io/client-go/tools/clientcmd" | |
| ) | |
| func main() { | |
| // ========================================== | |
| // 1. SETUP SSH CONNECTION | |
| // ========================================== | |
| sshHost := "IP/domain" | |
| sshPort := "22" | |
| sshUser := "root" | |
| sshKeyPath := filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa") | |
| // Read the private key | |
| key, err := os.ReadFile(sshKeyPath) | |
| if err != nil { | |
| log.Fatalf("unable to read private key: %v", err) | |
| } | |
| signer, err := ssh.ParsePrivateKey(key) | |
| if err != nil { | |
| log.Fatalf("unable to parse private key: %v", err) | |
| } | |
| sshConfig := &ssh.ClientConfig{ | |
| User: sshUser, | |
| Auth: []ssh.AuthMethod{ | |
| ssh.PublicKeys(signer), | |
| }, | |
| // INSECURE: In production, use ssh.FixedHostKey(parsedPublicKey) | |
| HostKeyCallback: ssh.InsecureIgnoreHostKey(), | |
| Timeout: 5 * time.Second, | |
| } | |
| // Dial the SSH Jump Host | |
| log.Printf("Dialing SSH bastion %s...", sshHost) | |
| sshClient, err := ssh.Dial("tcp", net.JoinHostPort(sshHost, sshPort), sshConfig) | |
| if err != nil { | |
| log.Fatalf("Failed to dial SSH: %v", err) | |
| } | |
| defer sshClient.Close() | |
| log.Println("SSH connection established.") | |
| kubeconfigPath := filepath.Join(os.Getenv("HOME"), ".kube", "config") | |
| config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) | |
| if err != nil { | |
| log.Fatalf("Error loading kubeconfig: %v", err) | |
| } | |
| config.Dial = func(ctx context.Context, network, addr string) (net.Conn, error) { | |
| log.Println("Dialing through SSH tunnel to:", addr) | |
| return sshClient.Dial(network, addr) | |
| } | |
| clientset, err := kubernetes.NewForConfig(config) | |
| if err != nil { | |
| log.Fatalf("Error creating k8s client: %v", err) | |
| } | |
| log.Println("Fetching nodes via SSH tunnel...") | |
| nodes, err := clientset.CoreV1().Nodes().List(context.Background(), v1.ListOptions{}) | |
| if err != nil { | |
| log.Fatalf("Failed to list nodes: %v", err) | |
| } | |
| for _, node := range nodes.Items { | |
| log.Printf("Node: %s - Status: %s\n", node.Name, node.Status.Phase) | |
| log.Printf(" - Taints: %v\n", node.Spec.Taints) | |
| log.Printf(" - Labels: %v\n", node.Labels) | |
| } | |
| pods, err := clientset.CoreV1().Pods("").List(context.Background(), v1.ListOptions{}) | |
| if err != nil { | |
| log.Fatalf("Failed to list pods: %v", err) | |
| } | |
| for _, pod := range pods.Items { | |
| log.Printf("Pod: %s/%s - Status: %s\n", pod.Namespace, pod.Name, pod.Status.Phase) | |
| } | |
| } |
Author
dipankardas011
commented
Feb 8, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment