6、深入理解 Kubernetes 资源操作与常见类型

深入理解 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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值