Skip to content

Commit 05a090c

Browse files
(chore): Add plugin for Alpha Update
Co-authored-by: Vitor Floriano <[email protected]>
1 parent 6d293e7 commit 05a090c

File tree

13 files changed

+540
-6
lines changed

13 files changed

+540
-6
lines changed

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang"
3333
deployimagev1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
3434
golangv4 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4"
35+
autoupdatev1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha"
3536
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
3637
helmv1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
3738
)
@@ -74,6 +75,7 @@ func Run() {
7475
&deployimagev1alpha1.Plugin{},
7576
&grafanav1alpha1.Plugin{},
7677
&helmv1alpha1.Plugin{},
78+
&autoupdatev1alpha1.Plugin{},
7779
),
7880
cli.WithPlugins(externalPlugins...),
7981
cli.WithDefaultPlugins(cfgv3.Version, gov4Bundle),

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
- [Plugins][plugins]
121121

122122
- [Available Plugins](./plugins/available-plugins.md)
123+
- [autoupdate/v1-alpha](./plugins/available/autoupdate-v1-alpha.md)
123124
- [go/v4](./plugins/available/go-v4-plugin.md)
124125
- [grafana/v1-alpha](./plugins/available/grafana-v1-alpha.md)
125126
- [deploy-image/v1-alpha](./plugins/available/deploy-image-plugin-v1-alpha.md)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# AutoUpdate (`autoupdate/v1-alpha`)
2+
3+
Keeping your Kubebuilder project up to date with the latest improvements shouldn’t be a chore.
4+
With a small amount of setup, you can receive **automatic Pull Request** suggestions whenever a new
5+
Kubebuilder release is available — keeping your project **maintained, secure, and aligned with ecosystem changes**.
6+
7+
This automation uses the [`kubebuilder alpha update`][alpha-update-command] command with a **3-way merge strategy** to
8+
refresh your project scaffold, and wraps it in a GitHub Actions workflow that opens an **Issue** with a **Pull Request compare link** so you can create the PR and review it.
9+
10+
## When to use it?
11+
12+
- When you don’t deviate too much from the default scaffold — ensure that you see the note about customization [here](https://book.kubebuilder.io/versions_compatibility_supportability#project-customizations).
13+
- When you want to reduce the burden of keeping the project updated and well-maintained.
14+
15+
## How to use
16+
17+
Run:
18+
19+
```
20+
kubebuilder init|edit --plugins="autoupdate.kubebuilder.io/v1-alpha"
21+
```
22+
23+
This will scaffold and GitHub Actions workflow that runs the [kubebuilder alpha update][alpha-update-command] command.
24+
Then, whenever a new Kubebuilder release is available, it will open an Issue with a
25+
Pull Request compare link so you can create the PR and review it, such as:
26+
27+
<TODO ISSUE ADD IMAGE>
28+
29+
<aside class="warning">
30+
<h1>Protect your main branches</h1>
31+
32+
This workflow by default **only** creates and pushes the merged files to a branch
33+
called `kubebuilder-update-from-<from-version>-to-<to-version>`.
34+
35+
To keep your codebase safe, protect your main branches (e.g. `main`) so changes
36+
can not land outside an PR review.
37+
38+
</aside>
39+
40+
[alpha-update-command]: ./../../reference/commands/alpha_update.md

docs/book/src/plugins/to-add-optional-features.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
The following plugins are useful to generate code and take advantage of optional features
44

5-
| Plugin | Key | Description |
6-
|---------------------------------------------------|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
7-
| [grafana.kubebuilder.io/v1-alpha][grafana] | `grafana/v1-alpha` | Optional helper plugin which can be used to scaffold Grafana Manifests Dashboards for the default metrics which are exported by controller-runtime. |
8-
| [deploy-image.go.kubebuilder.io/v1-alpha][deploy] | `deploy-image/v1-alpha` | Optional helper plugin which can be used to scaffold APIs and controller with code implementation to Deploy and Manage an Operand(image). |
9-
| [helm.kubebuilder.io/v1-alpha][helm] | `helm/v1-alpha` | Optional helper plugin which can be used to scaffold a Helm Chart to distribute the project under the `dist` directory |
5+
| Plugin | Key | Description |
6+
|-----------------------------------------------------|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| [grafana.kubebuilder.io/v1-alpha][grafana] | `grafana/v1-alpha` | Optional helper plugin which can be used to scaffold Grafana Manifests Dashboards for the default metrics which are exported by controller-runtime. |
8+
| [deploy-image.go.kubebuilder.io/v1-alpha][deploy] | `deploy-image/v1-alpha` | Optional helper plugin which can be used to scaffold APIs and controller with code implementation to Deploy and Manage an Operand(image). |
9+
| [helm.kubebuilder.io/v1-alpha][helm] | `helm/v1-alpha` | Optional helper plugin which can be used to scaffold a Helm Chart to distribute the project under the `dist` directory |
10+
| [autoupdate.kubebuilder.io/v1-alpha][autoupdate] | `autoupdate/v1-alpha` | Optional helper which scaffolds a scheduled worker that helps keep your project updated with changes in the ecosystem, significantly reducing the burden of manual maintenance.|
1011

1112
[grafana]: ./available/grafana-v1-alpha.md
1213
[deploy]: ./available/deploy-image-plugin-v1-alpha.md
13-
[helm]: ./available/helm-v1-alpha.md
14+
[helm]: ./available/helm-v1-alpha.md
15+
[autoupdate]: ./available/autoupdate-v1-alpha.md

pkg/cli/alpha/internal/generate.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3232
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
3333
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
34+
autoupdate "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha"
3435
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
3536
hemlv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
3637
)
@@ -106,6 +107,10 @@ func (opts *Generate) Generate() error {
106107
return fmt.Errorf("error migrating Grafana plugin: %w", err)
107108
}
108109

