diff --git a/cmd/zrok/adminBootstrap.go b/cmd/zrok/adminBootstrap.go index 878e5296..383b0620 100644 --- a/cmd/zrok/adminBootstrap.go +++ b/cmd/zrok/adminBootstrap.go @@ -3,6 +3,7 @@ package main import ( "github.com/michaelquigley/cf" "github.com/openziti/zrok/controller" + "github.com/openziti/zrok/controller/config" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -32,7 +33,7 @@ func newAdminBootstrap() *adminBootstrap { func (cmd *adminBootstrap) run(_ *cobra.Command, args []string) { configPath := args[0] - inCfg, err := controller.LoadConfig(configPath) + inCfg, err := config.LoadConfig(configPath) if err != nil { panic(err) } diff --git a/cmd/zrok/adminGc.go b/cmd/zrok/adminGc.go index ff597ec7..4179d558 100644 --- a/cmd/zrok/adminGc.go +++ b/cmd/zrok/adminGc.go @@ -3,6 +3,7 @@ package main import ( "github.com/michaelquigley/cf" "github.com/openziti/zrok/controller" + "github.com/openziti/zrok/controller/config" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -27,7 +28,7 @@ func newAdminGcCommand() *adminGcCommand { } func (gc *adminGcCommand) run(_ *cobra.Command, args []string) { - cfg, err := controller.LoadConfig(args[0]) + cfg, err := config.LoadConfig(args[0]) if err != nil { panic(err) } diff --git a/cmd/zrok/controller.go b/cmd/zrok/controller.go index 6e498797..68250dd4 100644 --- a/cmd/zrok/controller.go +++ b/cmd/zrok/controller.go @@ -3,6 +3,7 @@ package main import ( "github.com/michaelquigley/cf" "github.com/openziti/zrok/controller" + "github.com/openziti/zrok/controller/config" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,7 +32,7 @@ func newControllerCommand() *controllerCommand { } func (cmd *controllerCommand) run(_ *cobra.Command, args []string) { - cfg, err := controller.LoadConfig(args[0]) + cfg, err := config.LoadConfig(args[0]) if err != nil { panic(err) } diff --git a/cmd/zrok/controllerValidate.go b/cmd/zrok/controllerValidate.go index a09d106f..bf632c29 100644 --- a/cmd/zrok/controllerValidate.go +++ b/cmd/zrok/controllerValidate.go @@ -2,7 +2,7 @@ package main import ( "github.com/michaelquigley/cf" - "github.com/openziti/zrok/controller" + "github.com/openziti/zrok/controller/config" "github.com/openziti/zrok/tui" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -28,7 +28,7 @@ func newControllerValidateCommand() *controllerValidateCommand { } func (cmd *controllerValidateCommand) run(_ *cobra.Command, args []string) { - cfg, err := controller.LoadConfig(args[0]) + cfg, err := config.LoadConfig(args[0]) if err != nil { tui.Error("controller config validation failed", err) } diff --git a/cmd/zrok/metrics.go b/cmd/zrok/metrics.go index 4001c224..fb482276 100644 --- a/cmd/zrok/metrics.go +++ b/cmd/zrok/metrics.go @@ -2,6 +2,8 @@ package main import ( "github.com/michaelquigley/cf" + "github.com/openziti/zrok/controller/config" + "github.com/openziti/zrok/controller/env" "github.com/openziti/zrok/controller/metrics" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -31,13 +33,13 @@ func newMetricsCommand() *metricsCommand { } func (cmd *metricsCommand) run(_ *cobra.Command, args []string) { - cfg, err := metrics.LoadConfig(args[0]) + cfg, err := config.LoadConfig(args[0]) if err != nil { 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 { panic(err) } diff --git a/controller/bootstrap.go b/controller/bootstrap.go index 729ce523..b413263d 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -12,7 +12,8 @@ import ( "github.com/openziti/edge/rest_model" rest_model_edge "github.com/openziti/edge/rest_model" "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/zrokEdgeSdk" "github.com/openziti/zrok/model" @@ -22,7 +23,7 @@ import ( "time" ) -func Bootstrap(skipCtrl, skipFrontend bool, inCfg *Config) error { +func Bootstrap(skipCtrl, skipFrontend bool, inCfg *zrok_config.Config) error { cfg = inCfg if v, err := store.Open(cfg.Store); err == nil { @@ -138,7 +139,7 @@ func getIdentityId(identityName string) (string, error) { if err != nil { 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 { return "", errors.Wrapf(err, "error loading ziti config from file '%v'", zif) } diff --git a/controller/config.go b/controller/config/config.go similarity index 90% rename from controller/config.go rename to controller/config/config.go index 7be959f0..cdc7c2b7 100644 --- a/controller/config.go +++ b/controller/config/config.go @@ -1,7 +1,9 @@ -package controller +package config import ( + "github.com/openziti/zrok/controller/env" "github.com/openziti/zrok/controller/limits" + "github.com/openziti/zrok/controller/metrics" "github.com/openziti/zrok/controller/zrokEdgeSdk" "time" @@ -17,9 +19,9 @@ type Config struct { Admin *AdminConfig Endpoint *EndpointConfig Email *EmailConfig - Influx *InfluxConfig Limits *limits.Config Maintenance *MaintenanceConfig + Metrics *metrics.Config Registration *RegistrationConfig ResetPassword *ResetPasswordConfig Store *store.Config @@ -53,13 +55,6 @@ type ResetPasswordConfig struct { ResetUrlTemplate string } -type InfluxConfig struct { - Url string - Bucket string - Org string - Token string `cf:"+secret"` -} - type MaintenanceConfig struct { ResetPassword *ResetPasswordMaintenanceConfig Registration *RegistrationMaintenanceConfig @@ -97,7 +92,7 @@ func DefaultConfig() *Config { func LoadConfig(path string) (*Config, error) { 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) } if cfg.V != ConfigVersion { diff --git a/controller/configuration.go b/controller/configuration.go index 89071813..1cc10cfe 100644 --- a/controller/configuration.go +++ b/controller/configuration.go @@ -3,15 +3,16 @@ package controller import ( "github.com/go-openapi/runtime/middleware" "github.com/openziti/zrok/build" + "github.com/openziti/zrok/controller/config" "github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_server_zrok/operations/metadata" ) type configurationHandler struct { - cfg *Config + cfg *config.Config } -func newConfigurationHandler(cfg *Config) *configurationHandler { +func newConfigurationHandler(cfg *config.Config) *configurationHandler { return &configurationHandler{ cfg: cfg, } diff --git a/controller/controller.go b/controller/controller.go index 6d5db1a3..f5e2c79a 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -2,6 +2,8 @@ package controller import ( "context" + "github.com/openziti/zrok/controller/config" + "github.com/sirupsen/logrus" "github.com/go-openapi/loads" influxdb2 "github.com/influxdata/influxdb-client-go/v2" @@ -13,11 +15,11 @@ import ( "github.com/pkg/errors" ) -var cfg *Config +var cfg *config.Config var str *store.Store var idb influxdb2.Client -func Run(inCfg *Config) error { +func Run(inCfg *config.Config) error { cfg = inCfg 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") } - if cfg.Influx != nil { - idb = influxdb2.NewClient(cfg.Influx.Url, cfg.Influx.Token) + if cfg.Metrics != nil && cfg.Metrics.Influx != nil { + 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()) diff --git a/controller/env/cf.go b/controller/env/cf.go new file mode 100644 index 00000000..e0fb0cf5 --- /dev/null +++ b/controller/env/cf.go @@ -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 +} diff --git a/controller/environmentDetail.go b/controller/environmentDetail.go index 4b9b5331..145947c4 100644 --- a/controller/environmentDetail.go +++ b/controller/environmentDetail.go @@ -41,11 +41,13 @@ func (h *environmentDetailHandler) Handle(params metadata.GetEnvironmentDetailPa return metadata.NewGetEnvironmentDetailInternalServerError() } var sparkData map[string][]int64 - if cfg.Influx != nil { + if cfg.Metrics != nil && cfg.Metrics.Influx != nil { sparkData, err = sparkDataForShares(shrs) if err != nil { 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 { feEndpoint := "" diff --git a/controller/gc.go b/controller/gc.go index ad58d89c..e0df53b9 100644 --- a/controller/gc.go +++ b/controller/gc.go @@ -8,6 +8,7 @@ import ( "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_policy" + zrok_config "github.com/openziti/zrok/controller/config" "github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/pkg/errors" @@ -16,7 +17,7 @@ import ( "time" ) -func GC(inCfg *Config) error { +func GC(inCfg *zrok_config.Config) error { cfg = inCfg if v, err := store.Open(cfg.Store); err == nil { str = v diff --git a/controller/invite.go b/controller/invite.go index 2bcc207c..ab2f80f0 100644 --- a/controller/invite.go +++ b/controller/invite.go @@ -2,6 +2,7 @@ package controller import ( "github.com/go-openapi/runtime/middleware" + "github.com/openziti/zrok/controller/config" "github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/rest_server_zrok/operations/account" "github.com/openziti/zrok/util" @@ -9,10 +10,10 @@ import ( ) type inviteHandler struct { - cfg *Config + cfg *config.Config } -func newInviteHandler(cfg *Config) *inviteHandler { +func newInviteHandler(cfg *config.Config) *inviteHandler { return &inviteHandler{ cfg: cfg, } diff --git a/controller/maintenance.go b/controller/maintenance.go index 1076b9e8..e9039b7b 100644 --- a/controller/maintenance.go +++ b/controller/maintenance.go @@ -3,6 +3,7 @@ package controller import ( "context" "fmt" + "github.com/openziti/zrok/controller/config" "strings" "time" @@ -11,11 +12,11 @@ import ( ) type maintenanceRegistrationAgent struct { - cfg *RegistrationMaintenanceConfig + cfg *config.RegistrationMaintenanceConfig ctx context.Context } -func newRegistrationMaintenanceAgent(ctx context.Context, cfg *RegistrationMaintenanceConfig) *maintenanceRegistrationAgent { +func newRegistrationMaintenanceAgent(ctx context.Context, cfg *config.RegistrationMaintenanceConfig) *maintenanceRegistrationAgent { return &maintenanceRegistrationAgent{ cfg: cfg, ctx: ctx, @@ -78,11 +79,11 @@ func (ma *maintenanceRegistrationAgent) deleteExpiredAccountRequests() error { } type maintenanceResetPasswordAgent struct { - cfg *ResetPasswordMaintenanceConfig + cfg *config.ResetPasswordMaintenanceConfig ctx context.Context } -func newMaintenanceResetPasswordAgent(ctx context.Context, cfg *ResetPasswordMaintenanceConfig) *maintenanceResetPasswordAgent { +func newMaintenanceResetPasswordAgent(ctx context.Context, cfg *config.ResetPasswordMaintenanceConfig) *maintenanceResetPasswordAgent { return &maintenanceResetPasswordAgent{ cfg: cfg, ctx: ctx, diff --git a/controller/metrics/agent.go b/controller/metrics/agent.go index f6036e16..b01ddc44 100644 --- a/controller/metrics/agent.go +++ b/controller/metrics/agent.go @@ -1,6 +1,7 @@ package metrics import ( + "github.com/openziti/zrok/controller/store" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -11,24 +12,21 @@ type MetricsAgent struct { join chan struct{} } -func Run(cfg *Config) (*MetricsAgent, error) { +func Run(cfg *Config, strCfg *store.Config) (*MetricsAgent, error) { logrus.Info("starting") - if cfg.Store == nil { - return nil, errors.New("no 'store' configured; exiting") - } - cache, err := newShareCache(cfg.Store) + cache, err := newShareCache(strCfg) if err != nil { return nil, errors.Wrap(err, "error creating share cache") } - if cfg.Source == nil { - return nil, errors.New("no 'source' configured; exiting") + if cfg.Strategies == nil || cfg.Strategies.Source == nil { + return nil, errors.New("no 'strategies/source' configured; exiting") } - src, ok := cfg.Source.(Source) + src, ok := cfg.Strategies.Source.(Source) if !ok { - return nil, errors.New("invalid 'source'; exiting") + return nil, errors.New("invalid 'strategies/source'; exiting") } if cfg.Influx == nil { diff --git a/controller/metrics/cf.go b/controller/metrics/cf.go deleted file mode 100644 index e68ae9c4..00000000 --- a/controller/metrics/cf.go +++ /dev/null @@ -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 -} diff --git a/controller/metrics/config.go b/controller/metrics/config.go index 4f08f5a1..4cdafcbb 100644 --- a/controller/metrics/config.go +++ b/controller/metrics/config.go @@ -1,15 +1,8 @@ package metrics -import ( - "github.com/michaelquigley/cf" - "github.com/openziti/zrok/controller/store" - "github.com/pkg/errors" -) - type Config struct { - Source interface{} - Influx *InfluxConfig - Store *store.Config + Influx *InfluxConfig + Strategies *StrategiesConfig } type InfluxConfig struct { @@ -19,10 +12,6 @@ type InfluxConfig struct { Token string `cf:"+secret"` } -func LoadConfig(path string) (*Config, error) { - cfg := &Config{} - if err := cf.BindYaml(cfg, path, GetCfOptions()); err != nil { - return nil, errors.Wrapf(err, "error loading config from '%v'", path) - } - return cfg, nil +type StrategiesConfig struct { + Source interface{} } diff --git a/controller/metrics/fileSource.go b/controller/metrics/fileSource.go index b85bd770..67007552 100644 --- a/controller/metrics/fileSource.go +++ b/controller/metrics/fileSource.go @@ -5,11 +5,16 @@ import ( "encoding/json" "github.com/michaelquigley/cf" "github.com/nxadm/tail" + "github.com/openziti/zrok/controller/env" "github.com/pkg/errors" "github.com/sirupsen/logrus" "os" ) +func init() { + env.GetCfOptions().AddFlexibleSetter("file", loadFileSourceConfig) +} + type FileSourceConfig struct { Path string IndexPath string diff --git a/controller/metrics/websocketSource.go b/controller/metrics/websocketSource.go index 4e757c3b..3b1b2898 100644 --- a/controller/metrics/websocketSource.go +++ b/controller/metrics/websocketSource.go @@ -14,6 +14,7 @@ import ( "github.com/openziti/fabric/pb/mgmt_pb" "github.com/openziti/identity" "github.com/openziti/sdk-golang/ziti/constants" + "github.com/openziti/zrok/controller/env" "github.com/pkg/errors" "github.com/sirupsen/logrus" "io" @@ -22,6 +23,10 @@ import ( "time" ) +func init() { + env.GetCfOptions().AddFlexibleSetter("websocket", loadWebsocketSourceConfig) +} + type WebsocketSourceConfig struct { WebsocketEndpoint string ApiEndpoint string diff --git a/controller/shareDetail.go b/controller/shareDetail.go index 453d0eb1..dbccdc1b 100644 --- a/controller/shareDetail.go +++ b/controller/shareDetail.go @@ -43,12 +43,14 @@ func (h *shareDetailHandler) Handle(params metadata.GetShareDetailParams, princi return metadata.NewGetShareDetailNotFound() } var sparkData map[string][]int64 - if cfg.Influx != nil { + if cfg.Metrics != nil && cfg.Metrics.Influx != nil { sparkData, err = sparkDataForShares([]*store.Share{shr}) logrus.Info(sparkData) if err != nil { logrus.Errorf("error querying spark data for share: %v", err) } + } else { + logrus.Debug("skipping spark data; no influx configuration") } feEndpoint := "" if shr.FrontendEndpoint != nil { diff --git a/controller/sparkData.go b/controller/sparkData.go index 60cce565..bc793660 100644 --- a/controller/sparkData.go +++ b/controller/sparkData.go @@ -10,7 +10,7 @@ func sparkDataForShares(shrs []*store.Share) (map[string][]int64, error) { out := make(map[string][]int64) 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)) if err != nil { diff --git a/controller/util.go b/controller/util.go index 59ee7340..68e55a6c 100644 --- a/controller/util.go +++ b/controller/util.go @@ -3,6 +3,7 @@ package controller import ( errors2 "github.com/go-openapi/errors" "github.com/jaevor/go-nanoid" + "github.com/openziti/zrok/controller/config" "github.com/openziti/zrok/rest_model_zrok" "github.com/sirupsen/logrus" "net/http" @@ -10,10 +11,10 @@ import ( ) type zrokAuthenticator struct { - cfg *Config + cfg *config.Config } -func newZrokAuthenticator(cfg *Config) *zrokAuthenticator { +func newZrokAuthenticator(cfg *config.Config) *zrokAuthenticator { return &zrokAuthenticator{cfg} }