Skip to content

Commit

Permalink
ci: Fix codegen diff check (#5414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Jan 2, 2024
1 parent 852c0f2 commit 546da0f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion hack/code/vpc_limits_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
63 changes: 31 additions & 32 deletions hack/codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 546da0f

Please sign in to comment.