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

Change nfd-worker to use Ticker instead of After. #1050

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

VillePihlava
Copy link
Contributor

This pull request changes the nfd-worker to use time.Ticker instead of time.After to implement core.sleepInterval. An infinite interval is represented by stopping the Ticker.

Issue: #899

@netlify
Copy link

netlify bot commented Feb 6, 2023

Deploy Preview for kubernetes-sigs-nfd ready!

Name Link
🔨 Latest commit 2101cb2
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-nfd/deploys/63e50f0a244d7e0008b61c1d
😎 Deploy Preview https://deploy-preview-1050--kubernetes-sigs-nfd.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 6, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: VillePihlava / name: Ville Pihlava (4d1fe19)

@k8s-ci-robot
Copy link
Contributor

Welcome @VillePihlava!

It looks like this is your first PR to kubernetes-sigs/node-feature-discovery 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/node-feature-discovery has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 6, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @VillePihlava. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 6, 2023
Copy link
Contributor

@ArangoGutierrez ArangoGutierrez left a comment

Choose a reason for hiding this comment

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

Patch looks good, thanks for this @VillePihlava !!
Just a small comment
/ok-to-test
The rest I'll leave it to
/assign @marquiz

func (w *nfdWorker) createTicker() *time.Ticker {
if w.config.Core.SleepInterval.Duration > 0 {
return time.NewTicker(w.config.Core.SleepInterval.Duration)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

This Else is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The else is now removed

Copy link
Contributor

Choose a reason for hiding this comment

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

TO me this looks better like a https://gobyexample.com/switch since the func doesn't have a return a Switch will be more elegant here

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Feb 6, 2023
@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Feb 6, 2023
pkg/nfd-worker/nfd-worker.go Outdated Show resolved Hide resolved
pkg/nfd-worker/nfd-worker.go Outdated Show resolved Hide resolved
@ArangoGutierrez
Copy link
Contributor

/assign @marquiz

Copy link
Member

@fmuyassarov fmuyassarov left a comment

Choose a reason for hiding this comment

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

Looks good. Can you perhaps squash the commits?

Comment on lines 180 to 182
t := time.NewTicker(time.Hour)
t.Stop()
return t
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could simplify this a bit with just

Suggested change
t := time.NewTicker(time.Hour)
t.Stop()
return t
return &time.Ticker{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not entirely sure what happens when calling Stop() with an uninitialized ticker, so I changed the implementation a little to not call it on an uninitialized one and incorporated this change in a sense

Copy link
Contributor

Choose a reason for hiding this comment

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

Mm, that's a fair point. Even if it works now it's not clearly specified (from what I can tell) if calling Stop() on uninitialized Ticker is legit which might cause a breakage later.

@VillePihlava
Copy link
Contributor Author

Looks good. Can you perhaps squash the commits?

Done

Comment on lines 188 to 189
func (infiniteTicker *infiniteTicker) startTicker(w *nfdWorker) {
// If the sleep interval is not a positive number, the ticker will act as if it was set to an infinite duration by not ticking.
if w.config.Core.SleepInterval.Duration > 0 {
infiniteTicker.T = time.NewTicker(w.config.Core.SleepInterval.Duration)
infiniteTicker.initialized = true
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the update @VillePihlava. Quite a bike-shedding on this PR, but somehow this latest version feels just a tad too complicated (in terms of lines-of-code) for such a simple thing 😇

How about using struct composition to wrap the Ticker type, with something like

type infiniteTicker struct {
        *time.Ticker
}

func (i *infiniteTicker) Reset(d time.Duration) {
        if d > 0 {
                i.Ticker.Reset(d)
        } else {
                i.Ticker.Stop()
        }
}

...then you'd initialize with smth like (you could specify a dedicated create function if this looks too bad):

labelTrigger := infiniteTicker{Ticker: time.NewTicker(1)} // just any non-zero duration here, we'll do the actual configuration below
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)

use the channel directly:

case <-labelTrigger.C:

and simply use reset for re-configuring (instead of creating a new Ticker instance)

labelTrigger.Reset(w.config.Core.SleepInterval.Duration)

WDYT? Haven't tested but I think that should work out

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this makes sense, I made these changes now. Also tested manually that making changes to the sleepinterval in the config worked

Copy link
Contributor

@marquiz marquiz left a comment

Choose a reason for hiding this comment

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

Thanks @VillePihlava! Looks good to me 🤓

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 9, 2023
@marquiz
Copy link
Contributor

marquiz commented Feb 9, 2023

/assign @ArangoGutierrez

func (w *nfdWorker) createTicker() *time.Ticker {
if w.config.Core.SleepInterval.Duration > 0 {
return time.NewTicker(w.config.Core.SleepInterval.Duration)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

TO me this looks better like a https://gobyexample.com/switch since the func doesn't have a return a Switch will be more elegant here

i.Ticker.Reset(d)
} else {
// If the sleep interval is not a positive number the ticker will act as if it was set to an infinite duration by not ticking.
i.Ticker.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you format this comment to fit in 80 columns, see how other comments when too long they are several lines. makes it easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the if-else to a switch and made the comment fit into 80 columns

Copy link
Contributor

@ArangoGutierrez ArangoGutierrez left a comment

Choose a reason for hiding this comment

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

Thanks for the effort @VillePihlava !

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 9, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 922bfb8952a57ef5029c26b9e548a31ed048895e

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ArangoGutierrez, marquiz, VillePihlava

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 ac271b3 into kubernetes-sigs:master Feb 9, 2023
@marquiz marquiz mentioned this pull request Apr 12, 2023
24 tasks
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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