|
| 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