Handling error while connecting to kubernetes server using config file

2/21/2020

I intentionally broke my .config file which is responsible for connecting to kubernetes server by changing the content in the user section and I am getting this error:

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

goroutine 1 [running]:
k8s.io/client-go/kubernetes.NewForConfig(0x0, 0x0, 0xc42031c120, 0x1c)
        /home/saivamsi/go/src/k8s.io/client-go/kubernetes/clientset.go:371 +0x4e
exit status 2

I was doing this to test what happens if somehow invalid config was supplied so I want to handle this error by retrying to connect to server for next 5sec to see if it. How can I achieve this?

I am using kubernetes go client with this code to make the connection:

var KubeConfigPath = os.Getenv("HOME") + "/.kube/config2"
var config, ConfigErr = clientcmd.BuildConfigFromFlags("", KubeConfigPath)
var clientset, ClientErr = kubernetes.NewForConfig(config)
-- Sai Vamsi
go
kubernetes-go-client

1 Answer

2/21/2020

You can likely avoid the segfault if you check that the error from BuildConfigFromFlags is not nil. If it is nil then sleep and retry, otherwise continue.

-- Keith Burdis
Source: StackOverflow