To get the Kubernetes version of a node using Go, you can use the Kubernetes Client-Go library. The version of the node is stored in the node’s status, specifically in the NodeInfo field, which contains information about the node, such as the Kubelet version (the version of Kubernetes running on that node).
Steps to Retrieve Node Version in Go
- Install Client-Go: Ensure you have the necessary Go packages installed for Kubernetes interaction.
- Set Up Kubernetes Client: Authenticate with the cluster using either the In-Cluster configuration or the Kubeconfig file.
- Query Node Version: Fetch the list of nodes and extract the Kubelet version.
Step 1: Install Kubernetes Client-Go
Install the necessary Go packages for Kubernetes:
go get k8s.io/client-go@v0.26.1 # Adjust version as needed
go get k8s.io/client-go/tools/clientcmd
go get k8s.io/apimachinery/pkg/apis/meta/v1
Step 2: Initialize the Kubernetes Client
Here, the code will try to use the In-Cluster configuration if it’s running inside the Kubernetes cluster. Otherwise, it will default to using the local Kubeconfig file.
Step 3: Retrieve the Node’s Kubelet Version
The Node.Status.NodeInfo.KubeletVersion field contains the version of the Kubelet running on each node.
Here is an example of how to get the version of each node in the cluster:
package main
import (
"context"
"fmt"
"log"
"os"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var config *rest.Config
var err error
// Use InClusterConfig if running inside Kubernetes, otherwise use kubeconfig
if config, err = rest.InClusterConfig(); err != nil {
kubeconfig := os.Getenv("KUBECONFIG")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatalf("Error building kubeconfig: %v", err)
}
}
// Create a new Kubernetes client
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create Kubernetes client: %v", err)
}
// Get a list of nodes in the cluster
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
if err != nil {
log.Fatalf("Error fetching nodes: %v", err)
}
// Iterate over the nodes and print their Kubelet version
for _, node := range nodes.Items {
fmt.Printf("Node Name: %s\n", node.Name)
fmt.Printf(" Kubelet Version: %s\n", node.Status.NodeInfo.KubeletVersion)
fmt.Println("----------------------------")
}
}
Explanation:
- Cluster Configuration: The program attempts to use the In-Cluster configuration if it’s running inside the Kubernetes cluster. If it’s running locally, it uses the Kubeconfig file to authenticate with the cluster.
- Client Initialization: The Kubernetes client is created using
kubernetes.NewForConfig(), allowing it to interact with the Kubernetes API. - Fetch Node Information: The program fetches a list of all nodes using
clientset.CoreV1().Nodes().List(). For each node, it retrieves theNodeInfowhich contains theKubeletVersion. - Kubelet Version: The
KubeletVersionfield undernode.Status.NodeInfoholds the version of Kubernetes that is running on the node.
Example Output:
Node Name: gke-cluster-1-default-pool-xyz
Kubelet Version: v1.23.5-gke.200
----------------------------
Node Name: gke-cluster-1-default-pool-abc
Kubelet Version: v1.23.5-gke.200
----------------------------
This program will print the name of each node along with its Kubelet version, which gives you the Kubernetes version running on that node.
