1 year ago
#348313
Mortl
How to use inherited contexts in Go
I am writing an Kubernetes Operator in Go. I am struggling with the clean handling of the given context object. It is a good practice to to pass an inherited context as a parameter to further methods. When doing not so the linter shows the following messages:
instance, err := k8sConnector.getInstance(r.ctx, req.NamespacedName)
root@8794fe3af767:/go/xpro-update-manager# golangci-lint run --timeout=15m ./...
controllers/release_controller.go:79:43: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
instance, err := k8sConnector.getInstance(r.ctx, req.NamespacedName)
^
But when doing so (as documented in https://pkg.go.dev/context#WithCancel) the linter shows the same message:
k8sContext, cancel := context.WithCancel(r.ctx)
defer cancel()
instance, err := k8sConnector.getInstance(k8sContext, req.NamespacedName)
root@8794fe3af767:/go/xpro-update-manager# golangci-lint run --timeout=15m ./...
controllers/xprorelease_controller.go:79:43: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
instance, err := k8sConnector.getInstance(k8sContext, req.NamespacedName)
^
What is the correct handling of the context object? It seems to me that the linter has a false positive because in my opinion it is indeed a inherited context which I am passing in contrast to what the linter says.
go
linter
0 Answers
Your Answer