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

Improve logic for setting LastTransitionTime #16

Merged
merged 5 commits into from
May 30, 2023
Merged

Conversation

inteon
Copy link
Member

@inteon inteon commented May 30, 2023

This PR fixes a bug that caused the LastTransitionTime to be updated every time a condition is changed.
Instead, we now only change the LastTransitionTime value when the status of the condition changed (as described in the comments for this field).
This PR also makes it so the Clock is passed explicitly in the reconcilers/ functions. This allows us to better test using fake Clocks.

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
@jetstack-bot jetstack-bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 30, 2023
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
@jetstack-bot jetstack-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 30, 2023
// Update the status with the provided condition details & return
// the added condition.
// NOTE: this code is just a workaround for cmutil only accepting a GenericIssuer interface
Copy link
Member Author

@inteon inteon May 30, 2023

Choose a reason for hiding this comment

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

Instead of calling the cert-manager code, we now just host this logic in this repo. This makes it much clearer what is going on & allowed me to customize the logic for our needs (SSA).

Copy link
Member

Choose a reason for hiding this comment

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

Definitely makes sense for his repo's issuer type to have these kinds of functions be more specific!

Copy link
Member

@SgtCoDFish SgtCoDFish left a comment

Choose a reason for hiding this comment

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

I think this seems good and it makes sense; I've got a suggestion on handling time in tests which I hope might be useful!

Comment on lines 65 to 71
fakeTime1 := time.Now().Truncate(time.Second)
fakeTimeObj1 := metav1.NewTime(fakeTime1)
fakeClock1 := clocktesting.NewFakeClock(fakeTime1)

fakeTime2 := time.Now().Add(4 * time.Hour).Truncate(time.Second)
fakeTimeObj2 := metav1.NewTime(fakeTime2)
fakeClock2 := clocktesting.NewFakeClock(fakeTime2)
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: I think using time.Now here adds complexity which doesn't benefit us and might allow us to make more mistakes in the future.

It's entirely possible we could see a function which mistakenly calls time.Now() instead of clock.Now(). It's possible that in that situation it could pass a test when it shouldn't - especially if we're using Truncate(time.Second)

If we used a fixed test time here, there'd be no risk of that type of mistake creeping in. For example:

fakeTime1 := time.UnixMicro(10)

What do you think?

Copy link
Member Author

@inteon inteon May 30, 2023

Choose a reason for hiding this comment

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

Thanks, I think there definitely could be an issue like that in the future. However, I think that some tests might still succeed incorrectly when the date is set far in the past or far in the future (depending on the test). I think it might be useful to use a random date here, such a test will fail eventually, indicating that we aren't using the correct clock somewhere in code.

Copy link
Member Author

Choose a reason for hiding this comment

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

see 642ff17

Comment on lines -27 to 28
// NOTE: this code is just a workaround for cmutil only accepting the certificaterequest object
func SetCertificateRequestStatusCondition(
Copy link
Member

Choose a reason for hiding this comment

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

comment: It's a shame to have this code here - functions which act on cert-manager resources such as CertificateRequest seem like a natural fit for the cert-manager codebase. Better to have one source of truth for "how do I update a CR status condition".

Don't think it's a dealbreaker - but I wonder if we could add something to the cert-manager codebase instead? Doesn't really matter though

// Update the status with the provided condition details & return
// the added condition.
// NOTE: this code is just a workaround for cmutil only accepting a GenericIssuer interface
Copy link
Member

Choose a reason for hiding this comment

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

Definitely makes sense for his repo's issuer type to have these kinds of functions be more specific!

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
Copy link
Member

@SgtCoDFish SgtCoDFish left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

I like the way you've done the random time, there! Thanks

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label May 30, 2023
@jetstack-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: inteon, SgtCoDFish

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

@jetstack-bot jetstack-bot merged commit f34b02d into main May 30, 2023
@inteon inteon deleted the lasttransitiontime branch May 30, 2023 17:05
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. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. lgtm Indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants