Generated Test/ Code Fails Linter In Scaffolded GitHub Action
What Happened?
When creating a new project and API using kubebuilder
, the scaffolded test code under test/
fails lint checks in the default GitHub Actions Lint
workflow (which is also scaffolded by Kubebuilder). This happens immediately after project initialization and before any user edits. The issue arises from the generated test code, which contains unnecessary type conversions and non-constant format strings, leading to linter violations.
How Can We Reproduce It (As Minimally and Precisely as Possible)?
To reproduce this issue, follow these steps:
kubebuilder init --domain example.com --repo example.com/memcached-operator
kubebuilder create api --group cache --version v1 --kind Memcached --resource --controller
go mod tidy
Then commit and push to GitHub. The generated Lint
GitHub Action runs this:
name: Lint
on:
push:
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '~1.22'
- uses: golangci/golangci-lint-action@v6
with:
version: v1.61
What Did You Expect to Happen?
That all scaffolded code (including under test/
) would pass the linter, since it’s intended to be a working, ready-to-use project. The expectation is that the generated test code would be free from linter violations, allowing users to start working on their project without any issues.
What Actually Happened?
The GitHub Action fails due to linter violations in test/
, including:
test/utils/utils.go:95:36: unnecessary conversion (unconvert)
test/utils/utils.go:156:36: unnecessary conversion (unconvert)
test/e2e/e2e_test.go:281:30: unnecessary conversion (unconvert)
test/e2e/e2e_test.go:96:38: printf: non-constant format string in call to fmt.Fprintf (govet)
test/e2e/e2e_test.go:98:38: printf: non-constant format string in call to fmt.Fprintf (govet)
test/e2e/e2e_test.go:105:38: printf: non-constant format string in call to fmt.Fprintf (govet)
These issues are generated automatically and not the result of any user code. The linter violations are caused by unnecessary type conversions and non-constant format strings in the generated test code.
Anything Else We Need to Know?
- Occurs on a brand-new repo with no custom edits.
- The offending code is in
test/
files generated by Kubebuilder. - Linter version (
v1.61
) and Go version (1.22
) are from Kubebuilder’s own GitHub Action template. - I would like to contribute a PR to fix this.
Environment
- Kubebuilder version: 4.3.1
- Go version: go1.24.2
- OS: Ubuntu 22.04
- GitHub Action:
golangci-lint-action@v6
(v1.61)
Proposed
Update the test scaffold templates to:
- Avoid unnecessary type conversions (e.g.,
string(output)
) - Avoid passing non-constant format strings to
fmt.Fprintf
- Ensure compatibility with default
golangci-lint
rules
Step 1: Update Test Scaffold Templates
Update the test scaffold templates to avoid unnecessary type conversions and non-constant format strings. This can be achieved by modifying the generated test code to use more idiomatic Go code.
Step 2: Ensure Compatibility with Default Golangci-Lint Rules
Ensure that the updated test scaffold templates are compatible with the default golangci-lint
rules. This can be achieved by running the linter on the updated code and addressing any new issues that arise.
Step 3: Test the Updated Code
Test the updated code to ensure that it passes the linter and does not introduce any new issues. This can be achieved by running the GitHub Action on the updated code and verifying that it passes the linter.
Step 4: Contribute a PR to Fix the Issue
Contribute a PR to fix the issue by updating the test scaffold templates and ensuring compatibility with the default golangci-lint
rules. This will allow users to start working on their project without any issues related to linter violations.
By following these steps, we can update the test scaffold templates to avoid unnecessary type conversions and non-constant format strings, ensuring that the generated test code passes the linter and is compatible with the default golangci-lint
rules.
Q: What is the issue with the generated test code in Kubebuilder?
A: The generated test code in Kubebuilder contains unnecessary type conversions and non-constant format strings, leading to linter violations. This causes the GitHub Action to fail when running the linter.
Q: Why does this issue occur?
A: This issue occurs because the generated test code is not optimized for linter rules. The unnecessary type conversions and non-constant format strings are generated automatically by Kubebuilder and are not the result of any user code.
Q: What are the specific linter violations that occur?
A: The specific linter violations that occur are:
test/utils/utils.go:95:36: unnecessary conversion (unconvert)
test/utils/utils.go:156:36: unnecessary conversion (unconvert)
test/e2e/e2e_test.go:281:30: unnecessary conversion (unconvert)
test/e2e/e2e_test.go:96:38: printf: non-constant format string in call to fmt.Fprintf (govet)
test/e2e/e2e_test.go:98:38: printf: non-constant format string in call to fmt.Fprintf (govet)
test/e2e/e2e_test.go:105:38: printf: non-constant format string in call to fmt.Fprintf (govet)
Q: What is the expected behavior?
A: The expected behavior is that the generated test code would pass the linter, allowing users to start working on their project without any issues.
Q: What is the proposed fix?
A: The proposed fix is to update the test scaffold templates to:
- Avoid unnecessary type conversions (e.g.,
string(output)
) - Avoid passing non-constant format strings to
fmt.Fprintf
- Ensure compatibility with default
golangci-lint
rules
Q: How can I contribute to fixing this issue?
A: You can contribute to fixing this issue by updating the test scaffold templates and ensuring compatibility with the default golangci-lint
rules. This can be achieved by following the steps outlined in the proposed fix.
Q: What is the environment in which this issue occurs?
A: The environment in which this issue occurs is:
- Kubebuilder version: 4.3.1
- Go version: go1.24.2
- OS: Ubuntu 22.04
- GitHub Action:
golangci-lint-action@v6
(v1.61)
Q: How can I reproduce this issue?
A: You can reproduce this issue by following the steps outlined in the "How Can We Reproduce It" section of the original article.
Q: What are the benefits of fixing this issue?
A: The benefits of fixing this issue are that users will be able to start working on their project without any issues related to linter violations. This will make it easier for users to get started with Kubebuilder and will improve the overall user experience.