Skip to content

Commit 1f099ea

Browse files
authored
Merge pull request #3032 from camilamacedo86/go-v4-base
✨ Add go/v4-alpha base and migration guide from go/v3 to go/v4-alpha
2 parents 89f58d3 + 00dfad7 commit 1f099ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3475
-39
lines changed

cmd/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
deployimagev1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/deploy-image/v1alpha1"
3535
golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
3636
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
37+
golangv4 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4"
3738
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha"
3839
)
3940

@@ -44,10 +45,10 @@ func main() {
4445
kustomizecommonv1.Plugin{},
4546
golangv3.Plugin{},
4647
)
47-
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v3 with kustomize alpha-v2
48+
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v4 with kustomize alpha-v2
4849
gov4Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 4, Stage: stage.Alpha},
4950
kustomizecommonv2alpha.Plugin{},
50-
golangv3.Plugin{},
51+
golangv4.Plugin{},
5152
)
5253

5354
fs := machinery.Filesystem{
@@ -64,6 +65,7 @@ func main() {
6465
cli.WithPlugins(
6566
golangv2.Plugin{},
6667
golangv3.Plugin{},
68+
golangv4.Plugin{},
6769
gov3Bundle,
6870
gov4Bundle,
6971
&kustomizecommonv1.Plugin{},

docs/book/src/SUMMARY.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,20 @@
6060

6161
- [Migrations](./migrations.md)
6262

63-
- [Kubebuilder v1 vs v2](./migration/v1vsv2.md)
63+
- [Legacy (before <= v3.0.0)](./migration/legacy.md)
64+
- [Kubebuilder v1 vs v2](migration/legacy/v1vsv2.md)
6465

65-
- [Migration Guide](./migration/legacy/migration_guide_v1tov2.md)
66+
- [Migration Guide](./migration/legacy/migration_guide_v1tov2.md)
6667

67-
- [Kubebuilder v2 vs v3](./migration/v2vsv3.md)
68-
69-
- [Migration Guide](./migration/migration_guide_v2tov3.md)
70-
- [Migration by updating the files](./migration/manually_migration_guide_v2_v3.md)
68+
- [Kubebuilder v2 vs v3](migration/legacy/v2vsv3.md)
7169

70+
- [Migration Guide](migration/legacy/migration_guide_v2tov3.md)
71+
- [Migration by updating the files](migration/legacy/manually_migration_guide_v2_v3.md)
72+
- [From v3.0.0 with plugins](./migration/v3-plugins.md)
73+
- [go/v3 vs go/v4-alpha](migration/v3vsv4.md)
74+
75+
- [Migration Guide](migration/migration_guide_gov3_to_gov4.md)
76+
- [Migration by updating the files](migration/manually_migration_guide_gov3_to_gov4.md)
7277
- [Single Group to Multi-Group](./migration/multi-group.md)
7378

7479
---

docs/book/src/migration/legacy.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Migration guides from Legacy versions < 3.0.0
2+
3+
Follow the migration guides from the legacy Kubebuilder versions up the required latest v3x version.
4+
Note that from v3, a new ecosystem using plugins is introduced for better maintainability, reusability and user
5+
experience .
6+
7+
For more info, see the design docs of:
8+
9+
- [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc]
10+
- [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5]
11+
- [Extensible CLI and Scaffolding Plugins - Phase 2][plugins-phase2-design-doc]
12+
13+
Also, you can check the [Plugins section][plugins-section].
14+
15+
[plugins-phase1-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1.md
16+
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
17+
[plugins-phase2-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-2.md
18+
[plugins-section]: ./../plugins/plugins.md
File renamed without changes.

docs/book/src/migration/migration_guide_v2tov3.md renamed to docs/book/src/migration/legacy/migration_guide_v2tov3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Change the image name in the Makefile if needed.
173173
Finally, we can run `make` and `make docker-build` to ensure things are working
174174
fine.
175175
176-
[v2vsv3]: ./v2vsv3.md
176+
[v2vsv3]: v2vsv3.md
177177
[quick-start]: /quick-start.md#installation
178178
[controller-tools]: https://github.com/kubernetes-sigs/controller-tools/releases
179179
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime/releases

docs/book/src/migration/v1vsv2.md renamed to docs/book/src/migration/legacy/v1vsv2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Kubebuilder v1 vs v2
1+
# Kubebuilder v1 vs v2 (Legacy v1.0.0+ to v2.0.0 Kubebuilder CLI versions)
22

33
This document cover all breaking changes when migrating from v1 to v2.
44

docs/book/src/migration/v2vsv3.md renamed to docs/book/src/migration/legacy/v2vsv3.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Kubebuilder v2 vs v3
1+
# Kubebuilder v2 vs v3 (Legacy Kubebuilder v2.0.0+ layout to 3.0.0+)
22

33
This document covers all breaking changes when migrating from v2 to v3.
44

@@ -13,7 +13,9 @@ v3 projects use Go modules and request Go 1.18+. Dep is no longer supported for
1313

1414
## Kubebuilder
1515

16-
- Preliminary support for plugins was added. For more info see the [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc] and [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5]
16+
- Preliminary support for plugins was added. For more info see the [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc],
17+
the [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5] and the [Extensible CLI and Scaffolding Plugins - Phase 2][plugins-phase2-design-doc]
18+
design docs. Also, you can check the [Plugins section][plugins-section].
1719

1820
- The `PROJECT` file now has a new layout. It stores more information about what resources are in use, to better enable plugins to make useful decisions when scaffolding.
1921

@@ -89,8 +91,10 @@ You will check that you can still using the previous layout by using the `go/v2`
8991

9092
[plugins-phase1-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1.md
9193
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
94+
[plugins-phase2-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-2.md
95+
[plugins-section]: ./../../plugins/plugins.md
9296
[manually-upgrade]: manually_migration_guide_v2_v3.md
93-
[component-config-tutorial]: ../component-config-tutorial/tutorial.md
97+
[component-config-tutorial]: ../../component-config-tutorial/tutorial.md
9498
[issue-1893]: https://github.com/kubernetes-sigs/kubebuilder/issues/1839
9599
[migration-guide-v2-to-v3]: migration_guide_v2tov3.md
96100
[healthz-ping]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/healthz#CheckHandler
@@ -102,5 +106,5 @@ You will check that you can still using the previous layout by using the `go/v2`
102106
[cert-manager-docs]: https://cert-manager.io/docs/installation/upgrading/
103107
[kb-releases]: https://github.com/kubernetes-sigs/kubebuilder/releases
104108
[kube-rbac-proxy]: https://github.com/brancz/kube-rbac-proxy/releases
105-
[basic-project-doc]: ../cronjob-tutorial/basic-project.md
109+
[basic-project-doc]: ../../cronjob-tutorial/basic-project.md
106110
[kustomize]: https://github.com/kubernetes-sigs/kustomize
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Migration from go/v3 to go/v4-alpha by updating the files manually
2+
3+
Make sure you understand the [differences between Kubebuilder go/v3 and go/v4-alpha][v3vsv4]
4+
before continuing.
5+
6+
Please ensure you have followed the [installation guide][quick-start]
7+
to install the required components.
8+
9+
The following guide describes the manual steps required to upgrade your PROJECT config file to begin using `go/v4-alpha`.
10+
11+
This way is more complex, susceptible to errors, and success cannot be assured. Also, by following these steps you will not get the improvements and bug fixes in the default generated project files.
12+
13+
Usually it is suggested to do it manually if you have customized your project and deviated too much from the proposed scaffold. Before continuing, ensure that you understand the note about [project customizations][project-customizations]. Note that you might need to spend more effort to do this process manually than to organize your project customizations. The proposed layout will keep your project maintainable and upgradable with less effort in the future.
14+
15+
The recommended upgrade approach is to follow the [Migration Guide go/v3 to go/v4-alpha][migration-guide-gov3-to-gov4] instead.
16+
17+
## Migration from project config version "go/v3" to "go/v4"
18+
19+
Update `PROJECT` file layout which stores the information about the resources are use to enable plugins to make useful decisions when scaffolding.
20+
21+
Furthermore, the `PROJECT` file itself is now versioned. The `version` field corresponds to the version of the `PROJECT` file itself, while the `layout` field indicates the scaffolding and the primary plugin version in use.
22+
23+
Update:
24+
25+
```yaml
26+
layout:
27+
- go.kubebuilder.io/v3
28+
```
29+
30+
With:
31+
32+
```yaml
33+
layout:
34+
- go.kubebuilder.io/v4-alpha
35+
36+
```
37+
38+
### Steps to migrate
39+
40+
- Update the `main.go` with the changes which can be found in the samples under testdata for the release tag used. (see for example `testdata/project-v4/main.go`).
41+
- Update the Makefile with the changes which can be found in the samples under testdata for the release tag used. (see for example `testdata/project-v4/Makefile`)
42+
- Update the `go.mod` with the changes which can be found in the samples under `testdata` for the release tag used. (see for example `testdata/project-v4/go.mod`). Then, run
43+
`go mod tidy` to ensure that you get the latest dependencies and your Golang code has no breaking changes.
44+
- Update the manifest under `config/` directory with all changes performed in the default scaffold done with `go/v4-alpha` plugin. (see for example `testdata/project-v4/config/`) to get all changes in the
45+
default scaffolds to be applied on your project
46+
- Replace the import `admissionv1beta1 "k8s.io/api/admission/v1beta1"` with `admissionv1 "k8s.io/api/admission/v1"` in the webhook test files
47+
48+
<aside class="warning">
49+
<h1>`config/` directory with changes into the scaffold files</h1>
50+
51+
Note that under the `config/` directory you will find scaffolding changes since using
52+
`go/v4-alpha` you will ensure that you are no longer using Kustomize v3x.
53+
54+
You can mainly compare the `config/` directory from the samples scaffolded under the `testdata`directory by
55+
checking the differences between the `testdata/project-v3/config/` with `testdata/project-v4/config/` which
56+
are samples created with the same commands with the only difference being versions.
57+
58+
However, note that if you create your project with Kubebuilder CLI 3.0.0, its scaffolds
59+
might change to accommodate changes up to the latest releases using `go/v3` which are not considered
60+
breaking for users and/or are forced by the changes introduced in the dependencies
61+
used by the project such as [controller-runtime][controller-runtime] and [controller-tools][controller-tools].
62+
63+
</aside>
64+
65+
### Verification
66+
67+
In the steps above, you updated your project manually with the goal of ensuring that it follows
68+
the changes in the layout introduced with the `go/v4-alpha` plugin that update the scaffolds.
69+
70+
There is no option to verify that you properly updated the `PROJECT` file of your project.
71+
The best way to ensure that everything is updated correctly, would be to initialize a project using the `go/v4-alpha` plugin,
72+
(ie) using `kubebuilder init --domain tutorial.kubebuilder.io plugins=go/v4-alpha` and generating the same API(s),
73+
controller(s), and webhook(s) in order to compare the generated configuration with the manually changed configuration.
74+
75+
Also, after all updates you would run the following commands:
76+
77+
- `make manifests` (to re-generate the files using the latest version of the contrller-gen after you update the Makefile)
78+
- `make all` (to ensure that you are able to build and perform all operations)
79+
80+
[v3vsv4]: v3vsv4.md
81+
[quick-start]: ./../quick-start.md#installation
82+
[migration-guide-gov3-to-gov4]: migration_guide_gov3_to_gov4.md
83+
[controller-tools]: https://github.com/kubernetes-sigs/controller-tools/releases
84+
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime/releases
85+
[multi-group]: multi-group.md
86+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Migration from go/v3 to go/v4-alpha
2+
3+
Make sure you understand the [differences between Kubebuilder go/v3 and go/v4-alpha][v3vsv4]
4+
before continuing.
5+
6+
Please ensure you have followed the [installation guide][quick-start]
7+
to install the required components.
8+
9+
The recommended way to migrate a go/v3 project is to create a new go/v4-alpha project and
10+
copy over the API and the reconciliation code. The conversion will end up with a
11+
project that looks like a native go/v4-alpha project layout (latest version).
12+
13+
However, in some cases, it's possible to do an in-place upgrade (i.e. reuse the go/v3 project layout, upgrading
14+
the PROJECT file, and scaffolds manually). For further information see [Migration from go/v3 to go/v4-alpha by updating the files manually][manually-upgrade]
15+
16+
## Initialize a go/v4-alpha Project
17+
18+
<aside class="note">
19+
<h1>Project name</h1>
20+
21+
For the rest of this document, we are going to use `migration-project` as the project name and `tutorial.kubebuilder.io` as the domain. Please, select and use appropriate values for your case.
22+
23+
</aside>
24+
25+
Create a new directory with the name of your project. Note that
26+
this name is used in the scaffolds to create the name of your manager Pod and of the Namespace where the Manager is deployed by default.
27+
28+
```bash
29+
$ mkdir migration-project-name
30+
$ cd migration-project-name
31+
```
32+
33+
Now, we need to initialize a go/v4-alpha project. Before we do that, we'll need
34+
to initialize a new go module if we're not on the `GOPATH`. While technically this is
35+
not needed inside `GOPATH`, it is still recommended.
36+
37+
```bash
38+
go mod init tutorial.kubebuilder.io/migration-project
39+
```
40+
41+
<aside class="note">
42+
<h1>The module of your project can found in the `go.mod` file at the root of your project:</h1>
43+
44+
```
45+
module tutorial.kubebuilder.io/migration-project
46+
```
47+
48+
</aside>
49+
50+
Now, we can finish initializing the project with kubebuilder.
51+
52+
```bash
53+
kubebuilder init --domain tutorial.kubebuilder.io plugins=go/v4-alpha
54+
```
55+
56+
<aside class="note">
57+
<h1>The domain of your project can be found in the PROJECT file:</h1>
58+
59+
```yaml
60+
...
61+
domain: tutorial.kubebuilder.io
62+
...
63+
```
64+
</aside>
65+
66+
## Migrate APIs and Controllers
67+
68+
Next, we'll re-scaffold out the API types and controllers.
69+
70+
<aside class="note">
71+
<h1>Scaffolding both the API types and controllers</h1>
72+
73+
For this example, we are going to consider that we need to scaffold both the API types and the controllers, but remember that this depends on how you scaffolded them in your original project.
74+
75+
</aside>
76+
77+
```bash
78+
kubebuilder create api --group batch --version v1 --kind CronJob
79+
```
80+
81+
### Migrate the APIs
82+
83+
<aside class="note">
84+
<h1>If you're using multiple groups</h1>
85+
86+
Please run `kubebuilder edit --multigroup=true` to enable multi-group support before migrating the APIs and controllers. Please see [this][multi-group] for more details.
87+
88+
</aside>
89+
90+
Now, let's copy the API definition from `api/v1/<kind>_types.go` in our old project to the new one.
91+
92+
These files have not been modified by the new plugin, so you should be able to replace your freshly scaffolded files by your old one. There may be some cosmetic changes. So you can choose to only copy the types themselves.
93+
94+
### Migrate the Controllers
95+
96+
Now, let's migrate the controller code from `controllers/cronjob_controller.go` in our old project to the new one.
97+
98+
## Migrate the Webhooks
99+
100+
<aside class="note">
101+
<h1>Skip</h1>
102+
103+
If you don't have any webhooks, you can skip this section.
104+
105+
</aside>
106+
107+
Now let's scaffold the webhooks for our CRD (CronJob). We'll need to run the
108+
following command with the `--defaulting` and `--programmatic-validation` flags
109+
(since our test project uses defaulting and validating webhooks):
110+
111+
```bash
112+
kubebuilder create webhook --group batch --version v1 --kind CronJob --defaulting --programmatic-validation
113+
```
114+
115+
Now, let's copy the webhook definition from `api/v1/<kind>_webhook.go` from our old project to the new one.
116+
117+
## Others
118+
119+
If there are any manual updates in `main.go` in v3, we need to port the changes to the new `main.go`. We’ll also need to ensure all of needed controller-runtime `schemes` have been registered.
120+
121+
If there are additional manifests added under config directory, port them as well. Please, be aware that
122+
the new version go/v4-alpha uses Kustomize v4x and no longer Kustomize v3. Therefore, if added customized
123+
implementations in the config you need to ensure that them can work with Kustomize v4 and/if not
124+
update/upgrade any breaking change that you might face.
125+
126+
Change the image name in the Makefile if needed.
127+
128+
## Verification
129+
130+
Finally, we can run `make` and `make docker-build` to ensure things are working
131+
fine.
132+
133+
[v3vsv4]: v3vsv4.md
134+
[quick-start]: ./../quick-start.md#installation
135+
[controller-tools]: https://github.com/kubernetes-sigs/controller-tools/releases
136+
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime/releases
137+
[multi-group]: multi-group.md
138+
[manually-upgrade]: manually_migration_guide_gov3_to_gov4.md

docs/book/src/migration/multi-group.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ to modify the default project structure to support it.
2020

2121
Let's migrate the [CronJob example][cronjob-tutorial].
2222

23+
<aside class="note warning">
24+
<h1>Using go/v4-alpha</h1>
25+
26+
If you create your project using go/v4-alpha plugin (you can verify it by looking at the PROJECT file )
27+
then, all steps are the same but you need to keep in mind that the api and controllers directory
28+
path is now under the `pkg` directory instead. So, you need ensure that you update the
29+
paths accordingly.
30+
31+
</aside>
32+
2333
Generally, we use the prefix for the API group as the directory name. We
2434
can check `api/v1/groupversion_info.go` to find that out:
2535

@@ -46,6 +56,8 @@ mkdir controllers/batch
4656
mv controllers/* controllers/batch/
4757
```
4858

59+
60+
4961
Next, we'll need to update all the references to the old package name.
5062
For CronJob, that'll be `main.go` and `controllers/batch/cronjob_controller.go`.
5163

0 commit comments

Comments
 (0)