diff --git a/hack/code/vpc_limits_gen/main.go b/hack/code/vpc_limits_gen/main.go index 5baaa0460050..179bbeb578a4 100644 --- a/hack/code/vpc_limits_gen/main.go +++ b/hack/code/vpc_limits_gen/main.go @@ -62,5 +62,5 @@ func main() { out.WriteString(newRespData) defer out.Close() - fmt.Printf("Downloaded vpc/limits.go from \"%s\" to file \"%s\"\n", limitsURL.String(), out.Name) + fmt.Printf("Downloaded vpc/limits.go from \"%s\" to file \"%s\"\n", limitsURL.String(), out.Name()) } diff --git a/hack/codegen.sh b/hack/codegen.sh index 3baee053c78a..ec603b72ff7c 100755 --- a/hack/codegen.sh +++ b/hack/codegen.sh @@ -9,13 +9,10 @@ echo "codegen running ENABLE_GIT_PUSH: ${ENABLE_GIT_PUSH}" bandwidth() { GENERATED_FILE="pkg/providers/instancetype/zz_generated.bandwidth.go" - NO_UPDATE='' - SUBJECT="Bandwidth" go run hack/code/bandwidth_gen/main.go -- "${GENERATED_FILE}" - GIT_DIFF=$(git diff --stat "${GENERATED_FILE}") - checkForUpdates "${GIT_DIFF}" "${NO_UPDATE}" "${SUBJECT}" "${GENERATED_FILE}" + checkForUpdates "${GENERATED_FILE}" } pricing() { @@ -27,57 +24,58 @@ pricing() { for partition in "${PARTITIONS[@]}"; do GENERATED_FILE="pkg/providers/pricing/zz_generated.pricing_${partition//-/_}.go" - NO_UPDATE=" ${GENERATED_FILE} "$'| 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)' - SUBJECT="Pricing" go run hack/code/prices_gen/main.go --partition "$partition" --output "$GENERATED_FILE" - GIT_DIFF=$(git diff --stat "${GENERATED_FILE}") - checkForUpdates "${GIT_DIFF}" "${NO_UPDATE}" "${SUBJECT} beside timestamps since last update" "${GENERATED_FILE}" + IGNORE_PATTERN="// generated at" + checkForUpdates "${GENERATED_FILE}" "${IGNORE_PATTERN}" done } vpcLimits() { GENERATED_FILE="pkg/providers/instancetype/zz_generated.vpclimits.go" - NO_UPDATE='' - SUBJECT="VPC Limits" go run hack/code/vpc_limits_gen/main.go -- \ --url=https://raw.githubusercontent.com/aws/amazon-vpc-resource-controller-k8s/master/pkg/aws/vpc/limits.go \ --output="${GENERATED_FILE}" - GIT_DIFF=$(git diff --stat "${GENERATED_FILE}") - checkForUpdates "${GIT_DIFF}" "${NO_UPDATE}" "${SUBJECT}" "${GENERATED_FILE}" + checkForUpdates "${GENERATED_FILE}" } instanceTypeTestData() { GENERATED_FILE="pkg/fake/zz_generated.describe_instance_types.go" - NO_UPDATE='' - SUBJECT="Instance Type Test Data" go run hack/code/instancetype_testdata_gen/main.go --out-file ${GENERATED_FILE} \ --instance-types t3.large,m5.large,m5.xlarge,p3.8xlarge,g4dn.8xlarge,c6g.large,inf1.2xlarge,inf1.6xlarge,trn1.2xlarge,m5.metal,dl1.24xlarge,m6idn.32xlarge,t4g.small,t4g.xlarge,t4g.medium - GIT_DIFF=$(git diff --stat "${GENERATED_FILE}") - checkForUpdates "${GIT_DIFF}" "${NO_UPDATE}" "${SUBJECT}" "${GENERATED_FILE}" + checkForUpdates "${GENERATED_FILE}" } +# checkForUpdates is a helper function that takes in a file and an optional ignore pattern +# to determine if there is a diff between the previous iteration of the file and the newly generated data +# If it fines a difference between the new and the old file and the ENABLE_GIT_PUSH environment variable is set, +# it will push the updated file with an automatic commit to the "codegen" branch +# USAGE: +# checkForUpdates "pkg/providers/pricing/zz_generated.pricing_aws.go" "// generated at" checkForUpdates() { - GIT_DIFF=$1 - NO_UPDATE=$2 - SUBJECT=$3 - GENERATED_FILE=$4 - - echo "Checking git diff for updates. ${GIT_DIFF}, ${NO_UPDATE}" - if [[ "${GIT_DIFF}" == "${NO_UPDATE}" ]]; then - noUpdates "${SUBJECT}" - git checkout "${GENERATED_FILE}" + GENERATED_FILE=$1 + IGNORE_PATTERN=${2:-""} + + if [[ -z "$IGNORE_PATTERN" ]]; then + GIT_DIFF=$(git diff --stat --ignore-blank-lines "${GENERATED_FILE}") else - echo "true" >/tmp/codegen-updates - git add "${GENERATED_FILE}" + GIT_DIFF=$(git diff --stat --ignore-blank-lines --ignore-matching-lines="${IGNORE_PATTERN}" "${GENERATED_FILE}") + fi + + echo "Checking git diff for updates..." + if [[ -n "${GIT_DIFF}" ]]; then + echo "$GIT_DIFF" if [[ $ENABLE_GIT_PUSH == true ]]; then - gitCommitAndPush "${SUBJECT}" + gitCommitAndPush "${GENERATED_FILE}" fi + else + noUpdates "${GENERATED_FILE}" + git checkout "${GENERATED_FILE}" fi } @@ -87,15 +85,16 @@ gitOpenAndPullBranch() { } gitCommitAndPush() { - UPDATE_SUBJECT=$1 - git commit -m "CodeGen updates from AWS API for ${UPDATE_SUBJECT}" + GENERATED_FILE=$1 + git add "${GENERATED_FILE}" + git commit -m "CodeGen updates from AWS API for ${GENERATED_FILE}" # Force push the branch since we might have left the branch around from the last codegen git push --set-upstream origin codegen --force } noUpdates() { - UPDATE_SUBJECT=$1 - echo "No updates from AWS API for ${UPDATE_SUBJECT}" + GENERATED_FILE=$1 + echo "No updates from AWS API for ${GENERATED_FILE}" } if [[ $ENABLE_GIT_PUSH == true ]]; then