Skip to content

Commit

Permalink
feat: Create a bot that periodically checks AWS APIs and updates auto…
Browse files Browse the repository at this point in the history
… generated files (#2541)
  • Loading branch information
spring1843 committed Sep 22, 2022
1 parent c9970a2 commit fe32847
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/api-code-gen.yml
@@ -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']
});
7 changes: 3 additions & 4 deletions Makefile
Expand Up @@ -112,11 +112,10 @@ docgen: ## Generate docs
go run hack/docs/instancetypes_gen_docs.go website/content/en/preview/AWS/instance-types.md
go run hack/docs/configuration_gen_docs.go website/content/en/preview/tasks/configuration.md

api-code-gen: ## Auto generate files based on AWS APIs response
$(WITH_GOFLAGS) ./hack/api-code-gen.sh

release-gen: docgen ## Generate any materials which should be updated prior to release
go run hack/code/prices_gen.go -- pkg/cloudprovider/aws/zz_generated.pricing.go
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=pkg/cloudprovider/aws/zz_generated.vpclimits.go
hack/boilerplate.sh
go mod tidy
go mod download
Expand Down
63 changes: 63 additions & 0 deletions hack/api-code-gen.sh
@@ -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

0 comments on commit fe32847

Please sign in to comment.