From 683fd7f0e5b24e855d5f8916f4e764449b648afe Mon Sep 17 00:00:00 2001 From: "Christopher M. Luciano" Date: Tue, 18 Apr 2017 15:07:00 -0400 Subject: [PATCH] Add containerdVersion flag Add version flag that only prints the static version for the binary. This commit does not include build details for containers since Makefile does not build them. Closes #8 Signed-off-by: Christopher M. Luciano --- Makefile | 13 +++++++-- cmd/cri-containerd/cri_containerd.go | 8 +++++ cmd/cri-containerd/options/options.go | 4 +++ pkg/version/version.go | 42 +++++++++++++++++++++++++++ pkg/version/version_test.go | 33 +++++++++++++++++++++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 pkg/version/version.go create mode 100644 pkg/version/version_test.go diff --git a/Makefile b/Makefile index ac1d83870..68ba697c1 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,9 @@ EPOCH_TEST_COMMIT ?= f9e02affccd51702191e5312665a16045ffef8ab PROJECT := github.com/kubernetes-incubator/cri-containerd BINDIR ?= ${DESTDIR}/usr/local/bin BUILD_DIR ?= _output +# VERSION is the version of the binary. +VERSION:=$(shell git describe --tags --dirty) +BUILD_TAGS:= -ldflags '-X $(PROJECT)/pkg/version.criContainerdVersion=$(VERSION)' all: binaries @@ -32,6 +35,7 @@ help: @echo " * 'verify' - Execute the source code verification tools" @echo " * 'install.tools' - Installs tools used by verify" @echo " * 'uninstall' - Remove installed binaries from system locations" + @echo " * 'version' - Print current cri-containerd release version" .PHONY: check-gopath @@ -42,6 +46,9 @@ endif verify: lint gofmt boiler +version: + @echo $(VERSION) + lint: check-gopath @echo "checking lint" @./hack/lint.sh @@ -56,7 +63,8 @@ boiler: cri-containerd: check-gopath $(GO) build -o $(BUILD_DIR)/$@ \ - $(PROJECT)/cmd/cri-containerd + $(BUILD_TAGS) \ + $(PROJECT)/cmd/cri-containerd test: go test -timeout=1m -v -race ./pkg/... $(BUILD_TAGS) @@ -102,4 +110,5 @@ install.tools: .install.gitvalidation .install.gometalinter help \ install \ lint \ - uninstall + uninstall \ + version diff --git a/cmd/cri-containerd/cri_containerd.go b/cmd/cri-containerd/cri_containerd.go index 175341506..9a0fe1458 100644 --- a/cmd/cri-containerd/cri_containerd.go +++ b/cmd/cri-containerd/cri_containerd.go @@ -17,11 +17,14 @@ limitations under the License. package main import ( + "os" + "github.com/golang/glog" "github.com/spf13/pflag" "github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options" "github.com/kubernetes-incubator/cri-containerd/pkg/server" + "github.com/kubernetes-incubator/cri-containerd/pkg/version" ) func main() { @@ -29,6 +32,11 @@ func main() { o.AddFlags(pflag.CommandLine) options.InitFlags() + if o.CRIContainerdVersion { + version.PrintVersion() + os.Exit(0) + } + glog.V(2).Infof("Connect to containerd socket %q with timeout %v", o.ContainerdSocketPath, o.ContainerdConnectionTimeout) conn, err := server.ConnectToContainerd(o.ContainerdSocketPath, o.ContainerdConnectionTimeout) if err != nil { diff --git a/cmd/cri-containerd/options/options.go b/cmd/cri-containerd/options/options.go index 6fa0c5b94..1d91f5a84 100644 --- a/cmd/cri-containerd/options/options.go +++ b/cmd/cri-containerd/options/options.go @@ -27,6 +27,8 @@ import ( type CRIContainerdOptions struct { // CRIContainerdSocketPath is the path to the socket which cri-containerd serves on. CRIContainerdSocketPath string + // CRIContainerdVersion is the git release version of cri-containerd + CRIContainerdVersion bool // ContainerdSocketPath is the path to the containerd socket. ContainerdSocketPath string // ContainerdConnectionTimeout is the connection timeout for containerd client. @@ -46,6 +48,8 @@ func (c *CRIContainerdOptions) AddFlags(fs *pflag.FlagSet) { "/run/containerd/containerd.sock", "Path to the containerd socket.") fs.DurationVar(&c.ContainerdConnectionTimeout, "containerd-connection-timeout", 2*time.Minute, "Connection timeout for containerd client.") + fs.BoolVar(&c.CRIContainerdVersion, "version", + false, "Print cri-containerd version information and quit.") } // InitFlags must be called after adding all cli options flags are defined and diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..05a2ca4b8 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,42 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package version + +import ( + "fmt" + + "github.com/blang/semver" +) + +var criContainerdVersion = "UNKNOWN" + +func validateSemver(sv string) error { + _, err := semver.Parse(sv) + if err != nil { + return fmt.Errorf("couldn't parse cri-containerd version %q: %v", sv, err) + } + return nil +} + +// PrintVersion outputs the release version of cri-containerd +func PrintVersion() { + err := validateSemver(criContainerdVersion) + if err != nil { + fmt.Println(err) + } + fmt.Println(criContainerdVersion) +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 000000000..22b0335a8 --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,33 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package version + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidateSemver(t *testing.T) { + err := validateSemver("UNKNOWN") + assert := assert.New(t) + assert.NotNil(err) + err = validateSemver("0.0.0-1-gdf6a1cc-dirty") + assert.Nil(err) + err = validateSemver(criContainerdVersion) + assert.Nil(err) +}