|
| 1 | +# Single Group to Multi-Group |
| 2 | + |
| 3 | +<aside class="note warning"> |
| 4 | + |
| 5 | +<h1>Work In Progress: Manual Work Incoming</h1> |
| 6 | + |
| 7 | +Multi-group scaffolding support is not present in the initial version of |
| 8 | +the KubeBuilder v2 scaffolding (as of KubeBuilder v2.0.0). |
| 9 | + |
| 10 | +Once you switch to a multi-group layout, additional manual work will be |
| 11 | +needed to create new API groups. |
| 12 | + |
| 13 | +Support will eventually be added for scaffolding -- see |
| 14 | +[kubernetes-sigs/kubebuilder#923][multi-group-issue] for more information. |
| 15 | + |
| 16 | +</aside> |
| 17 | + |
| 18 | +While KubeBuilder v2 will not scaffold out a project structure compatible |
| 19 | +with multiple API groups in the same repository by default, it's possible |
| 20 | +to modify the default project structure to support it. |
| 21 | + |
| 22 | +Let's migrate the [CronJob example][cronjob-tutorial]. |
| 23 | + |
| 24 | +Generally, we use the prefix for the API group as the directory name. We |
| 25 | +can check `api/v1/groupversion_info.go` to find that out: |
| 26 | + |
| 27 | +```go |
| 28 | +// +groupName=batch.tutorial.kubebuilder.io |
| 29 | +package v1 |
| 30 | +``` |
| 31 | + |
| 32 | +Then, we'll rename `api` to `apis` to be more clear, and we'll move our |
| 33 | +existing APIs into a new subdirectory, "batch": |
| 34 | + |
| 35 | +```bash |
| 36 | +mv api apis |
| 37 | +mkdir apis/batch |
| 38 | +mv apis/v* batch/ |
| 39 | +``` |
| 40 | + |
| 41 | +Next, we'll need to update all the references to the old package name. For |
| 42 | +CronJob, that'll be `main.go` and `controllers/cronjob_controller.go`. If |
| 43 | +you've added additional files to your project, you'll need to track down |
| 44 | +imports there as well. |
| 45 | + |
| 46 | +Finally, we'll add a new line to `PROJECT` that marks this as |
| 47 | +a multi-group project: |
| 48 | + |
| 49 | +```yaml |
| 50 | +version: "2" |
| 51 | +domain: tutorial.kubebuilder.io |
| 52 | +repo: tutorial.kubebuilder.io/project |
| 53 | +multigroup: true |
| 54 | +``` |
| 55 | +
|
| 56 | +While this option doesn't do anything right now, it'll indicate to |
| 57 | +KubeBuilder in future versions that this is a multi-group project. |
| 58 | +
|
| 59 | +Now that we've migrated this to a multi-group project, normal KubeBuilder |
| 60 | +scaffolding won't work (as of KubeBuilder v2.0.0 -- see the warning at the |
| 61 | +top of this page). You'll need to copy the setup that KubeBuilder |
| 62 | +normally does to add new groups for the moment: |
| 63 | +
|
| 64 | +- Copy over `<kind>_types.go` and `groupversion_info.go` to |
| 65 | + `apis/<group>/<version>` |
| 66 | + |
| 67 | +- Register the new group-version with the Scheme in `main.go` |
| 68 | + |
| 69 | +- Add a new `<kind>_controller.go` file |
| 70 | + |
| 71 | +- Add the controller to the manager in `main.go` |
| 72 | + |
| 73 | +The [CronJob tutorial][cronjob-tutorial] explains each of these changes in |
| 74 | +more detail (in the context of how they're generated by KubeBuilder for |
| 75 | +single-group projects). |
| 76 | + |
| 77 | +[multi-group-issue]: https://github.com/kubernetes-sigs/kubebuilder/issues/923 "KubeBuilder Issue #923" |
| 78 | + |
| 79 | +[cronjob-tutorial]: /cronjob-tutorial/cronjob-tutorial.md "Tutorial: Building CronJob" |
0 commit comments