diff --git a/.gitignore b/.gitignore index e737726cce..e0cb53356a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ certs Gopkg.lock .vscode gh-pages/public +_output \ No newline at end of file diff --git a/Makefile b/Makefile index 98128083a1..5a8fc794e7 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,24 @@ .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 MAIN ?=$(PACKAGE) -LD_FLAGS ="-s -w" -REPO=nirmata/kyverno -TAG=0.1 +LD_FLAGS="-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(GIT_VERSION) -X $(PACKAGE)/pkg/version.BuildHash=$(GIT_HASH) -X $(PACKAGE)/pkg/version.BuildTime=$(TIMESTAMP)" + +REPO=registry-v2.nirmata.io/nirmata/kyverno +IMAGE_TAG=$(GIT_VERSION) + +GOOS ?= $(shell go env GOOS) +OUTPUT=$(shell pwd)/_output/cli/$(BIN) build: GOOS=linux go build -ldflags=$(LD_FLAGS) $(MAIN) @@ -12,12 +26,22 @@ build: local: 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: - docker build -t $(REPO):$(TAG) . - docker tag $(REPO):$(TAG) $(REPO):latest + docker build -t $(REPO):$(IMAGE_TAG) . + docker tag $(REPO):$(IMAGE_TAG) $(REPO):latest push: - docker push $(REPO):$(TAG) + docker push $(REPO):$(IMAGE_TAG) docker push $(REPO):latest clean: diff --git a/examples/cli/policy-deployment.yaml b/examples/cli/policy_deployment.yaml similarity index 100% rename from examples/cli/policy-deployment.yaml rename to examples/cli/policy_deployment.yaml diff --git a/examples/mutate/overlay/set_image_pull_policy.yaml b/examples/mutate/overlay/policy_imagePullPolicy.yaml similarity index 100% rename from examples/mutate/overlay/set_image_pull_policy.yaml rename to examples/mutate/overlay/policy_imagePullPolicy.yaml diff --git a/init.go b/init.go index a985d88a49..d6840d3590 100644 --- a/init.go +++ b/init.go @@ -5,11 +5,18 @@ import ( client "github.com/nirmata/kyverno/pkg/dclient" tls "github.com/nirmata/kyverno/pkg/tls" - + "github.com/nirmata/kyverno/pkg/version" rest "k8s.io/client-go/rest" 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) { if kubeconfig == "" { log.Printf("Using in-cluster configuration") diff --git a/main.go b/main.go index 1d3497e674..0439bcf271 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,8 @@ var ( ) func main() { + printVersionInfo() + clientConfig, err := createClientConfig(kubeconfig) if err != nil { log.Fatalf("Error building kubeconfig: %v\n", err) diff --git a/pkg/kyverno/cmd.go b/pkg/kyverno/cmd.go index 66aafc5e20..ac31e2a24f 100644 --- a/pkg/kyverno/cmd.go +++ b/pkg/kyverno/cmd.go @@ -5,6 +5,7 @@ import ( "os" "github.com/nirmata/kyverno/pkg/kyverno/apply" + "github.com/nirmata/kyverno/pkg/kyverno/version" "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(version.NewCmdVersion(out)) return cmds } diff --git a/pkg/kyverno/version/version.go b/pkg/kyverno/version/version.go new file mode 100644 index 0000000000..8d1f5dfb75 --- /dev/null +++ b/pkg/kyverno/version/version.go @@ -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) +} diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000000..5b40432bdf --- /dev/null +++ b/pkg/version/version.go @@ -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, + } +}