Skip to content
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
49 changes: 33 additions & 16 deletions internal/flyetcd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"log"
"os"
"path/filepath"
"strconv"

yaml "gopkg.in/yaml.v3"
)

const (
var (
DataDir = "/data"
ConfigFilePath = "/data/etcd.yaml"
)

const (
MetricsBaseURL = "http://[::]:2381"
MetricsEndpoint = "http://[::]:2381/metrics"
)
Expand All @@ -36,15 +39,16 @@ type Config struct {
AutoCompactionRetention string `yaml:"auto-compaction-retention"`
AuthToken string `yaml:"auth-token"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding var handling for the aboves as well?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps separate PR if it matters. we have a more specific need right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added for AutoCompactionMode and AutoCompactionRetention, they could prove useful (:


MaxSnapshots int `yaml:"max-snapshots"`
MaxWals int `yaml:"max-wals"`
SnapshotCount int `yaml:"snapshot-count"`
MaxSnapshots int `yaml:"max-snapshots"`
MaxWals int `yaml:"max-wals"`
SnapshotCount int `yaml:"snapshot-count"`
QuotaBackendBytes int `yaml:"quota-backend-bytes"`
}

func NewConfig() (*Config, error) {
func DefaultConfig() *Config {
endpoint := NewEndpoint(os.Getenv("FLY_MACHINE_ID"))

cfg := &Config{
return &Config{
Name: endpoint.Name,
ListenPeerUrls: "http://[::]:2380",
ListenClientUrls: "http://[::]:2379",
Expand All @@ -65,8 +69,13 @@ func NewConfig() (*Config, error) {
AuthToken: "",
MaxSnapshots: 10,
MaxWals: 10,
SnapshotCount: 10000, // Default
SnapshotCount: 10000, // Default
QuotaBackendBytes: 2 * 1024 * 1024 * 1024, // 2GiB
}
}

func NewConfig() (*Config, error) {
cfg := DefaultConfig()

if err := cfg.SetAuthToken(); err != nil {
return nil, fmt.Errorf("failed to set auth token: %w", err)
Expand Down Expand Up @@ -127,18 +136,26 @@ func (c *Config) SetAuthToken() error {
return nil
}

func loadConfig() (*Config, error) {
var config Config
yamlFile, err := os.ReadFile(ConfigFilePath)
if err != nil {
return nil, err
func getEnvOrDefault[T string | int](key string, fallback T) T {
val, ok := os.LookupEnv(key)
if !ok {
return fallback
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return nil, err

var result any
switch any(fallback).(type) {
case string:
result = val
case int:
n, err := strconv.Atoi(val)
if err != nil {
log.Printf("invalid value for %s, using default: %v", key, err)
return fallback
}
result = n
}

return &config, nil
return result.(T)
}

func isJWTAuthEnabled() bool {
Expand Down
Loading
Loading