consolidated configuration; 'zrok metrics' and 'zrok ctrl' share config (#269)

This commit is contained in:
Michael Quigley 2023-03-13 14:19:38 -04:00
parent ee01b0dc00
commit d54fefb0fe
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
22 changed files with 88 additions and 73 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller" "github.com/openziti/zrok/controller"
"github.com/openziti/zrok/controller/config"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -32,7 +33,7 @@ func newAdminBootstrap() *adminBootstrap {
func (cmd *adminBootstrap) run(_ *cobra.Command, args []string) { func (cmd *adminBootstrap) run(_ *cobra.Command, args []string) {
configPath := args[0] configPath := args[0]
inCfg, err := controller.LoadConfig(configPath) inCfg, err := config.LoadConfig(configPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller" "github.com/openziti/zrok/controller"
"github.com/openziti/zrok/controller/config"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,7 +28,7 @@ func newAdminGcCommand() *adminGcCommand {
} }
func (gc *adminGcCommand) run(_ *cobra.Command, args []string) { func (gc *adminGcCommand) run(_ *cobra.Command, args []string) {
cfg, err := controller.LoadConfig(args[0]) cfg, err := config.LoadConfig(args[0])
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller" "github.com/openziti/zrok/controller"
"github.com/openziti/zrok/controller/config"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -31,7 +32,7 @@ func newControllerCommand() *controllerCommand {
} }
func (cmd *controllerCommand) run(_ *cobra.Command, args []string) { func (cmd *controllerCommand) run(_ *cobra.Command, args []string) {
cfg, err := controller.LoadConfig(args[0]) cfg, err := config.LoadConfig(args[0])
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -2,7 +2,7 @@ package main
import ( import (
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller" "github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/tui" "github.com/openziti/zrok/tui"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -28,7 +28,7 @@ func newControllerValidateCommand() *controllerValidateCommand {
} }
func (cmd *controllerValidateCommand) run(_ *cobra.Command, args []string) { func (cmd *controllerValidateCommand) run(_ *cobra.Command, args []string) {
cfg, err := controller.LoadConfig(args[0]) cfg, err := config.LoadConfig(args[0])
if err != nil { if err != nil {
tui.Error("controller config validation failed", err) tui.Error("controller config validation failed", err)
} }

View File

@ -2,6 +2,8 @@ package main
import ( import (
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/env"
"github.com/openziti/zrok/controller/metrics" "github.com/openziti/zrok/controller/metrics"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -31,13 +33,13 @@ func newMetricsCommand() *metricsCommand {
} }
func (cmd *metricsCommand) run(_ *cobra.Command, args []string) { func (cmd *metricsCommand) run(_ *cobra.Command, args []string) {
cfg, err := metrics.LoadConfig(args[0]) cfg, err := config.LoadConfig(args[0])
if err != nil { if err != nil {
panic(err) panic(err)
} }
logrus.Infof(cf.Dump(cfg, metrics.GetCfOptions())) logrus.Infof(cf.Dump(cfg, env.GetCfOptions()))
ma, err := metrics.Run(cfg) ma, err := metrics.Run(cfg.Metrics, cfg.Store)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -12,7 +12,8 @@ import (
"github.com/openziti/edge/rest_model" "github.com/openziti/edge/rest_model"
rest_model_edge "github.com/openziti/edge/rest_model" rest_model_edge "github.com/openziti/edge/rest_model"
"github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti"
config2 "github.com/openziti/sdk-golang/ziti/config" ziti_config "github.com/openziti/sdk-golang/ziti/config"
zrok_config "github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
@ -22,7 +23,7 @@ import (
"time" "time"
) )
func Bootstrap(skipCtrl, skipFrontend bool, inCfg *Config) error { func Bootstrap(skipCtrl, skipFrontend bool, inCfg *zrok_config.Config) error {
cfg = inCfg cfg = inCfg
if v, err := store.Open(cfg.Store); err == nil { if v, err := store.Open(cfg.Store); err == nil {
@ -138,7 +139,7 @@ func getIdentityId(identityName string) (string, error) {
if err != nil { if err != nil {
return "", errors.Wrapf(err, "error opening identity '%v' from zrokdir", identityName) return "", errors.Wrapf(err, "error opening identity '%v' from zrokdir", identityName)
} }
zcfg, err := config2.NewFromFile(zif) zcfg, err := ziti_config.NewFromFile(zif)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "error loading ziti config from file '%v'", zif) return "", errors.Wrapf(err, "error loading ziti config from file '%v'", zif)
} }

View File

@ -1,7 +1,9 @@
package controller package config
import ( import (
"github.com/openziti/zrok/controller/env"
"github.com/openziti/zrok/controller/limits" "github.com/openziti/zrok/controller/limits"
"github.com/openziti/zrok/controller/metrics"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"time" "time"
@ -17,9 +19,9 @@ type Config struct {
Admin *AdminConfig Admin *AdminConfig
Endpoint *EndpointConfig Endpoint *EndpointConfig
Email *EmailConfig Email *EmailConfig
Influx *InfluxConfig
Limits *limits.Config Limits *limits.Config
Maintenance *MaintenanceConfig Maintenance *MaintenanceConfig
Metrics *metrics.Config
Registration *RegistrationConfig Registration *RegistrationConfig
ResetPassword *ResetPasswordConfig ResetPassword *ResetPasswordConfig
Store *store.Config Store *store.Config
@ -53,13 +55,6 @@ type ResetPasswordConfig struct {
ResetUrlTemplate string ResetUrlTemplate string
} }
type InfluxConfig struct {
Url string
Bucket string
Org string
Token string `cf:"+secret"`
}
type MaintenanceConfig struct { type MaintenanceConfig struct {
ResetPassword *ResetPasswordMaintenanceConfig ResetPassword *ResetPasswordMaintenanceConfig
Registration *RegistrationMaintenanceConfig Registration *RegistrationMaintenanceConfig
@ -97,7 +92,7 @@ func DefaultConfig() *Config {
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {
cfg := DefaultConfig() cfg := DefaultConfig()
if err := cf.BindYaml(cfg, path, cf.DefaultOptions()); err != nil { if err := cf.BindYaml(cfg, path, env.GetCfOptions()); err != nil {
return nil, errors.Wrapf(err, "error loading controller config '%v'", path) return nil, errors.Wrapf(err, "error loading controller config '%v'", path)
} }
if cfg.V != ConfigVersion { if cfg.V != ConfigVersion {

View File

@ -3,15 +3,16 @@ package controller
import ( import (
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/build" "github.com/openziti/zrok/build"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/metadata" "github.com/openziti/zrok/rest_server_zrok/operations/metadata"
) )
type configurationHandler struct { type configurationHandler struct {
cfg *Config cfg *config.Config
} }
func newConfigurationHandler(cfg *Config) *configurationHandler { func newConfigurationHandler(cfg *config.Config) *configurationHandler {
return &configurationHandler{ return &configurationHandler{
cfg: cfg, cfg: cfg,
} }

View File

@ -2,6 +2,8 @@ package controller
import ( import (
"context" "context"
"github.com/openziti/zrok/controller/config"
"github.com/sirupsen/logrus"
"github.com/go-openapi/loads" "github.com/go-openapi/loads"
influxdb2 "github.com/influxdata/influxdb-client-go/v2" influxdb2 "github.com/influxdata/influxdb-client-go/v2"
@ -13,11 +15,11 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var cfg *Config var cfg *config.Config
var str *store.Store var str *store.Store
var idb influxdb2.Client var idb influxdb2.Client
func Run(inCfg *Config) error { func Run(inCfg *config.Config) error {
cfg = inCfg cfg = inCfg
swaggerSpec, err := loads.Embedded(rest_server_zrok.SwaggerJSON, rest_server_zrok.FlatSwaggerJSON) swaggerSpec, err := loads.Embedded(rest_server_zrok.SwaggerJSON, rest_server_zrok.FlatSwaggerJSON)
@ -62,8 +64,10 @@ func Run(inCfg *Config) error {
return errors.Wrap(err, "error opening store") return errors.Wrap(err, "error opening store")
} }
if cfg.Influx != nil { if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
idb = influxdb2.NewClient(cfg.Influx.Url, cfg.Influx.Token) idb = influxdb2.NewClient(cfg.Metrics.Influx.Url, cfg.Metrics.Influx.Token)
} else {
logrus.Warn("skipping influx client; no configuration")
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())

14
controller/env/cf.go vendored Normal file
View File

@ -0,0 +1,14 @@
package env
import (
"github.com/michaelquigley/cf"
)
var cfOpts *cf.Options
func GetCfOptions() *cf.Options {
if cfOpts == nil {
cfOpts = cf.DefaultOptions()
}
return cfOpts
}

View File

@ -41,11 +41,13 @@ func (h *environmentDetailHandler) Handle(params metadata.GetEnvironmentDetailPa
return metadata.NewGetEnvironmentDetailInternalServerError() return metadata.NewGetEnvironmentDetailInternalServerError()
} }
var sparkData map[string][]int64 var sparkData map[string][]int64
if cfg.Influx != nil { if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
sparkData, err = sparkDataForShares(shrs) sparkData, err = sparkDataForShares(shrs)
if err != nil { if err != nil {
logrus.Errorf("error querying spark data for shares for user '%v': %v", principal.Email, err) logrus.Errorf("error querying spark data for shares for user '%v': %v", principal.Email, err)
} }
} else {
logrus.Debug("skipping spark data for shares; no influx configuration")
} }
for _, shr := range shrs { for _, shr := range shrs {
feEndpoint := "" feEndpoint := ""

View File

@ -8,6 +8,7 @@ import (
"github.com/openziti/edge/rest_management_api_client/service" "github.com/openziti/edge/rest_management_api_client/service"
"github.com/openziti/edge/rest_management_api_client/service_edge_router_policy" "github.com/openziti/edge/rest_management_api_client/service_edge_router_policy"
"github.com/openziti/edge/rest_management_api_client/service_policy" "github.com/openziti/edge/rest_management_api_client/service_policy"
zrok_config "github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -16,7 +17,7 @@ import (
"time" "time"
) )
func GC(inCfg *Config) error { func GC(inCfg *zrok_config.Config) error {
cfg = inCfg cfg = inCfg
if v, err := store.Open(cfg.Store); err == nil { if v, err := store.Open(cfg.Store); err == nil {
str = v str = v

View File

@ -2,6 +2,7 @@ package controller
import ( import (
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_server_zrok/operations/account" "github.com/openziti/zrok/rest_server_zrok/operations/account"
"github.com/openziti/zrok/util" "github.com/openziti/zrok/util"
@ -9,10 +10,10 @@ import (
) )
type inviteHandler struct { type inviteHandler struct {
cfg *Config cfg *config.Config
} }
func newInviteHandler(cfg *Config) *inviteHandler { func newInviteHandler(cfg *config.Config) *inviteHandler {
return &inviteHandler{ return &inviteHandler{
cfg: cfg, cfg: cfg,
} }

View File

@ -3,6 +3,7 @@ package controller
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/openziti/zrok/controller/config"
"strings" "strings"
"time" "time"
@ -11,11 +12,11 @@ import (
) )
type maintenanceRegistrationAgent struct { type maintenanceRegistrationAgent struct {
cfg *RegistrationMaintenanceConfig cfg *config.RegistrationMaintenanceConfig
ctx context.Context ctx context.Context
} }
func newRegistrationMaintenanceAgent(ctx context.Context, cfg *RegistrationMaintenanceConfig) *maintenanceRegistrationAgent { func newRegistrationMaintenanceAgent(ctx context.Context, cfg *config.RegistrationMaintenanceConfig) *maintenanceRegistrationAgent {
return &maintenanceRegistrationAgent{ return &maintenanceRegistrationAgent{
cfg: cfg, cfg: cfg,
ctx: ctx, ctx: ctx,
@ -78,11 +79,11 @@ func (ma *maintenanceRegistrationAgent) deleteExpiredAccountRequests() error {
} }
type maintenanceResetPasswordAgent struct { type maintenanceResetPasswordAgent struct {
cfg *ResetPasswordMaintenanceConfig cfg *config.ResetPasswordMaintenanceConfig
ctx context.Context ctx context.Context
} }
func newMaintenanceResetPasswordAgent(ctx context.Context, cfg *ResetPasswordMaintenanceConfig) *maintenanceResetPasswordAgent { func newMaintenanceResetPasswordAgent(ctx context.Context, cfg *config.ResetPasswordMaintenanceConfig) *maintenanceResetPasswordAgent {
return &maintenanceResetPasswordAgent{ return &maintenanceResetPasswordAgent{
cfg: cfg, cfg: cfg,
ctx: ctx, ctx: ctx,

View File

@ -1,6 +1,7 @@
package metrics package metrics
import ( import (
"github.com/openziti/zrok/controller/store"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -11,24 +12,21 @@ type MetricsAgent struct {
join chan struct{} join chan struct{}
} }
func Run(cfg *Config) (*MetricsAgent, error) { func Run(cfg *Config, strCfg *store.Config) (*MetricsAgent, error) {
logrus.Info("starting") logrus.Info("starting")
if cfg.Store == nil { cache, err := newShareCache(strCfg)
return nil, errors.New("no 'store' configured; exiting")
}
cache, err := newShareCache(cfg.Store)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error creating share cache") return nil, errors.Wrap(err, "error creating share cache")
} }
if cfg.Source == nil { if cfg.Strategies == nil || cfg.Strategies.Source == nil {
return nil, errors.New("no 'source' configured; exiting") return nil, errors.New("no 'strategies/source' configured; exiting")
} }
src, ok := cfg.Source.(Source) src, ok := cfg.Strategies.Source.(Source)
if !ok { if !ok {
return nil, errors.New("invalid 'source'; exiting") return nil, errors.New("invalid 'strategies/source'; exiting")
} }
if cfg.Influx == nil { if cfg.Influx == nil {

View File

@ -1,10 +0,0 @@
package metrics
import "github.com/michaelquigley/cf"
func GetCfOptions() *cf.Options {
opts := cf.DefaultOptions()
opts.AddFlexibleSetter("file", loadFileSourceConfig)
opts.AddFlexibleSetter("websocket", loadWebsocketSourceConfig)
return opts
}

View File

@ -1,15 +1,8 @@
package metrics package metrics
import (
"github.com/michaelquigley/cf"
"github.com/openziti/zrok/controller/store"
"github.com/pkg/errors"
)
type Config struct { type Config struct {
Source interface{}
Influx *InfluxConfig Influx *InfluxConfig
Store *store.Config Strategies *StrategiesConfig
} }
type InfluxConfig struct { type InfluxConfig struct {
@ -19,10 +12,6 @@ type InfluxConfig struct {
Token string `cf:"+secret"` Token string `cf:"+secret"`
} }
func LoadConfig(path string) (*Config, error) { type StrategiesConfig struct {
cfg := &Config{} Source interface{}
if err := cf.BindYaml(cfg, path, GetCfOptions()); err != nil {
return nil, errors.Wrapf(err, "error loading config from '%v'", path)
}
return cfg, nil
} }

View File

@ -5,11 +5,16 @@ import (
"encoding/json" "encoding/json"
"github.com/michaelquigley/cf" "github.com/michaelquigley/cf"
"github.com/nxadm/tail" "github.com/nxadm/tail"
"github.com/openziti/zrok/controller/env"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"os" "os"
) )
func init() {
env.GetCfOptions().AddFlexibleSetter("file", loadFileSourceConfig)
}
type FileSourceConfig struct { type FileSourceConfig struct {
Path string Path string
IndexPath string IndexPath string

View File

@ -14,6 +14,7 @@ import (
"github.com/openziti/fabric/pb/mgmt_pb" "github.com/openziti/fabric/pb/mgmt_pb"
"github.com/openziti/identity" "github.com/openziti/identity"
"github.com/openziti/sdk-golang/ziti/constants" "github.com/openziti/sdk-golang/ziti/constants"
"github.com/openziti/zrok/controller/env"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"io" "io"
@ -22,6 +23,10 @@ import (
"time" "time"
) )
func init() {
env.GetCfOptions().AddFlexibleSetter("websocket", loadWebsocketSourceConfig)
}
type WebsocketSourceConfig struct { type WebsocketSourceConfig struct {
WebsocketEndpoint string WebsocketEndpoint string
ApiEndpoint string ApiEndpoint string

View File

@ -43,12 +43,14 @@ func (h *shareDetailHandler) Handle(params metadata.GetShareDetailParams, princi
return metadata.NewGetShareDetailNotFound() return metadata.NewGetShareDetailNotFound()
} }
var sparkData map[string][]int64 var sparkData map[string][]int64
if cfg.Influx != nil { if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
sparkData, err = sparkDataForShares([]*store.Share{shr}) sparkData, err = sparkDataForShares([]*store.Share{shr})
logrus.Info(sparkData) logrus.Info(sparkData)
if err != nil { if err != nil {
logrus.Errorf("error querying spark data for share: %v", err) logrus.Errorf("error querying spark data for share: %v", err)
} }
} else {
logrus.Debug("skipping spark data; no influx configuration")
} }
feEndpoint := "" feEndpoint := ""
if shr.FrontendEndpoint != nil { if shr.FrontendEndpoint != nil {

View File

@ -10,7 +10,7 @@ func sparkDataForShares(shrs []*store.Share) (map[string][]int64, error) {
out := make(map[string][]int64) out := make(map[string][]int64)
if len(shrs) > 0 { if len(shrs) > 0 {
qapi := idb.QueryAPI(cfg.Influx.Org) qapi := idb.QueryAPI(cfg.Metrics.Influx.Org)
result, err := qapi.Query(context.Background(), sparkFluxQuery(shrs)) result, err := qapi.Query(context.Background(), sparkFluxQuery(shrs))
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package controller
import ( import (
errors2 "github.com/go-openapi/errors" errors2 "github.com/go-openapi/errors"
"github.com/jaevor/go-nanoid" "github.com/jaevor/go-nanoid"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"net/http" "net/http"
@ -10,10 +11,10 @@ import (
) )
type zrokAuthenticator struct { type zrokAuthenticator struct {
cfg *Config cfg *config.Config
} }
func newZrokAuthenticator(cfg *Config) *zrokAuthenticator { func newZrokAuthenticator(cfg *config.Config) *zrokAuthenticator {
return &zrokAuthenticator{cfg} return &zrokAuthenticator{cfg}
} }