Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

WIP: Split config PoC #351

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions cmd/tarmak/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ func init() {
"override the current cluster set in the config",
)

RootCmd.PersistentFlags().StringSliceVar(
&globalFlags.ConfigPrefixes,
"config-prefixes",
[]string{
"tarmak",
"cluster",
"environment",
"provider",
},
"set prefixes of files containing tarmak configuration to be used",
)

if version == "dev" {
RootCmd.PersistentFlags().BoolVar(
&globalFlags.WingDevMode,
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/tarmak/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type Flags struct {
Version string // expose tarmak's build time version

WingDevMode bool // use a bundled wing version rather than a tagged release from GitHub

ConfigPrefixes []string // the prefixes used for imported configuration fragments
}

// This contains the cluster specifc operation flags
Expand Down
47 changes: 43 additions & 4 deletions pkg/tarmak/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package config

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -10,6 +11,7 @@ import (
"regexp"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand Down Expand Up @@ -319,16 +321,16 @@ func (c *Config) configPath() string {
}

func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
path := c.configPath()
var config *tarmakv1alpha1.Config

configBytes, err := ioutil.ReadFile(path)
configBytes, err := c.readConfigFragments(c.flags.ConfigPrefixes)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to read tarmak configs: %v", err)
}

configObj, gvk, err := c.codecs.UniversalDecoder(tarmakv1alpha1.SchemeGroupVersion).Decode(configBytes, nil, nil)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode config bytes from file: %v", err)
}

config, ok := configObj.(*tarmakv1alpha1.Config)
Expand All @@ -340,6 +342,43 @@ func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
return config, nil
}

func (c *Config) readConfigFragments(prefixes []string) ([]byte, error) {
dir, err := ioutil.ReadDir(c.tarmak.ConfigPath())
if err != nil {
return nil, err
}

var fragFiles []os.FileInfo
for _, f := range dir {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".yaml") {

for _, prefix := range prefixes {
if strings.HasPrefix(f.Name(), prefix) {
fragFiles = append(fragFiles, f)
break
}
}

}
}

var buff bytes.Buffer
var result *multierror.Error
for _, f := range fragFiles {
b, err := ioutil.ReadFile(filepath.Join(c.tarmak.ConfigPath(), f.Name()))
if err != nil {
result = multierror.Append(result, err)
continue
}

if _, err := buff.Write(b); err != nil {
result = multierror.Append(result, err)
}
}

return buff.Bytes(), result.ErrorOrNil()
}

func (c *Config) Contact() string {
return c.conf.Contact
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/tarmak/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func newFakeConfig(t *testing.T) *fakeConfig {
}
c.fakeTarmak.EXPECT().Log().AnyTimes().Return(logger.WithField("app", "tarmak"))

c.Config, err = New(c.fakeTarmak, &tarmakv1alpha1.Flags{})
c.Config, err = New(c.fakeTarmak, &tarmakv1alpha1.Flags{
ConfigPrefixes: []string{"tarmak"},
})

if err != nil {
t.Error("unexpected error: ", err)
Expand Down