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

feat: Add infracost_breakdown hook #252

Merged
merged 41 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
48e1a54
feat: Add infracost hook
MaxymVlasov Oct 18, 2021
4aacabe
Provide option to disable color
MaxymVlasov Oct 18, 2021
13e77ed
Fix jq calculator
MaxymVlasov Oct 18, 2021
f4ab440
Run check only once
MaxymVlasov Oct 18, 2021
b4d65dd
Do not run hook on docs update and so on
MaxymVlasov Oct 18, 2021
b570bf1
Read multiline config
MaxymVlasov Oct 18, 2021
01aa7f5
Add usage docs and refactor script
MaxymVlasov Oct 18, 2021
d31d593
Merge branch 'master' into feat/GH-206/infracost
antonbabenko Oct 19, 2021
397970b
Apply suggestions from code review
MaxymVlasov Oct 19, 2021
22812ed
Regex optimization
MaxymVlasov Oct 19, 2021
25ab2b3
Simplify initialize function
MaxymVlasov Oct 19, 2021
f6824d5
Update infracost_breakdown.sh
MaxymVlasov Oct 19, 2021
5de8c97
Implement review suggestions
MaxymVlasov Oct 19, 2021
09b0a0f
Fix check
MaxymVlasov Oct 19, 2021
f9c7064
Document neat thing
MaxymVlasov Oct 19, 2021
7ea7a03
Implement review sugeestions
MaxymVlasov Oct 19, 2021
4709401
Fix misspell
MaxymVlasov Oct 19, 2021
6d302aa
Update infracost_breakdown.sh
MaxymVlasov Oct 19, 2021
32d3d80
Update infracost_breakdown.sh
MaxymVlasov Oct 19, 2021
4868f7b
Merge 32d3d80e0aa70e2d1629bf733d99a862780e072b into c0b9b3c5622f09123…
MaxymVlasov Oct 19, 2021
ba4797b
pre-commit fixes
Oct 19, 2021
5fdfd35
Update counter
MaxymVlasov Oct 19, 2021
8696f20
Update infracost_breakdown.sh
MaxymVlasov Oct 19, 2021
55c7622
Replace the biggest crutch
MaxymVlasov Oct 19, 2021
2873ffa
Update infracost_breakdown.sh
MaxymVlasov Oct 19, 2021
206f0d3
Fix misspels
MaxymVlasov Oct 19, 2021
df2e3d8
Added some comments aimed to simplify the code a bit
antonbabenko Oct 20, 2021
6cbe331
Implement review suggestions
MaxymVlasov Oct 20, 2021
1b98012
Fix short arg
MaxymVlasov Oct 20, 2021
5c8137d
Implement review suggestions
MaxymVlasov Oct 25, 2021
8176669
fixup
MaxymVlasov Oct 25, 2021
ba12b81
Update infracost_breakdown.sh
MaxymVlasov Oct 25, 2021
70e95e6
Document difficult place
MaxymVlasov Oct 25, 2021
aa71a72
Apply suggestions from code review
MaxymVlasov Oct 25, 2021
ad0df87
Fixed texts a bit
antonbabenko Oct 25, 2021
e4bfbaf
Added note about shortcut .diffTotalHourlyCost
antonbabenko Oct 25, 2021
2c1735f
Update README.md
MaxymVlasov Oct 25, 2021
b6884cc
Update .github/CONTRIBUTING.md
antonbabenko Oct 25, 2021
084d0c2
Fix misspels
MaxymVlasov Oct 25, 2021
6c37179
Unify functions style
MaxymVlasov Oct 25, 2021
16ddb89
Make common functions trully common
MaxymVlasov Oct 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,15 @@ For example, the hook tracks `--path=./env/dev` and `./env/dev` depend on `./mai
* Hook uses `jq` to parse `infracost` output, so paths to values like `.totalHourlyCost` and `.totalMonthlyCost` should be in jq-compatible format.
To check available structure use `infracost breakdown -p PATH_TO_TF_DIR --format json | jq -r . > infracost.json`. And play with it on [jqplay.org](https://jqplay.org/)
* Supported comparison operators: `<`, `<=`, `==`, `!=`, `>=`, `>`.
* Most useful paths:
* Most useful paths and checks:
* `.totalHourlyCost` (same to `.projects[].breakdown.totalHourlyCost`) - show total hourly infra cost
* `.totalMonthlyCost` (same to `.projects[].breakdown.totalMonthlyCost`) - show total monthly infra cost
* `.projects[].diff.totalHourlyCost` - show hourly cost diff between existing infra and tf plan
* `.projects[].diff.totalMonthlyCost` - show monthly cost diff between existing infra and tf plan
* You can setup only one path per one hook (`- id: infracost_breakdown`) - this is `infracost` limitation.
* `[.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000`:
* fail if changes push the total monthly cost estimate above $1K
* fail if changes increase the cost by $1K.
* You can set up only one path per one hook (`- id: infracost_breakdown`) - this is `infracost` limitation.
* Set `verbose: true` to see cost even when the checks are passed.
* To disable hook color output, set `PRE_COMMIT_COLOR=never` env var

Expand Down
36 changes: 27 additions & 9 deletions infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ function infracost_breakdown_ {

for check in "${checks[@]}"; do
check=$(echo "$check" | sed 's/^[[:space:]]*//')

MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
operation="$(echo "$check" | grep -oE '[!<>=]+')"
IFS="$operation" read -r -a jq_check <<< "$check"
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
compare_value="${jq_check[1]}${jq_check[2]}"
# Check types
jq_check_type="$(jq -r "${jq_check[0]} | type" <<< "$RESULTS")"
compare_value_type="$(jq -r "$compare_value | type" <<< "$RESULTS")"
# Fail if compare difаerent types
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
if [ "$jq_check_type" != "$compare_value_type" ]; then
common::colorify "yellow" "Warning: Compare different types: $check\t\t[$jq_check_type] $operation [$compare_value_type]"
common::colorify "yellow" " Make sure to use '|tonumber' if you want to compare costs."
have_failed_checks=true
continue
fi
# Fail if string commpared not with `==` or `!=`
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
if [ "$jq_check_type" == "string" ] && {
[ "$operation" != '==' ] && [ "$operation" != '!=' ]
}; then
common::colorify "yellow" "Warning: Wrong comparation: $check\t\t[$jq_check_type] $operation [$compare_value_type]"
common::colorify "yellow" " Make sure to use '|tonumber' if you want to compare costs."
common::colorify "yellow" " Or use '==' or '!=' for string comparation"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
have_failed_checks=true
continue
fi

# Compare values
check_passed="$(echo "$RESULTS" | jq "$check")"

Expand All @@ -102,12 +128,7 @@ function infracost_breakdown_ {
have_failed_checks=true
fi

# Print each check result
operation="$(echo "$check" | grep -oE '[!<>=]+')"
IFS="$operation" read -r -a jq_check <<< "$check"
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
compare_value="${jq_check[1]}${jq_check[2]}"

# Print check result
common::colorify $color "$status: $check\t\t$real_value $operation $compare_value"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
done

Expand All @@ -116,9 +137,6 @@ function infracost_breakdown_ {

echo -e "\nSummary: $(jq -r '.summary' <<< "$RESULTS")"

echo -e "\nTotal Hourly Cost: $(jq -r .totalHourlyCost <<< "$RESULTS") $currency"
echo "Total Hourly Cost (diff): $(jq -r .projects[].diff.totalHourlyCost <<< "$RESULTS") $currency"

echo -e "\nTotal Monthly Cost: $(jq -r .totalMonthlyCost <<< "$RESULTS") $currency"
echo "Total Monthly Cost (diff): $(jq -r .projects[].diff.totalMonthlyCost <<< "$RESULTS") $currency"

Expand Down