K8s CRD开发

k8s CRD开发

  • 开发前置环境需求
    • kubebuilder
    • kustomize
    • k8s
    • kubectl
    • golang
    • docker

创建crd

1
2
kubebuilder init --domain qipajun.com --license apache2 --owner "alex"
kubebuilder create api --group infra --version v1 --kind VirtulMachine
  • 安装启动crd
1
2
# k8s创建资源
make install
1
2
3
4
# 查看资源
➜ kubectl get crd
NAME CREATED AT
virtulmachines.infra.qipajun.com 2020-04-07T12:58:51Z
1
2
# 运行项目
make run
1
2
# 创建k8s crd资源
kubectl apply -f config/samples/
  • 部署controller到集群中
    1
    2
    3
    4
    # 构建镜像
    make docker-build docker-push IMG=liuhaogui/crd-demo-infra-controller:v0.0.1
    # 部署到集群
    make deploy IMG=liuhaogui/crd-demo-infra-controller:v0.0.1
1
2
3
4
5
6
# 查看deploy
➜ kubectl get po -A |grep crd-demo
crd-demo-system crd-demo-controller-manager-6bfc589b88-hbzxj 2/2 Running 3 100s
➜ kubectl get deploy -n crd-demo-system
NAME READY UP-TO-DATE AVAILABLE AGE
crd-demo-controller-manager 1/1 1 1 4m36s

开发需求

配置参数读取
  • 结构体参数定义api/v1/virtulmachine_types.go
1
2
3
4
5
6
7
8
9
10
type VirtulMachineSpec 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 VirtulMachine. Edit VirtulMachine_types.go to remove/update
Foo string `json:"foo,omitempty"`
Cpu string `json:"cpu,omitempty"`
Memory string `json:"memory,omitempty"`
// 定义所需参数
}
  • 配置文件yaml设置参数 config/samples
1
2
3
4
5
6
7
8
9
apiVersion: infra.qipajun.com/v1
kind: VirtulMachine
metadata:
name: virtulmachine-sample
spec:
# Add fields here
foo: bar
cpu: "1"
memory: "1G"
  • 实现相关事件处理 : Reconcile接口负责实现controller事件监听处理
1
2
3
4
5
6
7
8
9
10
11
12
13
func (r *VirtulMachineReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("virtulmachine", req.NamespacedName)

vm := &infrav1.VirtulMachine{}
if err := r.Get(ctx, req.NamespacedName, vm); err != nil {
log.Error(err, "unable to fetch vm info ")
} else {
log.Info("[vm info] ", vm.Spec.Cpu, vm.Spec.Memory)
}

return ctrl.Result{}, nil
}
  • 更新、构建运行
1
2
kubectl apply -f config/samples/
make && make install && make run
1
2
3
4
# 启动后打印配置参数信息
...
2020-04-07T21:58:02.333+0800 INFO controllers.VirtulMachine [vm info] {"virtulmachine": "default/virtulmachine-sample", "1": "1G"}
...
状态更新
  • 定义状态参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type VirtulMachineStatus struct {
Status string `json:"status"`
}

// +kubebuilder:subresource:status # 加build参数
// +kubebuilder:object:root=true

// VirtulMachine is the Schema for the virtulmachines API
type VirtulMachine struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec VirtulMachineSpec `json:"spec,omitempty"`
Status VirtulMachineStatus `json:"status,omitempty"`
}

  • controller对状态做变动
1
2
3
4
5
6
7
8
9
10
11
12
13
vm.Status.Status = "Running"
if err := r.Status().Update(ctx, vm); err != nil {
log.Error(err, "unable to update vm status")
} else {
log.Info("update status success ,vm status :", vm.Status.Status)
}

time.Sleep(time.Second * 10)
if err := r.Delete(ctx, vm); err != nil {
log.Error(err, "unable to delete vm ", "vm", vm)
} else {
log.Info("delete vm success ,vm status :", vm.Status.Status)
}
  • 运行查看状态变更
1
2
3
4
# 构建运行
make && make install && make run
# apply config
kubectl apply -f config/samples/
  • log 查看状态变更和删除
1
2
3
4
5
6
7
8
INFO[0000] [vm info] cpu: 1 , memory : 1G                source="virtulmachine_controller.go:50"
INFO[0000] update status success ,vm status :Running source="virtulmachine_controller.go:57"
INFO[0010] delete vm success ,vm status :Running source="virtulmachine_controller.go:64"
2020-04-07T22:17:26.056+0800 DEBUG controller-runtime.controller Successfully Reconciled {"controller": "virtulmachine", "request": "default/virtulmachine-sample"}
ERRO[0010] VirtulMachine.infra.qipajun.com "virtulmachine-sample" not foundunable to fetch vm info source="virtulmachine_controller.go:48"
ERRO[0010] resource name may not be emptyunable to update vm status source="virtulmachine_controller.go:55"
ERRO[0020] resource name may not be emptyunable to delete vm vm&{{ } { 0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] [] []} { } {Running}} source="virtulmachine_controller.go:62"
2020-04-07T22:17:36.062+0800 DEBUG controller-runtime.controller Successfully Reconciled {"controller": "virtulmachine", "request": "default/virtulmachine-sample"}