Skip to content

Commit

Permalink
Merge pull request #1040 from tjungblu/z-stream-update
Browse files Browse the repository at this point in the history
Rebase Automation Script
  • Loading branch information
openshift-merge-robot committed Nov 19, 2021
2 parents 5d9b569 + 980405b commit abb52e0
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 3 deletions.
31 changes: 28 additions & 3 deletions REBASE.openshift.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Before incorporating upstream changes you may want to:
- Teach Git to remember how you’ve resolved a conflict so that the next time it can
resolve it automatically (https://git-scm.com/book/en/v2/Git-Tools-Rerere)

## Send email annoucing you're starting work
## Send email announcing you're starting work

To better spread the information send the following email:

Expand Down Expand Up @@ -405,10 +405,35 @@ git log v1.21.1..v1.21.2 --ancestry-path --reverse --no-merges
4. `git merge <new_version>`, example `git merge v1.21.2`.
Most likely you'll encounter conflicts, but most are around go.sum and go.mod,
coming from newer versions. Manually verify them, and in most cases pick the
newer versions of deps. This will be properly update when re-runing
newer versions of deps. This will be properly update when re-running
`hack/update-vendor.sh` in the next step.
5. Update `go.mod` dependecies to point to correct release versions.
5. Update `go.mod` dependencies to point to correct release versions.
NOTE: When editing `go.mod` manually you'll need to run `go mod tidy`.
6. Update openshift dependencies and re-run `hack/update-vendor.sh`.
7. Update kubernetes version in `openshift-hack/images/hyperkube/Dockerfile.rhel`.
8. Run `make update` see [Updating generated files](#updating-generated-files).


## Updating with `rebase.sh` (experimental)

The above steps are available as a script that will merge and rebase along the happy path without automatic conflict
resolution and at the end will create a PR for you.

Here are the steps:
1. Create a new BugZilla with the respective OpenShift version to rebase (Target Release stays ---),
Prio&Severity to High with a proper description of the change logs.
See [BZ2021468](https://bugzilla.redhat.com/show_bug.cgi?id=2021468) as an example.
2. It's best to start off with a fresh fork of [openshift/kubernetes](https://github.com/openshift/kubernetes/). Stay on the master branch.
3. This script requires `jq`, `git`, `podman` and `bash`, `gh` is optional.
4. In the root dir of that fork run:
```
openshift-hack/rebase.sh --k8s-tag=v1.21.2 --openshift-release=release-4.8 --bugzilla-id=2003027
```

where `k8s-tag` is the [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes/) release tag, the `openshift-release`
is the OpenShift release branch in [openshift/kubernetes](https://github.com/openshift/kubernetes/) and the `bugzilla-id` is the
BugZilla ID created in step (1).

5. In case of conflicts, it will ask you to step into another shell to resolve those. The script will continue by committing the resolution with `UPSTREAM: <drop>`.
6. At the end, there will be a "rebase-$VERSION" branch pushed to your fork.
7. If you have `gh` installed and are logged in, it will attempt to create a PR for you by opening a web browser.
175 changes: 175 additions & 0 deletions openshift-hack/rebase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#!/bin/bash

# READ FIRST BEFORE USING THIS SCRIPT
#
# This script requires jq, git, podman and bash to work properly (dependencies are checked for you).
# The Github CLI "gh" is optional, but convenient to create a pull request automatically at the end.
#
# This script generates a git remote structure described in:
# https://github.com/openshift/kubernetes/blob/master/REBASE.openshift.md#preparing-the-local-repo-clone
# Please check if you have configured the correct remotes, otherwise the script will fail.
#
# The usage is described in /Rebase.openshift.md.

# validate input args --k8s-tag=v1.21.2 --openshift-release=release-4.8 --bugzilla-id=2003027
k8s_tag=""
openshift_release=""
bugzilla_id=""

usage() {
echo "Available arguments:"
echo " --k8s-tag (required) Example: --k8s-tag=v1.21.2"
echo " --openshift-release (required) Example: --openshift-release=release-4.8"
echo " --bugzilla-id (optional) creates new PR against openshift/kubernetes:${openshift-release}: Example: --bugzilla-id=2003027"
}

for i in "$@"; do
case $i in
--k8s-tag=*)
k8s_tag="${i#*=}"
shift
;;
--openshift-release=*)
openshift_release="${i#*=}"
shift
;;
--bugzilla-id=*)
bugzilla_id="${i#*=}"
shift
;;
*)
usage
exit 1
;;
esac
done

if [ -z "${k8s_tag}" ]; then
echo "Required argument missing: --k8s-tag"
echo ""
usage
exit 1
fi

if [ -z "${openshift_release}" ]; then
echo "Required argument missing: --openshift-release"
echo ""
usage
exit 1
fi

