您现在的位置是:网站首页> 编程资料编程资料
从生成CRD到编写自定义控制器教程示例_Golang_
2023-05-26
368人已围观
简介 从生成CRD到编写自定义控制器教程示例_Golang_
介绍
我们可以使用code-generator 以及controller-tools来进行代码自动生成,通过代码自动生成可以帮我们自动生成 CRD 资源对象,以及客户端访问的 ClientSet、Informer、Lister 等工具包,接下来我们就来了解下如何编写一个自定义的控制器。
CRD定义
首先初始化项目:
$ mkdir operator-crd && cd operator-crd $ go mod init operator-crd $ mkdir -p pkg/apis/example.com/v1
在该文件夹下新建doc.go文件,内容如下所示:
// +k8s:deepcopy-gen=package // +groupName=example.com package v1
根据 CRD 的规范定义,这里我们定义的 group 为example.com,版本为v1,在顶部添加了一个代码自动生成的deepcopy-gen的 tag,为整个包中的类型生成深拷贝方法。
然后就是非常重要的资源对象的结构体定义,新建types.go文件,types.go内容可以使用type-scaffpld自动生成,具体文件内容如下:
package v1 import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // BarSpec defines the desired state of Bar type BarSpec struct { // INSERT ADDITIONAL SPEC FIELDS -- desired state of cluster DeploymentName string `json:"deploymentName"` Image string `json:"image"` Replicas *int32 `json:"replicas"` } // BarStatus defines the observed state of Bar. // It should always be reconstructable from the state of the cluster and/or outside world. type BarStatus struct { // INSERT ADDITIONAL STATUS FIELDS -- observed state of cluster } // 下面这个一定不能少,少了的话不能生成 lister 和 informer // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // Bar is the Schema for the bars API // +k8s:openapi-gen=true type Bar struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec BarSpec `json:"spec,omitempty"` Status BarStatus `json:"status,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // BarList contains a list of Bar type BarList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` Items []Bar `json:"items"` } 然后可以参考系统内置的资源对象,还需要提供 AddToScheme 与 Resource 两个变量供 client 注册,新建 register.go 文件,内容如下所示:
package v1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" ) // SchemeGroupVersion 注册自己的自定义资源 var SchemeGroupVersion = schema.GroupVersion{Group: "example.com", Version: "v1"} // 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 initializes a scheme builder SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) // AddToScheme is a global function that registers this API group & version to a scheme AddToScheme = SchemeBuilder.AddToScheme ) // Adds the list of known types to Scheme. func addKnownTypes(scheme *runtime.Scheme) error { // 添加 Bar 与 BarList这两个资源到 scheme scheme.AddKnownTypes(SchemeGroupVersion, &Bar{}, &BarList{}, ) metav1.AddToGroupVersion(scheme, SchemeGroupVersion) return nil } 使用controller-gen生成crd:
$ controller-gen crd paths=./... output:crd:dir=crd
生成example.com_bars.yaml文件如下所示:
--- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: (devel) creationTimestamp: null name: bars.example.com spec: group: example.com names: kind: Bar listKind: BarList plural: bars singular: bar scope: Namespaced versions: - name: v1 schema: openAPIV3Schema: description: Bar is the Schema for the bars API properties: apiVersion: description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' type: string kind: description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' type: string metadata: type: object spec: description: BarSpec defines the desired state of Bar properties: deploymentName: description: INSERT ADDITIONAL SPEC FIELDS -- desired state of cluster type: string image: type: string replicas: format: int32 type: integer required: - deploymentName - image - replicas type: object status: description: BarStatus defines the observed state of Bar. It should always be reconstructable from the state of the cluster and/or outside world. type: object type: object served: true storage: true
最终项目结构如下所示:
$ tree . ├── crd │ └── example.com_bars.yaml ├── go.mod ├── go.sum └── pkg └── apis └── example.com └── v1 ├── doc.go ├── register.go └── types.go 5 directories, 6 files
生成客户端相关代码
上面我们准备好资源的 API 资源类型后,就可以使用开始生成 CRD 资源的客户端使用的相关代码了。
首先创建生成代码的脚本,下面这些脚本均来源于sample-controller提供的示例:
$ mkdir hack && cd hack
在该目录下面新建 tools.go 文件,添加 code-generator 依赖,因为在没有代码使用 code-generator 时,go module 默认不会为我们依赖此包。文件内容如下所示:
// +build tools // 建立 tools.go 来依赖 code-generator // 因为在没有代码使用 code-generator 时,go module 默认不会为我们依赖此包. package tools import _ "k8s.io/code-generator"
然后新建 update-codegen.sh 脚本,用来配置代码生成的脚本:
#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} bash "${CODEGEN_PKG}"/generate-groups.sh "deepcopy,client,informer,lister" \ operator-crd/pkg/client operator-crd/pkg/apis example.com:v1 \ --output-base "${SCRIPT_ROOT}"/../ \ --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt # To use your own boilerplate text append: # --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt 同样还有 verify-codegen.sh 脚本,用来校验生成的代码是否是最新的:
#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. DIFFROOT="${SCRIPT_ROOT}/pkg" TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg" _tmp="${SCRIPT_ROOT}/_tmp" cleanup() { rm -rf "${_tmp}" } trap "cleanup" EXIT SIGINT cleanup mkdir -p "${TMP_DIFFROOT}" cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}" "${SCRIPT_ROOT}/hack/update-codegen.sh" echo "diffing ${DIFFROOT} against freshly generated codegen" ret=0 diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$? cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}" if [[ $ret -eq 0 ]] then echo "${DIFFROOT} up to date." else echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh" exit 1 fi 还有一个为生成的代码文件添加头部内容的 boilerplate.go.txt 文件,内容如下所示,其实就是为每个生成的代码文件头部添加上下面的开源协议声明:
/* Copyright 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. */
接下来我们就可以来执行代码生成的脚本了,首先将依赖包放置到 vendor 目录中去:
$ go mod vendor
然后执行脚本生成代码:
$ chmod +x ./hack/update-codegen.sh $./hack/update-codegen.sh Generating deepcopy funcs Generating clientset for example.com:v1 at operator-crd/pkg/client/clientset Generating listers for example.com:v1 at operator-crd/pkg/client/listers Generating informers for example.com:v1 at operator-crd/pkg/client/informers
代码生成后,整个项目的 pkg 包变成了下面的样子:
$ tree pkg pkg ├── apis │ └── example.com │ └── v1 │ ├── doc.go │ ├── register.go │ ├── types.go │ └── zz_generated.deepcopy.go └── client ├── clientset │ └── versioned │ ├── clientset.go │ ├── doc.go │ ├── fake │ │ ├── clientset_generated.go │ │ ├── doc.go │ │ └── register.go │ ├── scheme │ │ ├── doc.go │ │ └── register.go │ └── typed │ └── example.com │ └── v1 │ ├── bar.go │ ├── doc.go │ ├── example.com_client.go │ ├── fake │ │ ├── doc.go │ │ ├── fake_bar.go │ │ └── fake_example.com_client.go │ └── generated_expansion.go ├── informers │ └── externalversions │ ├── example.com │ │ ├── interface.go │ │ └── v1 │ │ ├── bar.go │ │
相关内容
- 自动生成代码controller tool的简单使用_Golang_
- go语言代码生成器code generator使用示例介绍_Golang_
- golang读取各种配置文件(ini、json、yaml)_Golang_
- Go语言利用Unmarshal解析json字符串的实现_Golang_
- 300行代码实现go语言即时通讯聊天室_Golang_
- go语言中如何使用select的实现示例_Golang_
- Go语言上下文context底层原理_Golang_
- Golang map实践及实现原理解析_Golang_
- go中import包的大坑解决方案_Golang_
- windows下使用GoLand生成proto文件的方法步骤_Golang_
点击排行
本栏推荐
