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

Datasource logic cleanup #12608

Merged
merged 6 commits into from Oct 5, 2023
Merged

Datasource logic cleanup #12608

merged 6 commits into from Oct 5, 2023

Conversation

lbajolet-hashicorp
Copy link
Collaborator

This PR is linked to #12607 as it is part of the ongoing refactoring efforts regarding the HCL parsing/execution logic.

This focuses on datasources, and simplifies the code a bit by not invoking the datasources that have no dependency prior to invoking the recursive execution function.

The code to extract the variables from the expressions in the block is moved away from the datasource evaluation as it will be reusable for other block types, but needs to be improved as it only works on the top-level block here, which makes dependency evaluation for datasources dependent on their presence as an attribute in a non-nested block.

Cycle detection now also returns the location of the conflict, this way it is easier for users to troubleshoot their configs if such a situation occurs.

@lbajolet-hashicorp lbajolet-hashicorp requested a review from a team as a code owner August 24, 2023 20:31
@lbajolet-hashicorp lbajolet-hashicorp marked this pull request as draft August 25, 2023 13:08
@lbajolet-hashicorp
Copy link
Collaborator Author

Note: marking as draft for now since I realise the change in the datasource evaluation scheme may increase the likelihood of panics appearing in client code. We definitely need to figure out a more reliable way to detect dependencies before we can proceed with merging this PR.

@lbajolet-hashicorp lbajolet-hashicorp marked this pull request as ready for review August 30, 2023 14:35
@lbajolet-hashicorp
Copy link
Collaborator Author

Update: after discussing and investigating some more, this code should work on our templates, both in JSON and HCL.
HCL bodies are always hclsyntax.Body, so we can recursively walk the contents of their bodies by directly accessing the structure (hoping it doesn't go private someday...).
HCL JSON bodies are always json.body, we don't have access to the structure, but getting the values from the expressions does walk recursively on the body, and since there's no block in the body, we can safely invoke this on it.

I have not seen in the code or the docs other types being possible after parsing, without some extra wrapping, so I would think handling both cases makes it reasonably reliable to get the dependencies of a datasource.
This function should also be usable for other components, which should pave the way to first iterations of a DAG.

In the meantime, I suggest we review and merge this eventually, as it should make dependency detection between datasources more reliable, and reduce the likelihood of evaluation in a wrong order.

Copy link
Member

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

FWIW this looks good to me. I haven't approved because I wanted to understand #12607 which you mention is related

@lbajolet-hashicorp
Copy link
Collaborator Author

So for clarity, I mention #12607 as it is part of the series of fixes/improvements that I stumbled upon while working on refactoring the execution logic.
The code itself can be merged in any order AFAIK, this shouldn't conflict (or if it does it will be very manageable to resolve), both fixes are independent from one another.

Copy link
Member

@nywilken nywilken 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 to me. Nicely done on distilling the datasource parsing and execution logic. The utils for getting variable attributes from an HCL body looks good.

I left one suggestion to help with readability but this is otherwise good. Feel free to ping once you've rolled in the suggested changes.

hcl2template/types.packer_config.go Outdated Show resolved Hide resolved
hcl2template/utils.go Outdated Show resolved Hide resolved
Not sure why this was defined and returned, but the value was set, but
never used, as such this is not useful to keep in the code, so let's
simplify this now.
Since datasources are recursively evaluated depending on their
dependencies, we don't need to pre-execute those that depend on nothing,
as the recursive traversal of the datasources will take care of that for
us.
When a datasource fails to be evaluated because a cycle has been
detected, we point out one of the links of the chain now so that users
have a better idea of what to look at.
Datasources use their attribute's expressions to determine whether or
not they depend on another datasource, in order to get the list of
dependencies and execute them before executing a datasource.

This code may be useful later on for figuring out the dependencies for
any block, so we move this code to the utils.go file, and use this for
datasources.
The previous implementation of the GetVarsByType function worked only on
top-level attributes, ignoring the nested blocks in the structure.

This implies that if a datasource depends on another through an
expression within a nested block, we may not execute it first, and then
executing this datasource before its dependent is possible, resulting in
an error in the end.

This commit is an attempt at making this more reliable for HCL configs,
but only works on configs lifted from HCL files for now. We need to make
this more reliable for later iterations.
@vercel
Copy link

vercel bot commented Oct 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
packer ⬜️ Ignored (Inspect) Visit Preview Oct 3, 2023 6:21pm

Copy link
Member

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@lbajolet-hashicorp lbajolet-hashicorp added bug backport/1.9.x Backport PR changes to `release/1.9.x` labels Oct 5, 2023
@lbajolet-hashicorp lbajolet-hashicorp merged commit 434a163 into main Oct 5, 2023
14 checks passed
Copy link

github-actions bot commented Nov 5, 2023

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/1.9.x Backport PR changes to `release/1.9.x` bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants