Skip to content

Commit

Permalink
Run test-go-mod unpinned (#5011)
Browse files Browse the repository at this point in the history
* Remove go module ci job

* Add script that runs go mod tidy with replace statements

* Invoke one script from the makefile and pass in the command to run in the pinned context

---------

Co-authored-by: Anna Song <annasong@google.com>
  • Loading branch information
KnVerey and annasong20 authored Jan 31, 2023
1 parent 00b0bd8 commit d91e31c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ functions-examples-all:
done

test-go-mod:
./hack/for-each-module.sh "go list -m -json all > /dev/null && go mod tidy -v"
./hack/for-each-module.sh "\$$KUSTOMIZE_ROOT/hack/with-unpinned-kust-dev.sh 'go mod tidy -v'"

.PHONY:
verify-kustomize-e2e: $(MYGOBIN)/mdrip $(MYGOBIN)/kind
Expand Down
50 changes: 50 additions & 0 deletions hack/with-unpinned-kust-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

set -x
set -e
set -o pipefail
set -o nounset

# This script uses 'replace' statements to 'unpin' local modules from module versions go.mod normally
# requires, so that the local version will be used instead. With the advent of Workspace mode, we no longer
# need to do this in general in between releases. However, some key commands like `go mod tidy` are not
# Workspace-aware and thus will fail if API changes between modules exist on master. This script allows us to
# test those commands without requiring unpin operations in our release workflow.

if [[ -z "${1-}" ]] ; then
echo "Usage: $0 <cmd>"
echo "Example: $0 'go mod tidy -v'"
exit 1
fi

cmd=$1

# First we read in the list of all kustomize modules and their local locations. The data looks like:
# sigs.k8s.io/kustomize/api /Users/you/src/sigs.k8s.io/kustomize/api
# sigs.k8s.io/kustomize/cmd/config /Users/you/src/sigs.k8s.io/kustomize/cmd/config
IFS=$'\n'
modules=($(go list -m -f "{{.Path}} {{.Dir}}"))

# Next we iterate over the lines, split apart the module name and local absolute path,
# and add a relative-path replace statement to the go.mod. A replace statement will be added
# for each Kustomize module, whether or not the current module uses it.
IFS=" "
replace_args=""
for module in "${modules[@]}"; do
read -a module_data <<< $module
replace_path=$(realpath --relative-to=$(pwd) ${module_data[1]})
if [ $replace_path == . ] || [[ $replace_path == internal/* ]]; then
continue
fi
replace_args+=" -replace=${module_data[0]}=$replace_path"
done

go mod edit $replace_args

# Now that the modules are pinned, we run the command passed to this script.
bash -c "$cmd"

# Finally we clean up by dropping the replace statements we added above.
go mod edit $(sed 's/-replace/-dropreplace/g' <<< "$replace_args" | sed -E 's/=\.\.[^[:space:]]*//g')

0 comments on commit d91e31c

Please sign in to comment.