k8sgpt icon indicating copy to clipboard operation
k8sgpt copied to clipboard

[Bug]: k8sgpt serve panic: runtime error: invalid memory address or nil pointer dereference

Open shturec opened this issue 7 months ago • 2 comments

Checklist

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

Affected Components

  • [X] K8sGPT (CLI)
  • [ ] K8sGPT Operator

K8sGPT Version

0.3.37

Kubernetes Version

n/a

Host OS and its Version

Mac

Steps to reproduce

  1. Add amazonbedrock as backend with k8sgpt auth
  2. Run k8sgpt serve

Expected behaviour

  1. Panic should not hide the actual error
  2. Fail fast with descriptive error for missing --backend, -b flag with non-default (openai) backend configurations.
  3. Document to avoid invalid flag-configuration combinations

Actual behaviour

k8sgpt serve
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x102e297e0]

goroutine 1 [running]:
github.com/k8sgpt-ai/k8sgpt/cmd/serve.init.func1(0x140001dce00?, {0x102e2d786?, 0x4?, 0x102e2d78a?})
github.com/k8sgpt-ai/k8sgpt/cmd/serve/serve.go:152 +0x500
github.com/spf13/cobra.(*Command).execute(0x105b5b380, {0x105d9d388, 0x0, 0x0})
github.com/spf13/[email protected]/command.go:987 +0x828
github.com/spf13/cobra.(*Command).ExecuteC(0x105b57160)
github.com/spf13/[email protected]/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
github.com/spf13/[email protected]/command.go:1039
github.com/k8sgpt-ai/k8sgpt/cmd.Execute({0x1036baa18?, 0x0?}, {0x1036bde88?, 0x100c567c0?}, {0x1036bdf70?, 0x140000021c0?})
github.com/k8sgpt-ai/k8sgpt/cmd/root.go:59 +0xa0
main.main()
github.com/k8sgpt-ai/k8sgpt/main.go:25 +0x4c

Additional Information

configuration:

ai:
    providers:
        - name: localai
          model: llama3
          baseurl: http://localhost:11434/v1
          temperature: 0.7
          topp: 0.5
          topk: 50
          maxtokens: 2048
        - name: amazonbedrock
          model: anthropic.claude-v2:0
          temperature: 0.7
          providerregion: us-east-1
          topp: 0.5
          topk: 50
          maxtokens: 2048
    defaultprovider: ""
kubeconfig: ""
kubecontext: ""

Note that multiple calls to a nil reference along the k8sgpt serve scenaio exist.

Try for example:

  1. Run k8sgpt serve -b amazonbedrock
  2. Query with grpcurl -plaintext -d '{"explain": true, "namespace": "k8sgpt"}' localhost:8080 schema.v1.ServerService/Analyze (note the missing backend parameter in the query)

query output:

ERROR:
  Code: Unavailable
  Message: error reading from server: EOF

server output:

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

goroutine 61 [running]:
github.com/k8sgpt-ai/k8sgpt/pkg/server.(*handler).Analyze(0x10521bee0?, {0x10381cdc8, 0x14000a3f800}, 0x140008d0360)
        github.com/k8sgpt-ai/k8sgpt/pkg/server/analyze.go:34 +0xcc
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc._ServerService_Analyze_Handler.func1({0x10381cdc8?, 0x14000a3f800?}, {0x1036594c0?, 0x140008d0360?})
        buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/[email protected]/schema/v1/schemav1grpc/server-service_grpc.pb.go:134 +0xd0
github.com/k8sgpt-ai/k8sgpt/pkg/server.(*Config).Serve.logInterceptor.func1({0x10381cdc8, 0x14000a3f800}, {0x1036594c0, 0x140008d0360}, 0x14000c09780, 0x14000bb6bd0)
        github.com/k8sgpt-ai/k8sgpt/pkg/server/log.go:19 +0x70
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc._ServerService_Analyze_Handler({0x103424fc0, 0x0}, {0x10381cdc8, 0x14000a3f800}, 0x14000246c00, 0x14000957830)
        buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/[email protected]/schema/v1/schemav1grpc/server-service_grpc.pb.go:136 +0x148
google.golang.org/grpc.(*Server).processUnaryRPC(0x14000bc2600, {0x10381cdc8, 0x14000a3f740}, {0x10382dec0, 0x140008fc180}, 0x14000af2360, 0x14000a3f590, 0x105246060, 0x0)
        google.golang.org/[email protected]/server.go:1379 +0xb58
google.golang.org/grpc.(*Server).handleStream(0x14000bc2600, {0x10382dec0, 0x140008fc180}, 0x14000af2360)
        google.golang.org/[email protected]/server.go:1790 +0xb20
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/[email protected]/server.go:1029 +0x8c
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 74
        google.golang.org/[email protected]/server.go:1040 +0x13c

The problem is at pkg/server/analyze.go where the config instance is nil due to an error yielded by the previous statement.
Following the idiomatic error handling in go should solve the problem.

shturec avatar Jul 10 '24 14:07 shturec