枯木谈自定义 Kubernetes CRD

2023-12-19 06:08

本文主要是介绍枯木谈自定义 Kubernetes CRD,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 01 理论概要

       我们知道 Kubernetes 提供 CRD 机制用以扩展资源,在某些场景下我们可以利用 CRD 扩展通过 Kubernetes 存储资源,也就是把 Kubernetes 当作存储来使用。按照官方的说法 CRD + Controller = Operator,kubebuilder 可以生成这样的代码框架,并且可自定义选择是否生成 Controller。client 相关代码则可以通过 code-generator 生成,生成 informer、lister、clientset 等系列代码。通过这种方式,我们只需要在生成的代码中填充业务代码,而无需关心框架之外的代码,这样可以极大的提升开发效率和降低 CRD 的上手门槛。本文通过引入 ConfigMapHistory 这个 CRD 为例,带你从零到一构建和部署 CRD。

      02 kubebuilder安装 

         kubebuilder 安装文档可以参考官档 Install kubebuilder 

os=$(go env GOOS)
arch=$(go env GOARCH)# download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/# move to a long-term location and put it on your path
# (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
sudo mv /tmp/kubebuilder_2.3.1_${os}_${arch} /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin

      03 kubebuilder初始化 

kubebuilder 代码初始化:

# github.com/opskumu 以实际仓库为主,本实例为 crd-example
mkdir -p $GOPATH/src/github.com/opskumu/crd-example
cd $GOPATH/src/github.com/opskumu/crd-example
# 域名以实际域名为主,本实例为 opskumu.com
kubebuilder init --domain opskumu.com
kubebuilder edit --multigroup=true

查看当前生成代码的目录结构:

# tree -L 2 ./
.
├── Dockerfile
├── Makefile
├── PROJECT
├── bin
│   └── manager
├── config
│   ├── certmanager
│   ├── default
│   ├── manager
│   ├── prometheus
│   ├── rbac
│   └── webhook
├── go.mod
├── go.sum
├── hack
│   └── boilerplate.go.txt
└── main.go

 04 kubebuilder创建API 

# kubebuilder create api --group test --version v1 --kind ConfigMapHistory
Create Resource [y/n]    # 创建 ConfigMapHistory 资源,此处选择 y
y
Create Controller [y/n]  # 创建控制器,此处不需要,选择 n
n
# tree apis/test/v1   # apis 目录存放 group test v1 相关的资源 API
apis/test/v1
├── configmaphistory_types.go
├── groupversion_info.go
└── zz_generated.deepcopy.go

 05 自定义代码生成client代码  

默认生成apis/test/v1/configmaphistory_types.go代码如下:

......
// +kubebuilder:object:root=true// ConfigMapHistory is the Schema for the configmaphistories API
type ConfigMapHistory struct {metav1.TypeMeta   `json:",inline"`metav1.ObjectMeta `json:"metadata,omitempty"`Spec   ConfigMapHistorySpec   `json:"spec,omitempty"`Status ConfigMapHistoryStatus `json:"status,omitempty"`
}
......

     我们创建 ConfigMapHistory 类型的资源的目的是存放 ConfigMap 的历史记录,所以需要针对以上代码做一些修改:

......
// +genclient
// +kubebuilder:object:root=true// ConfigMapHistory is the Schema for the configmaphistories API
type ConfigMapHistory struct {metav1.TypeMeta   `json:",inline"`metav1.ObjectMeta `json:"metadata,omitempty"`ConfigMap  string            `json:"configMap"`                // 关联 ConfigMap 名Data       map[string]string `json:"data,omitempty"`           // 对应 ConfigMap 版本数据BinaryData map[string]byte   `json:"binaryData,omitempty"`     // 对应 ConfigMap 版本二进制数据
}
......

     注意 // +genclient 是额外添加的,为后续 code-generator 生成 client 代码打标签。types 文件修改之后,可以执行 make manifests 指令生成 CRD 文件:

