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 v2 runtime error: invalid memory address or nil pointer dereference #766

Closed HenryXie1 closed 5 years ago

HenryXie1 commented 5 years ago

We follow https://book.kubebuilder.io/ using kubebuilder v2 to build a new sample "mycontab" After we make install, we run make run it error out runtime error: invalid memory address or nil pointer dereference kubernetes env: v.1.12.7

$make run
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-beta.1
go: finding sigs.k8s.io/controller-tools/cmd/controller-gen v0.2.0-beta.1
go: finding sigs.k8s.io/controller-tools/cmd v0.2.0-beta.1
/home/henryxie/go/bin/controller-gen object:headerFile=./hack/boilerplate.go.txt paths=./api/...
go fmt ./...
go vet ./...
go run ./main.go
E0606 08:44:18.053689    8598 client_go_adapter.go:318] descriptor Desc{fqName: "cronjob-application_depth", help: "Current depth of workqueue: cronjob-application", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_depth" is not a valid metric name
E0606 08:44:18.055477    8598 client_go_adapter.go:328] descriptor Desc{fqName: "cronjob-application_adds", help: "Total number of adds handled by workqueue: cronjob-application", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_adds" is not a valid metric name
E0606 08:44:18.060994    8598 client_go_adapter.go:339] descriptor Desc{fqName: "cronjob-application_queue_latency", help: "How long an item stays in workqueuecronjob-application before being requested.", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_queue_latency" is not a valid metric name
E0606 08:44:18.062821    8598 client_go_adapter.go:350] descriptor Desc{fqName: "cronjob-application_work_duration", help: "How long processing an item from workqueuecronjob-application takes.", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_work_duration" is not a valid metric name
E0606 08:44:18.064737    8598 client_go_adapter.go:363] descriptor Desc{fqName: "cronjob-application_unfinished_work_seconds", help: "How many seconds of work cronjob-application has done that is in progress and hasn't been observed by work_duration. Large values indicate stuck threads. One can deduce the number of stuck threads by observing the rate at which this increases.", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_unfinished_work_seconds" is not a valid metric name
E0606 08:44:18.066677    8598 client_go_adapter.go:374] descriptor Desc{fqName: "cronjob-application_longest_running_processor_microseconds", help: "How many microseconds has the longest running processor for cronjob-application been running.", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_longest_running_processor_microseconds" is not a valid metric name
E0606 08:44:18.068777    8598 client_go_adapter.go:384] descriptor Desc{fqName: "cronjob-application_retries", help: "Total number of retries handled by workqueue: cronjob-application", constLabels: {}, variableLabels: []} is invalid: "cronjob-application_retries" is not a valid metric name
2019-06-06T08:44:18.070+1000    INFO    controller-runtime.controller Starting EventSource     {"controller": "cronjob-application", "source": "kind source: /, Kind="}
2019-06-06T08:44:18.070+1000    INFO    controller-runtime.controller Starting EventSource     {"controller": "cronjob-application", "source": "kind source: /, Kind="}
2019-06-06T08:44:18.070+1000    INFO    setup   starting manager
2019-06-06T08:44:18.474+1000    INFO    controller-runtime.controller Starting Controller      {"controller": "cronjob-application"}
2019-06-06T08:44:18.575+1000    INFO    controller-runtime.controller Starting workers {"controller": "cronjob-application", "worker count": 1}

