$ mkdir memcached-operator $ cd memcached-operator $ operator-sdk init --domain=example.com --repo=github.com/example/memcached-operator Writing scaffold for you to edit... Get controller runtime: $ go get sigs.k8s.io/[email protected] Update go.mod: $ go mod tidy
Running make: $ make go: creating new go.mod: module tmp Downloading sigs.k8s.io/controller-tools/cmd/[email protected] go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.4.1 /Users/username/workspace/projects/memcached-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... go build -o bin/manager main.go
domin 用于 operator 创建的任何 API Group,因此这里的 API Group 是 *.example.com。
大家可能熟悉的一个 API Group 是 rbac.authorization.k8s.io,创建 RBAC 资源(如 ClusterRoles 和 ClusterBindings)的功能通常就设置在 Kubernetes 集群上。operator-sdk 允许您指定一个自定义域,将其附加到您定义的任何 API 组,以帮助避免名称冲突。
Writing scaffold for you to edit... api/v1alpha1/memcached_types.go controllers/memcached_controller.go Running make:
$ make /Users/username/workspace/projects/memcached-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... go build -o bin/manager main.go
--group 是自定义资源所在的组,因此它最终会出现在 API Group “cache.example.com “中。
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. // MemcachedSpec defines the desired state of Memcached type MemcachedSpec 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 Memcached. Edit Memcached_types.go to remove/update Foo string `json:"foo,omitempty"` } // MemcachedStatus defines the observed state of Memcached type MemcachedStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file }
// MemcachedSpec defines the desired state of Memcached type MemcachedSpec struct { // +kubebuilder:validation:Minimum=0 // Size is the size of the memcached deployment Size int32 `json:"size"` } // MemcachedStatus defines the observed state of Memcached type MemcachedStatus struct { // Nodes are the names of the memcached pods Nodes []string `json:"nodes"` }
Size 是一个整数,用于确定 Memcached 集群中的节点数量。我们在 Status 中添加了一个字符串数组,用于存储集群中包含的节点的 IP 地址。需要注意的是,这个特定的实现方式只是一个示例。 注意父 Memcached 结构 Status 字段的 Kubebuilder 子资源标记。这将在生成的 CRD 清单中添加 Kubernetes 状态子资源。这样,控制器就可以只更新状态字段,而无需更新整个对象,从而提高性能。
1 2 3 4 5 6 7 8
// Memcached is the Schema for the memcacheds API // +kubebuilder:subresource:status type Memcached struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec MemcachedSpec `json:"spec,omitempty"` Status MemcachedStatus `json:"status,omitempty"` }
// 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 Memcached 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/[email protected]/pkg/reconcile func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = r.Log.WithValues("memcached", req.NamespacedName) // your logic here return ctrl.Result{}, nil }
$ export USERNAME=<docker-username> $ make docker-build docker-push IMG=docker.io/$USERNAME/memcached-operator:v1.0.0
如果出现如下错误,则可能需要获取其他依赖项。运行建议的命令下载依赖项。
1 2
/controllers: package k8s.io/api/apps/v1 imported from implicitly required module; to add missing requirements, run: go get k8s.io/api/apps/[email protected]
$ kubectl get crds NAME CREATED AT catalogsources.operators.coreos.com 2021-01-22T00:13:22Z clusterserviceversions.operators.coreos.com 2021-01-22T00:13:22Z installplans.operators.coreos.com 2021-01-22T00:13:22Z memcacheds.cache.example.com 2021-02-06T00:52:38Z operatorgroups.operators.coreos.com 2021-01-22T00:13:22Z rbacsyncs.ibm.com 2021-01-22T00:08:59Z subscriptions.operators.coreos.com 2021-01-22T00:13:22Z
运行控制器的部署和 Pod:
1 2 3 4 5 6 7
$ kubectl --namespace memcached-operator-system get deployments NAME READY UP-TO-DATE AVAILABLE AGE memcached-operator-controller-manager 1/1 1 1 2m18s
$ kubectl --namespace memcached-operator-system get pods NAME READY STATUS RESTARTS AGE memcached-operator-controller-manager-76b588bbb5-wvl7b 2/2 Running 0 2m44s
当 pod 开始运行,我们的 Operator 就可以使用了。编辑 config/samples/cache_v1alpha1_memcached.yaml 中的示例 YAML,加入一个大小整数,就像在自定义资源规范中定义的那样: