深入理解 Kubernetes 资源操作与常见类型
1. 引用对象的 UID 获取
要获取引用对象的 UID,需通过 Kubernetes API 的 get 或 list 请求来获取对象。
2. 设置 APIVersion 和 Kind
使用 Client - go 库时,APIVersion 和 Kind 不会自动设置,需要手动设置。以下是两种解决方案:
- 方案一 :在引用对象上设置 APIVersion 和 Kind,然后复制对象信息。
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Get the object to reference
pod, err := clientset.CoreV1().Pods(myns).
Get(context.TODO(), mypodname, metav1.GetOptions{})
if err != nil {
return err
}
// set the APIVersion and Kind of the Pod
// then copy all information from the pod
pod.SetGroupVersionKind(
corev1.SchemeGroupVersion.WithKind("Pod"),
)
ownerRef := metav1.OwnerReference{
APIVersion:
超级会员免费看
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



