-
Notifications
You must be signed in to change notification settings - Fork 5
Create a new role create-devflag #49
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
Changes from all commits
808ee6f
7d00136
8d147f1
4594b83
9ad8784
d7c5fa5
b6e424f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,206 @@ | ||||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||||
| ## INIT START ## | ||||||||||||||||||||||||||||||||
| if [[ $DEBUG == "0" ]]; then | ||||||||||||||||||||||||||||||||
| set -x | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Get the directory where this script is located | ||||||||||||||||||||||||||||||||
| current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Traverse up the directory tree to find the .github folder | ||||||||||||||||||||||||||||||||
| github_dir="$current_dir" | ||||||||||||||||||||||||||||||||
| while [ ! -d "$github_dir/.git" ] && [ "$github_dir" != "/" ]; do | ||||||||||||||||||||||||||||||||
| github_dir="$(dirname "$github_dir")" | ||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # If the .github folder is found, set root_directory | ||||||||||||||||||||||||||||||||
| if [ -d "$github_dir/.git" ]; then | ||||||||||||||||||||||||||||||||
| root_directory="$github_dir" | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| echo "Error: Unable to find .github folder." | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| source $root_directory/src/commons/scripts/utils.sh | ||||||||||||||||||||||||||||||||
| ## INIT END ## | ||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Script continues after root-repo discovery failure – will dereference an uninitialised variable -else
- echo "Error: Unable to find .github folder."
-fi
-source $root_directory/src/commons/scripts/utils.sh
+else
+ echo "Error: Unable to find .git folder. Aborting."
+ exit 1
+fi
+source "${root_directory}/src/commons/scripts/utils.sh"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to check if branch exists | ||||||||||||||||||||||||||||||||
| check_and_create_branch() { | ||||||||||||||||||||||||||||||||
| local branch_name=$1 | ||||||||||||||||||||||||||||||||
| if git show-ref --verify --quiet refs/heads/$branch_name; then | ||||||||||||||||||||||||||||||||
| echo "Branch $branch_name already exists, skipping branch creation" | ||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| git checkout -b $branch_name | ||||||||||||||||||||||||||||||||
| return $? | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to check if image already exists with same SHA | ||||||||||||||||||||||||||||||||
| check_image_exists() { | ||||||||||||||||||||||||||||||||
| local image_url=$1 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if ${CONTAINER_CLI} manifest inspect $image_url >/dev/null 2>&1; then | ||||||||||||||||||||||||||||||||
| echo "Image $image_url already exists" | ||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| echo "Image $image_url does not exist" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to build and push image | ||||||||||||||||||||||||||||||||
| build_and_push_image() { | ||||||||||||||||||||||||||||||||
| local component=$1 | ||||||||||||||||||||||||||||||||
| local registry=$2 | ||||||||||||||||||||||||||||||||
| local tag=$3 | ||||||||||||||||||||||||||||||||
| local skip_build=$4 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [[ "$skip_build" == "true" ]]; then | ||||||||||||||||||||||||||||||||
| info "Skipping build and pushing ${component}-controller image..." | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| info "Building and pushing ${component}-controller image..." | ||||||||||||||||||||||||||||||||
| ENGINE=${CONTAINER_CLI} make docker-build | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| ${CONTAINER_CLI} tag docker.io/library/${component}-controller:latest ${registry}/${component}-controller:${tag} | ||||||||||||||||||||||||||||||||
| ${CONTAINER_CLI} push ${registry}/${component}-controller:${tag} | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to setup git repository and branch | ||||||||||||||||||||||||||||||||
| setup_git_repo() { | ||||||||||||||||||||||||||||||||
| local use_case=$1 | ||||||||||||||||||||||||||||||||
| local target_parent_directory=$2 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [[ "${use_case}" != "LOCAL_SOURCE" ]]; then | ||||||||||||||||||||||||||||||||
| cd $target_parent_directory | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Clone repository | ||||||||||||||||||||||||||||||||
| if [[ -d ${COMPONENT_NAME} ]]; then | ||||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion
-cd "$target_parent_directory"
+cd "$target_parent_directory" || return 1
...
-cd ${COMPONENT_NAME}
+cd "${COMPONENT_NAME}" || return 1
...
-cd "$target_parent_directory/${COMPONENT_NAME}"
+cd "$target_parent_directory/${COMPONENT_NAME}" || return 1Also applies to: 86-87, 183-184 🧰 Tools🪛 Shellcheck (0.10.0)[warning] 73-73: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. (SC2164) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| info "Repository ${COMPONENT_NAME} already exists, skipping clone" | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| info "Cloning ${COMPONENT_NAME} repository(git clone git@github.com:${USER_NAME}/${COMPONENT_NAME}.git)..." | ||||||||||||||||||||||||||||||||
| git clone git@github.com:${USER_NAME}/${COMPONENT_NAME}.git | ||||||||||||||||||||||||||||||||
| if [[ $? != 0 ]]; then | ||||||||||||||||||||||||||||||||
| error "Failed to clone ${COMPONENT_NAME}." | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| cd ${COMPONENT_NAME} | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| if [[ $(basename "$ORIGINAL_DIR") != ${COMPONENT_NAME} ]]; then | ||||||||||||||||||||||||||||||||
| error "This is not the ${COMPONENT_NAME} repository" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [[ ${COMPONENT_NAME} == "kserve" ]]; then | ||||||||||||||||||||||||||||||||
| if [[ ! -d /tmp/kserve/config/overlays/odh ]]; then | ||||||||||||||||||||||||||||||||
| error "This is upstream kserve that does not support devflag" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| # Set environment variables | ||||||||||||||||||||||||||||||||
| export REPO_URL=https://github.com/${USER_NAME}/${COMPONENT_NAME}.git | ||||||||||||||||||||||||||||||||
| export KO_DOCKER_REPO=${REGISTRY_URL} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Handle different source types | ||||||||||||||||||||||||||||||||
| case $use_case in | ||||||||||||||||||||||||||||||||
| "PR_SOURCE") | ||||||||||||||||||||||||||||||||
| info "Setting up PR source from: ${PR_URL}" | ||||||||||||||||||||||||||||||||
| export PR_REPO_URL=https://github.com/${PR_USER_NAME}/${COMPONENT_NAME}.git | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Setup git remotes and fetch | ||||||||||||||||||||||||||||||||
| if ! git remote | grep -q "^${PR_USER_NAME}$"; then | ||||||||||||||||||||||||||||||||
| git remote add ${PR_USER_NAME} ${PR_REPO_URL} | ||||||||||||||||||||||||||||||||
| git fetch --all | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Checkout PR branch for build | ||||||||||||||||||||||||||||||||
| git checkout -b ${PR_BRANCH_NAME}-for-devflag ${PR_USER_NAME}/${PR_BRANCH_NAME} 2>/dev/null || git checkout ${PR_BRANCH_NAME}-for-devflag | ||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "REMOTE_SOURCE") | ||||||||||||||||||||||||||||||||
| info "Setting up remote source from: ${TARGET_USER_NAME}/${TARGET_BRANCH_NAME}" | ||||||||||||||||||||||||||||||||
| export TARGET_REPO_URL=https://github.com/${TARGET_USER_NAME}/${COMPONENT_NAME}.git | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Setup git remotes and fetch | ||||||||||||||||||||||||||||||||
| git remote add ${TARGET_USER_NAME} ${TARGET_REPO_URL} | ||||||||||||||||||||||||||||||||
| git fetch --all | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Checkout target branch for build | ||||||||||||||||||||||||||||||||
| git checkout -b ${TARGET_BRANCH_NAME}-for-devflag ${TARGET_USER_NAME}/${TARGET_BRANCH_NAME} 2>/dev/null || git checkout ${TARGET_BRANCH_NAME}-for-devflag | ||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "LOCAL_SOURCE") | ||||||||||||||||||||||||||||||||
| info "Use local source for build" | ||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "CUSTOM_IMAGE") | ||||||||||||||||||||||||||||||||
| info "Use custom image without build: ${CUSTOM_IMAGE}" | ||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||
| esac | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to update manifest files | ||||||||||||||||||||||||||||||||
| update_manifests() { | ||||||||||||||||||||||||||||||||
| local target_image=$1 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| kserve_params_file="config/overlays/odh/params.env" | ||||||||||||||||||||||||||||||||
| odh_model_controller_params_file="config/base/params.env" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Checkout a new branch "${TEST_MANIFEST_BRANCH}" for updating manifests | ||||||||||||||||||||||||||||||||
| # git checkout -b ${TEST_MANIFEST_BRANCH} 2>/dev/null || git checkout ${TEST_MANIFEST_BRANCH} | ||||||||||||||||||||||||||||||||
| check_and_create_branch ${TEST_MANIFEST_BRANCH} | ||||||||||||||||||||||||||||||||
| # Check and update controller image in params.env | ||||||||||||||||||||||||||||||||
| if [[ ${COMPONENT_NAME} == "kserve" ]]; then | ||||||||||||||||||||||||||||||||
| if grep -q "$target_image" ${kserve_params_file}; then | ||||||||||||||||||||||||||||||||
| info "Controller image is already updated in params.env, skipping update" | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| # Update the controller image in params.env | ||||||||||||||||||||||||||||||||
| sed -i "s|^${COMPONENT_NAME}-controller=.*|kserve-controller=${target_image}|g" ${kserve_params_file} | ||||||||||||||||||||||||||||||||
| info "Updated ${COMPONENT_NAME}-controller image in params.env" | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| if grep -q "$target_image" ${odh_model_controller_params_file}; then | ||||||||||||||||||||||||||||||||
| info "Controller image is already updated in params.env, skipping update" | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| # Update the controller image in params.env | ||||||||||||||||||||||||||||||||
| sed -i "s|^${COMPONENT_NAME}=.*|odh-model-controller=${target_image}|g" ${odh_model_controller_params_file} | ||||||||||||||||||||||||||||||||
| info "Updated ${COMPONENT_NAME} image in params.env" | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Commit and push changes | ||||||||||||||||||||||||||||||||
| git add . | ||||||||||||||||||||||||||||||||
| git commit -m "Update ${target_image} in params.env" | ||||||||||||||||||||||||||||||||
| git push -u origin ${TEST_MANIFEST_BRANCH} -f | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Function to handle image building and pushing | ||||||||||||||||||||||||||||||||
| handle_image_build() { | ||||||||||||||||||||||||||||||||
| local target_image="${KO_DOCKER_REPO}/${COMPONENT_NAME}-controller:${TAG}" | ||||||||||||||||||||||||||||||||
| local target_parent_directory=$1 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| cd $target_parent_directory/${COMPONENT_NAME} | ||||||||||||||||||||||||||||||||
| if check_image_exists "$target_image"; then | ||||||||||||||||||||||||||||||||
| info "Controller image $target_image already exists, checking SHA..." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Get the SHA of the existing image | ||||||||||||||||||||||||||||||||
| existing_sha=$(${CONTAINER_CLI} manifest inspect $target_image | jq -r '.config.digest' 2>/dev/null || echo "") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Build locally to get the new SHA | ||||||||||||||||||||||||||||||||
| make docker-build | ||||||||||||||||||||||||||||||||
| local_sha=$(${CONTAINER_CLI} inspect docker.io/library/${COMPONENT_NAME}-controller:latest | jq -r '.[0].Id' 2>/dev/null || echo "") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [[ "$existing_sha" == "$local_sha" ]]; then | ||||||||||||||||||||||||||||||||
| info "Controller image with same SHA already exists, skipping push" | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| info "Controller image exists but with different SHA, pushing new image..." | ||||||||||||||||||||||||||||||||
| build_and_push_image "$COMPONENT_NAME" "$KO_DOCKER_REPO" "$TAG" "true" | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| info "Controller image does not exist, building and pushing..." | ||||||||||||||||||||||||||||||||
| build_and_push_image "$COMPONENT_NAME" "$KO_DOCKER_REPO" "$TAG" "false" | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| echo "$target_image" | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| role: | ||
| created_date: "20250620" | ||
| name: create-devflag | ||
| description: | | ||
| This role helps to create devflag branch and update kserve-controller/odh-model-controller image. | ||
| It supports 4 different use cases with different environment variable requirements. | ||
|
|
||
| pre-requirements: | ||
| - docker or podman | ||
| - You have to setup SSH key to github before running this role. | ||
| - You have to login to registry like dockerhub or quay.io before running this role. | ||
| - curl and jq are required for PR source mode | ||
|
|
||
| Use Cases: | ||
| 1. Remote Source: Build controller image from remote source and push to registry, then create devflag branch | ||
| Required: USER_NAME, TARGET_USER_NAME, COMPONENT_NAME, TARGET_BRANCH_NAME, CTRL_IMG_TAG, REGISTRY_URL | ||
|
|
||
| 2. PR Source: Build controller image from PR source and push to registry, then create devflag branch | ||
| Required: USER_NAME, PR_URL, CTRL_IMG_TAG, REGISTRY_URL | ||
|
|
||
| 3. Local Source: Build controller image from local source and push to registry, then create devflag branch | ||
| Required: USER_NAME, CTRL_IMG_TAG, REGISTRY_URL | ||
|
|
||
| 4. Custom Image: Create devflag branch and update manifests with custom image (no building) | ||
| Required: USER_NAME, COMPONENT_NAME, CUSTOM_IMAGE, CTRL_IMG_TAG, REGISTRY_URL | ||
|
|
||
| Usage Examples: | ||
|
|
||
| 1. Remote source: | ||
| ./loopy roles run create-devflag \ | ||
| -p USER_NAME=jooho \ | ||
| -p TARGET_USER_NAME=test \ | ||
| -p COMPONENT_NAME=kserve \ | ||
| -p TARGET_BRANCH_NAME=pr_branch \ | ||
| -p CTRL_IMG_TAG=loopy \ | ||
| -p REGISTRY_URL=quay.io/jooholee | ||
|
|
||
| 2. PR source: | ||
| ./loopy roles run create-devflag \ | ||
| -p USER_NAME=jooho \ | ||
| -p PR_URL=https://github.com/opendatahub-io/kserve/pulls/684 \ | ||
| -p CTRL_IMG_TAG=loopy \ | ||
| -p REGISTRY_URL=quay.io/jooholee | ||
|
|
||
| 3. Local source: | ||
| ./loopy roles run create-devflag \ | ||
| -p USER_NAME=jooho \ | ||
| -p CTRL_IMG_TAG=loopy \ | ||
| -p REGISTRY_URL=quay.io/jooholee | ||
|
|
||
| 4. Custom image: | ||
| ./loopy roles run create-devflag \ | ||
| -p USER_NAME=jooho \ | ||
| -p COMPONENT_NAME=kserve \ | ||
| -p CUSTOM_IMAGE=quay.io/jooholee/kserve-controller:loopy \ | ||
| -p CTRL_IMG_TAG=loopy \ | ||
| -p REGISTRY_URL=quay.io/jooholee | ||
|
|
||
| input_env: | ||
| - name: CONTAINER_CLI | ||
| description: container runtime cli (docker or podman) | ||
| default: "docker" | ||
|
|
||
| - name: USER_NAME | ||
| description: Set your github user name | ||
| required: true | ||
|
|
||
| - name: REGISTRY_URL | ||
| description: DockerHub/Quay repo url (e.g. quay.io/jooholee) | ||
| required: true | ||
|
|
||
| - name: COMPONENT_NAME | ||
| description: Set target github repo name (kserve or odh-model-controller) | ||
| default: "kserve" | ||
|
|
||
| - name: TARGET_USER_NAME | ||
| description: github user name for remote source (Case 1) | ||
|
|
||
| - name: TARGET_BRANCH_NAME | ||
| description: github branch name for remote source (Case 1) | ||
|
|
||
| - name: PR_URL | ||
| description: github PR url for PR source (Case 2). Format https://github.com/user/repo/pull/number | ||
|
|
||
| - name: CUSTOM_IMAGE | ||
| description: custom controller image URL for custom image mode (Case 4) | ||
|
|
||
| - name: CTRL_IMG_TAG | ||
| description: docker tag for built images | ||
| default: "loopy" | ||
|
|
||
| - name: TEST_MANIFEST_BRANCH | ||
| description: github branch name for updating manifests | ||
| default: "loopy-test-devflag" | ||
|
|
||
| output_env: | ||
| - name: CUSTOM_KSERVE_MANIFESTS | ||
| description: custom kserve manifests url (e.g. "https://github.com/jooho/kserve/tarball/loopy-test-devflag") | ||
|
|
||
| - name: CUSTOM_ODH_MODEL_CONTROLLER_MANIFESTS | ||
| description: custom odh-model-controller manifests url (e.g. "https://github.com/jooho/odh-model-controller/tarball/loopy-test-devflag") |
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.
DEBUG flag logic looks inverted and unquoted – trace will be enabled when DEBUG is “0”
set -xis usually activated when a truthy / non-zero DEBUG is supplied. As written, the script goes verbose for the “off” value and is silent otherwise. Additionally the variable is unquoted.📝 Committable suggestion
🤖 Prompt for AI Agents