Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

许可证头

这是什么?

Kubebuilder 会在 hack/boilerplate.go.txt 创建包含许可证头的文件。该头会自动添加到所有生成的 Go 文件中。

样板文件由以下使用:

  • Kubebuilder 在创建新文件时(create api、create webhook 等)
  • controller-gen when generating code (DeepCopy methods, etc.)
  • make generate and make manifests commands

默认情况下,Kubebuilder 使用 Apache 2.0 许可证。

如何自定义

选项 1:Use built-in licenses

在初始化时或更新现有项目时使用 --license 标志:

# During initialization
kubebuilder init --domain example.com --license apache2 --owner "Your Name"

# After initialization (update existing project)
kubebuilder edit --license apache2 --owner "Your Company"

可用的许可证值:

  • apache2 - Apache 2.0 License (default)
  • copyright - Copyright notice only (no license text)
  • none - No license header

选项 2:Use a custom license file

从文件提供你自己的许可证头。Kubebuilder 会读取文件内容并复制到 hack/boilerplate.go.txt:

# During initialization
kubebuilder init --domain example.com --license-file ./my-header.txt

# After initialization
kubebuilder edit --license-file ./my-header.txt

工作原理:

  1. Kubebuilder 读取你的自定义许可证文件(可为任意路径)
  2. 将内容复制到 hack/boilerplate.go.txt

在以下情况使用:

  • 内置模板中没有的许可证
  • 特定公司许可证格式
  • 自定义版权声明

选项 3:Edit the file directly

初始化后,你可以直接编辑 hack/boilerplate.go.txt:

vim hack/boilerplate.go.txt

自定义许可证文件格式

你的自定义许可证文件必须包含 Go 注释定界符(/* 和 */)。

示例(my-license-header.txt):

/*
Copyright YEAR Your Company Name.

Licensed under the MIT License.
See LICENSE file in the project root for full license text.
*/

将许可证变更应用到现有文件

更新 hack/boilerplate.go.txt 后,选择如何应用变更:

重新生成已生成的代码(推荐):

make generate  # Update DeepCopy methods
make manifests # Update CRDs, RBAC, webhooks

这会更新生成的代码,但保持手写文件不变。

重新生成整个项目:

kubebuilder alpha generate

仅新文件: 新文件将自动使用更新后的模板:

kubebuilder create api --group myapp --version v1 --kind MyNewKind

手动更新: 对于手写文件,以 hack/boilerplate.go.txt 为参考手动更新页眉。

如何使用

生成或重新生成文件时会使用 hack/boilerplate.go.txt:

  • kubebuilder create api 与 create webhook - 新文件
  • make generate - DeepCopy methods, etc.
  • make manifests - CRDs, RBAC, webhooks
  • kubebuilder alpha generate - entire project

所有工具自动引用 hack/boilerplate.go.txt。Makefile 已配置好——无需更改。

Examples

示例 1:Apache 2.0 许可证(默认)

使用内置的 Apache 2.0 许可证:

kubebuilder init --domain example.com --license apache2 --owner "Your Company"

这将生成:

/*
 版权 2025 The Kubernetes authors.
 根据 Apache 许可证,版本 2.0("许可证")授权;
 您只能在遵守该许可证的情况下使用此文件。
 您可以在以下地址获得该许可证的副本:

   http://www.apache.org/licenses/LICENSE-2.0 

 除非适用法律要求或书面同意,否则根据该许可证分发的软件
 是按"原样"基础分发的,
 不提供任何形式的担保或条件,无论是明示还是否明示。
有关许可证下的具体权限和限制,请参见许可证。
*/

示例 2:仅版权声明

使用内置的 copyright 许可证仅生成版权声明:

kubebuilder init --domain example.com --license copyright --owner "Your Company"

这将生成:

/*
Copyright YEAR Your Company.
*/

示例 3:自定义许可证(MIT)

对于非内置许可证,创建自定义文件 mit-header.txt:

/*
Copyright YEAR Your Name.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

然后初始化你的项目:

kubebuilder init --domain example.com --license-file ./mit-header.txt

会发生什么:

  1. Kubebuilder 从 ./mit-header.txt 读取内容
  2. 用该内容创建 hack/boilerplate.go.txt
  3. hack/boilerplate.go.txt 成为所有生成文件的权威来源