Skip to content

Commit

Permalink
feat: Set client-go version vars
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 6, 2023
1 parent 5461790 commit b2d0fcd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ builds:
- CGO_ENABLED=0
flags:
- -trimpath
ldflags:
- -s -w
- -X k8s.io/client-go/pkg/version.gitVersion=v{{ .Version }}-{{ .Branch }}+{{ .FullCommit }}
- -X k8s.io/client-go/pkg/version.gitCommit={{ .FullCommit }}
- -X k8s.io/client-go/pkg/version.gitTreeState={{ if .IsGitDirty }}dirty{{ else }}clean{{ end }}
- -X k8s.io/client-go/pkg/version.buildDate={{ .CommitDate }}
goarch:
- amd64
- arm
Expand Down
17 changes: 8 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
"github.com/clevyr/kubedb/cmd/port_forward"
"github.com/clevyr/kubedb/cmd/restore"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewCommand(version, commit string) *cobra.Command {
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "kubedb",
Short: "interact with a database inside of Kubernetes",
Version: buildVersion(version, commit),
Version: buildVersion(),
DisableAutoGenTag: true,
Long: `kubedb is a command to interact with a database running in a Kubernetes cluster.
Expand All @@ -42,9 +43,6 @@ Dynamic Env Var Variables:
`,

PersistentPreRunE: preRun,
Annotations: map[string]string{
"version": version,
},
}

flags.Kubeconfig(cmd)
Expand Down Expand Up @@ -172,9 +170,10 @@ func initConfig() error {
return nil
}

func buildVersion(version, commit string) string {
if commit != "" {
version += " (" + commit + ")"
func buildVersion() string {
result := util.GetVersion()
if commit := util.GetCommit(); commit != "" {
result += " (" + commit + ")"
}
return version
return result
}
2 changes: 1 addition & 1 deletion internal/generate/completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var Shells = []string{"bash", "zsh", "fish"}

func main() {
rootCmd := cmd.NewCommand("latest", "")
rootCmd := cmd.NewCommand()
var buf bytes.Buffer
rootCmd.SetOut(&buf)

Expand Down
2 changes: 1 addition & 1 deletion internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
log.Fatal(err)
}

rootCmd := cmd.NewCommand("latest", "")
rootCmd := cmd.NewCommand()

err = doc.GenMarkdownTree(rootCmd, output)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/manpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
panic(err)
}

rootCmd := cmd.NewCommand("beta", "")
rootCmd := cmd.NewCommand()
rootName := rootCmd.Name()

date, err := time.Parse(time.RFC3339, dateParam)
Expand Down
2 changes: 1 addition & 1 deletion internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error
"app.kubernetes.io/name": "kubedb",
"app.kubernetes.io/instance": "kubedb",
"app.kubernetes.io/component": actionName,
"app.kubernetes.io/version": cmd.Root().Annotations["version"],
"app.kubernetes.io/version": GetVersion(),
}

podLabels := map[string]string{
Expand Down
21 changes: 21 additions & 0 deletions internal/util/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import (
"strings"

"k8s.io/client-go/pkg/version"
)

func GetVersion() string {
v := version.Get()
result, _, _ := strings.Cut(v.GitVersion[1:], "-")
return result
}

func GetCommit() string {
commit := version.Get().GitCommit
if commit == "$Format:%H$" {
return ""
}
return commit
}
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import (
"github.com/clevyr/kubedb/cmd"
)

var (
version = "next"
commit = ""
)

func main() {
rootCmd := cmd.NewCommand(version, commit)
rootCmd := cmd.NewCommand()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit b2d0fcd

Please sign in to comment.