Skip to content

Commit

Permalink
Merge pull request #122 from droot/fix-dep-config
Browse files Browse the repository at this point in the history
fixed gopkg.toml for Kubebuilder and generated project
  • Loading branch information
droot authored May 4, 2018
2 parents 6308e83 + c1a4f6c commit 436988b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion build/build_vendor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 49 additions & 6 deletions cmd/kubebuilder/initproject/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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
}
27 changes: 24 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 )"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 436988b

Please sign in to comment.