1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00

Merge pull request #123 from nirmata/119_version

Show version info for each release
This commit is contained in:
Denys Bielyshev 2019-05-30 13:18:45 +01:00 committed by GitHub
commit 81ae5b0eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 96 additions and 7 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ certs
Gopkg.lock Gopkg.lock
.vscode .vscode
gh-pages/public gh-pages/public
_output

View file

@ -1,10 +1,24 @@
.DEFAULT_GOAL: build .DEFAULT_GOAL: build
# The CLI binary to build
BIN ?= kyverno
GIT_VERSION := $(shell git describe --dirty --always --tags)
GIT_BRANCH := $(shell git branch | grep \* | cut -d ' ' -f2)
GIT_HASH := $(GIT_BRANCH)/$(shell git log -1 --pretty=format:"%H")
TIMESTAMP := $(shell date '+%Y-%m-%d_%I:%M:%S%p')
PACKAGE ?=github.com/nirmata/kyverno PACKAGE ?=github.com/nirmata/kyverno
MAIN ?=$(PACKAGE) MAIN ?=$(PACKAGE)
LD_FLAGS ="-s -w"
REPO=nirmata/kyverno LD_FLAGS="-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(GIT_VERSION) -X $(PACKAGE)/pkg/version.BuildHash=$(GIT_HASH) -X $(PACKAGE)/pkg/version.BuildTime=$(TIMESTAMP)"
TAG=0.1
REPO=registry-v2.nirmata.io/nirmata/kyverno
IMAGE_TAG=$(GIT_VERSION)
GOOS ?= $(shell go env GOOS)
OUTPUT=$(shell pwd)/_output/cli/$(BIN)
build: build:
GOOS=linux go build -ldflags=$(LD_FLAGS) $(MAIN) GOOS=linux go build -ldflags=$(LD_FLAGS) $(MAIN)
@ -12,12 +26,22 @@ build:
local: local:
go build -ldflags=$(LD_FLAGS) $(MAIN) go build -ldflags=$(LD_FLAGS) $(MAIN)
cli: cli-dirs
GOOS=$(GOOS) \
go build \
-o $(OUTPUT) \
-ldflags $(LD_FLAGS) \
$(PACKAGE)/cmd/$(BIN)
cli-dirs:
@mkdir -p _output/cli
image: image:
docker build -t $(REPO):$(TAG) . docker build -t $(REPO):$(IMAGE_TAG) .
docker tag $(REPO):$(TAG) $(REPO):latest docker tag $(REPO):$(IMAGE_TAG) $(REPO):latest
push: push:
docker push $(REPO):$(TAG) docker push $(REPO):$(IMAGE_TAG)
docker push $(REPO):latest docker push $(REPO):latest
clean: clean:

View file

@ -5,11 +5,18 @@ import (
client "github.com/nirmata/kyverno/pkg/dclient" client "github.com/nirmata/kyverno/pkg/dclient"
tls "github.com/nirmata/kyverno/pkg/tls" tls "github.com/nirmata/kyverno/pkg/tls"
"github.com/nirmata/kyverno/pkg/version"
rest "k8s.io/client-go/rest" rest "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd" clientcmd "k8s.io/client-go/tools/clientcmd"
) )
func printVersionInfo() {
v := version.GetVersion()
log.Printf("Kyverno version: %s\n", v.BuildVersion)
log.Printf("Kyverno BuildHash: %s\n", v.BuildHash)
log.Printf("Kyverno BuildTime: %s\n", v.BuildTime)
}
func createClientConfig(kubeconfig string) (*rest.Config, error) { func createClientConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig == "" { if kubeconfig == "" {
log.Printf("Using in-cluster configuration") log.Printf("Using in-cluster configuration")

View file

@ -18,6 +18,8 @@ var (
) )
func main() { func main() {
printVersionInfo()
clientConfig, err := createClientConfig(kubeconfig) clientConfig, err := createClientConfig(kubeconfig)
if err != nil { if err != nil {
log.Fatalf("Error building kubeconfig: %v\n", err) log.Fatalf("Error building kubeconfig: %v\n", err)

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"github.com/nirmata/kyverno/pkg/kyverno/apply" "github.com/nirmata/kyverno/pkg/kyverno/apply"
"github.com/nirmata/kyverno/pkg/kyverno/version"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,5 +22,6 @@ func NewKyvernoCommand(in io.Reader, out, errout io.Writer) *cobra.Command {
} }
cmds.AddCommand(apply.NewCmdApply(in, out, errout)) cmds.AddCommand(apply.NewCmdApply(in, out, errout))
cmds.AddCommand(version.NewCmdVersion(out))
return cmds return cmds
} }

View file

@ -0,0 +1,29 @@
package version
import (
"fmt"
"io"
"github.com/nirmata/kyverno/pkg/version"
"github.com/spf13/cobra"
)
// NewCmdVersion is a command to display the build version
func NewCmdVersion(cmdOut io.Writer) *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "",
Run: func(cmd *cobra.Command, args []string) {
showVersion()
},
}
return versionCmd
}
func showVersion() {
fmt.Printf("Version: %s\n", version.BuildVersion)
fmt.Printf("Time: %s\n", version.BuildTime)
fmt.Printf("Git commit ID: %s\n", version.BuildHash)
}

24
pkg/version/version.go Normal file
View file

@ -0,0 +1,24 @@
package version
// These fields are set during an official build
var (
BuildVersion = "--"
BuildHash = "--"
BuildTime = "--"
)
// VersionInfo gets json info about the agent version
type VersionInfo struct {
BuildVersion string
BuildHash string
BuildTime string
}
// GetVersion gets the current agent version
func GetVersion() *VersionInfo {
return &VersionInfo{
BuildVersion: BuildVersion,
BuildHash: BuildHash,
BuildTime: BuildTime,
}
}