Skip to content

Commit

Permalink
refactor(architecture): move config sources into config package
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Braun <[email protected]>
  • Loading branch information
bluebrown committed Jul 23, 2023
1 parent 3271167 commit 533ff25
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 44 deletions.
11 changes: 6 additions & 5 deletions internal/gitbot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"sync"
"time"

"github.com/bluebrown/kobold/kobold"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/bluebrown/kobold/kobold/config"
)

type PullRequester interface {
Expand All @@ -25,9 +26,9 @@ type GitTransporter interface {
AddCommitPush(ctx context.Context, branch, title, description string) (bool, error)
}

func NewRepo(tranport GitTransporter, provider kobold.GitProvider) *repo {
func NewRepo(tranport GitTransporter, provider config.GitProvider) *repo {
if provider == "" {
provider = kobold.InferGitProvider(tranport.URL())
provider = config.InferGitProvider(tranport.URL())
}
return &repo{
lock: sync.Mutex{},
Expand All @@ -38,7 +39,7 @@ func NewRepo(tranport GitTransporter, provider kobold.GitProvider) *repo {

type repo struct {
transport GitTransporter
provider kobold.GitProvider
provider config.GitProvider
lock sync.Mutex
}

Expand All @@ -51,7 +52,7 @@ func (r *repo) Transactions(fn func(path string, transport GitTransporter) error
return err
}

func (r *repo) Provider() kobold.GitProvider {
func (r *repo) Provider() config.GitProvider {
return r.provider
}

Expand Down
8 changes: 4 additions & 4 deletions internal/krm/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"

"github.com/bluebrown/kobold/internal/events"
"github.com/bluebrown/kobold/kobold"
"github.com/bluebrown/kobold/kobold/config"
)

type NopRenderer struct{}
Expand Down Expand Up @@ -316,10 +316,10 @@ func NewCustomResolver(name string, paths []string) Resolver {
// for example for a docker-compose.yaml, the compose resolver should be returned
type ResolverSelector struct {
resolvers map[string]Resolver
associations []kobold.FileTypeSpec
associations []config.FileTypeSpec
}

func NewSelector(resolvers []kobold.ResolverSpec, associations []kobold.FileTypeSpec) *ResolverSelector {
func NewSelector(resolvers []config.ResolverSpec, associations []config.FileTypeSpec) *ResolverSelector {
resolverMap := map[string]Resolver{
"ko": resolveKo,
"compose": resolveCompose,
Expand All @@ -332,7 +332,7 @@ func NewSelector(resolvers []kobold.ResolverSpec, associations []kobold.FileType

// TODO: merge defaults with user associations ?!
if len(associations) == 0 {
associations = []kobold.FileTypeSpec{
associations = []config.FileTypeSpec{
{Kind: "ko", Pattern: ".ko.yaml"},
{Kind: "compose", Pattern: "*compose*.y?ml"},
{Kind: "kubernetes", Pattern: "*"},
Expand Down
15 changes: 8 additions & 7 deletions internal/krm/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"testing"

"github.com/bluebrown/kobold/internal/events"
"github.com/bluebrown/kobold/kobold"
"github.com/google/go-containerregistry/pkg/name"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"

"github.com/bluebrown/kobold/internal/events"
"github.com/bluebrown/kobold/kobold/config"
"github.com/google/go-containerregistry/pkg/name"
)

type testPipeOptions struct {
associations []kobold.FileTypeSpec
resolvers []kobold.ResolverSpec
associations []config.FileTypeSpec
resolvers []config.ResolverSpec
}

func testPipe(caseDir string, opts testPipeOptions, events ...events.PushData) (filesys.FileSystem, error) {
Expand Down Expand Up @@ -222,10 +223,10 @@ func Test_renderer_Render(t *testing.T) {
name: "custom-resolver-helm",
giveDir: "custom-resolver-helm",
giveOpts: testPipeOptions{
resolvers: []kobold.ResolverSpec{
resolvers: []config.ResolverSpec{
{Name: "my-helm", Paths: []string{"path.to.image", "another.path"}},
},
associations: []kobold.FileTypeSpec{{Kind: "my-helm", Pattern: "values.yaml"}},
associations: []config.FileTypeSpec{{Kind: "my-helm", Pattern: "values.yaml"}},
},
giveEvents: []events.PushData{
{Image: "index.docker.io/bluebrown/echoserver", Tag: "latest", Digest: "sha256:3b3128d9df6bbbcc92e2358e596c9fbd722a437a62bafbc51607970e9e3b8869"},
Expand Down
4 changes: 2 additions & 2 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/rs/zerolog/log"

"github.com/bluebrown/kobold/kobold"
"github.com/bluebrown/kobold/kobold/config"
)

func NewKeys(k8s bool, auth kobold.RegistryAuthSpec) (keys authn.Keychain, err error) {
func NewKeys(k8s bool, auth config.RegistryAuthSpec) (keys authn.Keychain, err error) {
if k8s {
log.Debug().Msg("using k8s key chain")
ss := make([]string, len(auth.ImagePullSecrets))
Expand Down
18 changes: 9 additions & 9 deletions internal/server/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/bluebrown/kobold/internal/gitbot/transport"
"github.com/bluebrown/kobold/internal/krm"
"github.com/bluebrown/kobold/internal/registry"
"github.com/bluebrown/kobold/kobold"
"github.com/bluebrown/kobold/kobold/config"
)

type generator struct {
Expand All @@ -29,7 +29,7 @@ type generator struct {
imagerefTemplate string
}

func (g generator) Generate(conf *kobold.NormalizedConfig) (http.Handler, error) {
func (g generator) Generate(conf *config.NormalizedConfig) (http.Handler, error) {
// initialize all repositories
repos := make(gitbot.Repos, 0)
for _, r := range conf.Repositories {
Expand Down Expand Up @@ -72,16 +72,16 @@ func (g generator) Generate(conf *kobold.NormalizedConfig) (http.Handler, error)
standardClient := retryClient.StandardClient()

switch sub.Strategy {
case kobold.StrategyCommit:
case config.StrategyCommit:
prClient = nil
case kobold.StrategyPullRequest:
case config.StrategyPullRequest:
log.Debug().Str("sub", sub.Name).Str("provider", string(repo.Provider())).Msg("setup pull requests")
switch repo.Provider() {
case "":
return nil, fmt.Errorf("using pull-requests requires a known provider")
case kobold.ProviderGithub:
case config.ProviderGithub:
prClient, err = github.NewPrClient(repo.URL(), repo.Auth(), standardClient)
case kobold.ProviderAzure:
case config.ProviderAzure:
prClient, err = azure.NewPrClient(repo.URL(), repo.Auth(), standardClient)
default:
return nil, fmt.Errorf("provider %s not supported for pull-requests", repo.Provider())
Expand Down Expand Up @@ -134,11 +134,11 @@ func (g generator) Generate(conf *kobold.NormalizedConfig) (http.Handler, error)
log.Info().Str("endpoint", endpoint.Name).Str("path", endpoint.Path).Msg("setup endpoint")
var ph events.PayloadHandler
switch endpoint.Type {
case kobold.EndpointTypeGeneric:
case config.EndpointTypeGeneric:
ph = generic.NewPayloadHandler()
case kobold.EndpointTypeACR:
case config.EndpointTypeACR:
ph = acr.NewPayloadHandler()
case kobold.EndpointTypeDockerhub:
case config.EndpointTypeDockerhub:
ph = dockerhub.NewPayloadHandler(registry.NewDigestFetcher(g.defaultRegistry, keys))
default:
return nil, fmt.Errorf("unsupported endpoint type: %s", endpoint.Type)
Expand Down
4 changes: 2 additions & 2 deletions internal/server/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package server

import "github.com/bluebrown/kobold/kobold"
import "github.com/bluebrown/kobold/kobold/config"

func WithConfigPath(path string) Option {
return func(o *Options) {
Expand All @@ -14,7 +14,7 @@ func WithWatch(enabled bool) Option {
}
}

func WithConfig(c *kobold.NormalizedConfig) Option {
func WithConfig(c *config.NormalizedConfig) Option {
return func(o *Options) {
o.Config = c
}
Expand Down
16 changes: 8 additions & 8 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"

"github.com/bluebrown/kobold/kobold"
"github.com/bluebrown/kobold/kobold/config"
)

const (
Expand All @@ -25,7 +25,7 @@ const (
)

type muxGenerator interface {
Generate(conf *kobold.NormalizedConfig) (http.Handler, error)
Generate(conf *config.NormalizedConfig) (http.Handler, error)
}

type Server struct {
Expand All @@ -36,7 +36,7 @@ type Server struct {
type Options struct {
Watch bool
ConfigPath string
Config *kobold.NormalizedConfig
Config *config.NormalizedConfig
Datapath string
UseK8sChain bool
muxGenerator muxGenerator
Expand All @@ -59,14 +59,14 @@ func NewOrDie(options ...Option) *Server {

if opts.ConfigPath != "" {
var err error
opts.Config, err = kobold.ReadPath(opts.ConfigPath)
opts.Config, err = config.ReadPath(opts.ConfigPath)
if err != nil {
panic(err)
}
}

if opts.Config == nil {
opts.Config = &kobold.NormalizedConfig{}
opts.Config = &config.NormalizedConfig{}
}

if opts.Datapath == "" {
Expand Down Expand Up @@ -122,7 +122,7 @@ func NewOrDie(options ...Option) *Server {
s.atomicHandler.Store(mux)

if opts.Watch && opts.ConfigPath != "" {
go WatchConfigOrDie(opts.ConfigPath, func(c *kobold.NormalizedConfig) {
go WatchConfigOrDie(opts.ConfigPath, func(c *config.NormalizedConfig) {
log.Info().Msg("reloading config")
m, err := s.generator.Generate(c)
if err != nil {
Expand All @@ -144,7 +144,7 @@ func (s *Server) Reload(handler http.Handler) {
s.atomicHandler.Store(handler)
}

func WatchConfigOrDie(path string, onChange func(c *kobold.NormalizedConfig)) {
func WatchConfigOrDie(path string, onChange func(c *config.NormalizedConfig)) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
Expand Down Expand Up @@ -181,7 +181,7 @@ func WatchConfigOrDie(path string, onChange func(c *kobold.NormalizedConfig)) {

// finally load the new config

conf, err := kobold.ReadPath(path)
conf, err := config.ReadPath(path)
if err != nil {
log.Error().Err(err).Msg("failed to config")
continue
Expand Down
4 changes: 2 additions & 2 deletions internal/server/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

"github.com/bluebrown/kobold/internal/events"
"github.com/bluebrown/kobold/internal/krm"
"github.com/bluebrown/kobold/kobold"
"github.com/bluebrown/kobold/kobold/config"
)

func RequireHeaders(headers []kobold.Header, handler http.Handler) http.Handler {
func RequireHeaders(headers []config.Header, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, h := range headers {
if val := r.Header.Get(h.Key); val == "" || val != h.Value {
Expand Down
2 changes: 1 addition & 1 deletion kobold/config.go → kobold/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kobold
package config

// in memory representation of the config used to setup kobold
// this is used so that the user facing config can have different
Expand Down
2 changes: 1 addition & 1 deletion kobold/config_test.go → kobold/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kobold
package config

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion kobold/aux.go → kobold/config/git.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kobold
package config

import "strings"

Expand Down
2 changes: 1 addition & 1 deletion kobold/read.go → kobold/config/read.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kobold
package config

import (
"fmt"
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion kobold/v1.go → kobold/config/v1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kobold
package config

// NOTE: The v1 user-config has the same layout as the normalized config itself
// the below is done to establish a pattern that can be used for other versions
Expand Down

0 comments on commit 533ff25

Please sign in to comment.