# make manifests
# ls config/crd/bases
test.opskumu.com_configmaphistories.yaml

     如果代码结构变更,需要再次执行 make manifest 以生成新的 CRD 文件创建 apis/test/v1/doc.go 、 apis/test/v1/register.go 、 hack/tools.go 和 hack/update-codegen.sh 文件

  • apis/test/v1/doc.go

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/// +k8s:deepcopy-gen=package
// 注意 groupName 和实际相同
// +groupName=test.opskumu.com// Package v1 is the v1 version of the API.
package v1
  • apis/test/v1/register.go

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/package v1import ("k8s.io/apimachinery/pkg/runtime/schema"
)// SchemeGroupVersion is group version used to register these objects.
var SchemeGroupVersion = GroupVersionfunc Resource(resource string) schema.GroupResource {return SchemeGroupVersion.WithResource(resource).GroupResource()
}
  • hack/tools.go

// +build tools/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/// This package imports things required by build scripts, to force `go mod` to see them as dependencies
package toolsimport _ "k8s.io/code-generator"

tools.go 主要为了引入 k8s.io/code-generator 依赖。

  • hack/update-codegen.sh

#!/usr/bin/env bash# Copyright 2017 The Kubernetes Authors.                                                                                       #                                                                                                                              # Licensed under the Apache License, Version 2.0 (the "License");                                                              # you may not use this file except in compliance with the License.                                                             # You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.set -o errexit
set -o nounset
set -o pipefailSCRIPT_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)}# 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.
bash "${CODEGEN_PKG}"/generate-groups.sh "client,informer,lister" \github.com/opskumu/crd-example/generated github.com/opskumu/crd-example/apis \test:v1 \--output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \--go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt# To use your own boilerplate text append:
#   --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt

以上代码可以参考 sample-controller doc.go/register.go/tools.go/update-codegen.sh 内容,本文中 CRD 是 kubebuilder 生成的,因此内容稍有不同。

创建完成以上文件之后,执行以下命令生成代码:

# 注意此处版本以下几个包要一致,否则可能出现不兼容的情况
# K8SVERSION=v0.19.6
# go get -v k8s.io/code-generator@${K8SVERSION}
# go get -v k8s.io/client-go@${K8SVERSION}
# go get -v k8s.io/apimachinery@${K8SVERSION}
# go mod vendor

把相关依赖包放入 vendor 中,以便 hack/update-codegen.sh 可以调用 code-generator 中的脚本。完成之后,执行生成代码指令:

# bash hack/update-codegen.sh
Generating clientset for test:v1 at github.com/opskumu/crd-example/generated/clientset
Generating listers for test:v1 at github.com/opskumu/crd-example/generated/listers
Generating informers for test:v1 at github.com/opskumu/crd-example/generated/informers
# crd-example tree -L 1 generated
generated
├── clientset
├── informers
└── listers

      至此完成了添加 ConfigMapHistory CRD 和相关 client 代码的所有操作,如果需要生成其它 Kind 类型的重复以上操作即可(以上创建的文件不需要重复创建)。

 06 创建CRD 

kubectl create -f config/crd/bases/test.opskumu.com_configmaphistories.yaml

创建测试资源

# cat test.yaml
apiVersion: "test.opskumu.com/v1"
kind: ConfigMapHistory
metadata:name: test
configMap: "test"
data:test: test
# kubectl create -f ./test.yaml
configmaphistory.test.opskumu.com/test created
# kubectl get configmaphistory.test.opskumu.com
NAME   AGE
test   27s

完整示例代码参见 crd-example

参考文档:

  • sample-controller

  • Programming Kubernetes CRDs

  • 混合kubebuilder与code generator编写CRD

8de1e282f34f166e462567839ebdae04.jpeg

长按二维码关注公众号

041de4f9877e6a4e5afffc9464d41204.gif

*本公众号所发布内容仅代表作者观点,不代表社区立场

这篇关于枯木谈自定义 Kubernetes CRD的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/511149

相关文章

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3