client-go使用技巧

Pod使用spec.serviceAccountName对应的token

import "k8s.io/client-go/rest"

cfg, err := rest.InClusterConfig()
if err != nil {
	klog.Fatalf("Error building kubeconfig: %s", err.Error())
}

list/watch指定namespace

informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0, informers.WithNamespace("default"))

list/watch指定fieldSelector(cr无法使用fieldSelector来指定spec.xxx)

informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0, informers.WithTweakListOptions(
	func(options *metav1.ListOptions) {
		options.FieldSelector = "metadata.name=test"
	}))

支持参数

metadata.name
metadata.namespace
spec.nodeName
spec.restartPolicy
spec.schedulerName
spec.serviceAccountName
status.phase
status.podIP
status.podIPs
status.nominatedNodeName

list/watch指定labelSelector

informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0, informers.WithTweakListOptions(
	func(options *metav1.ListOptions) {
		options.LabelSelector = "app=test"
	}))

判断client-go执行报错

k8s.io/apimachinery/pkg/api/errors

 

posted on 2024-03-13 08:51  王景迁  阅读(55)  评论(0)    收藏  举报

导航