我一直在尝试检索 pod 详细信息并将这些详细信息填充到 struct 的字段中。但是,在将详细信息填充到结构中时,我确实遇到了错误。
package main
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// corev1"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type PodsList struct{
podNamespace string
podName string
}
func main() {
var targetPods []TargetPodsList
config, inClusterConfigError := rest.InClusterConfig()
if inClusterConfigError != nil {
fmt.Println("Retrieval of K8s InCluster Config not successful")
log.Fatal(inClusterConfigError)
}
// Set k8s client configuration
clientset, clientSetError := kubernetes.NewForConfig(config)
if clientSetError != nil {
fmt.Println("K8s Client for Incluster Configuration not successful")
log.Fatal(clientSetError)
}
// Retrieve Pods for all target K8s nodes
for index := range targetNodes.TargetNodes {
targetPodsList, _ := clientset.CoreV1().Pods("").List(metav1.ListOptions{
FieldSelector: "spec.nodeName=" + someNodeName)
for podIndex := range targetPodsList.Items {
targetPods.podNamespace = targetPods[podIndex].podNamespace
targetPods.podName = targetPods[podIndex].podName
}
}
}
我知道这是我可能缺少的基本东西。
请建议并更正我的代码。
提前致谢。