Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add config defaults #107

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *TLSConfig) RegisterFlags(fs *pflag.FlagSet, prefix string) {
fs.StringVar(
&c.RootCAs,
prefix+"root-cas",
"",
c.RootCAs,
`
A path to a certificate PEM file containing root certificiate authorities to
validate the TLS connection to the Piko server.
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *ConnectConfig) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.URL,
"connect.url",
"http://localhost:8001",
c.URL,
`
The Piko server URL to connect to. Note this must be configured to use the
Piko server 'upstream' port.`,
Expand All @@ -199,15 +199,15 @@ Piko server 'upstream' port.`,
fs.StringVar(
&c.Token,
"connect.token",
"",
c.Token,
`
Token is a token to authenticate with the Piko server.`,
)

fs.DurationVar(
&c.Timeout,
"connect.timeout",
time.Second*30,
c.Timeout,
`
Timeout attempting to connect to the Piko server on boot. Note if the agent
is disconnected after the initial connection succeeds it will keep trying to
Expand All @@ -233,7 +233,7 @@ func (c *ServerConfig) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.BindAddr,
"server.bind-addr",
":5000",
c.BindAddr,
`
The host/port to bind the server to.

Expand All @@ -257,6 +257,22 @@ type Config struct {
GracePeriod time.Duration `json:"grace_period" yaml:"grace_period"`
}

func Default() *Config {
return &Config{
Connect: ConnectConfig{
URL: "http://localhost:8001",
Timeout: time.Second * 30,
},
Server: ServerConfig{
BindAddr: ":5000",
},
Log: log.Config{
Level: "info",
},
GracePeriod: time.Minute,
}
}

func (c *Config) Validate() error {
// Note don't validate the number of listeners, as some commands don't
// require any.
Expand Down Expand Up @@ -296,7 +312,7 @@ func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.DurationVar(
&c.GracePeriod,
"grace-period",
time.Minute,
c.GracePeriod,
`
Maximum duration after a shutdown signal is received (SIGTERM or
SIGINT) to gracefully shutdown each listener.
Expand Down
10 changes: 5 additions & 5 deletions cli/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ Examples:
`,
}

var conf config.Config
conf := config.Default()
var loadConf pikoconfig.Config

// Register flags and set default values.
conf.RegisterFlags(cmd.PersistentFlags())
loadConf.RegisterFlags(cmd.PersistentFlags())

cmd.PersistentPreRun = func(_ *cobra.Command, _ []string) {
if err := loadConf.Load(&conf); err != nil {
if err := loadConf.Load(conf); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
Expand All @@ -87,9 +87,9 @@ Examples:
}
}

cmd.AddCommand(newStartCommand(&conf))
cmd.AddCommand(newHTTPCommand(&conf))
cmd.AddCommand(newTCPCommand(&conf))
cmd.AddCommand(newStartCommand(conf))
cmd.AddCommand(newHTTPCommand(conf))
cmd.AddCommand(newTCPCommand(conf))

return cmd
}
Expand Down
8 changes: 4 additions & 4 deletions cli/forward/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ Examples:
`,
}

var conf config.Config
conf := config.Default()
var loadConf pikoconfig.Config

// Register flags and set default values.
conf.RegisterFlags(cmd.PersistentFlags())
loadConf.RegisterFlags(cmd.PersistentFlags())

cmd.PersistentPreRun = func(_ *cobra.Command, _ []string) {
if err := loadConf.Load(&conf); err != nil {
if err := loadConf.Load(conf); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
Expand All @@ -61,8 +61,8 @@ Examples:
}
}

cmd.AddCommand(newStartCommand(&conf))
cmd.AddCommand(newTCPCommand(&conf))
cmd.AddCommand(newStartCommand(conf))
cmd.AddCommand(newTCPCommand(conf))

return cmd
}
Expand Down
6 changes: 3 additions & 3 deletions cli/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Examples:
`,
}

var conf config.Config
conf := config.Default()
var loadConf pikoconfig.Config

// Register flags and set default values.
Expand All @@ -81,7 +81,7 @@ Examples:
var logger log.Logger

cmd.PreRun = func(_ *cobra.Command, _ []string) {
if err := loadConf.Load(&conf); err != nil {
if err := loadConf.Load(conf); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ Examples:
}

cmd.Run = func(_ *cobra.Command, _ []string) {
if err := runServer(&conf, logger); err != nil {
if err := runServer(conf, logger); err != nil {
logger.Error("failed to run agent", zap.Error(err))
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/workload/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Examples:
`,
}

var conf config.RequestsConfig
conf := config.DefaultRequestsConfig()

// Register flags and set default values.
conf.RegisterFlags(cmd.Flags())
Expand All @@ -67,7 +67,7 @@ Examples:
os.Exit(1)
}

if err := runRequests(&conf, logger); err != nil {
if err := runRequests(conf, logger); err != nil {
logger.Error("failed to run server", zap.Error(err))
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/workload/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Examples:
`,
}

var conf config.UpstreamsConfig
conf := config.DefaultUpstreamsConfig()

// Register flags and set default values.
conf.RegisterFlags(cmd.Flags())
Expand All @@ -62,7 +62,7 @@ Examples:
os.Exit(1)
}

if err := runUpstreams(&conf, logger); err != nil {
if err := runUpstreams(conf, logger); err != nil {
logger.Error("failed to run server", zap.Error(err))
os.Exit(1)
}
Expand Down
15 changes: 12 additions & 3 deletions forward/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *TLSConfig) RegisterFlags(fs *pflag.FlagSet, prefix string) {
fs.StringVar(
&c.RootCAs,
prefix+"root-cas",
"",
c.RootCAs,
`
A path to a certificate PEM file containing root certificiate authorities to
validate the TLS connection to the Piko server.
Expand Down Expand Up @@ -125,7 +125,7 @@ func (c *ConnectConfig) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.URL,
"connect.url",
"http://localhost:8000",
c.URL,
`
The Piko server URL to connect to. Note this must be configured to use the
Piko server 'proxy' port.`,
Expand All @@ -134,7 +134,7 @@ Piko server 'proxy' port.`,
fs.DurationVar(
&c.Timeout,
"connect.timeout",
time.Second*30,
c.Timeout,
`
Timeout attempting to connect to the Piko server.`,
)
Expand All @@ -150,6 +150,15 @@ type Config struct {
Log log.Config `json:"log" yaml:"log"`
}

func Default() *Config {
return &Config{
Connect: ConnectConfig{
URL: "http://localhost:8000",
Timeout: time.Second * 30,
},
}
}

func (c *Config) Validate() error {
// Note don't validate the number of ports, as some commands don't
// require any.
Expand Down
8 changes: 4 additions & 4 deletions pkg/gossip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.BindAddr,
"gossip.bind-addr",
":8003",
c.BindAddr,
`
The host/port to listen for inter-node gossip traffic.

Expand All @@ -49,7 +49,7 @@ If the host is unspecified it defaults to all listeners, such as
fs.StringVar(
&c.AdvertiseAddr,
"gossip.advertise-addr",
"",
c.AdvertiseAddr,
`
Gossip listen address to advertise to other nodes in the cluster. This is the
address other nodes will used to gossip with the node.
Expand All @@ -66,7 +66,7 @@ advertise address of '10.26.104.14:8003'.`,
fs.DurationVar(
&c.Interval,
"gossip.interval",
time.Millisecond*500,
c.Interval,
`
The interval to initiate rounds of gossip.

Expand All @@ -76,7 +76,7 @@ Each gossip round selects another known node to synchronize with.`,
fs.IntVar(
&c.MaxPacketSize,
"gossip.max-packet-size",
1400,
c.MaxPacketSize,
`
The maximum size of any packet sent.

Expand Down
4 changes: 2 additions & 2 deletions pkg/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.Level,
"log.level",
"info",
c.Level,
`
Minimum log level to output.

Expand All @@ -39,7 +39,7 @@ The available levels are 'debug', 'info', 'warn' and 'error'.`,
fs.StringSliceVar(
&c.Subsystems,
"log.subsystems",
nil,
c.Subsystems,
`
Each log has a 'subsystem' field where the log occured.

Expand Down
10 changes: 5 additions & 5 deletions server/auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.TokenHMACSecretKey,
"auth.token-hmac-secret-key",
"",
c.TokenHMACSecretKey,
`
Secret key to authenticate HMAC endpoint connection JWTs.`,
)
fs.StringVar(
&c.TokenRSAPublicKey,
"auth.token-rsa-public-key",
"",
c.TokenRSAPublicKey,
`
Public key to authenticate RSA endpoint connection JWTs.`,
)
fs.StringVar(
&c.TokenECDSAPublicKey,
"auth.token-ecdsa-public-key",
"",
c.TokenECDSAPublicKey,
`
Public key to authenticate ECDSA endpoint connection JWTs.`,
)
fs.StringVar(
&c.TokenAudience,
"auth.token-audience",
"",
c.TokenAudience,
`
Audience of endpoint connection JWT token to verify.

Expand All @@ -67,7 +67,7 @@ is ignored.`,
fs.StringVar(
&c.TokenIssuer,
"auth.token-issuer",
"",
c.TokenIssuer,
`
Issuer of endpoint connection JWT token to verify.

Expand Down
Loading
Loading