kubernetes-sigs / kubebuilder

Kubebuilder - SDK for building Kubernetes APIs using CRDs
http://book.kubebuilder.io
Apache License 2.0
7.77k stars 1.43k forks source link

Kubebuilder with multi-group and same Kind names doesn't generate proper crd/patch and rbac manifests #2495

Closed rmb938 closed 2 years ago

rmb938 commented 2 years ago

What broke? What's expected?

In a multi-group project when 2 groups have the same CRD name for example

---
apiVersion: foo.rmb938.com/v1alpha1
kind: Bar
...
---
apiVersion: faz.rmb938.com/v1alpha1
kind: Bar

The crd patches generated in config/crd/patches and the rbac generated (for viewer and editor) in config/rbac are only for the first group and kind that gets generated. They should be generated for all groups.

Reproducing this issue

Run the following

kubebuilder create api --controller=false --group foo --version v1alpha1 --kind Bar
kubebuilder create api --controller=false --group faz --version v1alpha1 --kind Bar

KubeBuilder (CLI) Version

Version: main.version{KubeBuilderVersion:"3.3.0", KubernetesVendor:"1.23.1", GitCommit:"47859bf2ebf96a64db69a2f7074ffdec7f15c1ec", BuildDate:"2022-01-18T17:03:29Z", GoOs:"darwin", GoArch:"amd64"}

PROJECT version

3

Plugin versions

layout:
- go.kubebuilder.io/v3

Other versions

 %  go version
go version go1.17.3 darwin/amd64
sigs.k8s.io/controller-runtime v0.11.0
sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0
%  kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T21:51:21Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.5-gke.1302", GitCommit:"639f3a74abf258418493e9b75f2f98a08da29733", GitTreeState:"clean", BuildDate:"2021-10-21T21:35:48Z", GoVersion:"go1.16.7b7", Compiler:"gc", Platform:"linux/amd64"}
%  kustomize version
{Version:kustomize/v4.4.0 GitCommit:63ec6bdb3d737a7c66901828c5743656c49b60e1 BuildDate:2021-09-27T16:13:36Z GoOs:darwin GoArch:amd64}

Extra Labels

No response

camilamacedo86 commented 2 years ago

Hi @rmb938,

Could you please provide some additional info?

1) Are the multi-group support enabled in your project? see: https://book.kubebuilder.io/migration/multi-group.html 2) Could you please copy and paste here the PROJECT file content?

See that we have sample projects generated at testdata/ directory and I could not find the issue raised by you in the project which has multi-group support (see, kubebuilder/testdata/project-v3-multigroup).

The crd patches generated in config/crd/patches and the rbac generated (for viewer and editor) in config/rbac are only for the first group and kind that gets generated. They should be generated for all groups.

Note the config/crd/patches generated for all groups:

Also, see the rbac generated for all: https://github.com/kubernetes-sigs/kubebuilder/tree/master/testdata/project-v3-multigroup/config/rbac

If you want see how the sample/testdata project is generated see the script: https://github.com/kubernetes-sigs/kubebuilder/blob/master/test/testdata/generate.sh

rmb938 commented 2 years ago

I created a kubebuilder project and push it to a repo for easy reference.

PROJECT file: https://github.com/rmb938/multi-group-test/blob/main/PROJECT as you can see there's 2 groups each with a kind that is named the same.

APIs https://github.com/rmb938/multi-group-test/tree/main/apis

Controllers https://github.com/rmb938/multi-group-test/tree/main/controllers

CRD Patches https://github.com/rmb938/multi-group-test/tree/main/config/crd/patches Notice they only exist for the single group foo, the one for group fiz does not exist.

RBAC https://github.com/rmb938/multi-group-test/tree/main/config/rbac notice that the editor and viewer roles only exist for foo not for fiz.

You can replicate this project by running the following commands:

kubebuilder init --domain rmb938.com --repo github.com/rmb938/multi-group-test
kubebuilder edit --multigroup=true
kubebuilder create api --group foo --version v1alpha1 --kind Bar
kubebuilder create api --group fiz --version v1alpha1 --kind Bar
make generate
make manifests
camilamacedo86 commented 2 years ago

Hi @rmb938,

Thank you for the clarifications

That shows that when the multi-group support is enabled we are not using the group in the name of the files and generating them. So we need to:

a) Ensure that we will consider the group to do the scaffolds

Such as, change the boilerplates to use the group, see: https://github.com/kubernetes-sigs/kubebuilder/tree/master/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches. In the same way that we do here (https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/v2/scaffolds/internal/templates/api/group.go#L37-L43). ( The same implementation actually)

b) Update the sample/testdata to have the same kind for different groups to ensure the scenario The change must be done in: https://github.com/kubernetes-sigs/kubebuilder/blob/master/test/testdata/generate.sh for the sample kubebuilder/testdata/project-v3-multigroup.

Could you help out with this fix? WDYT about a collab with a PR to fix this scenario?

rmb938 commented 2 years ago

Yup I can get that done. I've been working on some internal kubebuilder scaffolds myself so I'm familiar with the modifications. I'll get the changes committed and a PR ready.