Skip to content

Commit 7711b8f

Browse files
juanluisvaladasvarshaprasad96camilamacedo86
authored andcommitted
doc: Fix multigroup migration guide for v4
Co-authored-by: Varsha <[email protected]> Co-authored-by: Camila Macedo <[email protected]> Signed-off-by: Juan-Luis de Sousa-Valadas Castaño <[email protected]>
1 parent 804fed4 commit 7711b8f

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

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

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44

55
<h1>Note</h1>
66

7-
Multi-group scaffolding support was not present in the initial version of
8-
the Kubebuilder v2 scaffolding (as of Kubebuilder v2.0.0).
7+
While Kubebuilder will not scaffold out a project structure compatible
8+
with multiple API groups in the same repository by default, it's possible
9+
to modify the default project structure to support it.
910

10-
To change the layout of your project to support Multi-Group run the command
11-
`kubebuilder edit --multigroup=true`. Once you switch to a multi-group layout, the new Kinds
12-
will be generated in the new layout but additional manual work is needed
13-
to move the old API groups to the new layout.
11+
Note that the process mainly is to ensure that your API(s) and controller(s) will be moved under new directories with their respective group name.
1412

1513
</aside>
1614

17-
While Kubebuilder v2 will not scaffold out a project structure compatible
18-
with multiple API groups in the same repository by default, it's possible
19-
to modify the default project structure to support it.
20-
2115
Let's migrate the [CronJob example][cronjob-tutorial].
2216

2317
<aside class="note warning">
24-
<h1>Using go/v4</h1>
18+
<h1>Instructions vary per project layout</h1>
19+
20+
You can verify the version by looking at the PROJECT file. The currently default and
21+
recommended version is go/v4.
2522

26-
If you create your project using go/v4 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.
23+
The layout go/v3 is **deprecated**, if you are using go/v3 it is recommended that you migrate to
24+
go/v4, however this documentation is still valid. [Migration from go/v3 to go/v4][migration-guide].
3025

3126
</aside>
3227

28+
To change the layout of your project to support Multi-Group run the command
29+
`kubebuilder edit --multigroup=true`. Once you switch to a multi-group layout, the new Kinds
30+
will be generated in the new layout but additional manual work is needed
31+
to move the old API groups to the new layout.
32+
3333
Generally, we use the prefix for the API group as the directory name. We
3434
can check `api/v1/groupversion_info.go` to find that out:
3535

