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 <cmluciano@us.ibm.com>
This commit is contained in:
Christopher M. Luciano 2017-04-18 15:07:00 -04:00
parent d6978e3b7f
commit 683fd7f0e5
No known key found for this signature in database
GPG Key ID: 5148DBB31F2843F1
5 changed files with 98 additions and 2 deletions

View File

@ -17,6 +17,9 @@ EPOCH_TEST_COMMIT ?= f9e02affccd51702191e5312665a16045ffef8ab
PROJECT := github.com/kubernetes-incubator/cri-containerd PROJECT := github.com/kubernetes-incubator/cri-containerd
BINDIR ?= ${DESTDIR}/usr/local/bin BINDIR ?= ${DESTDIR}/usr/local/bin
BUILD_DIR ?= _output 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 all: binaries
@ -32,6 +35,7 @@ help:
@echo " * 'verify' - Execute the source code verification tools" @echo " * 'verify' - Execute the source code verification tools"
@echo " * 'install.tools' - Installs tools used by verify" @echo " * 'install.tools' - Installs tools used by verify"
@echo " * 'uninstall' - Remove installed binaries from system locations" @echo " * 'uninstall' - Remove installed binaries from system locations"
@echo " * 'version' - Print current cri-containerd release version"
.PHONY: check-gopath .PHONY: check-gopath
@ -42,6 +46,9 @@ endif
verify: lint gofmt boiler verify: lint gofmt boiler
version:
@echo $(VERSION)
lint: check-gopath lint: check-gopath
@echo "checking lint" @echo "checking lint"
@./hack/lint.sh @./hack/lint.sh
@ -56,6 +63,7 @@ boiler:
cri-containerd: check-gopath cri-containerd: check-gopath
$(GO) build -o $(BUILD_DIR)/$@ \ $(GO) build -o $(BUILD_DIR)/$@ \
$(BUILD_TAGS) \
$(PROJECT)/cmd/cri-containerd $(PROJECT)/cmd/cri-containerd
test: test:
@ -102,4 +110,5 @@ install.tools: .install.gitvalidation .install.gometalinter
help \ help \
install \ install \
lint \ lint \
uninstall uninstall \
version

View File

@ -17,11 +17,14 @@ limitations under the License.
package main package main
import ( import (
"os"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options" "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/server"
"github.com/kubernetes-incubator/cri-containerd/pkg/version"
) )
func main() { func main() {
@ -29,6 +32,11 @@ func main() {
o.AddFlags(pflag.CommandLine) o.AddFlags(pflag.CommandLine)
options.InitFlags() 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) glog.V(2).Infof("Connect to containerd socket %q with timeout %v", o.ContainerdSocketPath, o.ContainerdConnectionTimeout)
conn, err := server.ConnectToContainerd(o.ContainerdSocketPath, o.ContainerdConnectionTimeout) conn, err := server.ConnectToContainerd(o.ContainerdSocketPath, o.ContainerdConnectionTimeout)
if err != nil { if err != nil {

View File

@ -27,6 +27,8 @@ import (
type CRIContainerdOptions struct { type CRIContainerdOptions struct {
// CRIContainerdSocketPath is the path to the socket which cri-containerd serves on. // CRIContainerdSocketPath is the path to the socket which cri-containerd serves on.
CRIContainerdSocketPath string CRIContainerdSocketPath string
// CRIContainerdVersion is the git release version of cri-containerd
CRIContainerdVersion bool
// ContainerdSocketPath is the path to the containerd socket. // ContainerdSocketPath is the path to the containerd socket.
ContainerdSocketPath string ContainerdSocketPath string
// ContainerdConnectionTimeout is the connection timeout for containerd client. // 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.") "/run/containerd/containerd.sock", "Path to the containerd socket.")
fs.DurationVar(&c.ContainerdConnectionTimeout, "containerd-connection-timeout", fs.DurationVar(&c.ContainerdConnectionTimeout, "containerd-connection-timeout",
2*time.Minute, "Connection timeout for containerd client.") 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 // InitFlags must be called after adding all cli options flags are defined and

42
pkg/version/version.go Normal file
View File

@ -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)
}

View File

@ -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)
}