Skip to content

Commit

Permalink
doc: Fix multigroup migration guide for v4
Browse files Browse the repository at this point in the history
Co-authored-by: Varsha <varshaprasad96@gmail.com>
Signed-off-by: Juan-Luis de Sousa-Valadas Castaño <jvaladas@mirantis.com>
  • Loading branch information
juanluisvaladas and varshaprasad96 committed May 11, 2023
1 parent 6c11f05 commit e812a43
Showing 1 changed file with 106 additions and 18 deletions.
124 changes: 106 additions & 18 deletions docs/book/src/migration/multi-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ the Kubebuilder v2 scaffolding (as of Kubebuilder v2.0.0).

To change the layout of your project to support Multi-Group run the command
`kubebuilder edit --multigroup=true`. Once you switch to a multi-group layout, the new Kinds
will be generated in the new layout but additional manual work is needed
will be generated in the new layout but additional manual work is needed
to move the old API groups to the new layout.

</aside>
Expand All @@ -21,15 +21,14 @@ to modify the default project structure to support it.
Let's migrate the [CronJob example][cronjob-tutorial].

<aside class="note warning">
<h1>Using go/v4</h1>

If you create your project using go/v4 plugin (you can verify it by looking at the PROJECT file )
then, all steps are the same but you need to keep in mind that the api and controllers directory
path is now under the `pkg` directory instead. So, you need ensure that you update the
paths accordingly.
<h1>Using go/v3 or go/v4</h1>

The instructions vary depending on the project layout. You can verify the version by looking at
the PROJECT file.
</aside>

<h1>Using Go/v3</h1>

Generally, we use the prefix for the API group as the directory name. We
can check `api/v1/groupversion_info.go` to find that out:

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

```bash
mkdir apis/batch
mkdir -p apis/batch
mv api/* apis/batch
# After ensuring that all was moved successfully remove the old directory `api/`
rm -rf api/
rm -rf api/
```


Expand All @@ -58,8 +57,8 @@ mv controllers/* controllers/batch/



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

If you've added additional files to your project, you'll need to track down
imports there as well.
Expand All @@ -70,23 +69,23 @@ Finally, we'll run the command which enable the multi-group layout in the projec
kubebuilder edit --multigroup=true
```

When the command `kubebuilder edit --multigroup=true` is executed it will add a new line
When the command `kubebuilder edit --multigroup=true` is executed it will add a new line
to `PROJECT` that marks this a multi-group project:

```yaml
version: "2"
domain: tutorial.kubebuilder.io
repo: tutorial.kubebuilder.io/project
multigroup: true
```

Note that this option indicates to Kubebuilder that this is a multi-group project.
Note that this option indicates to Kubebuilder that this is a multi-group project.

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

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.
Expand All @@ -104,3 +103,92 @@ single-group projects).

[multi-group-issue]: https://github.com/kubernetes-sigs/kubebuilder/issues/923 "Kubebuilder Issue #923"
[cronjob-tutorial]: /cronjob-tutorial/cronjob-tutorial.md "Tutorial: Building CronJob"

<h1>Using Go/v4</h1>

Generally, we use the prefix for the API group as the directory name. We
can check `api/v1/groupversion_info.go` to find that out:

```go
// +groupName=batch.tutorial.kubebuilder.io
package v1
```

Then, we'll rename move our existing APIs into a new subdirectory, "batch":

```bash
mv api/* api/batch
```


After moving the APIs to a new directory, the same needs to be applied to the controllers:

```bash
mkdir internal/controller/batch
mv internal/controller/* internal/controller/batch/
```



Next, we'll need to update all the references to the old package name.
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.

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

Finally, fix the PROJECT file manually, the command `kubebuilder edit --multigroup=true`
sets our project to multigroup, but it doesn't fix the path of the existing APIs.
For each resource we need to modify the path.

For instance, for a file:

```yaml
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: tutorial.kubebuilder.io
layout:
- go.kubebuilder.io/v4
multigroup: true
projectName: test
repo: tutorial.kubebuilder.io/project
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: tutorial.kubebuilder.io
group: batch
kind: CronJob
path: tutorial.kubebuilder.io/project/api/v1beta1
version: v1beta1
version: "3"
```

Replace `path: tutorial.kubebuilder.io/project/api/v1beta1` for
`path: tutorial.kubebuilder.io/project/api/batch/v1beta1`


In this process, if the project is not new and has previous implemented APIs they would still need to be modified as needed.
Notice that with the `multi-group` project the Kind API's files are created under `api/<group>/<version>` instead of `api/<version>`.
Also, note that the controllers will be created under `internal/controller/<group>` instead of `internal/controller`.

That is the reason why we moved the previously generated APIs to their respective locations in the new structure.
Remember to update the references in imports accordingly.

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.

```go
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
}
```

The [CronJob tutorial][cronjob-tutorial] explains each of these changes in
more detail (in the context of how they're generated by Kubebuilder for
single-group projects).

[multi-group-issue]: https://github.com/kubernetes-sigs/kubebuilder/issues/923 "Kubebuilder Issue #923"
[cronjob-tutorial]: /cronjob-tutorial/cronjob-tutorial.md "Tutorial: Building CronJob"

0 comments on commit e812a43

Please sign in to comment.