Skip to content

Commit

Permalink
Move Config to separate package (part of #47)
Browse files Browse the repository at this point in the history
This is a prerequisite for adding provider config validation since we can't have circular imports.
  • Loading branch information
itsdalmo committed Jul 1, 2021
1 parent 9209d00 commit 728886a
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 243 deletions.
14 changes: 5 additions & 9 deletions cmd/sidecred-lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/telia-oss/sidecred"
"github.com/telia-oss/sidecred/config"
"github.com/telia-oss/sidecred/internal/cli"

"github.com/alecthomas/kingpin"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
environment "github.com/telia-oss/aws-env"
"sigs.k8s.io/yaml"
)

var version string
Expand Down Expand Up @@ -55,7 +55,7 @@ type Event struct {
func runFunc(configBucket *string) func(*sidecred.Sidecred, sidecred.StateBackend) error {
return func(s *sidecred.Sidecred, backend sidecred.StateBackend) error {
lambda.Start(func(event Event) error {
config, err := loadConfig(*configBucket, event.ConfigPath)
cfg, err := loadConfig(*configBucket, event.ConfigPath)
if err != nil {
return fmt.Errorf("failed to load config: %s", err)
}
Expand All @@ -64,13 +64,13 @@ func runFunc(configBucket *string) func(*sidecred.Sidecred, sidecred.StateBacken
return fmt.Errorf("failed to load state: %s", err)
}
defer backend.Save(event.StatePath, state)
return s.Process(config, state)
return s.Process(cfg, state)
})
return nil
}
}

func loadConfig(bucket, key string) (*sidecred.Config, error) {
func loadConfig(bucket, key string) (sidecred.Config, error) {
sess, err := session.NewSession()
if err != nil {
return nil, err
Expand All @@ -91,9 +91,5 @@ func loadConfig(bucket, key string) (*sidecred.Config, error) {
return nil, err
}

var config sidecred.Config
if err := yaml.UnmarshalStrict(b.Bytes(), &config); err != nil {
return nil, err
}
return &config, nil
return config.Parse(b.Bytes())
}
14 changes: 7 additions & 7 deletions cmd/sidecred/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"os"

"github.com/telia-oss/sidecred"
"github.com/telia-oss/sidecred/config"
"github.com/telia-oss/sidecred/internal/cli"

"github.com/alecthomas/kingpin"
"sigs.k8s.io/yaml"
)

var version string
Expand All @@ -25,21 +25,21 @@ func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
}

func runFunc(namespace *string, config *string, statePath *string) func(*sidecred.Sidecred, sidecred.StateBackend) error {
func runFunc(namespace *string, cfg *string, statePath *string) func(*sidecred.Sidecred, sidecred.StateBackend) error {
return func(s *sidecred.Sidecred, backend sidecred.StateBackend) error {
b, err := ioutil.ReadFile(*config)
b, err := ioutil.ReadFile(*cfg)
if err != nil {
return fmt.Errorf("failed to read config: %s", err)
}
var config sidecred.Config
if err := yaml.UnmarshalStrict(b, &config); err != nil {
return fmt.Errorf("failed to unmarshal config: %s", err)
cfg, err := config.Parse(b)
if err != nil {
return fmt.Errorf("failed to parse config: %s", err)
}
state, err := backend.Load(*statePath)
if err != nil {
return fmt.Errorf("failed to load state: %s", err)
}
defer backend.Save(*statePath, state)
return s.Process(&config, state)
return s.Process(cfg, state)
}
}
9 changes: 4 additions & 5 deletions cmd/sidecred/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"io/ioutil"
"testing"

"github.com/telia-oss/sidecred/config"

"github.com/stretchr/testify/require"
"github.com/telia-oss/sidecred"
"sigs.k8s.io/yaml"
)

// Verify that the testdata referenced in README.md is valid.
func TestUnmarshalTestData(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/config.yml")
require.NoError(t, err)

var config sidecred.Config
err = yaml.UnmarshalStrict(b, &config)
cfg, err := config.Parse(b)
require.NoError(t, err)

err = config.Validate()
err = cfg.Validate()
require.NoError(t, err)
}
186 changes: 0 additions & 186 deletions config.go

This file was deleted.

Loading

0 comments on commit 728886a

Please sign in to comment.