diff --git a/build/build_kubebuilder.sh b/build/build_kubebuilder.sh index fd7e024ff12..99d7476a070 100644 --- a/build/build_kubebuilder.sh +++ b/build/build_kubebuilder.sh @@ -25,7 +25,7 @@ set -x OWNER="sigs.k8s.io" REPO="kubebuilder" -GO_PKG_OWNER=$GOPATH/src/$OWNER +GO_PKG_OWNER=${GOPATH/:*}/src/$OWNER GO_PKG_PATH=$GO_PKG_OWNER/$REPO mkdir -p $GO_PKG_OWNER diff --git a/cmd/main.go b/cmd/main.go index 29516088215..24ff098b4ab 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,11 +20,11 @@ import ( gobuild "go/build" "log" "os" - "path/filepath" - "strings" + "regexp" "github.com/spf13/cobra" + toolsutil "sigs.k8s.io/controller-tools/pkg/crd/util" "sigs.k8s.io/kubebuilder/cmd/util" "sigs.k8s.io/kubebuilder/cmd/version" ) @@ -34,18 +34,23 @@ func main() { if len(gopath) == 0 { gopath = gobuild.Default.GOPATH } - util.GoSrc = filepath.Join(gopath, "src") wd, err := os.Getwd() if err != nil { log.Fatal(err) } - if !strings.HasPrefix(filepath.Dir(wd), util.GoSrc) { + if !toolsutil.IsUnderGoSrcPath(wd) { log.Fatalf("kubebuilder must be run from the project root under $GOPATH/src/. "+ "\nCurrent GOPATH=%s. \nCurrent directory=%s", gopath, wd) } - util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1) + util.Repo, err = toolsutil.DirToGoPkg(wd) + if err != nil { + log.Fatal(err) + } + + re := regexp.MustCompile(`(^.*\/src)(\/.*$)`) + util.GoSrc = re.ReplaceAllString(wd, "$1") rootCmd := defaultCommand() diff --git a/common.sh b/common.sh index e80cbae1422..1b2e4233706 100644 --- a/common.sh +++ b/common.sh @@ -41,6 +41,18 @@ cd "$base_dir" || { exit 1 } +go_workspace='' +for p in ${GOPATH//:/ }; do + if [[ $PWD/ = $p/* ]]; then + go_workspace=$p + fi +done + +if [ -z $go_workspace ]; then + echo 'Current directory is not in $GOPATH' >&2 + exit 1 +fi + # k8s_version=1.11.0 k8s_version=1.13.1 goarch=amd64 @@ -130,7 +142,7 @@ function build_kb { } function prepare_testdir_under_gopath { - kb_test_dir=$GOPATH/src/sigs.k8s.io/kubebuilder-test + kb_test_dir=${go_workspace}/src/sigs.k8s.io/kubebuilder-test header_text "preparing test directory $kb_test_dir" rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir" header_text "running kubebuilder commands in test directory $kb_test_dir" diff --git a/pkg/scaffold/project/project.go b/pkg/scaffold/project/project.go index f474640dc88..e35d764248c 100644 --- a/pkg/scaffold/project/project.go +++ b/pkg/scaffold/project/project.go @@ -76,20 +76,24 @@ func (Project) repoFromGopathAndWd(gopath string, getwd func() (string, error)) if len(gopath) == 0 { gopath = build.Default.GOPATH } - goSrc := filepath.Join(gopath, "src") - // Make sure the GOPATH is set and the working dir is under the GOPATH - if !strings.HasPrefix(filepath.Dir(wd), goSrc) { - return "", fmt.Errorf("working directory must be a project directory under "+ - "$GOPATH/src/\n- GOPATH=%s\n- WD=%s", gopath, wd) - } + for _, workspace := range filepath.SplitList(gopath) { + goSrc := filepath.Join(workspace, "src") + + // Make sure the GOPATH is set and the working dir is under the GOPATH + if strings.HasPrefix(filepath.Dir(wd), goSrc) { - // Figure out the repo name by removing $GOPATH/src from the working directory - e.g. - // '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools' - repo := "" - for wd != goSrc { - repo = filepath.Join(filepath.Base(wd), repo) - wd = filepath.Dir(wd) + // Figure out the repo name by removing $GOPATH/src from the working directory - e.g. + // '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools' + repo := "" + for wd != goSrc { + repo = filepath.Join(filepath.Base(wd), repo) + wd = filepath.Dir(wd) + } + return repo, nil + } } - return repo, nil + + return "", fmt.Errorf("working directory must be a project directory under "+ + "$GOPATH/src/\n- GOPATH=%s\n- WD=%s", gopath, wd) } diff --git a/test.sh b/test.sh index 3c20613ef13..22506d3d726 100755 --- a/test.sh +++ b/test.sh @@ -127,7 +127,7 @@ prepare_testdir_under_gopath dump_cache test_create_namespaced_coretype_controller -cd $GOPATH/src/sigs.k8s.io/kubebuilder +cd ${go_workspace}/src/sigs.k8s.io/kubebuilder go test ./cmd/... ./pkg/...