Merge pull request #613 from owncloud/konnectd-version-command

add version command and add build information to metrics
This commit is contained in:
David Christofas
2020-09-28 17:12:46 +02:00
committed by GitHub
10 changed files with 119 additions and 15 deletions

View File

@@ -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

View File

@@ -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),
},
}

View File

@@ -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),

View File

@@ -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
},
}
}

View File

@@ -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.

View File

@@ -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,
},
}
}

View File

@@ -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
}

View File

@@ -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),

View File

@@ -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,

View File

@@ -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