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

Move variable defaults to -var-file #1079

Merged
merged 8 commits into from
Nov 21, 2022
Merged

Conversation

cartermckinnon
Copy link
Member

Description of changes:

These variables appear in multiple templates internally, so this will centralize the values.

I also fixed a weird spacing issue with the PACKER_VARIABLES logic. The foreach function in make will join the expanded 3rd argument with a space character. When the loop evaluates for packer variables that aren't defined, it results in a ton of extra space being inserted. Example before this change:

packer validate -var-file=version-variables.json  -var instance_type='m4.large'      -var arch='x86_64'  -var ami_name='amazon-eks-node-.-v20221102'              -var aws_region='us-west-2'        -var binary_bucket_region='us-west-2'                                           eks-worker-al2.json

and after this change:

packer validate -var-file=version-variables.json -var instance_type='m4.large' -var arch='x86_64' -var ami_name='amazon-eks-node-.-v20221102' -var aws_region='us-west-2' -var binary_bucket_region='us-west-2' eks-worker-al2.json

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Testing Done

Succeeded:

make 1.22

@cartermckinnon cartermckinnon marked this pull request as ready for review November 2, 2022 23:54
eks-worker-al2.json Outdated Show resolved Hide resolved
@cartermckinnon
Copy link
Member Author

cartermckinnon commented Nov 3, 2022

After this change, possible usages include:

  1. make. This will use the values defined in the variables file.
  2. make PACKER_VARIABLE_FILE=my-variable-file.json. This will allow users to define their own values for docker_version or any other variable in the template. If they don't include a required variable, like docker_version, they'll get an error.
  3. make docker_version=1.2.3. This will override the value we have in our variable file. This is already how users should be providing these values.

Copy link
Member

@mmerkes mmerkes left a comment

Choose a reason for hiding this comment

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

LGTM, but let's update the README to explain how this works and how customers should override defaults.

@cartermckinnon
Copy link
Member Author

@mmerkes and I discussed this -- we're going to try moving as many variables into the var-file as possible, to avoid confusion about where defaults are defined. All variables that have a default value that isn't a go template will be moved to the variable file. New rev coming.

@cartermckinnon cartermckinnon changed the title Move software version variables to -var-file Move variables to -var-file Nov 3, 2022
@cartermckinnon cartermckinnon changed the title Move variables to -var-file Move variable defaults to -var-file Nov 3, 2022
@cartermckinnon
Copy link
Member Author

Packer allows multiple -var-file arguments, and will merge the contents in the order that you specify them. I moved all the defaults into the variables file. Users can specify their own variables file to override a subset of the variables, or they can specify individual key-value variable overrides to make. (Or some combination of both).

@cartermckinnon cartermckinnon added documentation Documentation issue enhancement New feature or request and removed documentation Documentation issue labels Nov 4, 2022
Copy link
Contributor

@saurav-agarwalla saurav-agarwalla 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 change. It'll simplify a bunch of things.

@@ -1,42 +1,43 @@
{
"_comment": "All template variables are enumerated here; and most variables have their default values defined in eks-worker-al2-variables.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the benefit in duplicating the variables here? Can't eks-worker-al2-variables.json be the source of truth?

Copy link
Member Author

@cartermckinnon cartermckinnon Nov 4, 2022

Choose a reason for hiding this comment

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

This PR dynamically discovers the variables from the template using packer inspect, so they need to be present in the template. ☹️ Because we support overriding them in the Makefile via this mechanism, we have to maintain the list of variables somewhere (either in the Makefile or in the template).

We could pull these out of the variables file with something else like jq, but I'd prefer to use the Packer mechanism if it's reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the packer inspect usage I'm referring to:

AVAILABLE_PACKER_VARIABLES := $(shell $(PACKER_BINARY) inspect -machine-readable eks-worker-al2.json | grep 'template-variable' | awk -F ',' '{print $$4}')


Users have the following options for specifying their own values:

1. Provide a variable file with the `PACKER_VARIABLE_FILE` argument to `make`. Values in this file will override values in the default variable file. Your variable file does not need to include all possible variables, as it will be merged with the default variable file.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be possible to write tests for these behaviors?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind, I saw your comment stating that packer is the one which does this so no point in testing that. Contents of that comment might be good for this section too. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd err on the side of fewer impl details in this doc, but I don't feel too strongly. Can totally add if you think it's necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants