From 4f5531334f334934b59dbde6fd5e91a94c26c2be Mon Sep 17 00:00:00 2001 From: David Christofas Date: Fri, 25 Sep 2020 13:37:07 +0200 Subject: [PATCH] add version command and add build information to metrics Signed-off-by: David Christofas --- .../unreleased/add-version-command.md | 6 +++ konnectd/pkg/command/root.go | 2 + konnectd/pkg/command/server.go | 2 + konnectd/pkg/command/version.go | 45 +++++++++++++++++++ konnectd/pkg/config/config.go | 17 ++++--- konnectd/pkg/flagset/flagset.go | 28 +++++++++++- konnectd/pkg/metrics/metrics.go | 13 ++++++ konnectd/pkg/server/debug/server.go | 5 +-- konnectd/pkg/server/http/server.go | 11 +++-- ocis/pkg/command/konnectd.go | 5 +++ 10 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 konnectd/changelog/unreleased/add-version-command.md create mode 100644 konnectd/pkg/command/version.go diff --git a/konnectd/changelog/unreleased/add-version-command.md b/konnectd/changelog/unreleased/add-version-command.md new file mode 100644 index 0000000000..adc2f41bb7 --- /dev/null +++ b/konnectd/changelog/unreleased/add-version-command.md @@ -0,0 +1,6 @@ +Enhancement: add version command + +Added a command to list the versions of all running ocis-konnectd instances. +Also added build information to the metrics. + +https://github.com/owncloud/product/issues/226 diff --git a/konnectd/pkg/command/root.go b/konnectd/pkg/command/root.go index 2d31524108..91b2924060 100644 --- a/konnectd/pkg/command/root.go +++ b/konnectd/pkg/command/root.go @@ -32,12 +32,14 @@ func Execute() error { Flags: flagset.RootWithConfig(cfg), Before: func(c *cli.Context) error { + cfg.Service.Version = version.String return ParseConfig(c, cfg) }, Commands: []*cli.Command{ Server(cfg), Health(cfg), + PrintVersion(cfg), }, } diff --git a/konnectd/pkg/command/server.go b/konnectd/pkg/command/server.go index 17457a4e7a..d928ef3ff3 100644 --- a/konnectd/pkg/command/server.go +++ b/konnectd/pkg/command/server.go @@ -150,6 +150,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() + metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1) + { server, err := http.Server( http.Logger(logger), diff --git a/konnectd/pkg/command/version.go b/konnectd/pkg/command/version.go new file mode 100644 index 0000000000..377b611206 --- /dev/null +++ b/konnectd/pkg/command/version.go @@ -0,0 +1,45 @@ +package command + +import ( + "fmt" + "os" + + "github.com/micro/cli/v2" + "github.com/micro/go-micro/v2/registry/mdns" + tw "github.com/olekukonko/tablewriter" + "github.com/owncloud/ocis/konnectd/pkg/config" + "github.com/owncloud/ocis/konnectd/pkg/flagset" +) + +// PrintVersion prints the service versions of all running instances. +func PrintVersion(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "Print the versions of the running instances", + Flags: flagset.ListKonnectdWithConfig(cfg), + Action: func(c *cli.Context) error { + reg := mdns.NewRegistry() + services, err := reg.GetService(cfg.Service.Namespace + "." + cfg.Service.Name) + if err != nil { + fmt.Println(fmt.Errorf("could not get konnectd services from the registry: %v", err)) + return err + } + + if len(services) == 0 { + fmt.Println("No running konnectd service found.") + return nil + } + + table := tw.NewWriter(os.Stdout) + table.SetHeader([]string{"Version", "Address", "Id"}) + table.SetAutoFormatHeaders(false) + for _, s := range services { + for _, n := range s.Nodes { + table.Append([]string{s.Version, n.Address, n.Id}) + } + } + table.Render() + return nil + }, + } +} diff --git a/konnectd/pkg/config/config.go b/konnectd/pkg/config/config.go index c276cb6d76..e3b1e670ac 100644 --- a/konnectd/pkg/config/config.go +++ b/konnectd/pkg/config/config.go @@ -21,12 +21,18 @@ type Debug struct { // HTTP defines the available http configuration. type HTTP struct { - Addr string + Addr string + Root string + TLSCert string + TLSKey string + TLS bool +} + +// Service defines the available service configuration. +type Service struct { + Name string Namespace string - Root string - TLSCert string - TLSKey string - TLS bool + Version string } // Tracing defines the available tracing configuration. @@ -52,6 +58,7 @@ type Config struct { Tracing Tracing Asset Asset Konnectd bootstrap.Config + Service Service } // New initializes a new configuration with or without defaults. diff --git a/konnectd/pkg/flagset/flagset.go b/konnectd/pkg/flagset/flagset.go index 7f56fc744e..6542658a75 100644 --- a/konnectd/pkg/flagset/flagset.go +++ b/konnectd/pkg/flagset/flagset.go @@ -134,7 +134,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { Value: "com.owncloud.web", Usage: "Set the base namespace for service discovery", EnvVars: []string{"KONNECTD_HTTP_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, + Destination: &cfg.Service.Namespace, + }, + &cli.StringFlag{ + Name: "name", + Value: "konnectd", + Usage: "Service name", + EnvVars: []string{"KONNECTD_NAME"}, + Destination: &cfg.Service.Name, }, &cli.StringFlag{ Name: "identity-manager", @@ -307,3 +314,22 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { }, } } + +// ListKonnectdWithConfig applies the config to the list commands flags +func ListKonnectdWithConfig(cfg *config.Config) []cli.Flag { + return []cli.Flag{&cli.StringFlag{ + Name: "http-namespace", + Value: "com.owncloud.web", + Usage: "Set the base namespace for service discovery", + EnvVars: []string{"KONNECTD_HTTP_NAMESPACE"}, + Destination: &cfg.Service.Namespace, + }, + &cli.StringFlag{ + Name: "name", + Value: "konnectd", + Usage: "Service name", + EnvVars: []string{"KONNECTD_NAME"}, + Destination: &cfg.Service.Name, + }, + } +} diff --git a/konnectd/pkg/metrics/metrics.go b/konnectd/pkg/metrics/metrics.go index d79e27f6fd..8c658395e2 100644 --- a/konnectd/pkg/metrics/metrics.go +++ b/konnectd/pkg/metrics/metrics.go @@ -1,5 +1,7 @@ package metrics +import "github.com/prometheus/client_golang/prometheus" + var ( // Namespace defines the namespace for the defines metrics. Namespace = "ocis" @@ -11,6 +13,7 @@ var ( // Metrics defines the available metrics of this service. type Metrics struct { // Counter *prometheus.CounterVec + BuildInfo *prometheus.GaugeVec } // New initializes the available metrics. @@ -22,11 +25,21 @@ func New() *Metrics { // Name: "greet_total", // Help: "How many greeting requests processed", // }, []string{}), + BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: Namespace, + Subsystem: Subsystem, + Name: "build_info", + Help: "Build Information", + }, []string{"version"}), } // prometheus.Register( // m.Counter, // ) + _ = prometheus.Register( + m.BuildInfo, + ) + return m } diff --git a/konnectd/pkg/server/debug/server.go b/konnectd/pkg/server/debug/server.go index e0cec8cb93..d88639978b 100644 --- a/konnectd/pkg/server/debug/server.go +++ b/konnectd/pkg/server/debug/server.go @@ -5,7 +5,6 @@ import ( "net/http" "github.com/owncloud/ocis/konnectd/pkg/config" - "github.com/owncloud/ocis/konnectd/pkg/version" "github.com/owncloud/ocis/ocis-pkg/service/debug" ) @@ -15,8 +14,8 @@ func Server(opts ...Option) (*http.Server, error) { return debug.NewService( debug.Logger(options.Logger), - debug.Name("konnectd"), - debug.Version(version.String), + debug.Name(options.Config.Service.Name), + debug.Version(options.Config.Service.Version), debug.Address(options.Config.Debug.Addr), debug.Token(options.Config.Debug.Token), debug.Pprof(options.Config.Debug.Pprof), diff --git a/konnectd/pkg/server/http/server.go b/konnectd/pkg/server/http/server.go index f5f232d72a..1f302d3b58 100644 --- a/konnectd/pkg/server/http/server.go +++ b/konnectd/pkg/server/http/server.go @@ -6,7 +6,6 @@ import ( "github.com/owncloud/ocis/konnectd/pkg/crypto" svc "github.com/owncloud/ocis/konnectd/pkg/service/v0" - "github.com/owncloud/ocis/konnectd/pkg/version" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" ) @@ -44,9 +43,9 @@ func Server(opts ...Option) (http.Service, error) { service := http.NewService( http.Logger(options.Logger), - http.Namespace(options.Config.HTTP.Namespace), - http.Name("konnectd"), - http.Version(version.String), + http.Namespace(options.Config.Service.Namespace), + http.Name(options.Config.Service.Name), + http.Version(options.Config.Service.Version), http.Address(options.Config.HTTP.Addr), http.Context(options.Context), http.Flags(options.Flags...), @@ -63,8 +62,8 @@ func Server(opts ...Option) (http.Service, error) { middleware.Cors, middleware.Secure, middleware.Version( - "konnectd", - version.String, + options.Config.Service.Name, + options.Config.Service.Version, ), middleware.Logger( options.Logger, diff --git a/ocis/pkg/command/konnectd.go b/ocis/pkg/command/konnectd.go index ef37c30d3b..83cc601556 100644 --- a/ocis/pkg/command/konnectd.go +++ b/ocis/pkg/command/konnectd.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/konnectd/pkg/flagset" "github.com/owncloud/ocis/ocis/pkg/config" "github.com/owncloud/ocis/ocis/pkg/register" + "github.com/owncloud/ocis/ocis/pkg/version" ) // KonnectdCommand is the entrypoint for the konnectd command. @@ -16,6 +17,9 @@ func KonnectdCommand(cfg *config.Config) *cli.Command { Usage: "Start konnectd server", Category: "Extensions", Flags: flagset.ServerWithConfig(cfg.Konnectd), + Subcommands: []*cli.Command{ + command.PrintVersion(cfg.Konnectd), + }, Action: func(c *cli.Context) error { konnectdCommand := command.Server(configureKonnectd(cfg)) @@ -33,6 +37,7 @@ func configureKonnectd(cfg *config.Config) *svcconfig.Config { cfg.Konnectd.Log.Pretty = cfg.Log.Pretty cfg.Konnectd.Log.Color = cfg.Log.Color cfg.Konnectd.HTTP.TLS = false + cfg.Konnectd.Service.Version = version.String if cfg.Tracing.Enabled { cfg.Konnectd.Tracing.Enabled = cfg.Tracing.Enabled