Skip to content

Commit

Permalink
Add virtletctl version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Apr 17, 2018
1 parent 9e01a48 commit 88258e1
Show file tree
Hide file tree
Showing 37 changed files with 1,030 additions and 21 deletions.
51 changes: 36 additions & 15 deletions build/cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ bindata_modtime=1522279343
bindata_out="pkg/tools/bindata.go"
bindata_dir="deploy/data"
bindata_pkg="tools"
ldflags=()
go_package=github.com/Mirantis/virtlet

function image_tags_filter {
local tag="${1}"
Expand Down Expand Up @@ -367,18 +369,37 @@ function run_integration_internal {
( cd tests/integration && ./go.test )
}

function set_version {
# TODO: always generate & set version in addition to the tag
if [[ ! ${SET_VIRTLET_IMAGE_TAG:-} ]]; then
return
function get_ldflags {
# XXX: use kube::version::ldflag (-ldflags -X package.Var=...)
# see also versioning.mk in helm
# https://stackoverflow.com/questions/11354518/golang-application-auto-build-versioning
# see pkg/version/version.go in k8s
# for GoVersion / Compiler / Platform
local vfile="${project_dir}/pkg/version/version.go"
local git_version="$(git describe --tags --abbrev=14 'HEAD^{commit}' | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")"
local git_commit="$(git rev-parse "HEAD^{commit}")"
local git_tree_state=$([[ $(git status --porcelain) ]] && echo "dirty" || echo "clean")
if [[ ${git_tree_state} == dirty ]]; then
git_version+="-dirty"
fi
cat >"${project_dir}/pkg/version/version.go" <<EOF
package version
func init () {
VirtletImageTag = "${SET_VIRTLET_IMAGE_TAG:-}"
}
EOF
local build_date="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
local git_major=""
local git_minor=""
local version_pkg="${go_package}/pkg/version"
local ldflags=(-X "${version_pkg}.gitVersion=${git_version}"
-X "${version_pkg}.gitCommit=${git_commit}"
-X "${version_pkg}.gitTreeState=${git_tree_state}"
-X "${version_pkg}.buildDate=${build_date}")
if [[ ${git_version} =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
git_major=${BASH_REMATCH[1]}
git_minor=${BASH_REMATCH[2]}
ldflags+=(-X "${version_pkg}.gitMajor=${git_major}"
-X "${version_pkg}.gitMinor=${git_minor}")
fi
if [[ ${SET_VIRTLET_IMAGE_TAG:-} ]]; then
ldflags+=(-X "${version_pkg}.imageTag=${SET_VIRTLET_IMAGE_TAG}")
fi
echo "${ldflags[*]}"
}

function build_internal {
Expand All @@ -390,11 +411,11 @@ function build_internal {
exit 1
fi
install_vendor_internal
ldflags="$(get_ldflags)"
mkdir -p "${project_dir}/_output"
set_version
go build -i -o "${project_dir}/_output/virtlet" ./cmd/virtlet
go build -i -o "${project_dir}/_output/virtletctl" ./cmd/virtletctl
GOOS=darwin go build -i -o "${project_dir}/_output/virtletctl.darwin" ./cmd/virtletctl
go build -i -o "${project_dir}/_output/virtlet" -ldflags "${ldflags}" ./cmd/virtlet
go build -i -o "${project_dir}/_output/virtletctl" -ldflags "${ldflags}" ./cmd/virtletctl
GOOS=darwin go build -i -o "${project_dir}/_output/virtletctl.darwin" -ldflags "${ldflags}" ./cmd/virtletctl
go build -i -o "${project_dir}/_output/vmwrapper" ./cmd/vmwrapper
go build -i -o "${project_dir}/_output/flexvolume_driver" ./cmd/flexvolume_driver
go test -i -c -o "${project_dir}/_output/virtlet-e2e-tests" ./tests/e2e
Expand Down
23 changes: 21 additions & 2 deletions cmd/virtlet/virtlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/Mirantis/virtlet/pkg/stream"
"github.com/Mirantis/virtlet/pkg/tapmanager"
"github.com/Mirantis/virtlet/pkg/utils"
"github.com/Mirantis/virtlet/pkg/version"
)

var (
Expand All @@ -56,6 +57,8 @@ var (
"Path to fd server socket")
imageTranslationConfigsDir = flag.String("image-translations-dir", "",
"Image name translation configs directory")
displayVersion = flag.Bool("version", false, "Display version and exit")
versionFormat = flag.String("version-format", "text", "Version format to use (text, short, json, yaml)")
)

const (
Expand Down Expand Up @@ -136,7 +139,7 @@ func runTapManager() {
os.Exit(1)
}
if err := libvirttools.ChownForEmulator(*fdServerSocketPath); err != nil {
glog.Warningf("couldn't set tapmanager socket permissions: %v", err)
glog.Warningf("Couldn't set tapmanager socket permissions: %v", err)
}
for {
time.Sleep(1000 * time.Hour)
Expand All @@ -153,14 +156,30 @@ func startTapManagerProcess() {
// https://github.com/golang/go/issues/9263
setPdeathsig(cmd)
if err := cmd.Start(); err != nil {
glog.Errorf("error starting tapmanager process: %v", err)
glog.Errorf("Error starting tapmanager process: %v", err)
os.Exit(1)
}
}

func printVersion() {
out, err := version.Get().ToBytes(*versionFormat)
if err == nil {
_, err = os.Stdout.Write(out)
}
if err != nil {
glog.Errorf("Error printing version info: %v", err)
os.Exit(1)
}
os.Exit(0)
}

func main() {
utils.HandleNsFixReexec()
flag.Parse()
if *displayVersion {
printVersion()
}

rand.Seed(time.Now().UnixNano())
if os.Getenv(WantTapManagerEnv) == "" {
startTapManagerProcess()
Expand Down
1 change: 1 addition & 0 deletions cmd/virtletctl/virtletctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func newRootCmd() *cobra.Command {
cmd.AddCommand(tools.NewInstallCmd(cmd, "", ""))
cmd.AddCommand(tools.NewGenDocCmd(cmd))
cmd.AddCommand(tools.NewGenCmd(os.Stdout))
cmd.AddCommand(tools.NewVersionCommand(client, os.Stdout, nil))

for _, c := range cmd.Commands() {
c.PreRunE = func(*cobra.Command, []string) error {
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions pkg/tools/TestVersionCommand__client.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Client:
Version: v1.0.0-6+318faff6ad0609-dirty
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: darwin/amd64
ImageTag: ivan4th_version
1 change: 1 addition & 0 deletions pkg/tools/TestVersionCommand__client_json.out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientVersion":{"major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609-dirty","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"dirty","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"darwin/amd64","imageTag":"ivan4th_version"}}
1 change: 1 addition & 0 deletions pkg/tools/TestVersionCommand__client_json.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientVersion":{"major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609-dirty","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"dirty","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"darwin/amd64","imageTag":"ivan4th_version"}}
1 change: 1 addition & 0 deletions pkg/tools/TestVersionCommand__client_short.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Client: v1.0.0-6+318faff6ad0609-dirty
11 changes: 11 additions & 0 deletions pkg/tools/TestVersionCommand__client_yaml.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
clientVersion:
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: dirty
gitVersion: v1.0.0-6+318faff6ad0609-dirty
goVersion: go1.8.3
imageTag: ivan4th_version
major: "1"
minor: "0"
platform: darwin/amd64
11 changes: 11 additions & 0 deletions pkg/tools/TestVersionCommand__client_yaml.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
clientVersion:
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: dirty
gitVersion: v1.0.0-6+318faff6ad0609-dirty
goVersion: go1.8.3
imageTag: ivan4th_version
major: "1"
minor: "0"
platform: darwin/amd64
22 changes: 22 additions & 0 deletions pkg/tools/TestVersionCommand__inconsistent.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Client:
Version: v1.0.0-6+318faff6ad0609-dirty
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: darwin/amd64
ImageTag: ivan4th_version
Node kube-node-1:
Version: v1.0.0-6+318faff6ad0609
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: linux/amd64
Node kube-node-2:
Version: v1.0.0-6+318faff6ad0609
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-17T07:16:01Z
Go Version: go1.8.3
Compiler: gc
Platform: linux/amd64
1 change: 1 addition & 0 deletions pkg/tools/TestVersionCommand__json.out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientVersion":{"major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609-dirty","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"dirty","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"darwin/amd64","imageTag":"ivan4th_version"},"nodeVersions":[{"NodeName":"kube-node-1","major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"clean","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"linux/amd64","imageTag":""},{"NodeName":"kube-node-2","major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"clean","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"linux/amd64","imageTag":""}]}
1 change: 1 addition & 0 deletions pkg/tools/TestVersionCommand__json.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clientVersion":{"major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609-dirty","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"dirty","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"darwin/amd64","imageTag":"ivan4th_version"},"nodeVersions":[{"NodeName":"kube-node-1","major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"clean","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"linux/amd64","imageTag":""},{"NodeName":"kube-node-2","major":"1","minor":"0","gitVersion":"v1.0.0-6+318faff6ad0609","gitCommit":"318faff6ad060954387c1ff594bcbb4bb128577a","gitTreeState":"clean","buildDate":"2018-04-16T21:02:05Z","goVersion":"go1.8.3","compiler":"gc","platform":"linux/amd64","imageTag":""}]}
3 changes: 3 additions & 0 deletions pkg/tools/TestVersionCommand__short.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Client: v1.0.0-6+318faff6ad0609-dirty
Node kube-node-1: v1.0.0-6+318faff6ad0609
Node kube-node-2: v1.0.0-6+318faff6ad0609
22 changes: 22 additions & 0 deletions pkg/tools/TestVersionCommand__text.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Client:
Version: v1.0.0-6+318faff6ad0609-dirty
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: darwin/amd64
ImageTag: ivan4th_version
Node kube-node-1:
Version: v1.0.0-6+318faff6ad0609
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: linux/amd64
Node kube-node-2:
Version: v1.0.0-6+318faff6ad0609
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: linux/amd64
8 changes: 8 additions & 0 deletions pkg/tools/TestVersionCommand__text_01.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Client:
Version: v1.0.0-6+318faff6ad0609-dirty
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: darwin/amd64
ImageTag: ivan4th_version
8 changes: 8 additions & 0 deletions pkg/tools/TestVersionCommand__text__empty.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Client:
Version: v1.0.0-6+318faff6ad0609-dirty
Commit: 318faff6ad060954387c1ff594bcbb4bb128577a
Build Date: 2018-04-16T21:02:05Z
Go Version: go1.8.3
Compiler: gc
Platform: darwin/amd64
ImageTag: ivan4th_version
34 changes: 34 additions & 0 deletions pkg/tools/TestVersionCommand__yaml.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
clientVersion:
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: dirty
gitVersion: v1.0.0-6+318faff6ad0609-dirty
goVersion: go1.8.3
imageTag: ivan4th_version
major: "1"
minor: "0"
platform: darwin/amd64
nodeVersions:
- NodeName: kube-node-1
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: clean
gitVersion: v1.0.0-6+318faff6ad0609
goVersion: go1.8.3
imageTag: ""
major: "1"
minor: "0"
platform: linux/amd64
- NodeName: kube-node-2
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: clean
gitVersion: v1.0.0-6+318faff6ad0609
goVersion: go1.8.3
imageTag: ""
major: "1"
minor: "0"
platform: linux/amd64
34 changes: 34 additions & 0 deletions pkg/tools/TestVersionCommand__yaml.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
clientVersion:
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: dirty
gitVersion: v1.0.0-6+318faff6ad0609-dirty
goVersion: go1.8.3
imageTag: ivan4th_version
major: "1"
minor: "0"
platform: darwin/amd64
nodeVersions:
- NodeName: kube-node-1
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: clean
gitVersion: v1.0.0-6+318faff6ad0609
goVersion: go1.8.3
imageTag: ""
major: "1"
minor: "0"
platform: linux/amd64
- NodeName: kube-node-2
buildDate: "2018-04-16T21:02:05Z"
compiler: gc
gitCommit: 318faff6ad060954387c1ff594bcbb4bb128577a
gitTreeState: clean
gitVersion: v1.0.0-6+318faff6ad0609
goVersion: go1.8.3
imageTag: ""
major: "1"
minor: "0"
platform: linux/amd64
2 changes: 1 addition & 1 deletion pkg/tools/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewGenCmd(out io.Writer) *cobra.Command {
}
cmd.Flags().BoolVar(&g.dev, "dev", false, "Development mode for use with kubeadm-dind-cluster")
cmd.Flags().BoolVar(&g.compat, "compat", false, "Produce YAML that's compatible with older Kubernetes versions")
cmd.Flags().StringVar(&g.tag, "tag", version.VirtletImageTag, "Set virtlet image tag")
cmd.Flags().StringVar(&g.tag, "tag", version.Get().ImageTag, "Set virtlet image tag")
return cmd
}

Expand Down
Loading

0 comments on commit 88258e1

Please sign in to comment.