@@ -38,58 +38,75 @@ can check `api/v1/groupversion_info.go` to find that out:
3838
package v1
3939
```
4040

41-
Then, we'll rename `api` to `apis` to be more clear, and we'll move our
42-
existing APIs into a new subdirectory, "batch":
41+
Then, we'll rename move our existing APIs into a new subdirectory, "batch":
4342

4443
```bash
45-
mkdir apis/batch
46-
mv api/* apis/batch
47-
# After ensuring that all was moved successfully remove the old directory `api/`
48-
rm -rf api/
44+
mkdir api/batch
45+
mv api/* api/batch
4946
```
5047

51-
52-
After moving the APIs to a new directory, the same needs to be applied to the controllers:
48+
After moving the APIs to a new directory, the same needs to be applied to the controllers. For go/v4:
5349

5450
```bash
55-
mkdir controllers/batch
56-
mv controllers/* controllers/batch/
51+
mkdir internal/controller/batch
52+
mv internal/controller/* internal/controller/batch/
5753
```
5854

55+
<aside class="note">
56+
<h1>If you are using the deprecated layout go/v3</h1>
57+
Then, your layout has not the internal directory. So, you will move the controller(s) under a directory with the name of the API group which it is responsible for manage.
58+
```bash
59+
mkdir controller/batch
60+
mv controller/* controller/batch/
61+
```
5962

60-
61-
Next, we'll need to update all the references to the old package name.
62-
For CronJob, that'll be `main.go` and `controllers/batch/cronjob_controller.go`.
63+
Next, we'll need to update all the references to the old package name.
64+
For CronJob, the paths to be replaced would be `main.go` and `controllers/batch/cronjob_controller.go` to their respective locations in the new project structure.
6365

6466
If you've added additional files to your project, you'll need to track down
6567
imports there as well.
6668

67-
Finally, we'll run the command which enable the multi-group layout in the project:
69+
Finally, fix the PROJECT file manually, the command `kubebuilder edit --multigroup=true`
70+
sets our project to multigroup, but it doesn't fix the path of the existing APIs.
71+
For each resource we need to modify the path.
6872

69-
```
70-
kubebuilder edit --multigroup=true
71-
```
73+
For instance, for a file:
7274

73-
When the command `kubebuilder edit --multigroup=true` is executed it will add a new line
74-
to `PROJECT` that marks this a multi-group project:
75-
7675
```yaml
77-
version: "2"
76+
# Code generated by tool. DO NOT EDIT.
77+
# This file is used to track the info used to scaffold your project
78+
# and allow the plugins properly work.
79+
# More info: https://book.kubebuilder.io/reference/project-config.html
7880
domain: tutorial.kubebuilder.io
79-
repo: tutorial.kubebuilder.io/project
81+
layout:
82+
- go.kubebuilder.io/v4
8083
multigroup: true
84+
projectName: test
85+
repo: tutorial.kubebuilder.io/project
86+
resources:
87+
- api:
88+
crdVersion: v1
89+
namespaced: true
90+
controller: true
91+
domain: tutorial.kubebuilder.io
92+
group: batch
93+
kind: CronJob
94+
path: tutorial.kubebuilder.io/project/api/v1beta1
95+
version: v1beta1
96+
version: "3"
8197
```
8298
83-
Note that this option indicates to Kubebuilder that this is a multi-group project.
99+
Replace `path: tutorial.kubebuilder.io/project/api/v1beta1` for
100+
`path: tutorial.kubebuilder.io/project/api/batch/v1beta1`
101+
102+
In this process, if the project is not new and has previous implemented APIs they would still need to be modified as needed.
103+
Notice that with the `multi-group` project the Kind API's files are created under `api/<group>/<version>` instead of `api/<version>`.
104+
Also, note that the controllers will be created under `internal/controller/<group>` instead of `internal/controller`.
84105

85-
In this way, if the project is not new and has previous APIs already implemented will be in the previous structure.
86-
Notice that with the `multi-group` project the Kind API's files are
87-
created under `apis/<group>/<version>` instead of `api/<version>`.
88-
Also, note that the controllers will be created under `controllers/<group>` instead of `controllers`.
89-
That is the reason why we moved the previously generated APIs with the provided scripts in the previous steps.
90-
Remember to update the references afterwards.
106+
That is the reason why we moved the previously generated APIs to their respective locations in the new structure.
107+
Remember to update the references in imports accordingly.
91108

92-
For envtest to install CRDs correctly into the test environment, the relative path to the CRD directory needs to be updated accordingly in each `controllers/<group>/suite_test.go` file. We need to add additional `".."` to our CRD directory relative path as shown below.
109+
For envtest to install CRDs correctly into the test environment, the relative path to the CRD directory needs to be updated accordingly in each `internal/controller/<group>/suite_test.go` file. We need to add additional `".."` to our CRD directory relative path as shown below.
93110

94111
```go
95112
By("bootstrapping test environment")
@@ -104,3 +121,4 @@ single-group projects).
104121

105122
[multi-group-issue]: https://github.com/kubernetes-sigs/kubebuilder/issues/923 "Kubebuilder Issue #923"
106123
[cronjob-tutorial]: /cronjob-tutorial/cronjob-tutorial.md "Tutorial: Building CronJob"
124+
[migration-guide]: /migration/migration_guide_gov3_to_gov4.md "Migration from go/v3 to go/v4"

0 commit comments

Comments
 (0)