mirror of
https://github.com/compute-blade-community/compute-blade-agent.git
synced 2026-04-21 17:45:43 +02:00
feat(bladectl): add server version information to output (#100)
add server version information to output ensure the blade-name is always set --------- Co-authored-by: Cedric Kienzler <cedric@specht-labs.de>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
@@ -27,6 +28,9 @@ import (
|
||||
var (
|
||||
Version string
|
||||
Commit string
|
||||
// Date is the CommitTimestamp when the build was done as UNIX Timestamp
|
||||
Date string
|
||||
BuildTime time.Time
|
||||
)
|
||||
|
||||
var debug = pflag.BoolP("debug", "v", false, "enable verbose logging")
|
||||
@@ -59,7 +63,6 @@ func main() {
|
||||
zapLogger := baseLogger.With(
|
||||
zap.String("app", "compute-blade-agent"),
|
||||
zap.String("version", Version),
|
||||
zap.String("commit", Commit),
|
||||
)
|
||||
defer func() {
|
||||
_ = zapLogger.Sync()
|
||||
@@ -153,8 +156,28 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
log.FromContext(ctx).Info("Bootstrapping compute-blade-agent", zap.String("version", Version), zap.String("commit", Commit))
|
||||
computebladeAgent, err := internal_agent.NewComputeBladeAgent(ctx, cbAgentConfig)
|
||||
if Date != "" {
|
||||
if unixTimestamp, err := strconv.ParseInt(Date, 10, 64); err == nil {
|
||||
BuildTime = time.Unix(unixTimestamp, 0)
|
||||
} else {
|
||||
BuildTime = time.Unix(0, 0)
|
||||
log.FromContext(context.Background()).WithError(err).Warn("Failed to parse build timestamp")
|
||||
}
|
||||
}
|
||||
|
||||
log.FromContext(ctx).Info("Bootstrapping compute-blade-agent",
|
||||
zap.String("version", Version),
|
||||
zap.String("commit", Commit),
|
||||
zap.String("date", BuildTime.Format(time.RFC3339)),
|
||||
)
|
||||
|
||||
cbAgentInfo := agent.ComputeBladeAgentInfo{
|
||||
Version: Version,
|
||||
Commit: Commit,
|
||||
BuildTime: BuildTime,
|
||||
}
|
||||
|
||||
computebladeAgent, err := internal_agent.NewComputeBladeAgent(ctx, cbAgentConfig, cbAgentInfo)
|
||||
if err != nil {
|
||||
cancelCtx(err)
|
||||
log.FromContext(ctx).WithError(err).Fatal("Failed to create agent")
|
||||
|
||||
@@ -92,14 +92,15 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
clients := make([]bladeapiv1alpha1.BladeAgentServiceClient, len(bladeNames))
|
||||
for idx, bladeName := range bladeNames {
|
||||
var blade *config.Blade
|
||||
blade, herr := bladectlCfg.FindBlade(bladeName)
|
||||
namedBlade, herr := bladectlCfg.FindBlade(bladeName)
|
||||
if herr != nil {
|
||||
cancelCtx(herr)
|
||||
return errors.New(herr.Display())
|
||||
}
|
||||
|
||||
client, herr := buildClient(blade)
|
||||
bladeNames[idx] = namedBlade.Name
|
||||
|
||||
client, herr := buildClient(&namedBlade.Blade)
|
||||
if herr != nil {
|
||||
cancelCtx(herr)
|
||||
return errors.New(herr.Display())
|
||||
|
||||
@@ -2,8 +2,15 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/olekukonko/tablewriter/tw"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -15,9 +22,49 @@ var cmdVersion = &cobra.Command{
|
||||
Short: "Shows version information",
|
||||
Example: "bladectl version",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("Version: %s\n", Version)
|
||||
fmt.Printf("Date: %s\n", Date)
|
||||
fmt.Printf("Commit: %s\n", Commit)
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
clients := clientsFromContext(ctx)
|
||||
|
||||
header := []string{
|
||||
"Component",
|
||||
"Version",
|
||||
"Commit",
|
||||
"Build Time",
|
||||
}
|
||||
|
||||
// Table writer setup
|
||||
tbl := tablewriter.NewTable(os.Stdout,
|
||||
tablewriter.WithHeader(header),
|
||||
tablewriter.WithHeaderAlignment(tw.AlignLeft),
|
||||
tablewriter.WithHeaderAutoFormat(tw.Off),
|
||||
)
|
||||
|
||||
_ = tbl.Append([]string{"bladectl", Version, Commit, BuildTime.Format(time.RFC3339)})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for idx, client := range clients {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if status, err := client.GetStatus(ctx, &emptypb.Empty{}); err == nil && status.Version != nil {
|
||||
_ = tbl.Append([]string{
|
||||
fmt.Sprintf("api: %s", bladeNames[idx]),
|
||||
status.Version.Version,
|
||||
status.Version.Commit,
|
||||
time.Unix(status.Version.Date, 0).Format(time.RFC3339),
|
||||
})
|
||||
} else {
|
||||
log.Printf("Error (%s) getting status: %v", bladeNames[idx], err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
_ = tbl.Render()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ type Certificate struct {
|
||||
ClientKeyData string `yaml:"client-key-data,omitempty" mapstructure:"client-key-data,omitempty"`
|
||||
}
|
||||
|
||||
func (c *BladectlConfig) FindBlade(name string) (*Blade, humane.Error) {
|
||||
func (c *BladectlConfig) FindBlade(name string) (*NamedBlade, humane.Error) {
|
||||
if len(name) == 0 {
|
||||
name = c.CurrentBlade
|
||||
}
|
||||
|
||||
for _, blade := range c.Blades {
|
||||
if blade.Name == name {
|
||||
return &blade.Blade, nil
|
||||
return &blade, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
bladeapiv1alpha1 "github.com/compute-blade-community/compute-blade-agent/api/bladeapi/v1alpha1"
|
||||
"github.com/spf13/viper"
|
||||
@@ -17,9 +19,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
BuildTime time.Time
|
||||
)
|
||||
|
||||
func clientIntoContext(ctx context.Context, client bladeapiv1alpha1.BladeAgentServiceClient) context.Context {
|
||||
@@ -47,6 +50,14 @@ func clientsFromContext(ctx context.Context) []bladeapiv1alpha1.BladeAgentServic
|
||||
}
|
||||
|
||||
func main() {
|
||||
if Date != "" {
|
||||
if unixTimestamp, err := strconv.ParseInt(Date, 10, 64); err == nil {
|
||||
BuildTime = time.Unix(unixTimestamp, 0)
|
||||
} else {
|
||||
BuildTime = time.Unix(0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// Setup configuration
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
Reference in New Issue
Block a user