Skip to content

Commit

Permalink
feat: add docker-build make job
Browse files Browse the repository at this point in the history
feat: add '--version' flag to print version info
  • Loading branch information
nashtsai authored and ldming committed Aug 23, 2022
1 parent ab62332 commit 0c82c9b
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 37 deletions.
34 changes: 29 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
# Mac OS X files
.DS_Store

# Binaries for programs and plugins
*.exe
*.exe~
*.swp
.DS_Store
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.coverage/
.idea/

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

# IDE files
.vimrc
.history/
.vscode/
_dist/
bin/
.idea/

# Dependency directories (remove the comment below to include it)
vendor/
tmp/
pkg/resources/static/k3s
pkg/resources/static/k3d/

# Distribution directories
_dist/
bin/
tmp/

3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
errcheck:
check-blank: false # to keep `_ = viper.BindPFlag(...)` from throwing errors
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot

WORKDIR /
COPY bin/opencli.* /
USER nonroot:nonroott
23 changes: 18 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
include dependency.mk

VERSION ?= 0.1.0
IMG ?= docker.io/infracreate/opencli
CLI_VERSION ?= 0.1.0
TAG ?= v$(CLI_VERSION)

K3S_VERSION ?= v1.23.8+k3s1
K3D_VERSION ?= 5.4.4
Expand All @@ -25,9 +27,12 @@ export GOPRIVATE=jihulab.com/infracreate
export GOPROXY=https://goproxy.cn,direct


#LD_FLAGS="-s -w -X main.version=v${VERSION} -X main.buildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` -X main.gitCommit=`git rev-parse HEAD` -X ./pkg/types/types.K3sImageTag=${K3S_IMG_TAG} -X ./pkg/types/types.K3dVersion=${K3D_VERSION} "
#LD_FLAGS="-s -w -X main.version=v${VERSION} -X main.buildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` -X main.gitCommit=`git rev-parse HEAD` -X jihulab.com/infracreate/dbaas-system/opencli/pkg/types/types.K3sImageTag=${K3S_IMG_TAG} -X jihulab.com/infracreate/dbaas-system/opencli/pkg/types/types.K3dVersion=${K3D_VERSION} "
LD_FLAGS="-s -w -X jihulab.com/infracreate/dbaas-system/opencli/pkg/version.Version=v${VERSION} -X main.buildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` -X main.gitCommit=`git rev-parse HEAD`"
LD_FLAGS="-s -w \
-X jihulab.com/infracreate/dbaas-system/opencli/version.BuildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` \
-X jihulab.com/infracreate/dbaas-system/opencli/version.GitCommit=`git rev-parse HEAD` \
-X jihulab.com/infracreate/dbaas-system/opencli/version.Version=${CLI_VERSION} \
-X jihulab.com/infracreate/dbaas-system/opencli/version.K3sImageTag=${K3S_IMG_TAG} \
-X jihulab.com/infracreate/dbaas-system/opencli/version.K3dVersion=${K3D_VERSION}"


.DEFAULT_GOAL := bin/opencli
Expand Down Expand Up @@ -64,7 +69,7 @@ lint: golangci
staticcheck: staticchecktool
$(STATICCHECK) ./...

goimports:
goimports: goimportstool
$(GOIMPORTS) -local jihulab.com/infracreate/dbaas-system/opencli -w $$(go list -f {{.Dir}} ./...)

.PHONY: go-check
Expand All @@ -91,6 +96,14 @@ mod-vendor:
$(GO) mod vendor
$(GO) mod verify


# Run docker build
.PHONY: docker-build
docker-build: clean bin/opencli.linux.amd64 bin/opencli.linux.arm64 bin/opencli.darwin.arm64 bin/opencli.darwin.amd64 bin/opencli.windows.amd64
DOCKER_BUILDKIT=1 docker build . -t ${IMG}:${TAG}



.PHONY: reviewable
reviewable: lint staticcheck fmt go-check
$(GO) mod tidy -compat=1.17
Expand Down
4 changes: 2 additions & 2 deletions dependency.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ else
STATICCHECK=$(shell which staticcheck)
endif