2019-06-06T08:45:34.001+1000    DEBUG   controllers.CronJob     job count      {"cronjob": "default/cronjob-sample", "active jobs": 0, "successful jobs": 0, "failed jobs": 0}
2019-06-06T08:45:34.311+1000    DEBUG   controllers.CronJob     no upcoming scheduled times, sleeping until next       {"cronjob": "default/cronjob-sample", "now": "2019-06-06T08:45:34.311+1000", "next run": "2019-06-06T08:46:00.000+1000"}
2019-06-06T08:45:34.311+1000    DEBUG   controllers.CronJob     job count      {"cronjob": "default/cronjob-sample", "active jobs": 0, "successful jobs": 0, "failed jobs": 0}
2019-06-06T08:45:34.540+1000    DEBUG   controllers.CronJob     no upcoming scheduled times, sleeping until next       {"cronjob": "default/cronjob-sample", "now": "2019-06-06T08:45:34.540+1000", "next run": "2019-06-06T08:46:00.000+1000"}
2019-06-06T08:46:00.002+1000    DEBUG   controllers.CronJob     job count      {"cronjob": "default/cronjob-sample", "active jobs": 0, "successful jobs": 0, "failed jobs": 0}
E0606 08:46:00.233259    8598 runtime.go:69] Observed a panic: "invalid memory address or nil pointer dereference" (runtime error: invalid memory address or nil pointer dereference)
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/runtime/runtime.go:76
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/runtime/runtime.go:65
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/runtime/runtime.go:51
/usr/local/go/src/runtime/panic.go:522
/usr/local/go/src/runtime/panic.go:82
/usr/local/go/src/runtime/signal_unix.go:390
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/runtime/scheme.go:256
/home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/client/apiutil/apimachinery.go:48
/home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/controller/controllerutil/controllerutil.go:62
/home/henryxie/go/kubebuilder-src/mycrontab/controllers/cronjob_controller.go:332
/home/henryxie/go/kubebuilder-src/mycrontab/controllers/cronjob_controller.go:340
/home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/internal/controller/controller.go:210
/home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/internal/controller/controller.go:158
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:152
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:153
/home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:88
/usr/local/go/src/runtime/asm_amd64.s:1337
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x685fd9]

goroutine 179 [running]:
k8s.io/apimachinery/pkg/util/runtime.HandleCrash(0x0, 0x0, 0x0)
        /home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/runtime/runtime.go:58 +0x105
panic(0x12a4160, 0x20f4c40)
        /usr/local/go/src/runtime/panic.go:522 +0x1b5
k8s.io/apimachinery/pkg/runtime.(*Scheme).ObjectKinds(0x0, 0x15e4f00, 0xc0000db700, 0x38, 0xc0001cd370, 0x1, 0x40a2ac, 0xc000032000, 0x12c0280)
        /home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/runtime/scheme.go:256 +0xe9
sigs.k8s.io/controller-runtime/pkg/client/apiutil.GVKForObject(0x15e4f00, 0xc0000db700, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc000691130, ...)
        /home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/client/apiutil/apimachinery.go:48 +0x75
sigs.k8s.io/controller-runtime/pkg/controller/controllerutil.SetControllerReference(0x1641560, 0xc0000db700, 0x16412e0, 0xc0000e4d80, 0x0, 0xc000040660, 0x19)
        /home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/controller/controllerutil/controllerutil.go:62 +0x17f
mycrontab/controllers.(*CronJobReconciler).Reconcile.func6(0xc0000db700, 0x0, 0xed48a3d64, 0x2108e40, 0x182baab130, 0x2108e40, 0x2108e00)
        /home/henryxie/go/kubebuilder-src/mycrontab/controllers/cronjob_controller.go:332 +0x55d
mycrontab/controllers.(*CronJobReconciler).Reconcile(0xc000393f40, 0xc000613d19, 0x7, 0xc000613d00, 0xe, 0x2108e40, 0x5ed818fd6, 0x0, 0x0)
        /home/henryxie/go/kubebuilder-src/mycrontab/controllers/cronjob_controller.go:340 +0x152c
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc0000c60a0, 0x0)
        /home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/internal/controller/controller.go:210 +0x1ba
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1()
        /home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/internal/controller/controller.go:158 +0x36
k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc0004ab140)
        /home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:152 +0x54
k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc0004ab140, 0x3b9aca00, 0x0, 0x1, 0xc0003b8a20)
        /home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:153 +0xf8
