我正在尝试编写一个 Go 程序来从集群中获取 pod 日志。我正在使用 AKS kubernetes 集群。如何访问脚本中的 kubeconfig 文件?以下是我的代码:
package main
import (
"context"
"flag"
"fmt"
"time"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
//"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
//
// Uncomment to load all auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
func main() {
/*// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}*/
fmt.Printf("Creating cluster config")
kubePtr := flag.Bool("use-kubeconfig", false, "use kubeconfig on local system")
flag.Parse()
fmt.Printf("Updating the existing config")
var kubeconfig string
if *kubePtr == true {
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config")
} else {
kubeconfig = ""
}
fmt.Printf("Building config from flags")
config, err := clientcmd.BuildConfigFromKubeconfigGetter("", kubeconfig)
fmt.Printf("creating the clientset")
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
for {
// get pods in all the namespaces by omitting namespace
// Or specify namespace to get pods in particular namespace
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
慕村225694
胡说叔叔
相关分类