110+
if err = migrateAutoUpdatePlugin(projectConfig); err != nil {
111+
return fmt.Errorf("error migrating AutoUpdate plugin: %w", err)
112+
}
113+
109114
if hasHelmPlugin(projectConfig) {
110115
if err = kubebuilderHelmEdit(); err != nil {
111116
return fmt.Errorf("error editing Helm plugin: %w", err)
@@ -233,6 +238,24 @@ func migrateGrafanaPlugin(s store.Store, src, des string) error {
233238
return kubebuilderGrafanaEdit()
234239
}
235240

241+
// Migrates the Grafana plugin.
242+
func migrateAutoUpdatePlugin(s store.Store) error {
243+
var autoUpdatePlugin struct{}
244+
err := s.Config().DecodePluginConfig(plugin.KeyFor(autoupdate.Plugin{}), autoUpdatePlugin)
245+
if errors.As(err, &config.PluginKeyNotFoundError{}) {
246+
log.Info("Auto Update plugin not found, skipping migration")
247+
return nil
248+
} else if err != nil {
249+
return fmt.Errorf("failed to decode autoupdate plugin config: %w", err)
250+
}
251+
252+
args := []string{"edit", "--plugins", plugin.KeyFor(v1alpha.Plugin{})}
253+
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
254+
return fmt.Errorf("failed to run edit subcommand for Auto plugin: %w", err)
255+
}
256+
return nil
257+
}
258+
236259
// Migrates the Deploy Image plugin.
237260
func migrateDeployImagePlugin(s store.Store) error {
238261
var deployImagePlugin v1alpha1.PluginConfig
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v4/pkg/config"
23+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
24+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
25+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha/scaffolds"
26+
)
27+
28+
var _ plugin.EditSubcommand = &editSubcommand{}
29+
30+
type editSubcommand struct {
31+
config config.Config
32+
}
33+
34+
func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
35+
subcmdMeta.Description = metaDataDescription
36+
37+
subcmdMeta.Examples = fmt.Sprintf(` # Edit a common project with this plugin
38+
%[1]s edit --plugins=%[2]s
39+
`, cliMeta.CommandName, pluginKey)
40+
}
41+
42+
func (p *editSubcommand) InjectConfig(c config.Config) error {
43+
p.config = c
44+
return nil
45+
}
46+
47+
func (p *editSubcommand) PreScaffold(machinery.Filesystem) error {
48+
if len(p.config.GetCliVersion()) == 0 {
49+
return fmt.Errorf(
50+
"you must manually upgrade your project to a version that records the CLI version in PROJECT (`cliVersion`) " +
51+
"to allow the `alpha update` command to work properly before using this plugin.\n" +
52+
"More info: https://book.kubebuilder.io/migrations",
53+
)
54+
}
55+
return nil
56+
}
57+
58+
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
59+
if err := insertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
60+
return fmt.Errorf("error inserting project plugin meta to configuration: %w", err)
61+
}
62+
63+
scaffolder := scaffolds.NewInitScaffolder()
64+
scaffolder.InjectFS(fs)
65+
if err := scaffolder.Scaffold(); err != nil {
66+
return fmt.Errorf("error scaffolding edit subcommand: %w", err)
67+
}
68+
69+
return nil
70+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/kubebuilder/v4/pkg/config"
23+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
24+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
25+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha/scaffolds"
26+
)
27+
28+
var _ plugin.InitSubcommand = &initSubcommand{}
29+
30+
type initSubcommand struct {
31+
config config.Config
32+
}
33+
34+
func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
35+
subcmdMeta.Description = metaDataDescription
36+
37+
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with this plugin
38+
%[1]s init --plugins=%[2]s
39+
`, cliMeta.CommandName, pluginKey)
40+
}
41+
42+
func (p *initSubcommand) InjectConfig(c config.Config) error {
43+
p.config = c
44+
return nil
45+
}
46+
47+
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
48+
if err := insertPluginMetaToConfig(p.config, pluginConfig{}); err != nil {
49+
return fmt.Errorf("error inserting project plugin meta to configuration: %w", err)
50+
}
51+
52+
scaffolder := scaffolds.NewInitScaffolder()
53+
scaffolder.InjectFS(fs)
54+
if err := scaffolder.Scaffold(); err != nil {
55+
return fmt.Errorf("error scaffolding init subcommand: %w", err)
56+
}
57+
58+
return nil
59+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
23+
"sigs.k8s.io/kubebuilder/v4/pkg/config"
24+
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
25+
"sigs.k8s.io/kubebuilder/v4/pkg/model/stage"
26+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
27+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
28+
)
29+
30+
//nolint:lll
31+
const metaDataDescription = `This plugin scaffolds a GitHub Action that helps you keep your project aligned with the latest Kubebuilder improvements. With a tiny amount of setup, you’ll receive **automatic issue notifications** whenever a new Kubebuilder release is available. Each issue includes a **compare link** so you can open a Pull Request with one click and review the changes safely.
32+
33+
Under the hood, the workflow runs 'kubebuilder alpha update' using a **3-way merge strategy** to refresh your scaffold while preserving your code. It creates and pushes an update branch, then opens a GitHub **Issue** containing the PR URL you can use to review and merge.
34+
35+
### How to set it up
36+
37+
1) **Add the plugin**: Use the Kubebuilder CLI to scaffold the automation into your repo.
38+
2) **Review the workflow**: The file '.github/workflows/auto_update.yml' runs on a schedule to check for updates.
39+
3) **Permissions required** (via the built-in 'GITHUB_TOKEN'):
40+
- **contents: write** — needed to create and push the update branch.
41+
- **issues: write** — needed to create the tracking Issue with the PR link.
42+
4) **Protect your main branches**: Enable **branch protection rules** (e.g. for 'main'/'master') so automated changes **cannot** be pushed directly. All updates must go through a Pull Request for review.`
43+
44+
const pluginName = "autoupdate." + plugins.DefaultNameQualifier
45+
46+
var (
47+
pluginVersion = plugin.Version{Number: 1, Stage: stage.Alpha}
48+
supportedProjectVersions = []config.Version{cfgv3.Version}
49+
pluginKey = plugin.KeyFor(Plugin{})
50+
)
51+
52+
// Plugin implements the plugin.Full interface
53+
type Plugin struct {
54+
editSubcommand
55+
}
56+
57+
var _ plugin.Edit = Plugin{}
58+
59+
type pluginConfig struct{}
60+
61+
// Name returns the name of the plugin
62+
func (Plugin) Name() string { return pluginName }
63+
64+
// Version returns the version of the Helm plugin
65+
func (Plugin) Version() plugin.Version { return pluginVersion }
66+
67+
// SupportedProjectVersions returns an array with all project versions supported by the plugin
68+
func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions }
69+
70+
// GetEditSubcommand will return the subcommand which is responsible for adding and/or edit a helm chart
71+
func (p Plugin) GetEditSubcommand() plugin.EditSubcommand { return &p.editSubcommand }
72+
73+
// DeprecationWarning define the deprecation message or return empty when plugin is not deprecated
74+
func (p Plugin) DeprecationWarning() string {
75+
return ""
76+
}
77+
78+
// insertPluginMetaToConfig will insert the metadata to the plugin configuration
79+
func insertPluginMetaToConfig(target config.Config, cfg pluginConfig) error {
80+
err := target.DecodePluginConfig(pluginKey, cfg)
81+
if !errors.As(err, &config.UnsupportedFieldError{}) {
82+
if err != nil && !errors.As(err, &config.PluginKeyNotFoundError{}) {
83+
return fmt.Errorf("error decoding plugin configuration: %w", err)
84+
}
85+
86+
if err = target.EncodePluginConfig(pluginKey, cfg); err != nil {
87+
return fmt.Errorf("error encoding plugin configuration: %w", err)
88+
}
89+
}
90+
91+
return nil
92+
}

0 commit comments

Comments
 (0)