Missing "k8s.io/apimachinery/pkg/runtime" import in controller scaffolding.
I think my IDE added the import automatically as I can't find it in the scaffold file.
https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/scaffold/v2/controller.go#L71-L86
/kind bug
@camilamacedo86
Yes, this is a bug.
Thanks for bringing this up.
It seems @Adirio has already started to work on this in #1120.
@mengqiy I am wondering why isn't this caught by our tests ?
Hi @mengqiy, @droot and @Adirio,
I agree that makes sense the import be there besides all work and I am unable to reproduce the issue raised. So, how I saw that @Adirio has been facing issues with the golden script I did PR for we fix it. I hope that helps you @Adirio. See: https://github.com/kubernetes-sigs/kubebuilder/pull/1130
Note that I tested it locally and I could not find any issue regards to it. The import will be added in the controller as it was done by the golden script here in the testdata. Because of this all passed in the CI @droot.
Following the steps to check it.
$ mkdir example
$ cd example/
$ kubebuilder init --domain my.domain
$ kubebuilder create api --group webapp --version v1 --kind Guestbook
vim controllers/guestbook_controller.gopackage controllers
import (
"context"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
webappv1 "sigs.k8s.io/example/api/v1"
)
// GuestbookReconciler reconciles a Guestbook object
type GuestbookReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
See that "k8s.io/apimachinery/pkg/runtime" is in the imports and it is what is used in Scheme *runtime.Scheme.
So, I believe that was because of this that I did not add the import here: https://github.com/camilamacedo86/kubebuilder/blob/d1565e75cd4b8ade796baa590523b9681ff83fad/pkg/scaffold/v2/controller.go#L74. In some way, shows that it is injected in the code.
The reason why missing import haven't cause us problem in test is that the generated go code go through goimport. After processing, goimports seems to quite smart to add the proper imports.
We should not rely on goimports to add that for us.
Thanks @camilamacedo86 for the explanation.
Most helpful comment
The reason why missing import haven't cause us problem in test is that the generated go code go through goimport. After processing, goimports seems to quite smart to add the proper imports.
We should not rely on goimports to add that for us.