枯木谈自定义 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

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

什么是Kubernetes PodSecurityPolicy?

@TOC 💖The Begin💖点点关注,收藏不迷路💖 1、什么是PodSecurityPolicy? PodSecurityPolicy(PSP)是Kubernetes中的一个安全特性,用于在Pod创建前进行安全策略检查,限制Pod的资源使用、运行权限等,提升集群安全性。 2、为什么需要它? 默认情况下,Kubernetes允许用户自由创建Pod,可能带来安全风险。

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

容器编排平台Kubernetes简介

目录 什么是K8s 为什么需要K8s 什么是容器(Contianer) K8s能做什么? K8s的架构原理  控制平面(Control plane)         kube-apiserver         etcd         kube-scheduler         kube-controller-manager         cloud-controlle

【Kubernetes】K8s 的安全框架和用户认证

K8s 的安全框架和用户认证 1.Kubernetes 的安全框架1.1 认证:Authentication1.2 鉴权:Authorization1.3 准入控制:Admission Control 2.Kubernetes 的用户认证2.1 Kubernetes 的用户认证方式2.2 配置 Kubernetes 集群使用密码认证 Kubernetes 作为一个分布式的虚拟

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

kubernetes集群部署Zabbix监控平台

一、zabbix介绍 1.zabbix简介 Zabbix是一个基于Web界面的分布式系统监控的企业级开源软件。可以监视各种系统与设备的参数,保障服务器及设备的安全运营。 2.zabbix特点 (1)安装与配置简单。 (2)可视化web管理界面。 (3)免费开源。 (4)支持中文。 (5)自动发现。 (6)分布式监控。 (7)实时绘图。 3.zabbix的主要功能