Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ShaileshSurya
Copy link
Contributor

@ShaileshSurya ShaileshSurya commented Aug 23, 2020

There are quite a few places in Contour that call a
local check helper that prints to standard error and exits.
This approach loses error context and makes it difficult for
aggregated log collectors to recognize that this is a fatal error message

This commit replaces check functions in cmd/contour/cli.go by log.Fatal

Fixes #2811

Signed-off-by: Shailesh Suryawanshi [email protected]

@ShaileshSurya ShaileshSurya force-pushed the fix-replace-check-to-fatal branch from ff4d8cb to 8b9e3b5 Compare August 23, 2020 08:56
@codecov
Copy link

codecov bot commented Aug 23, 2020

Codecov Report

Merging #2824 into main will increase coverage by 0.03%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2824      +/-   ##
==========================================
+ Coverage   76.57%   76.61%   +0.03%     
==========================================
  Files          79       79              
  Lines        5866     5866              
==========================================
+ Hits         4492     4494       +2     
+ Misses       1283     1282       -1     
+ Partials       91       90       -1     
Impacted Files Coverage Δ
cmd/contour/cli.go 0.00% <0.00%> (ø)
internal/dag/cache.go 96.42% <0.00%> (+0.71%) ⬆️

Copy link
Contributor

@jpeach jpeach left a comment

Choose a reason for hiding this comment

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

This code is only used by the contour cli * subcommands, so exiting manually is appropriate here. The log.Fatal in (*Client) dial() is actually a bug.

We have a few options with this code:

  1. Assume that it is library code and propagate errors out to the caller, who can exit with an error
  2. Replace check with a function ErrExit which does the same thing, but has a clearer name
  3. Replace check with calls to kingpin.FatalIfError
  4. Leave it for now :)

The first option is a fair bit of churn, so you may want to consider the other options :)

@ShaileshSurya
Copy link
Contributor Author

Hi @jpeach,

I find the third option Replace check with calls to the kingpin.FatalIfError intuitive. I will make the changes accordingly and update the PR.

I will also update the existing log.fatal with the kingpin.FatalIfError.

Thank you for the suggestions.

@ShaileshSurya ShaileshSurya force-pushed the fix-replace-check-to-fatal branch from 3183bdc to fc9f789 Compare August 24, 2020 14:58
if ok := certPool.AppendCertsFromPEM(ca); !ok {
// TODO(nyoung) OMG yuck, thanks for this, crypto/tls. Suggestions on alternates welcomed.
log.Fatal("failed to append ca certs")
kingpin.Fatalf("failed to append ca certs")
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 am not very sure about this. I checked kingpins' documentation and could not find kingpin.Fatal. Please advise.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fatalf seems fine to me. Please update the message to say "CA certificates" instead of "ca certs".

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
kingpin.Fatalf("failed to append ca certs")
kingpin.Fatalf("failed to append CA certs")

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this.

@ShaileshSurya ShaileshSurya force-pushed the fix-replace-check-to-fatal branch from fc9f789 to 3d75c25 Compare August 24, 2020 15:15
@ShaileshSurya ShaileshSurya changed the title cmd/contour: Replace check function by log.Fatal() cmd/contour: Replace check() by kingpin.Fatalf() in cli.go Aug 24, 2020
Copy link
Contributor

@jpeach jpeach left a comment

Choose a reason for hiding this comment

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

Good progress, just need to update the exit messages. Can you manually trigger some of then and show the output?

Can you please update the log.Fatal on line 48 as well?

certificate, err := tls.LoadX509KeyPair(c.ClientCert, c.ClientKey)
check(err)

kingpin.FatalIfError(err, "err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will print the error twice, see https://play.golang.org/p/I9qfq3XMT-5

What you want here is:

kingpin.FatalIfError(err, "failed to load key pair")

Copy link
Contributor

Choose a reason for hiding this comment

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

Please update all the other places that format the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to provide a message string to give the context of the failure. For example, this:

$ contour cli cds --cafile=/tmp/t --cert-file=/tmp/t --key-file=/tmp/t
contour: error: failed to load CA certificates: open /tmp/t: no such file or directory

is a better message than this:

$ contour cli cds --cafile=/tmp/t --cert-file=/tmp/t --key-file=/tmp/t
contour: error: open /tmp/t: no such file or directory

Could you please add a message string to each call to kingpin.FatalIfError?

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 please add a message string to each call to kingpin.FatalIfError?

@ShaileshSurya ^^^

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 have made the required changes. We might want to check the wording for each and every error message once.

if ok := certPool.AppendCertsFromPEM(ca); !ok {
// TODO(nyoung) OMG yuck, thanks for this, crypto/tls. Suggestions on alternates welcomed.
log.Fatal("failed to append ca certs")
kingpin.Fatalf("failed to append ca certs")
Copy link
Contributor

Choose a reason for hiding this comment

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

Fatalf seems fine to me. Please update the message to say "CA certificates" instead of "ca certs".

@ShaileshSurya ShaileshSurya force-pushed the fix-replace-check-to-fatal branch from 3d75c25 to bc15f89 Compare August 26, 2020 12:34
There are quite a few places in Contour that call a
local check helper that prints to standard error and exits.
This approach loses error context and makes it difficult for
aggregated log collectors to recognize that this is a fatal error message

This commit replaces check functions in cmd/contour/cli.go by
kingpin libraries fatal logs

Fixes projectcontour#2811

Signed-off-by: Shailesh Suryawanshi <[email protected]>
@ShaileshSurya ShaileshSurya force-pushed the fix-replace-check-to-fatal branch from bc15f89 to 3250d08 Compare August 26, 2020 12:44
}
err := st.Send(req)
check(err)
kingpin.FatalIfError(err, "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please Check https://play.golang.org/p/1WiDkfYYoLp for the output.

certificate, err := tls.LoadX509KeyPair(c.ClientCert, c.ClientKey)
check(err)

kingpin.FatalIfError(err, "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check https://play.golang.org/p/1WiDkfYYoLp for the output.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be

kingpin.FatalIfError(err, "failed to load certificate")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made the changes to all FtalIfError. Added Error messages

@ShaileshSurya
Copy link
Contributor Author

@jpeach I have made the requested changes. Can you please have a look.

fixing the error message to make more consistent

fixes projectcontour#2811

Signed-off-by: Shailesh Suryawanshi <[email protected]>
@ShaileshSurya
Copy link
Contributor Author

@jpeach Please have a look, I have addressed the review comments.

@ShaileshSurya ShaileshSurya requested a review from jpeach August 31, 2020 05:04
Copy link
Contributor

@jpeach jpeach left a comment

Choose a reason for hiding this comment

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

This looks good, thanks @ShaileshSurya

@jpeach jpeach merged commit 78eb83f into projectcontour:main Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace check with Fatal logs

2 participants