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

🐛 Set unhealthyLimitKey for logging always #5110

Merged
merged 1 commit into from
Aug 26, 2021

Conversation

enxebre
Copy link
Member

@enxebre enxebre commented Aug 18, 2021

What this PR does / why we need it:
Without this the controller is panicking as the logger expect every key to be a string.

I0818 10:41:47.830532       1 machinehealthcheck_controller.go:108] controller-runtime/manager/controller/machinehealthcheck "msg"="Reconciling" "name"="agl" "namespace"="clusters-agl" "reconciler group"="cluster.x-k8s.io" "reconciler kind"="MachineHealthCheck"
panic: key is not a string: null

goroutine 640 [running]:
k8s.io/klog/v2/klogr.flatten(0xc00012bf80, 0x6, 0x6, 0xc000d16360, 0x8a)
	/go/pkg/mod/k8s.io/klog/[email protected]/klogr/klogr.go:158 +0x62e
k8s.io/klog/v2/klogr.klogger.Info(0x3, 0x0, 0xc00029a480, 0x38, 0xc001030500, 0xa, 0xa, 0x1b830d7, 0x9, 0x1b969b8, ...)
	/go/pkg/mod/k8s.io/klog/[email protected]/klogr/klogr.go:201 +0x625
sigs.k8s.io/cluster-api/controllers.(*MachineHealthCheckReconciler).reconcile(0xc000481810, 0x1dbd618, 0xc00084f290, 0x1dc70d8, 0xc00084b1d0, 0xc0004384e0, 0xc0009883c0, 0x9, 0xc0004384e0, 0x0, ...)
	/workspace/controllers/machinehealthcheck_controller.go:285 +0x12d4
sigs.k8s.io/cluster-api/controllers.(*MachineHealthCheckReconciler).Reconcile(0xc000481810, 0x1dbd618, 0xc00084f290, 0xc000620660, 0xc, 0xc00062064c, 0x3, 0xc00084ef00, 0x0, 0x0, ...)
	/workspace/controllers/machinehealthcheck_controller.go:164 +0x665
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc0001e4640, 0x1dbd570, 0xc0000a4140, 0x19d25c0, 0xc0003785a0)
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:298 +0x30d
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc0001e4640, 0x1dbd570, 0xc0000a4140, 0xc0004d7700)
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:253 +0x205
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2(0xc0001165f0, 0xc0001e4640, 0x1dbd570, 0xc0000a4140)
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:214 +0x6b
created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2
	/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:210 +0x425

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 18, 2021
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 18, 2021
@enxebre
Copy link
Member Author

enxebre commented Aug 18, 2021

cc @JoelSpeed

@JoelSpeed
Copy link
Contributor

Is there any way we can add a test for this that would panic if we ever regress?

Comment on lines 222 to 229
var unhealthyLimitKey, unhealthyLimitValue interface{}
if m.Spec.UnhealthyRange == nil {
unhealthyLimitKey = "max unhealthy"
unhealthyLimitValue = m.Spec.MaxUnhealthy
} else {
unhealthyLimitKey = "unhealthy range"
unhealthyLimitValue = *m.Spec.UnhealthyRange
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to optimize here? Could we just call the logger twice in two ifs below? We could potentially use a switch case if readability is a concern.

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'm not sure I follow, I updated to call the logger twice. PTAL.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 19, 2021
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

This definitely makes it easier to follow what's actually happening

I wonder if it's worth moving some of these strings to some consts?

Without this the controller is panicking as the logger expect every key to be a string
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 19, 2021
@enxebre
Copy link
Member Author

enxebre commented Aug 19, 2021

/retest

@enxebre
Copy link
Member Author

enxebre commented Aug 23, 2021

@vincepri PTAL

message = fmt.Sprintf("Remediation is not allowed, the number of not started or unhealthy machines does not fall within the range (total: %v, unhealthy: %v, unhealthyRange: %v)",
totalTargets,
len(unhealthy),
*m.Spec.UnhealthyRange)
}

logger.V(3).Info(
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like we could keep a single log call here, as well as below without needing to add an if/else block. The values can just be set in variables as they were, then only have one call out to the logger. The only thing missing from the existing code was to make sure the key was set to something if remediationAllowed.

Copy link
Member Author

@enxebre enxebre Aug 24, 2021

Choose a reason for hiding this comment

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

Thanks for feedback @stmcginnis. Is this suggesting just the opposite to #5110 (comment)? @vincepri

Seems @JoelSpeed finds it more readable with the if/else #5110 (review)

I don't really have any strong opinion as far as we stop the controller from panicking.

@stmcginnis @JoelSpeed @vincepri please let me know if you want to merge it as it is or make any specific change. Feedback seems to go in both directions atm.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a really strong opinion on this, but having seen both, I think I prefer the way it is now. While it's more code, it's also more explicit about what's happening and I personally find it easier to follow this way.

Also, thinking about how the logging works, with this approach the log line will include the actual line numbers of the log calls and might make it easier to trace the logging and which if-statements were entered, as opposed with today where you have to work out the values that were substituted and then work out where they were set

@vincepri
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vincepri

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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 25, 2021
@enxebre
Copy link
Member Author

enxebre commented Aug 26, 2021

/retest

1 similar comment
@sbueringer
Copy link
Member

/retest

@k8s-ci-robot k8s-ci-robot merged commit af60d1d into kubernetes-sigs:master Aug 26, 2021
@k8s-ci-robot k8s-ci-robot added this to the v0.4 milestone Aug 26, 2021
enxebre added a commit to enxebre/hypershift that referenced this pull request Sep 14, 2021
To pick kubernetes-sigs/cluster-api#5110 in capi and use capa 0.7.0 release image.
Will drop this when plumbing these images through our pipeline.
enxebre added a commit to enxebre/hypershift that referenced this pull request Sep 14, 2021
To pick kubernetes-sigs/cluster-api#5110 in capi and use capa 0.7.0 release image.
Will drop this when plumbing these images through our pipeline.
enxebre added a commit to enxebre/hypershift that referenced this pull request Sep 14, 2021
To pick kubernetes-sigs/cluster-api#5110 in capi and use capa 0.7.0 release image.
Will drop this when plumbing these images through our pipeline.
enxebre added a commit to enxebre/hypershift that referenced this pull request Sep 17, 2021
To pick kubernetes-sigs/cluster-api#5110 in capi and use capa 0.7.0 release image.
Will drop this when plumbing these images through our pipeline.
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants