Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Fix issue reading string map type via config map #18478

Merged
merged 2 commits into from
Jan 31, 2022

Conversation

sayboras
Copy link
Member

@sayboras sayboras commented Jan 14, 2022

Description

As mentioned in below upstream issue, there is a discrepancy in viper
while reading string map string data type i.e. kv pair format was not
supported, only {"k":"v"} format is allowed. This commit is to wrap
GetStringMapString implementation to handle such case. This is backward
compatible change.

Relates spf13/viper#911
Fixes #18328

Signed-off-by: Tam Mach tam.mach@isovalent.com

Testing

Testing was done locally as per below

before
$ cat tmp/subnet-tags-filter
k1=v1,k2=v2% 

/usr/lib/go-1.17/bin/go build -o /tmp/GoLand/___2go_build_github_com_cilium_cilium_operator github.com/cilium/cilium/operator #gosetup
/tmp/GoLand/___2go_build_github_com_cilium_cilium_operator --config-dir tmp/
level=info msg="Started gops server" address="127.0.0.1:9891" subsys=___2go_build_github_com_cilium_cilium_operator
level=warning msg="Running Cilium with \"kvstore\"=\"\" requires identity allocation via CRDs. Changing identity-allocation-mode to \"crd\"" subsys=config
...
level=info msg="  --subnet-ids-filter=''" subsys=___2go_build_github_com_cilium_cilium_operator
level=info msg="  --subnet-ids-filter=''" subsys=___2go_build_github_com_cilium_cilium_operator --> THIS IS EMPTY
level=info msg="  --synchronize-k8s-nodes='true'" subsys=___2go_build_github_com_cilium_cilium_operator
...
level=info msg="Cilium Operator  go version go1.17 linux/amd64" subsys=___2go_build_github_com_cilium_cilium_operator
after
/usr/lib/go-1.17/bin/go build -o /tmp/GoLand/___2go_build_github_com_cilium_cilium_operator github.com/cilium/cilium/operator #gosetup
/tmp/GoLand/___2go_build_github_com_cilium_cilium_operator --config-dir tmp/
level=info msg="Started gops server" address="127.0.0.1:9891" subsys=___2go_build_github_com_cilium_cilium_operator
level=warning msg="Running Cilium with \"kvstore\"=\"\" requires identity allocation via CRDs. Changing identity-allocation-mode to \"crd\"" subsys=config

...
level=info msg="  --subnet-ids-filter=''" subsys=___2go_build_github_com_cilium_cilium_operator
level=info msg="  --subnet-tags-filter='k1=v1,k2=v2'" subsys=___2go_build_github_com_cilium_cilium_operator --> THIS HAS CORRECT VALUE
level=info msg="  --synchronize-k8s-nodes='true'" subsys=___2go_build_github_com_cilium_cilium_operator
...
level=info msg="Cilium Operator  go version go1.17 linux/amd64" subsys=___2go_build_github_com_cilium_cilium_operator
cmd: Fix issue reading string map type via config map

@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 14, 2022
@sayboras sayboras added area/cli Impacts the command line interface of any command in the repository. release-note/minor This PR changes functionality that users may find relevant to operating Cilium. labels Jan 14, 2022
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 14, 2022
@sayboras sayboras added kind/bug This is a bug in the Cilium logic. release-note/bug This PR fixes an issue in a previous release of Cilium. and removed release-note/minor This PR changes functionality that users may find relevant to operating Cilium. labels Jan 14, 2022
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Authors of Cilium

package command
Copy link
Member Author

Choose a reason for hiding this comment

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

I look around, and find this package is the most relevant, feel free to let me know otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

We also have tools/, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

tools directory is used for extra utility (e.g. lint check, dev-doctor, etc), should not be used in any main application code.


# Simple script to make sure viper.GetStringMapString should not be used.
if grep -r --exclude-dir={.git,_build,vendor,contrib} -i --include \*.go "viper.GetStringMapString" .; then
echo "Found viper.GetStringMapString(key) usage. Please use command.GetStringMapString(viper.GetViper(), key) instead";
Copy link
Member Author

Choose a reason for hiding this comment

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

very naive check here, this will not work with import alias, but we all get used to viper. I reckon.

@sayboras sayboras marked this pull request as ready for review January 14, 2022 09:19
@sayboras sayboras requested a review from a team January 14, 2022 09:19
@sayboras sayboras requested review from a team as code owners January 14, 2022 09:19
Comment on lines 39 to 49
{
name: "value with invalid json format",
args: args{
key: "FOO_BAR",
value: `{"k1":"v1","k2":"v2",}`,
},
want: map[string]string{},
},
Copy link
Member