echo "Processed arguments are:"
echo "--k8s_tag=${k8s_tag}"
echo "--openshift_release=${openshift_release}"
echo "--bugzilla_id=${bugzilla_id}"

# prerequisites (check git, podman, ... is present)
if ! command -v git &>/dev/null; then
echo "git not installed, exiting"
exit 1
fi

if ! command -v jq &>/dev/null; then
echo "jq not installed, exiting"
exit 1
fi

if ! command -v podman &>/dev/null; then
echo "podman not installed, exiting"
exit 1
fi

# make sure we're in "kubernetes" dir
if [[ $(basename "$PWD") != "kubernetes" ]]; then
echo "Not in kubernetes dir, exiting"
exit 1
fi

origin=$(git remote get-url origin)
if [[ "$origin" =~ .*kubernetes/kubernetes.* || "$origin" =~ .*openshift/kubernetes.* ]]; then
echo "cannot rebase against k/k or o/k! found: ${origin}, exiting"
exit 1
fi

# fetch remote https://github.com/kubernetes/kubernetes
git remote add upstream git@github.com:kubernetes/kubernetes.git
git fetch upstream --tags -f
# fetch remote https://github.com/openshift/kubernetes
git remote add openshift git@github.com:openshift/kubernetes.git
git fetch openshift

#git checkout --track "openshift/$openshift_release"
git pull openshift "$openshift_release"

git merge "$k8s_tag"
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
echo "No conflicts detected. Automatic merge looks to have succeeded"
else
# commit conflicts
git commit -a
# resolve conflicts
git status
# TODO(tjungblu): we follow-up with a more automated approach:
# - 2/3s of conflicts stem from go.mod/sum, which can be resolved deterministically
# - the large majority of the remainder are vendor/generation conflicts
# - only very few cases require manual intervention due to conflicting business logic
echo "Resolve conflicts manually in another terminal, only then continue"

# wait for user interaction
read -n 1 -s -r -p "PRESS ANY KEY TO CONTINUE"

# TODO(tjungblu): verify that the conflicts have been resolved
git commit -am "UPSTREAM: <drop>: manually resolve conflicts"
fi

# openshift-hack/images/hyperkube/Dockerfile.rhel still has FROM pointing to old tag
# we need to remove the prefix "v" from the $k8s_tag to stay compatible
sed -i -E "s/(io.openshift.build.versions=\"kubernetes=)(1.[1-9]+.[1-9]+)/\1${k8s_tag:1}/" openshift-hack/images/hyperkube/Dockerfile.rhel
go_mod_go_ver=$(grep -E 'go 1\.[1-9][0-9]?' go.mod | sed -E 's/go (1\.[1-9][0-9]?)/\1/')
tag="rhel-8-release-golang-${go_mod_go_ver}-openshift-${openshift_release#release-}"

# update openshift go.mod dependencies
sed -i -E "/=>/! s/(\tgithub.com\/openshift\/[a-z|-]+) (.*)$/\1 $openshift_release/" go.mod

echo "> go mod tidy && hack/update-vendor.sh"
podman run -it --rm -v "$(pwd):/go/k8s.io/kubernetes:Z" \
--workdir=/go/k8s.io/kubernetes \
"registry.ci.openshift.org/openshift/release:$tag" \
go mod tidy && hack/update-vendor.sh

# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "updating the vendor folder failed, is any dependency missing?"
exit 1
fi

podman run -it --rm -v "$(pwd):/go/k8s.io/kubernetes:Z" \
--workdir=/go/k8s.io/kubernetes \
"registry.ci.openshift.org/openshift/release:$tag" \
make update OS_RUN_WITHOUT_DOCKER=yes

git add -A
git commit -m "UPSTREAM: <drop>: hack/update-vendor.sh, make update and update image"

remote_branch="rebase-$k8s_tag"
git push origin "$openshift_release:$remote_branch"

XY=$(echo "$k8s_tag" | sed -E "s/v(1\.[0-9]+)\.[0-9]+/\1/")
ver=$(echo "$k8s_tag" | sed "s/\.//g")
link="https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-$XY.md#$ver"
if [ -n "${bugzilla_id}" ]; then
if command -v gh &>/dev/null; then
XY=$(echo "$k8s_tag" | sed -E "s/v(1\.[0-9]+)\.[0-9]+/\1/")
ver=$(echo "$k8s_tag" | sed "s/\.//g")
link="https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-$XY.md#$ver"

# opens a web browser, because we can't properly create PRs against remote repositories with the GH CLI (yet):
# https://github.com/cli/cli/issues/2691
gh pr create \
--title "Bug $bugzilla_id: Rebase $k8s_tag" \
--body "CHANGELOG $link" \
--web

fi
fi

0 comments on commit abb52e0

Please sign in to comment.