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

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

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

问题

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

以下是要翻译的代码:

package main

import (
	"context"
	"fmt"

	routev1 "github.com/openshift/api/route/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

func main() {

	const (
		namespace = "test"
		routeName = "nodejs-basic"
	)

	var k8sClient client.Client

	route := &routev1.Route{}
	err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: routeName, Namespace: namespace}, route)
	if err != nil {
		fmt.Println("Some issue")
	}
	fmt.Println("Api call completed")
}

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

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10683e7]

goroutine 1 [running]:
main.main()
        /home/amit/go/src/github.com/amitkrout/usingopenshiftapi/route.go:22 +0x27
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

package main

import (
	"context"
	"fmt"

	routev1 "github.com/openshift/api/route/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

func main() {

	const (
		namespace = "test"
		routeName = "nodejs-basic"
	)

	var k8sClient client.Client

	route := &routev1.Route{}
	err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: routeName, Namespace: namespace}, route)
	if err != nil {
		fmt.Println("Some issue")
	}
	fmt.Println("Api call completed")
}

Panic out while executing it

$ go run route.go

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10683e7]

goroutine 1 [running]:
main.main()
        /home/amit/go/src/github.com/amitkrout/usingopenshiftapi/route.go:22 +0x27
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:

确定