Openshift go-client api call panic out – "runtime error: invalid memory address or nil pointer dereference"

huangapple go评论102阅读模式
英文:

Openshift go-client api call panic out - "runtime error: invalid memory address or nil pointer dereference"

问题

我正在使用基本的Go代码来验证使用OpenShift API模块(https://github.com/openshift/api)在命名空间中创建的路由,但出现了恐慌错误。

以下是要翻译的代码:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. routev1 "github.com/openshift/api/route/v1"
  6. "k8s.io/apimachinery/pkg/types"
  7. "sigs.k8s.io/controller-runtime/pkg/client"
  8. )
  9. func main() {
  10. const (
  11. namespace = "test"
  12. routeName = "nodejs-basic"
  13. )
  14. var k8sClient client.Client
  15. route := &routev1.Route{}
  16. err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: routeName, Namespace: namespace}, route)
  17. if err != nil {
  18. fmt.Println("Some issue")
  19. }
  20. fmt.Println("Api call completed")
  21. }

在执行时出现了恐慌错误:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10683e7]
  3. goroutine 1 [running]:
  4. main.main()
  5. /home/amit/go/src/github.com/amitkrout/usingopenshiftapi/route.go:22 +0x27
  6. exit status 2

有什么指针问题吗?可能的原因是什么,如何修复?

我已经创建了一个Git仓库-https://github.com/amitkrout/usingopenshiftapi。你也可以通过PR在该仓库中提出修复建议。

英文:

I am using a basic go code to verify the the route created in a namespace using OpenShift api module - https://github.com/openshift/api but it's panic out

$ cat route.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. routev1 "github.com/openshift/api/route/v1"
  6. "k8s.io/apimachinery/pkg/types"
  7. "sigs.k8s.io/controller-runtime/pkg/client"
  8. )
  9. func main() {
  10. const (
  11. namespace = "test"
  12. routeName = "nodejs-basic"
  13. )
  14. var k8sClient client.Client
  15. route := &routev1.Route{}
  16. err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: routeName, Namespace: namespace}, route)
  17. if err != nil {
  18. fmt.Println("Some issue")
  19. }
  20. fmt.Println("Api call completed")
  21. }

Panic out while executing it

$ go run route.go

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10683e7]
  3. goroutine 1 [running]:
  4. main.main()
  5. /home/amit/go/src/github.com/amitkrout/usingopenshiftapi/route.go:22 +0x27
  6. exit status 2

Any pointer ? What could be the reason and how to fix it ?

I have created a git repo - https://github.com/amitkrout/usingopenshiftapi. You can propose the fix in the repo too via pr

答案1

得分: 1

你还没有初始化k8sClient,所以它是nil,调用k8sClient.Get()最终会导致空指针解引用。

获取客户端的一种方法是使用clientcmd;那里的文档展示了如何进行操作。

英文:

You haven’t initialised k8sClient, so it’s nil, and calling k8sClient.Get() ultimately results in a nil pointer dereference.

One way to get a client is to use clientcmd; the documentation there shows how to go about it.

huangapple
  • 本文由 发表于 2022年4月30日 20:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/72068399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定