controller-tools 简介

code-generator自动生成代码存在的问题

在operator之code-generator 篇中,code-generator编写CRD控制器有两个问题:

  • 问题一:需要手动编写CRD的yaml,无法自动生成
  • 问题二:types.go文件全部内容都需要我们手写,无法自动生成框架
    这部分工作量其实也是挺大的,kubernetes提供了一个工具 controller-tools,可以对这部分内容也进行代码自动生成

controller-tools是什么

1、kubernetes-sigs 项目是什么

  • kubernetes-sigs 是一个由 Kubernetes 社区维护的 GitHub 组织,其中包含了许多与 Kubernetes 相关的项目,这些项目通常是为 Kubernetes 生态系统开发的,用于提供各种功能和工具。
  • 一些 kubernetes-sigs 组织中的流行项目包括:
    • kustomize:一种用于 Kubernetes 部署的配置管理工具,可以通过 YAML 声明文件对 Kubernetes 对象进行自定义,并且支持多环境部署(例如 dev、stage、prod)。
    • kubebuilder:一种用于构建 Kubernetes API 的 SDK 工具,可以帮助开发者快速构建和测试 Kubernetes 的自定义控制器。
    • cluster-api:一种 Kubernetes 的 API 扩展,用于管理 Kubernetes 集群的生命周期,包括创建、扩容和删除。它允许开发者使用 Kubernetes 的声明性 API 来管理整个集群的生命周期。
    • kubefed:用于跨 Kubernetes 集群联邦的控制平面。它提供了一种将多个 Kubernetes 集群组合成一个统一的逻辑实体的方法,同时保持每个集群的独立性。
    • controller-tools:用于简化 Kubernetes 控制器的开发,提供了一组工具来生成和更新 Kubernetes API 对象的代码,以及构建自定义控制器所需的代码框架。

2、controller-tools是什么

controller-tools其实是一个由 Kubernetes 社区维护的项目,用于简化 Kubernetes 控制器的开发。其中提供了一组工具来生成和更新 Kubernetes API 对象的代码,以及构建自定义控制器所需的代码框架。
controller-tools 的github地址:https://github.com/kubernetes-sigs/controller-tools

3、controller-tools 包含哪些工具

在controller-tools源码的cmd目录下,可以看到包含三个工具

  • controller-gen:用于生成 zz_xxx.deepcopy.go 文件以及 crd 文件【kubebuilder也是通过这个工具生成crd的相关框架的】
  • type-scaffold:用于生成所需的 types.go 文件
  • helpgen:用于生成针对 Kubernetes API 对象的代码文档,可以包括 API 对象的字段、标签和注释等信息

controller-tools 使用过程

controller-tools 的 安装

  • controller-tools 的 github 地址:https://github.com/kubernetes-sigs/controller-tools.git,克隆代码

    1
    git clone https://github.com/kubernetes-sigs/controller-tools.git
  • 将分支切换到 v0.9.0 的tag上

    1
    git checkout v0.9.0
  • 编译项目,安装代码生成工具,这里我们只安装需要的2个工具

    • controller-gen工具:生成 deepcopy方法 文件 + crd 文件
    • type-scaffold工具:生成 types.go 文件
      1
      2
      3
      4
      5
      6
      7
      cd controller-tools
      # linux下安装,执行这一条即可
      go install ./cmd/{controller-gen,type-scaffold}

      # windows下安装,需要执行两条命令
      go install ./cmd/controller-gen
      go install ./cmd/type-scaffold
  • 打开终端,执行 type-scaffold –help,如果报错:

    1
    2
    [root@master zgy]# type-scaffold --help
    -bash: type-scaffold: 未找到命令
  • 说明环境变量没有设置成功,需要将 gopath/bin 加入PATH

    1
    2
    3
    4
    5
    vim ~/.bashrc
    # 在~/.bashrc文件的末尾,加上这么一句
    export PATH="$PATH:$GOPATH/bin"
    # 然后,source一下
    source ~/.bashrc
  • 再执行 type-scaffold –help,就成功了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    [root@master zgy]# type-scaffold --help
    Quickly scaffold out the structure of a type for a Kubernetes kind and associated types.
    Produces:

    - a root type with appropriate metadata fields
    - Spec and Status types
    - a list type

    Also applies the appropriate comments to generate the code required to conform to runtime.Object.

    Usage:
    type-scaffold [flags]

    Examples:
    # Generate types for a Kind called Foo with a resource called foos
    type-scaffold --kind Foo

    # Generate types for a Kind called Bar with a resource of foobars
    type-scaffold --kind Bar --resource foobars

    Flags:
    -h, --help help for type-scaffold
    --kind string The kind of the typescaffold being scaffolded.
    --namespaced Whether or not the given resource is namespaced. (default true)
    --resource string The resource of the typescaffold being scaffolded (defaults to a lower-case, plural version of kind).

type-scaffold 的使用方法

  • type-scaffold 常用命令

    1
    2
    type-scaffold --kind <Kind> [flags]
    type-scaffold --help
  • –kind:参数用于指定要创建的资源类型(例如 Application)

controller-gen 的使用方法

  • controller-gen 常用命令
    1
    2
    3
    4
    # 生成 CRD 文件,并将生成的文件输出到 config/crds 目录中
    controller-gen crd paths=./... output:crd:dir=config/crds
    # 生成与对象相关的代码,通常是指生成控制器相关的代码模板
    controller-gen object paths=./...