diff --git a/cmd/tarmak/cmd/root.go b/cmd/tarmak/cmd/root.go index 53f3b0be75..ad3654d6e0 100644 --- a/cmd/tarmak/cmd/root.go +++ b/cmd/tarmak/cmd/root.go @@ -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, diff --git a/pkg/apis/tarmak/v1alpha1/types.go b/pkg/apis/tarmak/v1alpha1/types.go index a235ed67d0..d16c6e0984 100644 --- a/pkg/apis/tarmak/v1alpha1/types.go +++ b/pkg/apis/tarmak/v1alpha1/types.go @@ -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 diff --git a/pkg/tarmak/config/config.go b/pkg/tarmak/config/config.go index 2b8eaee973..871e461372 100644 --- a/pkg/tarmak/config/config.go +++ b/pkg/tarmak/config/config.go @@ -2,6 +2,7 @@ package config import ( + "bytes" "errors" "fmt" "io/ioutil" @@ -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" @@ -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) @@ -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 } diff --git a/pkg/tarmak/config/config_test.go b/pkg/tarmak/config/config_test.go index 19fe18080a..4bc0266cdc 100644 --- a/pkg/tarmak/config/config_test.go +++ b/pkg/tarmak/config/config_test.go @@ -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)