k8s之CRD--为自定义资源生成代码
CRD简介和使用姿势CustomResourceDefinition(CRD)是 v1.7 + 新增的无需改变代码就可以扩展 Kubernetes API 的机制,用来管理自定义对象。它实际上是 ThirdPartyResources(TPR) 的升级版本,而 TPR 已经在 v1.8 中删除。 一些使用场景:
其实crd在很多k8s周边开源项目中有使用,比如ingress-controller和众多的operator。 CRD 控制器在使用 CRD 扩展 Kubernetes API 时,通常还需要实现一个新建资源的控制器,监听改资源的变化情况,并作进一步的处理。官方提供的示例项目sample-controller。
编写crd controller之前,一定要使用k8s官方提供的代码生成工具k8s.io/code-generator 去生成 client,informers,listers and deep-copy函数.不仅代码风格符合k8s,而且减少出错和减少工作量都是有很大的帮助。 在项目中使用代码生成器下面展示了代码生成工具是如何工作的以及如何使用最少的代码行将它们应用到自己的项目中,从而为您生成 deepcopy 函数/typed clients/listers/informer,所有这些的生成仅需要一个 shell 脚本调用和部分代码注释。 #!/usr/bin/env bash # Copyright 2017 The Kubernetes Authors. # # Licensed under the Apache License,Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing,software # distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -o errexit set -o nounset set -o pipefail SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/.. CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} # generate the code with: # --output-base because this script should also be able to run inside the vendor dir of # k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir # instead of the $GOPATH directly. For normal projects this can be dropped. ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" k8s.io/canary-controller/pkg/client k8s.io/canary-controller/pkg/apis canarycontroller:v1alpha1 --output-base "$(dirname ${BASH_SOURCE})/../../.." --go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt # To use your own boilerplate text use: # --go-header-file ${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt update-codegen脚本将自动生成下面的文件和路径
脚本运行后大体会建立如下的包管理结构: /* Copyright 2017 The Kubernetes Authors. Licensed under the Apache License,Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // Canary is a specification for a Foo resource type Canary struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec CanarySpec `json:"spec"` Status CanaryStatus `json:"status"` } // CanarySpec is the spec for a Foo resource type CanarySpec struct { DeploymentName string `json:"deploymentName"` Replicas *int32 `json:"replicas"` } // CanaryStatus is the status for a Foo resource type CanaryStatus struct { AvailableReplicas int32 `json:"availableReplicas"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // CanaryList is a list of Foo resources type CanaryList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata"` Items []Canary `json:"items"` } registry.go /* Copyright 2017 The Kubernetes Authors. Licensed under the Apache License,either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" canarycontroller "k8s.io/canary-controller/pkg/apis/canarycontroller" ) // SchemeGroupVersion is group version used to register these objects var SchemeGroupVersion = schema.GroupVersion{Group: canarycontroller.GroupName,Version: "v1alpha1"} // Kind takes an unqualified kind and returns back a Group qualified GroupKind func Kind(kind string) schema.GroupKind { return SchemeGroupVersion.WithKind(kind).GroupKind() } // Resource takes an unqualified resource and returns a Group qualified GroupResource func Resource(resource string) schema.GroupResource { return SchemeGroupVersion.WithResource(resource).GroupResource() } var ( SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) AddToScheme = SchemeBuilder.AddToScheme ) // Adds the list of known types to Scheme. func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion,&Canary{},&CanaryList{},) metav1.AddToGroupVersion(scheme,SchemeGroupVersion) return nil } 更多相关细节见 Kubernetes Deep Dive: Code Generation for CustomResources给出了具体的步骤和关于tag的标注。 总结基于crd以及crd controller可以抽象很多业务场景。接下我司准备实现一个部署策略相关的项目。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Running standalone Groovy scripts
- delphi – TActionMainMenuBar和TActionToolbar丢失设置
- 在golang从html创建pdf
- delphi 不使用第三方变量对整数变量进行互换
- [转]使用余弦定理计算两篇文章的相似性
- perl – 使用Win32 :: API调用OpenComPort
- 一个密码输入控件,能防止大多数查看*号密码内容的软件取密码
- Perl: 什么时候需要 close 文件句柄?
- 控件之间的Delphi XE2和LiveBindings
- Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TP