kubernetes-sigs / kubebuilder

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

Cannot create controller for non-core APIs/CRDs #1575

Closed harpratap closed 4 years ago

harpratap commented 4 years ago

I want to create just a controller for a CRD that already exists, so followed the advice given here - https://github.com/kubernetes-sigs/kubebuilder/issues/1270 it works for core types like svc and pods but fails when you try on a custom type.

I expect the controller to run without any errors just like it did for the core types.

kubebuilder init --domain istio.io
kubebuilder create api --group networking.istio.io --version v1alpha3 --kind Gateway
Create Resource [y/n]
n
Create Controller [y/n]
y
Writing scaffold for you to edit...
controllers/gateway_controller.go
Running make:
$ make
go: creating new go.mod: module tmp
go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.2.5
/Users/harpratap.layal/go/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
main.go:29:2: package gateway-controller/api/v1alpha3 is not in GOROOT (/usr/local/Cellar/go/1.14.2/libexec/src/gateway-controller/api/v1alpha3)
Error: not all generators ran successfully
run `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./... -w` to see all available markers, or `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./... -h` for usage
make: *** [generate] Error 1
2020/06/25 18:05:30 failed to create API: exit status 2

After removing all API related code -

> make run
go: creating new go.mod: module tmp
go: found sigs.k8s.io/controller-tools/cmd/controller-gen in sigs.k8s.io/controller-tools v0.2.5
/Users/harpratap.layal/go/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
package gateway-controller/controllers (test): package gateway-controller/api/v1alpha3 is not in GOROOT (/usr/local/Cellar/go/1.14.2/libexec/src/gateway-controller/api/v1alpha3)
# sigs.k8s.io/controller-runtime/pkg/metrics
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/metrics/client_go_adapter.go:120:24: too many arguments in call to metrics.Register
    have (*latencyAdapter, *resultAdapter)
    want (metrics.RegisterOpts)
# sigs.k8s.io/controller-runtime/pkg/client
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:45:62: o.resourceMeta.Interface.Post().NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Body(obj).VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:65:62: o.resourceMeta.Interface.Put().NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Name(o.Object.GetName()).Body(obj).VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:85:37: o.resourceMeta.Interface.Delete().NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Name(o.Object.GetName()).Body(deleteOpts.AsDeleteOptions()).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:105:42: o.resourceMeta.Interface.Delete().NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).VersionedParams(deleteAllOfOpts.ListOptions.AsListOptions(), c.paramCodec).Body(deleteAllOfOpts.DeleteOptions.AsDeleteOptions()).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:129:13: o.resourceMeta.Interface.Patch(patch.Type()).NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Name(o.Object.GetName()).VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).Body(data).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:143:25: r.Interface.Get().NamespaceIfScoped(key.Namespace, r.isNamespaced()).Resource(r.resource()).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:159:58: r.Interface.Get().NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).Resource(r.resource()).VersionedParams(listOpts.AsListOptions(), c.paramCodec).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:181:89: o.resourceMeta.Interface.Put().NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Name(o.Object.GetName()).SubResource("status").Body(obj).VersionedParams((&UpdateOptions literal).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/typed_client.go:206:79: o.resourceMeta.Interface.Patch(patch.Type()).NamespaceIfScoped(o.Object.GetNamespace(), o.resourceMeta.isNamespaced()).Resource(o.resourceMeta.resource()).Name(o.Object.GetName()).SubResource("status").Body(data).VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).Context undefined (type *rest.Request has no field or method Context)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/unstructured_client.go:51:20: not enough arguments in call to r.Create
    have (*unstructured.Unstructured, v1.CreateOptions)
    want (context.Context, *unstructured.Unstructured, v1.CreateOptions, ...string)
../../go/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/client/unstructured_client.go:51:20: too many errors
make: *** [vet] Error 2

What versions of software are you using? Specifically, the following are often useful:

/kind bug

camilamacedo86 commented 4 years ago

It shows the same as https://github.com/kubernetes-sigs/kubebuilder/issues/786 (duplicated). The project requires fo 1.13. Also, see:

This issue is caused by go modules.
You need to set GO111MODULE=on to use go modules when in GOPATH.
harpratap commented 4 years ago

@camilamacedo86 I see. After switching to go 1.13.12 and setting GO111MODULE=on it seems to be trying to fetch the api package. Error: go [list -e -json -compiled=true -test=false -export=false -deps=true -find=false -tags ignore_autogenerated -- ./...]: exit status 1: go: finding istio.io/api latest build istio.io: cannot load istio.io/api/v1alpha3: module istio.io/api@latest found (v0.0.0-20200626203124-c07d1d63dab7), but does not contain package istio.io/api/v1alpha3

  1. It seems to fetching with a fixed format /api/\ but what if the following a different folder structure? like /api/\/\?

  2. What if there's no Go code at all to fetch the api from? What if the CRD is just a yaml? How to create controller for it?

camilamacedo86 commented 4 years ago

Hi @harpratap,

The istio.io/api/v1alpha3 shows are related to your implementation. Following some comments inline which I hope that helps you with.

It seems to fetching with a fixed format /api/ but what if the following a different folder structure? like /api//?

What if there's no Go code at all to fetch the api from? What if the CRD is just a yaml? How to create controller for it?

When following the tutorial, one of the first steps is to run the command kubebuilder create api. The purpose of this command is to generate Custom Resource (CR) and Custom Resource Definition (CRD) resources for the Kind. This command is creating the API with the group, and version which uniquely identifies the new CRD of the Kind.

Consequently, by using the tool, we can create our APIs and objects that will represent our solutions on these platforms. The quick-start adds only a single kind of resource; however, it could have as many Kinds as needed (1…N). Basically, the CRDs are a definition of our customized Objects, and the CRs are an instance of it.

In this way, If you are creating the API via kubebuilder to use it in your project it will generate the <kind>_types.go where you will define its specifications and status. So, based on your question, I assume that you are trying to use an API defined in another project which it is achievable. For further info, I'd recommend you check the blog How to use third-party APIs in Operator SDK projects. Note that the solution is done using the SDK tool to scaffold the project, however, the same could be applied in a project built with Kubebuilder as well.

camilamacedo86 commented 4 years ago

/assign @camilamacedo86 /kind bug cancel /kind support-triagge

k8s-ci-robot commented 4 years ago

@camilamacedo86: The label(s) kind/cancel, kind/support-triagge cannot be applied, because the repository doesn't have them

In response to [this](https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-651038556): >/assign @camilamacedo86 >/kind bug cancel >/kind support-triagge Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
camilamacedo86 commented 4 years ago

I am closing this since the main question of your issue is solved with https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-650790093 and we already started the second wave of questions which I believe that is sorted out with https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-651038192.

However, please feel free to raise new issues as well. Let's try to keep 1(one) subject 1 per issue. Because it allow answers more assertive an have the info tracked a better way to help others as well.

/close

k8s-ci-robot commented 4 years ago

@camilamacedo86: Closing this issue.

In response to [this](https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-651076060): >I am closing this since the main question of your issue is solved with https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-650790093 and we already started the second wave of questions which I believe that is sorted out with https://github.com/kubernetes-sigs/kubebuilder/issues/1575#issuecomment-651038192. > >However, please feel free to raise new issues as well. Let's try to keep 1(one) subject 1 per issue. Because it allow answers more assertive an have the info tracked a better way to help others as well. > >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.