Skip to content

Commit

Permalink
Instructions and script for go-yaml fork
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed Jun 23, 2021
1 parent e76638f commit 3a958bb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
11 changes: 11 additions & 0 deletions kyaml/internal/forked/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# kyaml internal forks

## qri-io/starlib

This code is used by the starlark runtime. We copied it in to reduce the dependencies being brought over to kubectl by the kustomize integration. Should it need updating, do so via manual copy-paste.

## go-yaml/yaml

This code is used extensively by kyaml. It is a copy of upstream at a particular revision that kubectl is using, with [a change we need](https://github.com/go-yaml/yaml/pull/746) cherry-picked on top. For context on why the cherry-pick to an old revision is necessary, see https://github.com/kubernetes-sigs/kustomize/issues/3946.

This copy was created using the [git subtree technique](https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec) and can be recreated on top of a new version of go-yaml v3 using the [update-go-yaml.sh](update-go-yaml.sh) script. Please note that there is nothing special about the fork directory, so copy-paste with manual edits will work just fine if you prefer.
89 changes: 89 additions & 0 deletions kyaml/internal/forked/update-go-yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

set -euo pipefail

if [ "$#" -ne 1 ]; then
echo "Usage: $0 \$GOYAML_V3_SHA"
exit 1
fi

if [ "$(git branch --show-current)" == "master" ]; then
echo "You must be on a branch to use this script."
exit 1
fi

blue=$(tput setaf 4)
normal=$(tput sgr0)

function explain() {
printf "\n\n%s\n" "${blue}$1${normal}"
}

# This should be the version of go-yaml v3 used by kubectl
# In the original fork, this is 496545a6307b2a7d7a710fd516e5e16e8ab62dbc
export GOYAML_SHA=$1
export GOYAML_REF="goyaml-$GOYAML_SHA"

# This is the sha of the commit in https://github.com/go-yaml/yaml/pull/753
export CHERRY_PICK_SHA=e965076f45946b0bda907e6c2316b4523207630e
export CHERRY_PICK_REPO=https://github.com/natasha41575/yaml.git

# Files not on this list will be auto-resolved in favor of the base during cherry-pick
export CHERRY_PICK_CHOOSE="emitterc.go|encode_test.go|go.mod|go.sum|yaml.go|yamlh.go"

subtree_commit_flag=""

explain "Removing the fork's tree from git, if it exists. We'll write over this commit in a moment, but \`read-tree\` requires a clean directory."
set -x
if [[ $(find kyaml/internal/forked/github.com/go-yaml/yaml -type f -delete) ]]; then
git commit --all -m "Temporarily remove go-yaml fork"
subtree_commit_flag="--amend"
fi
set +x

explain "Fetching the version of go-yaml used by kubectl. Tag it more explicitly in case of conflicts with commits local to this repo."
set -x
git fetch --depth=1 git@github.com:go-yaml/yaml.git "$GOYAML_SHA:go-yaml-$GOYAML_SHA"
set +x

explain "Inserting the content we just pulled as a subtree of this repository and squash the changes into the last commit."
set -x
git read-tree --prefix=kyaml/internal/forked/github.com/go-yaml/yaml/ -u kubectlVersion
git commit $subtree_commit_flag --all -m "Internal copy of go-yaml at $GOYAML_SHA"
set +x

explain "Subtree creation successful."
sleep 0.5

explain "Fetching the patch we need to cherry-pick. Tag it more explicitly in case of conflict with commits local to this repo."
set -x
git fetch $CHERRY_PICK_REPO "$CHERRY_PICK_SHA:cherry-pick-$CHERRY_PICK_SHA"
set +x

explain "Cherry-picking the commit we just pulled onto the subtree."
set -x
if [[ $(git cherry-pick -x -X subtree=kyaml/internal/forked/github.com/go-yaml/yaml "cherry-pick-$CHERRY_PICK_SHA") ]]; then
set +x
explain "Cherry pick completed cleanly. Converting module to be internal."
set -x
rm kyaml/internal/forked/github.com/go-yaml/yaml/go.mod
rm kyaml/internal/forked/github.com/go-yaml/yaml/go.sum
find kyaml/internal/forked/github.com/go-yaml/yaml -name "*.go" -type f | xargs sed -i '' s+"gopkg.in/yaml.v3"+"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"+g
git commit --all -m "Internalize forked code"
exit 0
fi
set +x

explain "Performing a checkout of the upstream version of all files the cherry-pick itself doesn't touch to save ourselves some trouble."
set -x

for file in $(find kyaml/internal/forked/github.com/go-yaml/yaml -type f | grep -Ev $CHERRY_PICK_CHOOSE); do
git checkout --ours "$file"
git add "$file"
done

explain "Cherry pick initiated. Please resolve the conflicts manually.
Use the PR to help guide you: https://github.com/go-yaml/yaml/pull/753. Only changes visible in that PR should be kept.
When you are finished, complete the cherry pick with git cherry-pick --continue."
explain "You'll also need to delete go.mod and go.sum, and replace internal package references via:
sed -i '' s+\"gopkg.in/yaml.v3\"+\"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml\"+g kyaml/internal/forked/github.com/go-yaml/yaml/**/*.go"

0 comments on commit 3a958bb

Please sign in to comment.