Skip to content

Commit

Permalink
tetra: version cmd default to CLI and add build info
Browse files Browse the repository at this point in the history
Version commands are usually expected to return quickly and can be used
to see if the CLI is well installed and works, I think it's best to put
the server version behind a flag. We could also adjust the timeout for
this command in order to return quickly.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Aug 10, 2023
1 parent 0d6bd13 commit ea1fdd6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 30 deletions.
66 changes: 36 additions & 30 deletions cmd/tetra/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,55 @@ import (

"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/version"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func printClientersion() {
fmt.Printf("cli version: %s\n", version.Version)
}
const examples = ` # Retrieve version from server
tetra version --server
func printVersion(res *tetragon.GetVersionResponse, err error) {
if err == nil {
fmt.Printf("server version: %s\n", res.Version)
printClientersion()
} else {
fmt.Printf("error getting server version: %s\n", err)
printClientersion()
}
}
# Get build info for the CLI
tetra version --build`

var (
server bool
build bool
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print version",
Args: cobra.NoArgs,
Use: "version",
Short: "Print version from CLI and server",
Example: examples,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("client") {
printClientersion()
return
fmt.Printf("CLI version: %s\n", version.Version)

if server {
common.CliRunErr(
func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
res, err := cli.GetVersion(ctx, &tetragon.GetVersionRequest{})
if err != nil {
logger.GetLogger().WithError(err).Error("error retrieving server version")
}
fmt.Printf("Server version: %s\n", res.Version)
},
func(err error) {
logger.GetLogger().WithError(err).Error("error retrieving server version")
},
)
}

if build {
info := version.ReadBuildInfo()
info.Print()
}
common.CliRunErr(
func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
res, err := cli.GetVersion(ctx, &tetragon.GetVersionRequest{})
printVersion(res, err)
},
func(err error) {
printVersion(nil, err)
},
)
},
}
flags := cmd.Flags()
flags.Bool("client", false, "Only print client version without attempting to connect to server")
viper.BindPFlags(flags)
flags.BoolVarP(&server, "server", "s", false, "Connect and retrieve version from the server")
flags.BoolVarP(&build, "build", "b", false, "Show CLI build information")
return cmd
}
57 changes: 57 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,61 @@

package version

import (
"fmt"
"runtime/debug"
)

var Version string

type BuildInfo struct {
GoVersion string
Commit string
Time string
Modified string
}

func ReadBuildInfo() *BuildInfo {
info := &BuildInfo{}
buildInfo, ok := debug.ReadBuildInfo()
if ok {
info.GoVersion = buildInfo.GoVersion
// unfortunately, it's not a Go map
for _, s := range buildInfo.Settings {
if s.Key == "vcs.revision" {
info.Commit = s.Value
continue
}
if s.Key == "vcs.time" {
info.Time = s.Value
continue
}
if s.Key == "vcs.modified" {
info.Modified = s.Value
continue
}
}
}
return info
}

func (info BuildInfo) Print() {
if info.GoVersion != "" {
fmt.Printf("GoVersion: %s\n", info.GoVersion)
}
if info.Time != "" {
fmt.Printf("Date: %s\n", info.Time)
}
if info.Commit != "" {
fmt.Printf("GitCommit: %s\n", info.Commit)
}
if info.Modified != "" {
var state string
if info.Modified == "true" {
state = "dirty"
} else {
state = "clean"
}
fmt.Printf("GitTreeState: %s\n", state)
}
}

0 comments on commit ea1fdd6

Please sign in to comment.