- We want to change the
k8s.Client
interface to match theclient.Client
interface. - The two interfaces are identical except for the fact that
client.Client
methods all take acontext.Context
as the first parameter. k8s.Client
is used everywhere in the codebase and manually changing all occurrences would be very tedious and error-prone.- In most places, when we modify the method call we need to add an import for
context
as well.
- Search and replace script
- Requires very complicated regex to be written
- Lots of false positives because it's not aware of the context. (E.g. It will update all
Get
methods regardless of whether those methods actually belong to thek8s.Client
interface.
- Go's example-based refactoring tool
eg
- Unfortunately it doesn't work on my machine and always gets stuck. I suspect it is because it doesn't understand Go modules and tries to refactor ALL the Go code it finds in my machine.
- Custom AST transform
- What could go wrong?
-
Convert
k8s.Client
to matchclient.Client
: https://github.com/charith-elastic/go-ast-refactoring/blob/k8s-client/refactor/main.go- Scan the codebase looking for method calls.
- Check whether the method call is done on a receiver that implements the
k8s.Client
interface. - Prepend
context.Background()
to the arg list of identified method calls. - Import
context
if required.
-
Redirect all log calls to go through the
utils/log
package instead of the controller-runtime log package: https://github.com/charith-elastic/go-ast-refactoring/blob/log-pkg/refactor/main.go