-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules): set variable default values (#6)
* feat(irsa): Align IRSA variables * feat: set module vars default values * feat: add variable syncing * feat(ci): add pip caching * feat(ci): add asdf cache * feat(ci): add pre-commit to pip deps cache * fix: path empty string defaults * docs(inputs): update * feat: review improvements * fix: integration defaults * fix: update oidc_assume_role_policy_condition_test default to StringLike * fix: update module source references * fix: update module source references to v0.0.12 --------- Co-authored-by: Martin Odstrčilík <[email protected]>
- Loading branch information
1 parent
ef9a727
commit ccdd46e
Showing
13 changed files
with
243 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,36 @@ jobs: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: asdf-vm/actions/install@v3 | ||
|
||
- name: Cache ASDF | ||
uses: actions/cache@v4 | ||
id: asdf-cache | ||
with: | ||
path: ~/.asdf/ | ||
key: ${{ runner.os }}-asdf-${{ hashFiles('.tool-versions') }} | ||
restore-keys: ${{ runner.os }}-asdf- | ||
|
||
- name: Setup ASDF | ||
uses: asdf-vm/actions/setup@v3 | ||
if: ${{ steps.asdf-cache.outputs.cache-hit == 'true' }} | ||
|
||
- name: Install ASDF | ||
uses: asdf-vm/actions/install@v3 | ||
if: ${{ steps.asdf-cache.outputs.cache-hit != 'true' }} | ||
|
||
- name: Reshim installed ASDF tools | ||
shell: bash | ||
run: asdf reshim | ||
|
||
- name: Cache pip dependencies | ||
uses: actions/cache@v4 | ||
with: | ||
path: ~/.cache/pip | ||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt', '.pre-commit-config.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pip- | ||
- name: Install pip dependencies | ||
run: pip install -r requirements.txt | ||
|
||
- uses: pre-commit/[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ tflint 0.50.3 | |
checkov 3.2.37 | ||
awscli 2.15.29 | ||
pre-commit 3.6.2 | ||
python 3.13.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
> [!IMPORTANT] | ||
> Variables defined in [variables-addon.tf](variables-addon.tf) defaults to `null` to have them overridable by the addon configuration defined though the [`local.addon.*`](main.tf) local variable with some default values defined in [addon.tf](addon.tf). | ||
> Variables defined in [variables-addon[-irsa|oidc].tf](variables-addon.tf) defaults to `null` to have them overridable by the addon configuration defined though the [`local.addon[_irsa|oidc].*`](main.tf) local variable with the default values defined in [addon[-irsa|oidc].tf](addon.tf). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
python-hcl2==5.1.1 | ||
Jinja2==3.1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import hcl2 | ||
import re | ||
import argparse | ||
import logging | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
def filter_terraform_type(value): | ||
# Currently there is a limition in handling Terraform complex types | ||
# https://github.com/amplify-education/python-hcl2/issues/179 | ||
# https://github.com/amplify-education/python-hcl2/issues/172 | ||
if isinstance(value, str): | ||
return re.sub(r'\${(.*)}', r'\1', value) | ||
return value | ||
|
||
def filter_terraform_default(value): | ||
if isinstance(value, bool): | ||
return str(value).lower() | ||
|
||
if isinstance(value, str): | ||
if value == "": | ||
return '\\"\\"' | ||
|
||
if value == None: | ||
return 'null' | ||
|
||
return re.sub(r'\'', r'\\"', str(value)) | ||
|
||
def get_template(): | ||
env = Environment(loader=FileSystemLoader(".")) | ||
env.filters['terraform_type'] = filter_terraform_type | ||
env.filters['terraform_default'] = filter_terraform_default | ||
|
||
return env.from_string("""# IMPORTANT: This file is synced with the "terraform-aws-eks-universal-addon" module. Any changes to this file might be overwritten upon the next release of that module. | ||
{%- for variable in variables %} | ||
{%- for name, spec in variable.items() %} | ||
{%- if name != 'enabled' %} | ||
variable "{{ name }}" { | ||
type = {{ spec.type | terraform_type }} | ||
default = null | ||
description = "{{ spec.description }}{% if spec.default is defined %} Defaults to `{{ spec.default | terraform_default }}`.{% endif %}" | ||
} | ||
{%- endif %} | ||
{%- endfor %} | ||
{% endfor %} | ||
""") | ||
|
||
def get_logger(args): | ||
log_level = args.log.upper() | ||
|
||
numeric_level = getattr(logging, log_level, None) | ||
if not isinstance(numeric_level, int): | ||
raise ValueError('Invalid log level `%s`' % log_level) | ||
|
||
logging.basicConfig(format='%(levelname)s: %(message)s', level=numeric_level) | ||
|
||
return logging.getLogger(__name__) | ||
|
||
def main(args): | ||
log = get_logger(args) | ||
log.info("Syncing variables from Terraform modules...") | ||
log.warning("Terraform variable complex types are NOT supported!") | ||
|
||
template = get_template() | ||
|
||
for module in os.listdir('.terraform/modules'): # Iterate over all initialized modules | ||
if not module.startswith('addon') or module.find(".") != -1: # Skip non-addon modules, ie. nested modules | ||
log.info("Skipping module `%s`", module) | ||
continue | ||
|
||
log.info("Processing module `%s`", module) | ||
|
||
source = '.terraform/modules/'+module+'/modules/'+module+'/variables.tf' | ||
target = 'variables-'+module+'.tf' | ||
|
||
with open(source, 'r') as f: | ||
log.info("Reading variables from `%s`", source) | ||
|
||
variables = hcl2.load(f).get('variable') | ||
log.info("Collected variables: %d", len(variables)) | ||
log.debug(variables) | ||
|
||
with open(target, "w") as f: | ||
log.info("Writing variables to `%s`", target) | ||
f.write(template.render(variables=variables)) | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Sync Terraform variables from the local addon modules to proxy variable files') | ||
parser.add_argument('--log', default='INFO', help='Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)') | ||
|
||
args = parser.parse_args() | ||
|
||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.