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

Closes #99: Make logging configurable. #100

Merged
merged 1 commit into from
Mar 30, 2022

Conversation

fgiloux
Copy link
Contributor

@fgiloux fgiloux commented Jan 25, 2022

Description of the change:

Closes #99
This pull request aligns the logging configuration in the prune feature with what is done in the rest of the library. It declares a package level variable for a logger. The logger is retrieved from the controller-runtime library, which is leveraging logr.

Motivation for the change:

The current prune implementation could not be used outside of the prune package as the log field of the Config struct was unexported.

A pull request for amending the documentation will follow.

@openshift-ci
Copy link

openshift-ci bot commented Jan 25, 2022

Hi @fgiloux. Thanks for your PR.

I'm waiting for a operator-framework 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.

@openshift-ci openshift-ci bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 25, 2022
@fgiloux
Copy link
Contributor Author

fgiloux commented Jan 25, 2022

cc @camilamacedo86 @Sijoma @ryantking

@fgiloux
Copy link
Contributor Author

fgiloux commented Jan 26, 2022

@camilamacedo86 can you please review the PR?

Copy link
Contributor

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jan 27, 2022
@camilamacedo86
Copy link
Contributor

/ok-to-test

@openshift-ci openshift-ci bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 27, 2022
@coveralls
Copy link

coveralls commented Jan 27, 2022

Pull Request Test Coverage Report for Build 2052966183

  • 22 of 24 (91.67%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 80.89%

Changes Missing Coverage Covered Lines Changed/Added Lines %
prune/prune.go 12 14 85.71%
Totals Coverage Status
Change from base Build 1710189297: -0.01%
Covered Lines: 618
Relevant Lines: 764

💛 - Coveralls

@fgiloux
Copy link
Contributor Author

fgiloux commented Jan 28, 2022

@theishshah @varshaprasad96 can you please take a look? I would very welcome if we can get it merged soon.
Thanks!

@fgiloux
Copy link
Contributor Author

fgiloux commented Mar 21, 2022

@theishshah @varshaprasad96 Anything preventing the merge of this PR?

@joelanford
Copy link
Member

@fgiloux I made this comment over on the issue. WDYT?

Copy link
Member

@varshaprasad96 varshaprasad96 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
/hold
holding for @joelanford's lgtm based on the last comment.

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 22, 2022
@openshift-ci
Copy link

openshift-ci bot commented Mar 22, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: varshaprasad96

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

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 22, 2022
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Mar 23, 2022
@fgiloux
Copy link
Contributor Author

fgiloux commented Mar 23, 2022

@joelanford @varshaprasad96 @camilamacedo86 reworked to address Joe's concerns

prune/prune.go Outdated
// Note that there is no compile time check whether a logger can be retrieved by either way.
// keysAndValues allow to add fields to the logs, cf logr documentation.
func Logger(ctx context.Context, cfg Config, keysAndValues ...interface{}) logr.Logger {
log := cfg.Log
Copy link
Member

Choose a reason for hiding this comment

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

I kind of feel like cfg.Log (if it's set) should win over logr.FromContext(). WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was that cfg.Log is set once whereas logr.FromContext() can be changed, although unlikely with every request. I picked the one with the scope having the shorter lifetime

Copy link
Member

Choose a reason for hiding this comment

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

My thought was that if I go to the trouble of setting the logger in the config, that's an extra explicit step I'm taking to say "use this logger". With the code as is, if I was using this in Reconcile, I'd have to create a new context that drops the logger from the Reconcile context before using the prune functions. That seems pretty awkward.

prune/prune.go Outdated
func Logger(ctx context.Context, cfg Config, keysAndValues ...interface{}) logr.Logger {
log := cfg.Log
if ctx != nil {
if logger, err := logr.FromContext(ctx); err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Should we use controller-runtime's version of this function so that if there isn't a logger in the context, we end up with the controller-runtime's global logger? Or is that too much coupling to controller-runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was to avoid coupling with controller-runtime... but the library has already a dependency on controller-runtime. I think Jeff wanted to make it available to other frameworks, hence

type Config struct {
        Clientset      kubernetes.Interface      // kube client used by pruning

Due to the already existing dependency I can use the controller-runtime version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also means that we will always get a logger from context. In that case this cannot have precedence over the logger set in the config struct otherwise this would never be set.

prune/prune.go Outdated
@@ -181,3 +181,17 @@ func (config Config) validate() (err error) {
}
return nil
}

// Logger returns a logger from the context using logr method or Config.Log if none is found
// controller-runtime automatically provides a logger in context.Context.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// controller-runtime automatically provides a logger in context.Context.
// controller-runtime automatically provides a logger in context.Context during Reconcile calls.

Maybe other times as well? This is the only one I know about though.

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 won't debate on that :-)

// controller-runtime automatically provides a logger in context.Context.
// Note that there is no compile time check whether a logger can be retrieved by either way.
// keysAndValues allow to add fields to the logs, cf logr documentation.
func Logger(ctx context.Context, cfg Config, keysAndValues ...interface{}) logr.Logger {
Copy link
Member

Choose a reason for hiding this comment

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

Does this function need to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is exported in controller-runtime and the user can leverage it for custom code that gets injected through PreDeleteHook and CustomStrategy. I am passing the context to them for the purpose.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Makes sense. Thanks for the explanation!

@joelanford
Copy link
Member

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Mar 24, 2022
@fgiloux fgiloux changed the title Closes #99: Remove log from Config and align with the library strategy. Closes #99: Make logging configurable. Mar 28, 2022
…xt (controller-runtime way)

Signed-off-by: Frederic Giloux <[email protected]>
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Mar 28, 2022
@camilamacedo86
Copy link
Contributor

camilamacedo86 commented Mar 29, 2022

Hi @joelanford @jmrodri @varshaprasad96

This project is still < 1.0.0, any reason for we do not move forward when the api diff fails?
Do we need to ensure backwards compatibility in the lib? I do not think so, but before we click on the button I'd like just to confirm. :-)

c/c @fgiloux

@joelanford
Copy link
Member

Do we know what the impact would be if this broke (what change is necessary for callers?, how many operators are using the library?). I'd say its okay to break this if necessary, but we should generally try to avoid it.

Did someone mention that @ryantking had a larger design or some follow-on changes to make? If so, I'd suggest that we try to avoid breaking these APIs twice.

@fgiloux
Copy link
Contributor Author

fgiloux commented Mar 29, 2022

@camilamacedo86 @joelanford as per #99 the current situation is that Config.log is not exported and not set automatically but it is called in the code. In my opinion this makes the library broken. Hence there is nothing to break. As the prune library is documented in multiple places the urgency is to make it usable That's the aim of this PR.
To answer Joe''s questions:

Do we know what the impact would be if this broke (what change is necessary for callers?

No impact, adding a field, which is optional is not breaking an API. This means if the library had happened to work it would work further but it did not by any way.

how many operators are using the library?). I'd say its okay to break this if necessary, but we should generally try to avoid it.

As log is called and not configurable my guess would be exactly 0.

Did someone mention that @ryantking had a larger design or some follow-on changes to make? If so, I'd suggest that we try to avoid breaking these APIs twice.

I mentioned that. The EP has been however stale waiting to get merged for months. The urgency is to make a documented library usable.

@joelanford
Copy link
Member

Seems like the breaking change is adding a context parameter to the PreDelete and StrategyFunc function signatures.

Based on everything @fgiloux said, it seems like a no brainer to get this library to a working state even if that means some minor breaking API changes.

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Mar 29, 2022
@camilamacedo86
Copy link
Contributor

/lgtm

@camilamacedo86 camilamacedo86 merged commit d389ad4 into operator-framework:main Mar 30, 2022
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. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prune logging cannot be set
5 participants