亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

缺少測試用戶的權限

缺少測試用戶的權限

Go
揚帆大魚 2022-05-18 09:44:52
我使用官方 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貢獻1963條經驗 獲得超6個贊

您可以使用以下命令檢查服務帳戶的權限:

kubectl auth can-i list services --namespace t1k --as=system:serviceaccount:t1k:t1k-test-serviceaccount

您不需要手動設置令牌...您可以InClusterConfig像本示例中那樣使用。Client-go 使用掛載在 Pod 內的服務帳戶令牌,位于 /var/run/secrets/kubernetes.io/serviceaccount 路徑時使用了 rest.InClusterConfig()。


查看完整回答
反對 回復 2022-05-18
?
慕森卡

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())

    ....

}


查看完整回答
反對 回復 2022-05-18
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號