laitimes

kubebuilder(3)实现operator

author:IT Technology Circle

In the previous article, we have looked at the basic structure of the operator project. Now let's write a little bit of simple code and deploy our CRD and Operator to the K8S cluster.

demand

It's a real need, but it's simplified.

In the development of the company's own PaaS platform, there is a requirement that users only need to fill in a few fields on the page when publishing the version, and we automatically pull up resources such as service and deployment in K8s to block the troubles of K8s's underlying technology to upper-level users.

We only need to ask the user to fill in the service name, image address, and number of replicas.

Define APIs

Modify the demo_types.go file in the api directory that defines the fields available for the spec in our CRD.

Note: JSON tags are required. In order to be able to serialize fields, any new fields you add must have JSON tags.

kubebuilder(3)实现operator

image-20240129171326962type DemoSpec struct {

// +kubebuilder:validation:Required

Image string `json:"image,omitempty"`

// +kubebuilder:validation:Required

SvcName string `json:"svcName,omitempty"`

// +kubebuilder:validation:Required

Replicas *int32 `json:"replicas,omitempty"`

}

Then define the status of the resource

type DemoStatus struct {

Replicas *int32 `json:"replicas,omitempty"`

}

Each time you modify an API definition, you need to run the command to automatically regenerate the CRD definition code

# make manifests

kubebuilder(3)实现operator

image-20240130112827332

You can see that our CRD is generated

Implement the controller

kubebuilder has been very kind to tell us where the code should be written

kubebuilder(3)实现operator

image-20240129172302802

Code flow

根据crd资源创建service和deployment

If you modify the replicas in CTD, the deployment changes are also triggered

The code is relatively long and put on github

https://github.com/jediang/demo/tre/masters/kubebuil

Here, it's okay not to develop the code first, just type a few lines of logs to get the process through

Configure a listener

We've written Reconcile(), so when will this Reconcile be executed?

这就要看SetupWithManager()方法

The SetupWithManager method is used to add changes to which resources we care about.

This is what is generated by default

func (r *DemoReconciler) SetupWithManager(mgr ctrl. Manager) error {

return ctrl. NewControllerManagedBy(mgr).

For(&tutorialv1. Demo{}).

Complete(r)

}

Indicates that we are listening to our custom tutorialv1. Demo resources. That is, as long as we change this crd in k8s, our Reconcile() method will be triggered

If modified to:

import (

...

appsv1 "k8s.io/api/apps/v1"

corev1 "k8s.io/api/core/v1"

)

func (r *DemoReconciler) SetupWithManager(mgr ctrl. Manager) error {

return ctrl. NewControllerManagedBy(mgr).

For(&tutorialv1. Demo{}).

Owns(&corev1. Pod{}).

Owns(&corev1. Service{}).

Owns(&appsv1. Deployment{}).

Complete(r)

}

就是对任何tutorialv1. Demo或Service、Deployment、Pod的变化,我们的Controller都会监听到,并生成事件,触发Reconcile()方法