Migrate from the deprecated mitchellh/mapstructure to go-viper/mapstructure - #83
Open
HadrienPatte wants to merge 2 commits into
Open
HadrienPatte wants to merge 2 commits into
HadrienPatte wants to merge 2 commits into
Conversation
The [mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) package is archived and no longer receives updates (see mitchellh/mapstructure#349). The blessed fork (see https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc) is [go-viper/mapstructure](https://github.com/go-viper/mapstructure), which viper itself has used since v1.20. This migration was attempted before in #49 and reverted in #53 because it broke Cilium's `TestNodeAddressWhitelist`. The API is almost the same, but go-viper/mapstructure#6 changed `StringToSliceHookFunc` so it only fires when the target type is exactly `[]string`. The mitchellh version fired for any slice kind. As a result, a comma-separated string coming from the environment or a configmap is no longer split before being decoded into slices of other element types, such as `[]netip.Prefix`. Each element then gets the whole unsplit string. Swapping only the import path, as #49 did, silently changes this behavior. Use `StringToWeakSliceHookFunc` instead. go-viper/mapstructure provides it to bring back the pre-v2 behavior, and its implementation matches the mitchellh `StringToSliceHookFunc`. Hook ordering is unchanged: `TextUnmarshallerHookFunc` still runs before the slice split, so types like `net.IP` (`[]byte`) are not split on commas. `TestHiveTextUnmarshaler` (added in #79, after the revert) covers the case that broke Cilium: it decodes "10.0.0.0/8,192.168.0.0/16" from the configmap into a `[]netip.Prefix`. It fails with a plain import swap and passes with this change. Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
Since v1.20, viper uses github.com/go-viper/mapstructure/v2 instead of the archived github.com/mitchellh/mapstructure. The previous commit moved hive's direct usage to go-viper/mapstructure/v2, and viper v1.18.2 was the last thing still pulling in mitchellh/mapstructure. With this bump, it is gone from the module graph entirely, for hive and for its dependents. Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
derailed
approved these changes
Sep 30, 2026
derailed
left a comment
There was a problem hiding this comment.
@HadrienPatte Tx for this update!
Do you feel we have adequate test coverage to make sure this does not break anything else?
HadrienPatte
added a commit
to cilium/cilium
that referenced
this pull request
Oct 1, 2026
See cilium/hive#83 Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
Member
Author
I tested updating hive to this PR's version in cilium/cilium and ran tests there and all passed so yeah I'm fairly confident that this will work this time: cilium/cilium@fa0750e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The mitchellh/mapstructure package is archived and no longer receives updates (see mitchellh/mapstructure#349). The blessed fork (see https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc) is go-viper/mapstructure, which viper itself has used since v1.20.
This migration was attempted before in #49 and reverted in #53 because it broke Cilium's
TestNodeAddressWhitelist.The API is almost the same, but go-viper/mapstructure#6 changed
StringToSliceHookFuncso it only fires when the target type is exactly[]string. The mitchellh version fired for any slice kind. As a result, a comma-separated string coming from the environment or a configmap is no longer split before being decoded into slices of other element types, such as[]netip.Prefix. Each element then gets the whole unsplit string. Swapping only the import path, as #49 did, silently changes this behavior.Use
StringToWeakSliceHookFuncinstead. go-viper/mapstructure provides it to bring back the pre-v2 behavior, and its implementation matches the mitchellhStringToSliceHookFunc. Hook ordering is unchanged:TextUnmarshallerHookFuncstill runs before the slice split, so types likenet.IP([]byte) are not split on commas.TestHiveTextUnmarshaler(added in #79, after the revert) covers the case that broke Cilium: it decodes "10.0.0.0/8,192.168.0.0/16" from the configmap into a[]netip.Prefix. It fails with a plain import swap and passes with this change.