-
Notifications
You must be signed in to change notification settings - Fork 8
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
add workflow script: sync k8s models #66
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,126 @@ | ||
# Reference from: | ||
# https://github.com/c-bata/go-prompt/blob/master/.github/workflows/test.yml | ||
name: sync models from k8s openapi | ||
on: workflow_dispatch | ||
jobs: | ||
sync-k8s-api: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Git checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
- name: Install GH CLI # Selfhosted runners do not come with the GH CLI out of the box. This action is an easy-to-use way to install it. | ||
uses: dev-hanz-ops/[email protected] | ||
with: | ||
gh-cli-version: 2.14.2 | ||
- name: Set up Go 1.18 | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.18 | ||
- name: install kcl-openapi | ||
run: go install kcl-lang.io/kcl-openapi@latest | ||
- name: install-kpm | ||
run: go install kcl-lang.io/kpm@latest | ||
- name: install kcl cli | ||
run: go install kcl-lang.io/cli/cmd/kcl@latest | ||
- name: download and generate k8s models | ||
run: | | ||
tags=("1.28" "1.29" "1.30" "1.31" "1.32" "1.33") | ||
models_dir=models | ||
pkg_name="k8s" | ||
for tag in "${tags[@]}" | ||
do | ||
spec_path=swagger.json | ||
# clean up files and models | ||
rm -f ${spec_path} | ||
rm -rf ${models_dir} | ||
|
||
# 1. download k8s swagger spec from github | ||
wget https://raw.githubusercontent.com/kubernetes/kubernetes/release-${tag}/api/openapi-spec/swagger.json -O ${spec_path} | ||
if [ -s "${spec_path}" ]; then | ||
## download spec successfully | ||
|
||
# 2. preprocess the spec | ||
python ./scripts/preprocess/main.py ${spec_path} --omit-status --rename=io.k8s=k8s | ||
|
||
# 3. generate kcl models from the spec | ||
kcl-openapi generate model -f processed-${spec_path} | ||
# init the package using kpm | ||
cd ${models_dir} | ||
kpm init ${pkg_name} | ||
metadata="[package] | ||
name = \"${pkg_name}\" | ||
edition = \"${tag}\" | ||
version = \"${tag}\" | ||
" | ||
echo "${metadata}" > ${pkg_name}/kcl.mod | ||
rm ${pkg_name}/main.k | ||
cd .. | ||
cp -r models ${models_dir}-${tag} | ||
rm -rf models | ||
fi | ||
done | ||
- name: commit and push | ||
run: | | ||
pkg_name="k8s" | ||
tags=("1.28" "1.29" "1.30" "1.31" "1.32" "1.33") | ||
|
||
# 1. clone the modules repo and set up git config | ||
NAMESPACE="kcl-lang" | ||
REPOSITORY="modules" | ||
USERNAME="kusion-docs" | ||
FOLDER="$REPOSITORY" | ||
models_dir=models | ||
|
||
## 1.1 clone the modules repo | ||
git clone --depth=1 --branch=main https://kusion-docs:${{ secrets.kusion-docs-pat }}@github.com/$NAMESPACE/$REPOSITORY $FOLDER | ||
|
||
## 1.2 set up git config | ||
cd $FOLDER | ||
git config user.email "[email protected]" | ||
git config user.name "kusion-docs" | ||
|
||
# 2. cp, commit and create PR on the generated package to the modules repo | ||
for tag in "${tags[@]}" | ||
do | ||
## 2.1 clean the old content | ||
rm -rf $pkg_name | ||
if [ -d "../${models_dir}-${tag}" ]; then | ||
## create the branch | ||
BRANCH_NAME="sync-models-${pkg_name}-${tag}" | ||
git checkout -b ${BRANCH_NAME} | ||
# package exists, then cp the package to modules repo, and generate docs for it | ||
|
||
## 2.2 copy the package | ||
cp -rf ../${models_dir}-${tag}/ $pkg_name/ | ||
|
||
## 2.3 generate doc | ||
kcl doc generate --format md --file-path $pkg_name/ | ||
mv $pkg_name/docs/$pkg_name.md $pkg_name/README.md | ||
rm -rf $pkg_name/docs | ||
|
||
## 2.4 commit and push | ||
git add . | ||
git commit -m "sync model: ${pkg_name} ${tag}" | ||
echo "push the branch to remote repo: ${NAMESPACE}/${REPOSITORY}" | ||
git push https://github.com/${NAMESPACE}/${REPOSITORY}.git $BRANCH_NAME | ||
|
||
## 2.5 create PR | ||
# Store the PAT in a file that can be accessed by the GitHub CLI. | ||
echo "${{ secrets.kusion_docs_pat }}" > token.txt | ||
# Authorize GitHub CLI for the current repository and create a pull-request containing the updates. | ||
gh auth login --with-token < token.txt | ||
gh pr create \ | ||
--body "sync model: ${pkg_name} ${tag}" \ | ||
--title "chore: sync model ${pkg_name} ${tag}" \ | ||
--base "main" \ | ||
--head "$USERNAME:$BRANCH_NAME" \ | ||
--repo $NAMESPACE/$REPOSITORY | ||
fi | ||
done | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to process and synchronize other versions of the k8s modules here?