kubebuilder+code-generator 无坑指南教程

柴飞星
2023-12-01

网上教程不全+过时,踩了很多坑才搞定

环境:

macos amd64
kubebuilder version3.7.0
code-generator 0.25.0

教程:

两步走,先搭kubebuilder再code-generator

环境搭建:

创建GoPath,在src下创建路径,路径要和kubebuilder init 中的--repo参数一致!!这是第一个坑,不然后面生成的代码引用有问题,我这里创建的结果是$GOPATH/git.xxx.com/libra

一、kubebuilder初始化项目

在上面创建好的路径下执行:
kubebuilder init --domain xxx.com --repo git.xxx.com/libra
kubebuilder create api --group libra --version v1alpha1 --kind Podgroup

二、code-generator生成代码

上面就初始化好项目了,接下来要做代码生成:

  1. 将上面生成的api/v1alpha1改变为api/podgroup/v1alpha1,podgroup替换为你的kind类型即可;
  2. 执行go mod tidy,go mod vendor
  3. 在api/podgroup下创建doc.go,内容为:
// +k8s:deepcopy-gen=package
  
// Package v1 is the v1alpha1 version of the API.
// +groupName=example.my.domain
package v1

再创建register.go ,内容为:

package v1
  
import (
    "k8s.io/apimachinery/pkg/runtime/schema"
)
  
// SchemeGroupVersion is group version used to register these objects.
var SchemeGroupVersion = GroupVersion
  
// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
    return SchemeGroupVersion.WithResource(resource).GroupResource()
}

改 api/v1/{crd}_types.go 文件,添加注释 // +genclient:

[root@kubebuilder example]# cat api/podgroup/v1/guestbook_types.go
...
// +genclient
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
 
// Guestbook is the Schema for the guestbooks API
type Guestbook struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`
 
	Spec   GuestbookSpec   `json:"spec,omitempty"`
	Status GuestbookStatus `json:"status,omitempty"`
}
...
  1. 编写脚本:
    项目根目录下创建hack目录,里面创建update-code.sh,内容为:
#!/usr/bin/env bash
#表示有报错即退出 跟set -e含义一样
set -o errexit
#执行脚本的时候,如果遇到不存在的变量,Bash 默认忽略它 ,跟 set -u含义一样
set -o nounset
# 只要一个子命令失败,整个管道命令就失败,脚本就会终止执行
set -o pipefail

#kubebuilder项目的MODULE
MODULE=//你的包名,或者你的Gopath/src目录下的代码路径,看环境准备那里说的!!

#api包
APIS_PKG=api

#代码生出输出,生成Resource时指定的group一样
OUTPUT_PKG=generated/libra

# group-version such as cronjob:v1
GROUP=libra
VERSION=v1alpha1
GROUP_VERSION=$GROUP:$VERSION

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)}

# kubebuilder2.3.2版本生成的api目录结构code-generator无法直接使用
# rm -rf "${APIS_PKG}/${GROUP}" && mkdir -p "${APIS_PKG}/${GROUP}" && cp -r "${APIS_PKG}/${VERSION}/" "${APIS_PKG}/${GROUP}"

# 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.
#client,informer,lister(注意: code-generator 生成的deepcopy不适配 kubebuilder 所生成的api)
bash "${CODEGEN_PKG}"/generate-groups.sh "client,informer,lister" \
  ${MODULE}/${OUTPUT_PKG} \
  ${MODULE}/${APIS_PKG} \
  ${GROUP_VERSION} \
  --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt \
  --output-base "${SCRIPT_ROOT}/../../.." \
  -v 10
#--output-base "${SCRIPT_ROOT}" \
#  --output-base git.iflytek.com/AIaaS/Libra \

参考:

  1. https://blog.csdn.net/cbmljs/article/details/122929593
  2. https://www.cnblogs.com/Cylon/p/16394839.html
  3. https://cloud.tencent.com/developer/article/1656317
 类似资料: