Skip to content

Migrate from the deprecated mitchellh/mapstructure to go-viper/mapstructure - #83

Open
HadrienPatte wants to merge 2 commits into
mainfrom
pr/HadrienPatte/update-mapstructure
Open

HadrienPatte wants to merge 2 commits into
mainfrom
pr/HadrienPatte/update-mapstructure

Conversation

@HadrienPatte

Copy link
Copy Markdown
Member

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 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.

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>
@HadrienPatte
HadrienPatte marked this pull request as ready for review September 29, 2026 15:04
@HadrienPatte
HadrienPatte requested a review from a team as a code owner September 29, 2026 15:04
@HadrienPatte
HadrienPatte requested review from derailed and removed request for a team September 29, 2026 15:04

@derailed derailed left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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>
@HadrienPatte

Copy link
Copy Markdown
Member Author

@HadrienPatte Tx for this update! Do you feel we have adequate test coverage to make sure this does not break anything else?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants