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

Handle unit length valueFrom values #1958

Merged
merged 2 commits into from
Feb 9, 2023
Merged

Conversation

rexagod
Copy link
Member

@rexagod rexagod commented Jan 20, 2023

What this PR does / why we need it: Handle unit length valueFrom values and skip strings where we expect them to be type-cast-able to float64, instead of erroring, since that is the expected behavior, and what's being done for other types.

How does this change affect the cardinality of KSM: No change.

Which issue(s) this PR fixes: Fixes #1947

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 20, 2023
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 20, 2023
Handle unit length `valueFrom` values and skip strings where we expect
them to be type-cast-able to `float64`, instead of erroring, since that
is the expected behavior, and what's being done for other types.
@logicalhan
Copy link
Member

/triage accepted
/assign @mrueg

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 26, 2023
@@ -173,6 +173,17 @@ func Test_values(t *testing.T) {
newEachValue(t, 2, "type", "type-a", "active", "1"),
newEachValue(t, 4, "type", "type-b", "active", "3"),
}},
{name: "path-relative valueFrom value", each: &compiledGauge{
Copy link
Member

@mrueg mrueg Jan 27, 2023

Choose a reason for hiding this comment

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

Can we add another Test for this path

							// This means we are here: [ <string>, <int>, ... ] (eg., [ "foo", "0", ... ], i.e., <path>.foo[0]...
							//                           ^
							// Skip over.
							return nil```

Copy link
Member Author

@rexagod rexagod Feb 7, 2023

Choose a reason for hiding this comment

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

This is actually another bug in and of itself. Basically, the same one but for multi-length valueFrom paths. In the spirit of keeping the changesets digestible and throughly tested and reviewed in the CRS realm, I'm thinking if it'd be a good idea to target that in another PR (and getting the unit-length path fix in this release)?


JFYI the test below returns nil for both main and this PR.

		{
			name: "multiple path-relative valueFrom values", each: &compiledGauge{
				compiledCommon: compiledCommon{
					path: mustCompilePath(t, "spec"),
					labelFromPath: map[string]valuePath{
						"version": mustCompilePath(t, "version"),
					},
				},
				ValueFrom: mustCompilePath(t, "order", "-1", "id"),
			}, wantResult: []eachValue{
				newEachValue(t, 3, "version", "v0.0.0"),
			},
		},

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me! Can you add a TODO to the code path?

sValueFrom := c.ValueFrom.String()
// No comma means we're looking at a unit-length path (in an array).
if !strings.Contains(sValueFrom, ",") &&
sValueFrom[0] == '[' &&
Copy link
Member

@mrueg mrueg Jan 27, 2023

Choose a reason for hiding this comment

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

can we add another check for sValueFrom[len(sValueFrom)-1] == ']' ?

@mrueg
Copy link
Member

mrueg commented Jan 27, 2023

I can confirm your change is fixing #1947.
/approve

I would like to get a second pair of eyes on the implementation from @kubernetes/kube-state-metrics-maintainers

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 27, 2023
@dgrisonnet
Copy link
Member

/assign
/hold

I asked some questions in #1947. It is unclear what we are trying to achieve here.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 2, 2023
Comment on lines +227 to +233
sValueFrom := c.ValueFrom.String()
// No comma means we're looking at a unit-length path (in an array).
if !strings.Contains(sValueFrom, ",") &&
sValueFrom[0] == '[' && sValueFrom[len(sValueFrom)-1] == ']' &&
// "[...]" and not "[]".
len(sValueFrom) > 2 {
extractedValueFrom := sValueFrom[1 : len(sValueFrom)-1]
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit odd to me. ValueFrom is orginally a string array and here we are manually trying to parse it as a string and extracting the first element from it, when we should have an easy access to it. What do you think about adding a new method to valuePath that would return a string array instead of a raw string?

Copy link
Member

Choose a reason for hiding this comment

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

@rexagod I am merging the PR without that change since it is mostly code quality related, but if my suggestion makes sense to you, let's address that in a follow-up

@dgrisonnet
Copy link
Member

/lgtm
/approve
/unhold

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Feb 9, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dgrisonnet, mrueg, rexagod

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 757055d into kubernetes:main Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dissimilar erroring behaviours between Gauge and other types
5 participants