我使用官方 golang kubernetes lib 支持的 watcherList 來獲取有關 kubernetes 命名空間內創建、更新和刪除服務的通知。這里是片段。func (kc *KubernetesCollector) streamEvents(ctx context.Context) { kc.debugChannel <- fmt.Sprintf("Start streaming events from kubernetes API") watchList := cache.NewListWatchFromClient(kc.k8sClient.RESTClient(), "services", kc.k8sNamespace, fields.Everything()) notificationCallbackToAddService := func(svc interface{}) { service := svc.(*v1.Service) kc.serviceNotificationChannel <- &serviceNotification{service, "add"} } notificationCallbackToDeleteService := func(svc interface{}) { service := svc.(*v1.Service) kc.serviceNotificationChannel <- &serviceNotification{service, "remove"} } callbacks := cache.ResourceEventHandlerFuncs{ AddFunc: notificationCallbackToAddService, DeleteFunc: notificationCallbackToDeleteService, } _, controller := cache.NewInformer(watchList, &v1.Service{}, time.Second*0, callbacks) go controller.Run(ctx.Done())}在我的測試中,我聲明了kc.k8sClient在k8sAPI變量中定義的公共 api 地址。此外,我將承載令牌設置為針對集群進行身份驗證并跳過驗證不安全的 ssl 證書。func TestK8sWatchList(t *testing.T) { require := require.New(t) ... k8sConfig, err := clientcmd.BuildConfigFromFlags(k8sAPI, "") require.NoError(err) k8sConfig.BearerToken = "<bearerToken>" k8sConfig.Transport = &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, } k8sClient, err := kubernetes.NewForConfig(k8sConfig) k8sCollector := NewK8sCollector(k8sClient, k8sNamespace) ...}我不明白為什么會收到錯誤消息,因為我認為服務帳戶“t1k-test-serviceaccount”具有所有必需的權限?,F在為測試用戶定義了服務帳戶、角色和角色綁定。
2 回答

慕森卡
TA貢獻1806條經驗 獲得超8個贊
我找到了解決方案。結構的k8sClientSet屬性KubernetesCollector是一個指針。包的反射函數pkg/mod/k8s.io/[email protected]不能處理指針對象。
type KubernetesCollector struct {
...
k8sClient *kubernetes.ClientSet
namespace string
...
}
CoreV1Interface我用from替換了 k8sClient k8s.io/client-go/kubernetes/typed/core/v1。因此,我更改了對 ListWatch 的調用。
type KubernetesCollector struct {
....
iface corev1.CoreV1Interface
namespace string
....
}
func (kc *KubernetesCollector) start(ctx context.Context) {
watchList := cache.NewListWatchFromClient(kc.iface.RESTClient(), "services", kc.namespace, fields.Everything())
....
}
- 2 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消