首页 > 其他分享 >Schema

Schema

时间:2023-01-01 11:35:11浏览次数:47  
标签:return DeepCopyInto Schema Deployment k8s AddToScheme out

资源类型 Schema

在 /staging/src/k8s.io/api/apps/v1/types.go 中定义了所有的资源类型。

// Deployment enables declarative updates for Pods and ReplicaSets.
type Deployment struct {
    metav1.TypeMeta `json:",inline"`
    // Standard object's metadata.
    // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
    // +optional
    metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

    // Specification of the desired behavior of the Deployment.
    // +optional
    Spec DeploymentSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

    // Most recently observed status of the Deployment.
    // +optional
    Status DeploymentStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

这些资源类型都实现了一个接口:

type Object interface {
    GetObjectKind() schema.ObjectKind
    DeepCopyObject() Object
}

比如 Deployment struct 中的 TypeMeta 已经实现了 GetObjectKind() ,而自动化工具已经生成了实现 DeepCopyObject() 的代码。

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Deployment) DeepCopyInto(out *Deployment) {
    *out = *in
    out.TypeMeta = in.TypeMeta
    in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
    in.Spec.DeepCopyInto(&out.Spec)
    in.Status.DeepCopyInto(&out.Status)
    return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Deployment.
func (in *Deployment) DeepCopy() *Deployment {
    if in == nil {
        return nil
    }
    out := new(Deployment)
    in.DeepCopyInto(out)
    return out
}

// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Deployment) DeepCopyObject() runtime.Object {
    if c := in.DeepCopy(); c != nil {
        return c
    }
    return nil
}

AddToSchema 将这些定义的资源对象注册到 Schema 中。client-go 创建一个全局的 Schema,将这些定义的资源对象全部添加进入。

// AddToScheme adds all types of this clientset into the given scheme. This allows composition
// of clientsets, like in:
//
//    import (
//      "k8s.io/client-go/kubernetes"
//      clientsetscheme "k8s.io/client-go/kubernetes/scheme"
//      aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme"
//    )
//
//    kclientset, _ := kubernetes.NewForConfig(c)
//    _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
//
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
// correctly.
var AddToScheme = localSchemeBuilder.AddToScheme

func init() {
    v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
    utilruntime.Must(AddToScheme(Scheme))
}

标签:return,DeepCopyInto,Schema,Deployment,k8s,AddToScheme,out
From: https://www.cnblogs.com/geraldkohn/p/17017880.html

相关文章