三大核心数据结构是什么?
Group(APIGroup) 资源组
Version(APIVersions) 资源版本
Resource/SubResource(APIResource) 资源/子资源
Kind 资源种类,描述Resource的种类
\
完整表现形式:资源组/资源版本/资源/子资源
<group>/<version>/<resource>/<subresource>
apps/v1/deployments/status
三大核心数据结构如何定义
以github当前最新版本Kubernetes v1.23.3为例
Group资源组
代码:vendor/k8s.io/apimachinery/pkg/apis/meta/v1/types.go
// APIGroup contains the name, the supported versions, and the preferred version
// of a group.
type APIGroup struct {
TypeMeta `json:",inline"`
// name is the name of the group.
Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
// versions are the versions supported in this group.
Versions []GroupVersionForDiscovery `json:"versions" protobuf:"bytes,2,rep,name=versions"`
// preferredVersion is the version preferred by the API server, which
// probably is the storage version.
// +optional
PreferredVersion GroupVersionForDiscovery `json:"preferredVersion,omitempty" protobuf:"bytes,3,opt,name=preferredVersion"`
// a map of client CIDR to server address that is serving this group.
// This is to help clients reach servers in the most network-efficient way possible.
// Clients can use the appropriate server address as per the CIDR that they match.
// In case of multiple matches, clients should use the longest matching CIDR.
// The server returns only those CIDRs that it thinks that the client can match.
// For example: the master will return an internal IP CIDR only, if the client reaches the server using an internal IP.
// Server looks at X-Forwarded-For header or X-Real-Ip header or request.RemoteAddr (in that order) to get the client IP.
// +optional
ServerAddressByClientCIDRs []ServerAddressByClientCIDR `json:"serverAddressByClientCIDRs,omitempty" protobuf:"bytes,4,rep,name=serverAddressByClientCIDRs"`
}
资源版本:Version
代码:vendor/k8s.io/apimachinery/pkg/apis/meta/v1/types.go
Alpha,第一阶段,内部测试,v1alpha1、v1alpha2、v2alpha1
Beta ,第二阶段,修复很不不完善的地方,仍可能有缺陷,v1beta1、v1beta2、v2beta1
Stable ,第三阶段,达到一定成熟度,可稳定运行,v1,v2,v3
// APIVersions lists the versions that are available, to allow clients to
// discover the API at /api, which is the root path of the legacy v1 API.
//
// +protobuf.options.(gogoproto.goproto_stringer)=false
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type APIVersions struct {
TypeMeta `json:",inline"`
// versions are the api versions that are available.
Versions []string `json:"versions" protobuf:"bytes,1,rep,name=versions"`
// a map of client CIDR to server address that is serving this group.
// This is to help clients reach servers in the most network-efficient way possible.
// Clients can use the appropriate server address as per the CIDR that they match.
// In case of multiple matches, clients should use the longest matching CIDR.
// The server returns only those CIDRs that it thinks that the client can match.
// For example: the master will return an internal IP CIDR only, if the client reaches the server using an internal IP.
// Server looks at X-Forwarded-For header or X-Real-Ip header or request.RemoteAddr (in that order) to get the client IP.
ServerAddressByClientCIDRs []ServerAddressByClientCIDR `json:"serverAddressByClientCIDRs" protobuf:"bytes,2,rep,name=serverAddressByClientCIDRs"`
}
资源 Resource
一个资源被实例化后被称为资源对象(Resource object,由资源组+资源版本+资源种类组成<group>/<version>,Kind<kind>,例如deployment的资源对象是:apps/v1,Kind=deployment),所有资源对象都是一个实体(Entity),k8s用Entity表示当前状态,可以通过k8s Apiserver查询和更新实体,分为:
持久性实体:Persistent Entity (例如:Deployment,异常退出后会被重新创建)
短暂性实体:Ephemeral Entity (例如:pod,异常退出后不会重新创建)
代码:k8s.io/apimachinery/pkg/apis/meta/v1/types.go
// APIResource specifies the name of a resource and whether it is namespaced.
type APIResource struct {
// name is the plural name of the resource.
Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
// singularName is the singular name of the resource. This allows clients to handle plural and singular opaquely.
// The singularName is more correct for reporting status on a single item and both singular and plural are allowed
// from the kubectl CLI interface.
SingularName string `json:"singularName" protobuf:"bytes,6,opt,name=singularName"`
// namespaced indicates if a resource is namespaced or not.
Namespaced bool `json:"namespaced" protobuf:"varint,2,opt,name=namespaced"`
// group is the preferred group of the resource. Empty implies the group of the containing resource list.
// For subresources, this may have a different value, for example: Scale".
Group string `json:"group,omitempty" protobuf:"bytes,8,opt,name=group"`
// version is the preferred version of the resource. Empty implies the version of the containing resource list
// For subresources, this may have a different value, for example: v1 (while inside a v1beta1 version of the core resource's group)".
Version string `json:"version,omitempty" protobuf:"bytes,9,opt,name=version"`
// kind is the kind for the resource (e.g. 'Foo' is the kind for a resource 'foo')
Kind string `json:"kind" protobuf:"bytes,3,opt,name=kind"`
// verbs is a list of supported kube verbs (this includes get, list, watch, create,
// update, patch, delete, deletecollection, and proxy)
Verbs Verbs `json:"verbs" protobuf:"bytes,4,opt,name=verbs"`
// shortNames is a list of suggested short names of the resource.
ShortNames []string `json:"shortNames,omitempty" protobuf:"bytes,5,rep,name=shortNames"`
// categories is a list of the grouped resources this resource belongs to (e.g. 'all')
Categories []string `json:"categories,omitempty" protobuf:"bytes,7,rep,name=categories"`
// The hash value of the storage version, the version this resource is
// converted to when written to the data store. Value must be treated
// as opaque by clients. Only equality comparison on the value is valid.
// This is an alpha feature and may change or be removed in the future.
// The field is populated by the apiserver only if the
// StorageVersionHash feature gate is enabled.
// This field will remain optional even if it graduates.
// +optional
StorageVersionHash string `json:"storageVersionHash,omitempty" protobuf:"bytes,10,opt,name=storageVersionHash"`
}
资源版本分为:外部版本和内部版本
外部版本(External Version,例如:Deployment展示:apps/v1)
内部版本(Internal Version,Deployment展示未:apps/__internal)
\
资源版本和内外部版本有关系吗?
有!有版本号的,属于外部版本(例如:apps/v1/deployments),没有版本号的,拥有runtime.APIVersionInternal(internal)标识的属于内部版本
资源对象分为:外部版本资源对象和内部版本资源对象
External Object 外部版本资源对象(Versioned Object,拥有资源版本的资源对象),用户通过yaml和json创建资源对象时,所使用的是外部资源对象,外部资源对象通过Alpha、Beta、Stable标识
Internal Object 内部版本资源对象,通过runtime.APIVersionInternal(_internal)标识,不对外暴露,内部使用,主要用于资源版本的转换,例如将v1beta1转为v1版本时候,顺序是先转为内部版本(internal),再转为v1版本,v1beta1→internal→v1
以pod资源为例: pod外部版本(External Version)
代码:vendor/k8s.io/api/core/v1/types.go (有版本号,有json tag和proto tag,用于序列化和反序列化操作)
type Pod struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
tions.md#spec-and-status
// +optional
Spec PodSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
Status PodStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
pod内部版本(Internal Version)定义:
pkg/apis/core/types.go (没有json tag)
type Pod struct {
metav1.TypeMeta
metav1.ObjectMeta
Spec PodSpec
Status PodStatus
}
Resource支持的两种资源
内置资源(Kubernetes Resource)和自定义资源(Custom Resource)
资源支持的8种操作方法
create、delete、deletecollection、get、list、patch、update、watch
资源的数据结构
GVR:Group Version Resource 资源组名称或者资源版本及资源名称
GV:Group Version 资源组以及资源版本
GVS:Group Versions 资源组内多个资源版本
GVK:Group VersionKind 资源组或者资源版本以及资源种类
GK:Group Kind 资源组或资源种类
GR:Group Resource 资源组或者资源
源码:k8s.io/apimachinery/pkg/runtime/schema/group_version.go
type GroupVersionResource struct {
Group string
Version string
Resource string
}