k8sgpt-ai/k8sgpt

[Bug]: segmentation violation during serve

Closed this issue · 0 comments

Checklist

  • I've searched for similar issues and couldn't find anything matching
  • I've included steps to reproduce the behavior

Affected Components

  • K8sGPT (CLI)
  • K8sGPT Operator

K8sGPT Version

391a3cd

Kubernetes Version

na

Host OS and its Version

go1.22.4 darwin/amd64

Steps to reproduce

clone
main branch
make build
./bin/k8sgpt server
 k8sgpt % ./bin/k8sgpt serve   
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xc900c9b]

goroutine 1 [running]:
github.com/k8sgpt-ai/k8sgpt/cmd/serve.init.func1(0xc000504700?, {0xc9035cf?, 0x4?, 0xc9035d3?})
	/Users/ronaldpetty/github.com/ronaldpetty/k8sgpt/cmd/serve/serve.go:152 +0x5db
github.com/spf13/cobra.(*Command).execute(0xf618f40, {0xf6afc60, 0x0, 0x0})
	/Users/ronaldpetty/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:989 +0xab1
github.com/spf13/cobra.(*Command).ExecuteC(0xf614d20)
	/Users/ronaldpetty/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1117 +0x3ff
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/ronaldpetty/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1041
github.com/k8sgpt-ai/k8sgpt/cmd.Execute({0xd15f7c0?, 0x0?}, {0xd15fd40?, 0xf42c800?}, {0xd15fe00?, 0xc0000061c0?})
	/Users/ronaldpetty/github.com/ronaldpetty/k8sgpt/cmd/root.go:59 +0x91
main.main()
	/Users/ronaldpetty/github.com/ronaldpetty/k8sgpt/main.go:25 +0x3d

Expected behaviour

Should show helpful error message.

Actual behaviour

Crash.

Additional Information

I think issue is in ServeCmd function of cmd/serve/serve.go

		if aiProvider == nil {
			for _, provider := range configAI.Providers {
				if backend == provider.Name {
					// the pointer to the range variable is not really an issue here, as there
					// is a break right after, but to prevent potential future issues, a temp
					// variable is assigned
					p := provider
					aiProvider = &p
					break
				}
			}
		}
		
		if aiProvider.Name == "" { // crash

When aiProvider is nil, we do a check and I have a provider (ollama). But since we didn't set backend (via -b or --backend) aiProvider remains nil. Then we crash at next guard, there is no aiProvider, and thus nil.Name sends us home early.

Fix for now is just set backend. Probably should not crash though, so maybe fix is exit like this instead?

		if aiProvider == nil {
			for _, provider := range configAI.Providers {
				if backend == provider.Name {
					// the pointer to the range variable is not really an issue here, as there
					// is a break right after, but to prevent potential future issues, a temp
					// variable is assigned
					p := provider
					aiProvider = &p
					break
				}
			}
			// maybe add this check?
			if aiProvider == nil {
			    color.Red("Error: AI backend not found in providers or not specified")
			    os.Exit(1)
                         }
		}