k8s.io/apimachinery/pkg/util/wait.Until(0xc0004ab140, 0x3b9aca00, 0xc0003b8a20)
        /home/henryxie/go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190404173353-6a84e37a896d/pkg/util/wait/wait.go:88 +0x4d
created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start
        /home/henryxie/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.2.0-beta.1/pkg/internal/controller/controller.go:157 +0x311
exit status 2
make: *** [Makefile:19: run] Error 1
HenryXie1 commented 5 years ago

the go.mod is below

module mycrontab

go 1.12

require (
        github.com/go-logr/logr v0.1.0
        github.com/onsi/ginkgo v1.8.0
        github.com/onsi/gomega v1.5.0
        github.com/robfig/cron v1.1.0
        golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09
        k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
        k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
        k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
        sigs.k8s.io/controller-runtime v0.2.0-beta.1
        sigs.k8s.io/controller-tools v0.2.0-beta.1 // indirect
)
HenryXie1 commented 5 years ago

if i use go.mod in the book https://book.kubebuilder.io/cronjob-tutorial/basic-project.html I got another error when i ran make install

go build sigs.k8s.io/controller-runtime/pkg/scheme: no Go files in 

Usage:
  controller-gen [flags]

Examples:
……………...
module mycrontab

go 1.12

require (
    github.com/go-logr/logr v0.1.0
    github.com/robfig/cron v1.1.0
    k8s.io/api v0.0.0-20190222213804-5cb15d344471
    k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628
    k8s.io/client-go v0.0.0-20190228174230-b40b2a5939e4
    sigs.k8s.io/controller-runtime v0.2.0-alpha.0.0.20190503051552-b666157c41da
    sigs.k8s.io/controller-tools v0.2.0-alpha.1 // indirect
)
DirectXMan12 commented 5 years ago

yep, go.mod/makefile in book need to be updated, it appears. As for the crash on your end, I'd need to see your source -- those lines don't line up with the ones in the book directory, and I can't reproduce the crash. Perhaps you've copied something incorrectly?

HenryXie1 commented 5 years ago

Thank you Solly. If you can put your sample codes in a public repo of github, it would be great. So I can compare with mine.

DirectXMan12 commented 5 years ago

They are in a public repo: this one ;-) All the code in the book is actually generated off of real, runnable code: https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/cronjob-tutorial/testdata/project. The makefile needs to be updated to use controller-tools v0.2.0-beta.2 (as mentioned above), but otherwise you can just run that.

HenryXie1 commented 5 years ago

When i compare with sample code, the differences i see is i used my test domain "mycrontab" and some doc-gen comments. Here is the public repo I am testing based on the sample code. go.mod is the one which "make install and make run" were running successfully. https://github.com/HenryXie1/mycrontab thank you

DirectXMan12 commented 5 years ago

Ah! I see the issue. You're missing scheme in main.go -- see https://book.kubebuilder.io/cronjob-tutorial/main-revisited.html. It's trying to use r.Scheme, which is nil.

HenryXie1 commented 5 years ago

Thank you Solly. I confirm the issue is fixed.

wyljpn commented 3 years ago

I met the runtime error: invalid memory address or nil pointer dereference with kubebuilder v3. It seems that if we want to use log in *_controller.go,

log := r.Log.WithValues("cronjob", req.NamespacedName)

we need to pass Log to CronJobReconciler in main.go

err = (&controllers.CronJobReconciler{
    Client: mgr.GetClient(),
    Log:    ctrl.Log.WithName("controllers").WithName("CronJob"),  # add this line
    Scheme: mgr.GetScheme(), // we've added this ourselves
}).SetupWithManager(mgr)

When i compare with sample code, the differences i see is i used my test domain "mycrontab" and some doc-gen comments. Here is the public repo I am testing based on the sample code. go.mod is the one which "make install and make run" were running successfully. https://github.com/HenryXie1/mycrontab thank you