Skip to content

LJTian/operator-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

operator-demo

kubebuilder 演示demo

目录

  • 1-下载kuberbuilder
  • 2-创建目录
  • 3-初始话项目
  • 4-API模型及控制器
  • 5-添加证书服务器
  • 6-添加webhook
  • 7-打包测试

下载kuberbuilder

安装执行脚本

curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)
chmod +x kubebuilder && mv kubebuilder /usr/local/bin/

创建目录

mkdir <PROJECT_DIR> && cd <PROJECT_DIR>

初始化项目-模型及控制器

一键初始化

kubebuilder init --domain ljtian.com --repo ljtian.com/mydaemonset

注意:国内执行时命令会拉取代码出现超时情况,请先配置好代理。出现超时情况且没有生成 PROJECT 文件情况,请删除重新执行。

检查项目信息

cat PROJECT

输出内容

# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: ljtian.com
layout:
- go.kubebuilder.io/v3
projectName: operator-demo
repo: ljtian.com/mydaemonset
version: "3"

API模型及控制器

kubebuilder create api --group apps --version v1 --kind MyDaemonset

交互输入:

Create Resource [y/n] 
y
Create Controller [y/n]
y

输出内容:

Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
api/v1/mydaemonset_types.go
controllers/mydaemonset_controller.go
Update dependencies:
$ go mod tidy
Running make:
$ make generate
mkdir -p /home/ljtian/file/git/operator-demo/bin
test -s /home/ljtian/file/git/operator-demo/bin/controller-gen && /home/ljtian/file/git/operator-demo/bin/controller-gen --version | grep -q v0.11.1 || \
GOBIN=/home/ljtian/file/git/operator-demo/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1
/home/ljtian/file/git/operator-demo/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests

修改API模型及控制器

API 文件为:api/v1/mydaemonset_types.go

// MyDaemonsetSpec defines the desired state of MyDaemonset
type MyDaemonsetSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	// Foo is an example field of MyDaemonset. Edit mydaemonset_types.go to remove/update
	Image string `json:"image,omitempty"`  // 修改的内容将 foo 字段替换成了 Image
}

// MyDaemonsetStatus defines the observed state of MyDaemonset
type MyDaemonsetStatus struct {
	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
	// Important: Run "make" to regenerate code after modifying this file
}

控制器文件为:controllers/mydaemonset_controller.go

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the MyDaemonset object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.1/pkg/reconcile
func (r *MyDaemonsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	_ = log.FromContext(ctx)

	// TODO(user): your logic here
	

	return ctrl.Result{}, nil
}

About

kubebuilder 演示demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published