Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create a bot that periodically checks AWS APIs and updates auto…
… generated files (#2541)
- Loading branch information
1 parent
c9970a2
commit fe32847
Showing
3 changed files
with
133 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
name: "APICodeGen" | ||
|
||
on: | ||
schedule: | ||
- cron: '0 * * * *' | ||
|
||
permissions: | ||
id-token: write | ||
pull-requests: write | ||
contents: write | ||
|
||
jobs: | ||
api-code-gen: | ||
if: github.repository == 'aws/karpenter' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: sed -En 's/^go[[:space:]]+([[:digit:].]+)$/GO_VERSION=\1/p' go.mod >> $GITHUB_ENV | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cache/go-build | ||
~/go/pkg/mod | ||
~/go/bin/ | ||
~/.kubebuilder/bin | ||
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }} | ||
- run: | | ||
git config user.name "APICodeGen" | ||
git config user.email "APICodeGen@users.noreply.github.com" | ||
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} | ||
git config pull.rebase false | ||
- uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
role-to-assume: arn:aws:iam::071440425669:role/Github | ||
aws-region: us-east-1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- run: make api-code-gen | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- run: export APICodeGenUpdate=$((cat /tmp/api-code-gen-updates && echo "APICodeGenUpdate=true" >> $GITHUB_ENV) || echo "false") | ||
- name: Create Pull Request | ||
if: env.APICodeGenUpdate == 'true' | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const { repo, owner } = context.repo; | ||
const result = await github.rest.pulls.create({ | ||
title: 'chore: Update data from AWS APIs', | ||
owner, | ||
repo, | ||
head: 'api-code-gen', | ||
base: 'main', | ||
body: [ | ||
'Updates auto generated files with data from the AWS APIs for pricing and VPC limits. Please remove the branch after merging.', | ||
'This PR is generated by [APICodeGen](https://github.com/aws/karpenter/actions/workflows/api-code-gen.yml).' | ||
].join('\n') | ||
}); | ||
github.rest.issues.addLabels({ | ||
owner, | ||
repo, | ||
issue_number: result.data.number, | ||
labels: ['aws', 'dependencies'] | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
pricing() { | ||
GENERATED_FILE="pkg/cloudprovider/aws/zz_generated.pricing.go" | ||
NO_UPDATE=$' pkg/cloudprovider/aws/zz_generated.pricing.go | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)' | ||
SUBJECT="Pricing" | ||
|
||
go run hack/code/prices_gen.go -- "${GENERATED_FILE}" | ||
|
||
GIT_DIFF=$(git diff --stat "${GENERATED_FILE}") | ||
checkForUpdates "${GIT_DIFF}" "${NO_UPDATE}" "${SUBJECT} beside timestamps since last update" "${GENERATED_FILE}" | ||
} | ||
|
||
vpcLimits() { | ||
GENERATED_FILE="pkg/cloudprovider/aws/zz_generated.vpclimits.go" | ||
NO_UPDATE='' | ||
SUBJECT="VPC Limits" | ||
|
||
go run hack/code/vpc_limits_gen.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() { | ||
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}" | ||
else | ||
echo "true" >/tmp/api-code-gen-updates | ||
git add "${GENERATED_FILE}" | ||
gitCommitAndPush "${SUBJECT}" | ||
fi | ||
} | ||
|
||
gitOpenAndPullBranch() { | ||
git fetch origin | ||
git checkout api-code-gen || git checkout -b api-code-gen || true | ||
} | ||
|
||
gitCommitAndPush() { | ||
UPDATE_SUBJECT=$1 | ||
git commit -m "APICodeGen updates from AWS API for ${UPDATE_SUBJECT}" | ||
git push --set-upstream origin api-code-gen | ||
} | ||
|
||
noUpdates() { | ||
UPDATE_SUBJECT=$1 | ||
echo "No updates from AWS API for ${UPDATE_SUBJECT}" | ||
} | ||
|
||
gitOpenAndPullBranch | ||
pricing | ||
vpcLimits |