License header
What is it?
Kubebuilder creates a file at hack/boilerplate.go.txt that contains your license header. This header is automatically added to all generated Go files.
The boilerplate file is used by:
- Kubebuilder when creating new files (
create api,create webhook, etc.) controller-genwhen generating code (DeepCopy methods, etc.)make generateandmake manifestscommands
By default, Kubebuilder uses the Apache 2.0 license.
How to customize
Option 1: Use built-in licenses
Use the --license flag during initialization or to update an existing project:
# During initialization
kubebuilder init --domain example.com --license apache2 --owner "Your Name"
# After initialization (update existing project)
kubebuilder edit --license apache2 --owner "Your Company"
Available license values:
apache2- Apache 2.0 License (default)copyright- Copyright notice only (no license text)none- No license header
Option 2: Use a custom license file
Provide your own license header from a file. Kubebuilder reads the content from your file and copies it to 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
How it works:
- Kubebuilder reads your custom license file (can be any path)
- Copies the content to
hack/boilerplate.go.txt
Use this when you need:
- A license not available in the built-in templates
- A specific company license format
- Custom copyright notices
Option 3: Edit the file directly
After initialization, you can edit hack/boilerplate.go.txt directly:
vim hack/boilerplate.go.txt
Custom license file format
Your custom license file must include Go comment delimiters (/* and */).
Example (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.
*/
Applying license changes to existing files
After updating hack/boilerplate.go.txt, choose how to apply the changes:
Regenerate Generated Code (Recommended):
make generate # Update DeepCopy methods
make manifests # Update CRDs, RBAC, webhooks
This updates generated code but keeps your hand-written files unchanged.
Regenerate Entire Project:
kubebuilder alpha generate
New Files Only: New files will automatically use the updated template:
kubebuilder create api --group myapp --version v1 --kind MyNewKind
Manual Updates:
For hand-written files, manually update headers using hack/boilerplate.go.txt as a reference.
How it is used
hack/boilerplate.go.txt is used when generating or regenerating files:
kubebuilder create apiandcreate webhook- new filesmake generate- DeepCopy methods, etc.make manifests- CRDs, RBAC, webhookskubebuilder alpha generate- entire project
All tools automatically reference hack/boilerplate.go.txt. The Makefile is already configured - no changes needed.
Examples
Example 1: Apache 2.0 License (Default)
Use the built-in Apache 2.0 license:
kubebuilder init --domain example.com --license apache2 --owner "Your Company"
This generates:
/*
Copyright YEAR Your Company.
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.
*/
Example 2: Copyright Only
Use the built-in copyright license for just a copyright notice:
kubebuilder init --domain example.com --license copyright --owner "Your Company"
This generates:
/*
Copyright YEAR Your Company.
*/
Example 3: Custom License (MIT)
For licenses not built-in, create a custom file 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.
*/
Then initialize your project:
kubebuilder init --domain example.com --license-file ./mit-header.txt
What happens:
- Kubebuilder reads the content from
./mit-header.txt - Creates
hack/boilerplate.go.txtwith that content hack/boilerplate.go.txtbecomes the source of truth for all generated files
Related
- Good Practices - Best practices for project structure
- Generating CRDs - How CRDs are generated with headers
- controller-gen CLI - Details on code generation