.PHONY: goimports
goimports:
.PHONY: goimportstool
goimportstool:
ifeq (, $(shell which goimports))
@{ \
set -e ;\
Expand Down
38 changes: 25 additions & 13 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
Expand All @@ -30,20 +31,36 @@ import (
"jihulab.com/infracreate/dbaas-system/opencli/pkg/cmd/dbaas"
"jihulab.com/infracreate/dbaas-system/opencli/pkg/cmd/dbcluster"
"jihulab.com/infracreate/dbaas-system/opencli/pkg/cmd/playground"
"jihulab.com/infracreate/dbaas-system/opencli/pkg/version"
"jihulab.com/infracreate/dbaas-system/opencli/version"
)

// RootFlags describes a struct that holds flags that can be set on root level of the command
type RootFlags struct {
version bool
}

var cfgFile string

var rootFlags = RootFlags{}

func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "opencli",
Short: "A Command Line Interface(CLI) library for dbaas.",
Run: runHelp,
Short: "A Command Line Interface(CLI) library for DBaaS.",
Run: func(cmd *cobra.Command, args []string) {
if rootFlags.version {
printVersion()
} else {
runHelp(cmd, args)
}
},
}

flags := rootCmd.PersistentFlags()

// add local flags
rootCmd.Flags().BoolVar(&rootFlags.version, "version", false, "Show version")

kubeConfigFlags := genericclioptions.NewConfigFlags(true)
kubeConfigFlags.AddFlags(flags)
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
Expand All @@ -60,7 +77,6 @@ func NewRootCmd() *cobra.Command {
dbaas.NewDbaasCmd(),
dbcluster.NewDbclusterCmd(f, ioStreams),
bench.NewBenchCmd(f),
newVersionCmd(),
)

cobra.OnInitialize(initConfig)
Expand Down Expand Up @@ -96,13 +112,9 @@ func initConfig() {
}
}

func newVersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Show opencli version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.Version)
},
}
return cmd
func printVersion() {
fmt.Printf("opencli version %s\n", version.GetVersion())
fmt.Printf("k3d version %s\n", version.K3dVersion)
fmt.Printf("k3s version %s (default)\n", strings.Replace(version.K3sImageTag, "-", "+", 1))
fmt.Printf("git commit %s (build date %s)\n", version.GitCommit, version.BuildDate)
}
24 changes: 12 additions & 12 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package types

import "jihulab.com/infracreate/dbaas-system/opencli/version"

const (
// OpenCliDefaultHome defines opencli default home name
OpenCliDefaultHome = ".opencli"
Expand All @@ -32,18 +34,6 @@ const (
// all cluster will be created in this network, so they can communicate with each other
CliDockerNetwork = "k3d-opencli-playground"

// K3sImageTag is k3s image tag
K3sImageTag = "v1.23.8-k3s1"
// K3sImage is k3s image repo
K3sImage = "rancher/k3s:" + K3sImageTag

// K3dVersion is k3d release version
K3dVersion = "5.4.4"
// K3dToolsImage is k3d tools image repo
K3dToolsImage = "ghcr.io/k3d-io/k3d-tools:" + K3dVersion
// K3dProxyImage is k3d proxy image repo
K3dProxyImage = "ghcr.io/k3d-io/k3d-proxy:" + K3dVersion

// GoosLinux is os.GOOS linux string
GoosLinux = "linux"
// GoosDarwin is os.GOOS darwin string
Expand All @@ -55,6 +45,16 @@ const (
PlaygroundSourceName = "innodbclusters"
)

var (
// K3sImage is k3s image repo
K3sImage = "rancher/k3s:" + version.K3sImageTag

// K3dToolsImage is k3d tools image repo
K3dToolsImage = "docker.io/infracreate/k3d-tools:" + version.K3dVersion
// K3dProxyImage is k3d proxy image repo
K3dProxyImage = "docker.io/infracreate/k3d-proxy:" + version.K3dVersion
)

type K3dImages struct {
K3s bool
K3dTools bool
Expand Down
45 changes: 45 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2022 The opencli Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package version

// Version is the string that contains version
var Version string

// BuildDate is the string of binary build date
var BuildDate string

// GitCommit is the string of git commit ID
var GitCommit string

// K3dVersion is k3d release version
var K3dVersion = "5.4.4"

// K3sImageTag is k3s image tag
var K3sImageTag = "v1.23.8-k3s1"

// GetVersion returns the version for cli, it gets it from "git describe --tags" or returns "dev" when doing simple go build
func GetVersion() string {
if len(Version) == 0 {
return "v1-dev"
}
return Version
}

0 comments on commit 0c82c9b

Please sign in to comment.