diff --git a/.travis.yml b/.travis.yml index 8753ae94b96..0b3f27d2e87 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,11 @@ go: git: depth: 3 - go_import_path: github.com/kubernetes-sigs/kubebuilder +before_install: +- go get -u github.com/golang/dep/cmd/dep + # Install must be set to prevent default `go get` to run. # The dependencies have already been vendored by `dep` so # we don't need to fetch them. diff --git a/Gopkg.lock b/Gopkg.lock index 13a9fe2736d..20f0e70b714 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -582,7 +582,6 @@ revision = "50ae88d24ede7b8bad68e23c805b5d3da5c8abaf" [[projects]] - branch = "master" name = "sigs.k8s.io/testing_frameworks" packages = [ "integration", @@ -593,6 +592,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "bdd3cd6805a7da6f0c03dfdc7b6daa9a96c0d563ee7c6fb36a5d7bb26481c6fc" + inputs-digest = "23dd99960429731773b751527608685fdd29b212b9ed7cedcd502b0f27d43c6c" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index d2641924d50..44c62b9fb19 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -17,10 +17,6 @@ required = ["sigs.k8s.io/testing_frameworks/integration", "k8s.io/client-go/plugin/pkg/client/auth", "github.com/spf13/pflag"] -[[constraint]] - version = "KUBEBUILDER_VERSION" - name = "github.com/kubernetes-sigs/kubebuilder" - [[constraint]] name = "sigs.k8s.io/testing_frameworks" revision = "f53464b8b84b4507805a0b033a8377b225163fea" diff --git a/build/build_vendor.sh b/build/build_vendor.sh index a41ebf064b4..6610dcd42ea 100644 --- a/build/build_vendor.sh +++ b/build/build_vendor.sh @@ -23,5 +23,4 @@ cp /workspace/LICENSE /workspace/vendor/github.com/kubernetes-sigs/kubebuilder/L export DEST=/workspace/_output/kubebuilder/bin/ mkdir -p $DEST || echo "" -sed -i "s/KUBEBUILDER_VERSION/"${VERSION-master}"/" Gopkg.toml tar -czvf $DEST/vendor.tar.gz vendor/ Gopkg.lock Gopkg.toml diff --git a/cmd/kubebuilder/initproject/vendor.go b/cmd/kubebuilder/initproject/vendor.go index 3f830b12b94..7e83f9fb916 100644 --- a/cmd/kubebuilder/initproject/vendor.go +++ b/cmd/kubebuilder/initproject/vendor.go @@ -19,13 +19,15 @@ package initproject import ( "archive/tar" "compress/gzip" - "fmt" + "fmt" "io/ioutil" "log" "os" "path/filepath" "github.com/spf13/cobra" + + "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version" ) var vendorInstallCmd = &cobra.Command{ @@ -95,7 +97,7 @@ func RunVendorInstall(cmd *cobra.Command, args []string) { deleteOld() } - // Get the executable directory + // Get the executable directory e, err := os.Executable() if err != nil { log.Fatal("unable to get directory of kubebuilder tools") @@ -121,10 +123,10 @@ func RunVendorInstall(cmd *cobra.Command, args []string) { tr := tar.NewReader(gr) for file, err := tr.Next(); err == nil; file, err = tr.Next() { - if file.FileInfo().IsDir() { - continue - } - p := filepath.Join(".", file.Name) + if file.FileInfo().IsDir() { + continue + } + p := filepath.Join(".", file.Name) if Update && filepath.Dir(p) == "." { continue } @@ -142,4 +144,45 @@ func RunVendorInstall(cmd *cobra.Command, args []string) { log.Fatalf("Could not write file %s: %v", p, err) } } + + err = updateDepConfig() + if err != nil { + log.Fatalf("Could not update Gopkg.toml file: %v", err) + } +} + +// updateDepConfig updates the Dep config Gopkg.toml to include Kubebuilder +// project dependency. It uses the Kubebuilder version to determine whether +// to include branch or version in the contraint stanza. +func updateDepConfig() error { + var depConstraint string + + kbVersion := version.GetVersion().KubeBuilderVersion + if kbVersion == "unknown" { + // KB is built from master branch + depConstraint = ` +[[constraint]] + branch = "master" + name = "github.com/kubernetes-sigs/kubebuilder" +` + } else { + depConstraint = fmt.Sprintf(` +[[constraint]] + version = "%s" + name = "github.com/kubernetes-sigs/kubebuilder" +`, kbVersion) + } + + f, err := os.OpenFile("Gopkg.toml", os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return err + } + + defer f.Close() + + if _, err = f.WriteString(depConstraint); err != nil { + return err + } + + return nil } diff --git a/test.sh b/test.sh index fe78d5b2626..e73b4e1da1a 100755 --- a/test.sh +++ b/test.sh @@ -25,6 +25,13 @@ if [ -n "$TRACE" ]; then set -x fi +# By setting INJECT_KB_VERSION variable in your environment, KB will be compiled +# with this version. This is to assist testing functionality which depends on +# version .e.g gopkg.toml generation. +# +# $ INJECT_KB_VERSION=0.1.7 test.sh +INJECT_KB_VERSION=${INJECT_KB_VERSION:-unknown} + # Make sure, we run in the root of the repo and # therefore run the tests on all packages base_dir="$( cd "$(dirname "$0")/" && pwd )" @@ -109,7 +116,13 @@ function fetch_tools { function build_kb { header_text "building kubebuilder" - go build -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder + + if [ "$INJECT_KB_VERSION" = "unknown" ]; then + go build -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder + else + go build -ldflags "-X github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/version.kubeBuilderVersion=$INJECT_KB_VERSION" -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder + fi + go build -o $tmp_root/kubebuilder/bin/kubebuilder-gen ./cmd/kubebuilder-gen } @@ -123,7 +136,6 @@ function prepare_vendor_deps { cp Gopkg.toml Gopkg.lock $tmp_root/ cp -a vendor/* $kb_vendor_dir/ cd $tmp_root - sed -i "s/KUBEBUILDER_VERSION/"${VERSION-master}"/" Gopkg.toml tar -czf $kb_root_dir/bin/vendor.tar.gz vendor/ Gopkg.lock Gopkg.toml } @@ -165,7 +177,7 @@ metadata: creationTimestamp: null labels: api: "" - kubebuilder.k8s.io: unknown + kubebuilder.k8s.io: $INJECT_KB_VERSION name: bees.insect.sample.kubernetes.io spec: group: insect.sample.kubernetes.io @@ -213,6 +225,11 @@ function test_generated_controller { go test -v ./pkg/... } +function run_dep_ensure { + header_text "running dep ensure" + dep ensure +} + prepare_staging_dir fetch_tools build_kb @@ -221,4 +238,8 @@ prepare_testdir_under_gopath generate_crd_resources test_generated_controller +run_dep_ensure +# Run controller tests after running dep ensure because we want ensure code +# compiles and tests pass after user ran dep ensure on the generated project. +test_generated_controller exit $rc