-
Notifications
You must be signed in to change notification settings - Fork 38
Extract Terraform error messages from JSON logs #143
Conversation
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ulucinar ! My general comment is that there are only a limited set of errors, shapes and even error content that we expect this library to be used for and I believe we should take advantage of this as much as possible by reducing the logic to handle only those instead of making it generic for future possibilities. Both approaches are valid but if we start with the smallest possible set, we won't have to pay the cost of generic-ness until we actually need it.
tfLogs := make([]*TerraformLog, 0, len(logLines)) | ||
for _, l := range logLines { | ||
log := &TerraformLog{} | ||
l := strings.TrimSpace(l) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any metadata information we can use to filter out the information from HCL? In the past, we were seeing the creds printed in errors and removed them from HCL but if there is a way to not include that at this level, I think we can remove Env []string
functionality we added back then to simplify things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would propose to see if we hit that situation again. Previously, we were supplying the provider credentials via the Terraform configuration file (main.hcl) which had caused that issue. Do we know any other cases which could cause a similar issue. If so, I would suggest investigating it in a separate issue maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, we were supplying the provider credentials via the Terraform configuration file (main.hcl) which had caused that issue.
I vaguely recall that the error was printed as one type of JSON and then HCL was printed in another JSON with different classification. What I'm proposing is that if we're able to differentiate programmatically by checking the properties of those JSONs, it'd be great to do it so that we revert to writing credentials to HCL to simplify that part of the codebase. If all was in a single error message that we can't differentiate, then there isn't much we can do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe env solution fixing more than just error messages and we should not revert it: #99 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ulucinar !
…ors, respectively - Do not defer error messages extraction to Error() Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
d7cdee9
to
0e3cd3d
Compare
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ulucinar !
} | ||
|
||
func (a *applyFailed) Error() string { | ||
return a.log | ||
func newTFError(message string, logs []byte) (string, *tfError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpitck
func newTFError(message string, logs []byte) (string, *tfError) { | |
func newTFError(message string, logs []byte) (*tfError, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because tfError
is shared (embedded) inside actual error types and because we need to consume the returned error instance as a tfError
, doing this will necessitate type assertions at those sites. I'd prefer to keep it as it's as it's not part of the public interface.
} | ||
|
||
// IsApplyFailed returns whether error is due to failure of an apply operation. | ||
func IsApplyFailed(err error) bool { | ||
_, ok := err.(*applyFailed) | ||
return ok | ||
r := &applyFailed{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: now it's possible to get away with simple type casting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I also considered. However, in case these errors are themselves wrapped, then the current implementations are more robust. In my opinion, as a principle, we had better always implement similar methods with wrapping in mind.
Description of your changes
Fixes #44
This PR proposes changes that parse the actual error messages from respective JSON-formatted Terraform CLI logs in case the Terraform CLI pipeline fails.
I have:
make reviewable
to ensure this PR is ready for review.backport release-x.y
labels to auto-backport this PR if necessary.How has this code been tested
On top of crossplane-contrib/provider-jet-azure#83, an errored condition without these changes results in:
With the proposed changes: