Skip to content

Commit

Permalink
Add err return in external provider init to avoid panic on boot. Brea…
Browse files Browse the repository at this point in the history
…king change. (#234, #233)

The breaking change in the external provider init will require the providers in the PR to get a `v2` tag.
  • Loading branch information
missedone authored Sep 23, 2023
1 parent 4e88645 commit f4d349a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 29 deletions.
25 changes: 20 additions & 5 deletions examples/complex-etcd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func main() {
Prefix: false,
Key: "single_key",
}
provider := etcd.Provider(providerCfg)
provider, err := etcd.Provider(providerCfg)
if err != nil {
log.Fatalf("Failed to instantiate etcd provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -109,7 +112,10 @@ func main() {
Prefix: true,
Key: "parent",
}
provider = etcd.Provider(providerCfg)
provider, err = etcd.Provider(providerCfg)
if err != nil {
log.Fatalf("Failed to instantiate etcd provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -154,7 +160,10 @@ func main() {
Key: "child",
}

provider = etcd.Provider(providerCfg)
provider, err = etcd.Provider(providerCfg)
if err != nil {
log.Fatalf("Failed to instantiate etcd provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -201,7 +210,10 @@ func main() {
Key: "child",
}

provider = etcd.Provider(providerCfg)
provider, err = etcd.Provider(providerCfg)
if err != nil {
log.Fatalf("Failed to instantiate etcd provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -241,7 +253,10 @@ func main() {
Key: "child",
}

provider = etcd.Provider(providerCfg)
provider, err = etcd.Provider(providerCfg)
if err != nil {
log.Fatalf("Failed to instantiate etcd provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down
6 changes: 5 additions & 1 deletion examples/read-appconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import (
var k = koanf.New(".")

func main() {
provider := appconfig.Provider(appconfig.Config{
provider, err := appconfig.Provider(appconfig.Config{
Application: os.Getenv("AWS_APPCONFIG_APPLICATION"),
ClientID: os.Getenv("AWS_APPCONFIG_CLIENT_ID"),
Configuration: os.Getenv("AWS_APPCONFIG_CONFIG_NAME"),
Environment: os.Getenv("AWS_APPCONFIG_ENVIRONMENT"),
})
if err != nil {
log.Fatalf("Failed to instantiate appconfig provider: %v", err)
}

// Load the provider and parse configuration as JSON.
if err := k.Load(provider, json.Parser()); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down
30 changes: 24 additions & 6 deletions examples/read-consul/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ func main() {
log.Printf("Couldn't put key.")
}

provider := consul.Provider(consul.Config{
provider, err := consul.Provider(consul.Config{
Key: sKey,
Recurse: false,
Detailed: false,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -91,12 +94,15 @@ func main() {
log.Fatalf("error loading config: %v", err)
}

provider = consul.Provider(consul.Config{
provider, err = consul.Provider(consul.Config{
Key: "parent",
Recurse: true,
Detailed: false,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -134,12 +140,15 @@ func main() {
log.Fatalf("error loading config: %v", err)
}

provider = consul.Provider(consul.Config{
provider, err = consul.Provider(consul.Config{
Key: "child",
Recurse: true,
Detailed: false,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down Expand Up @@ -181,12 +190,15 @@ func main() {
sFlags := uint64(42)
sVal = "father"

provider = consul.Provider(consul.Config{
provider, err = consul.Provider(consul.Config{
Key: sKey,
Recurse: false,
Detailed: true,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
fmt.Printf("error loading config: %v", err)
Expand All @@ -213,12 +225,15 @@ func main() {

sKey = "parent"

provider = consul.Provider(consul.Config{
provider, err = consul.Provider(consul.Config{
Key: sKey,
Recurse: true,
Detailed: true,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

if err := kCheck.Load(provider, nil); err != nil {
fmt.Printf("error loading config: %v", err)
Expand Down Expand Up @@ -255,12 +270,15 @@ func main() {
// Watch test

sKey = "parent"
provider = consul.Provider(consul.Config{
provider, err = consul.Provider(consul.Config{
Key: sKey,
Recurse: true,
Detailed: false,
Cfg: api.DefaultConfig(),
})
if err != nil {
log.Fatalf("Failed to instantiate consul provider: %v", err)
}

// Getting the old value
kCheck.Load(provider, nil)
Expand Down
5 changes: 4 additions & 1 deletion examples/read-vault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var k = koanf.New(".")

func main() {
provider := vault.Provider(vault.Config{
provider, err := vault.Provider(vault.Config{
Address: os.Getenv("VAULT_ADDRESS"),
Token: os.Getenv("VAULT_TOKEN"),
Path: "secret/data/my-app",
Expand All @@ -25,6 +25,9 @@ func main() {
// k.String("data.YOUR_KEY") etc. instead of k.String("YOUR_KEY").
ExcludeMeta: true,
})
if err != nil {
log.Fatalf("Failed to instantiate vault provider: %v", err)
}
// Load mapped config from Vault storage.
if err := k.Load(provider, nil); err != nil {
log.Fatalf("error loading config: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions providers/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type AppConfig struct {
}

// Provider returns an AWS AppConfig provider.
func Provider(cfg Config) *AppConfig {
func Provider(cfg Config) (*AppConfig, error) {
c, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil
return nil, err
}

if cfg.AWSRegion != "" {
Expand All @@ -89,7 +89,7 @@ func Provider(cfg Config) *AppConfig {
}
client := appconfig.NewFromConfig(c)

return &AppConfig{client: client, config: cfg}
return &AppConfig{client: client, config: cfg}, nil
}

// ProviderWithClient returns an AWS AppConfig provider
Expand Down
6 changes: 3 additions & 3 deletions providers/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ type Consul struct {
}

// Provider returns an instance of the Consul provider.
func Provider(cfg Config) *Consul {
func Provider(cfg Config) (*Consul, error) {
c, err := api.NewClient(cfg.Cfg)
if err != nil {
return nil
return nil, err
}

return &Consul{client: c, cfg: cfg}
return &Consul{client: c, cfg: cfg}, nil
}

// ReadBytes is not supported by the Consul provider.
Expand Down
6 changes: 3 additions & 3 deletions providers/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ type Etcd struct {
}

// Provider returns a provider that takes etcd config.
func Provider(cfg Config) *Etcd {
func Provider(cfg Config) (*Etcd, error) {
eCfg := clientv3.Config{
Endpoints: cfg.Endpoints,
DialTimeout: cfg.DialTimeout,
}

c, err := clientv3.New(eCfg)
if err != nil {
return nil
return nil, err
}

return &Etcd{client: c, cfg: cfg}
return &Etcd{client: c, cfg: cfg}, nil
}

// ReadBytes is not supported by etcd provider.
Expand Down
6 changes: 3 additions & 3 deletions providers/parameterstore/parameterstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ type ParameterStore[T Input] struct {
// - AWS_ACCESS_KEY_ID
// - AWS_SECRET_ACCESS_KEY
// - AWS_SESSION_TOKEN
func Provider[T Input](config Config[T]) *ParameterStore[T] {
func Provider[T Input](config Config[T]) (*ParameterStore[T], error) {
c, err := awsconfig.LoadDefaultConfig(context.TODO())
if err != nil {
return nil
return nil, err
}
return ProviderWithClient[T](config, ssm.NewFromConfig(c))
return ProviderWithClient[T](config, ssm.NewFromConfig(c)), nil
}

// ProviderWithClient returns a ParameterStore provider
Expand Down
8 changes: 4 additions & 4 deletions providers/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ type Vault struct {
}

// Provider returns a provider that takes a Vault config.
func Provider(cfg Config) *Vault {
func Provider(cfg Config) (*Vault, error) {
httpClient := &http.Client{Timeout: cfg.Timeout, Transport: cfg.Transport}
client, err := api.NewClient(&api.Config{Address: cfg.Address, HttpClient: httpClient})
if err != nil {
return nil
return nil, err
}
if cfg.AuthMethod != nil {
if _, err := client.Auth().Login(context.Background(), cfg.AuthMethod); err != nil {
return nil
return nil, err
}
} else {
client.SetToken(cfg.Token)
}

return &Vault{client: client, cfg: cfg}
return &Vault{client: client, cfg: cfg}, nil
}

// ReadBytes is not supported by the vault provider.
Expand Down

0 comments on commit f4d349a

Please sign in to comment.