Hello Mudit Surana
Welcome to Microsoft Q&A Platform, thanks for posting your query here.
I am not much familiar with Golang, just wanted to share few pointers that can help with your ask:
If you need to connect to an AKS cluster from outside the cluster without using any plugins, you can use the Azure Active Directory (AAD) authentication method. This involves acquiring a token from Azure AD and using it to authenticate requests to the AKS cluster.
Here is an example code snippet that shows how to do this:
import (
"context"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/profiles/latest/containerservice/mgmt/containerservice"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func main() {
// Get the AKS cluster credentials using Azure Active Directory authentication
authorizer, err := auth.NewAuthorizerFromEnvironment()
if err != nil {
panic(err.Error())
}
subscriptionID := "<your-subscription-id>"
resourceGroupName := "<your-resource-group-name>"
clusterName := "<your-aks-cluster-name>"
client := containerservice.NewManagedClustersClient(subscriptionID)
client.Authorizer = authorizer
credentials, err := client.ListClusterAdminCredentials(context.Background(), resourceGroupName, clusterName)
if err != nil {
panic(err.Error())
}
kubeconfig := string(credentials.Kubeconfigs[0].Value)
// Create a Kubernetes clientset
config, err := rest.InClusterConfig()
if err != nil {
config, err = clientcmd.NewClientConfigFromBytes([]byte(kubeconfig)).ClientConfig()
if err != nil {
panic(err.Error())
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// <span class=" active-doc-0" data-doc-items="0">List all the namespaces present in the AKS cluster[1](#doc-pos=0)</span>
namespaces, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
// Print the name of each namespace
for _, ns := range namespaces.Items {
fmt.Println(ns.Name)
}
}
This code first uses the Azure SDK for Go to authenticate with the AKS cluster using Azure Active Directory authentication and get the cluster credentials. Then it creates a Kubernetes clientset using the credentials and lists all the namespaces present in the AKS cluster.
Hope this helps.