Choose a reason for hiding this comment

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

in the error case like this i would usually check the error message for strings.Contains something specific like bad json or extra comma, I'm not sure what sort of a message is printed here.

Copy link
Member Author

Choose a reason for hiding this comment

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

good point, I see the actual error in debugger only, making sense to make it explicit so that others don't need to re-check it. Let me refactor a little bit, and address related comments below as well.

pkg/command/map_string_test.go Show resolved Hide resolved
pkg/command/map_string.go Outdated Show resolved Hide resolved
@ldelossa
Copy link
Contributor

ldelossa commented Jan 14, 2022

@sayboras did we look at viper's marshaling functionality to accomplish this? https://github.com/spf13/viper#unmarshaling
https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/

This very well may not work, I'm just used to seeing Go libraries implement Marshal functions when having to deal with arbitrary text values.

@sayboras
Copy link
Member Author

did we look at viper's marshaling functionality to accomplish this? https://github.com/spf13/viper#unmarshaling https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/

This very well may not work, I'm just used to seeing Go libraries implement Marshal functions when having to deal with arbitrary text values.

👋 I checked the custom marshall and unmarshall, however, it is nice apporach if we have a self-defined struct (e.g. config), which might not work in our case as we are getting value field by field.

@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Needs backport from master to Backport pending to v1.10 in 1.10.8 Feb 7, 2022
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Needs backport from master to Backport pending to v1.10 in 1.11.2 Feb 7, 2022
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Backport pending to v1.10 to Backport done to v1.10 in 1.10.8 Feb 8, 2022
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Backport pending to v1.10 to Backport done to v1.10 in 1.10.8 Feb 8, 2022
@joamaki joamaki added backport-done/1.11 The backport for Cilium 1.11.x for this PR is done. and removed backport-pending/1.11 labels Feb 8, 2022
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Backport pending to v1.10 to Backport done to v1.11 in 1.11.2 Feb 8, 2022
@sayboras sayboras deleted the tam/kv-flags branch February 19, 2022 03:39
sayboras added a commit to sayboras/cilium that referenced this pull request May 25, 2022
The previous PR cilium#18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: cilium#18478
Fixes: cilium#18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
jibi pushed a commit that referenced this pull request May 26, 2022
The previous PR #18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: #18478
Fixes: #18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
jibi pushed a commit that referenced this pull request May 31, 2022
[ upstream commit 070ded0 ]

The previous PR #18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: #18478
Fixes: #18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
Signed-off-by: Gilberto Bertin <jibi@cilium.io>
joamaki pushed a commit that referenced this pull request Jun 7, 2022
[ upstream commit 070ded0 ]

The previous PR #18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: #18478
Fixes: #18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
Signed-off-by: Gilberto Bertin <jibi@cilium.io>
joamaki pushed a commit to joamaki/cilium that referenced this pull request Jun 8, 2022
[ upstream commit 070ded0 ]

The previous PR cilium#18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: cilium#18478
Fixes: cilium#18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
Signed-off-by: Jussi Maki <jussi@isovalent.com>
nebril pushed a commit that referenced this pull request Jun 9, 2022
[ upstream commit 070ded0 ]

The previous PR #18478 wraps existing viper GetStringMapString function
to get around upstream bugs, however, it's unintentionally restricted a
few formats, which supported before in cilium, such as:

- --aws-instance-limit-mapping=c6a.2xlarge=4,15,15,m4.xlarge=1,5,10
- --api-rate-limit=endpoint-create=rate-limit:10/s,rate-burst:10,parallel-requests:10,auto-adjust:true

For complicated attribute, we are allowing comma character in value part
of key value pair. As golang didn't support look-ahead functionalities in
built-in regex library, this commit is to replace string.Split function
by custom implementation to handle such scenario.

Relates: #18478
Fixes: #18973
Signed-off-by: Tam Mach <tam.mach@cilium.io>
Signed-off-by: Jussi Maki <jussi@isovalent.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cli Impacts the command line interface of any command in the repository. backport-done/1.11 The backport for Cilium 1.11.x for this PR is done. kind/bug This is a bug in the Cilium logic. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/bug This PR fixes an issue in a previous release of Cilium.
Projects
No open projects
1.10.8
Backport done to v1.10
1.11.2
Backport done to v1.11
Development

Successfully merging this pull request may close these issues.

cmd: subnet-tags-filter: k=v (and other options) silently fail when provided via ConfigMap
9 participants