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

Commit b42a563

Browse files
committed
Provides suffixes flag configuration file names to merge
1 parent 4295441 commit b42a563

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

cmd/tarmak/cmd/root.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ func init() {
105105
"override the current cluster set in the config",
106106
)
107107

108+
RootCmd.PersistentFlags().StringSliceVar(
109+
&globalFlags.ConfigSuffixes,
110+
"config-suffixes",
111+
[]string{
112+
"tarmak",
113+
"cluster",
114+
"environment",
115+
"provider",
116+
},
117+
"set suffixes of files containing tarmak configuration",
118+
)
119+
108120
if version == "dev" {
109121
RootCmd.PersistentFlags().BoolVar(
110122
&globalFlags.WingDevMode,

pkg/apis/tarmak/v1alpha1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ type Flags struct {
127127
Version string // expose tarmak's build time version
128128

129129
WingDevMode bool // use a bundled wing version rather than a tagged release from GitHub
130+
131+
ConfigSuffixes []string // the suffixes used for imported configuration fragments
130132
}
131133

132134
// This contains the cluster specifc operation flags

pkg/tarmak/config/config.go

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package config
33

44
import (
5+
"bytes"
56
"errors"
67
"fmt"
78
"io/ioutil"
@@ -12,7 +13,6 @@ import (
1213

1314
"github.com/hashicorp/go-multierror"
1415
"github.com/sirupsen/logrus"
15-
//"gopkg.in/yaml.v2"
1616
"k8s.io/apimachinery/pkg/runtime"
1717
"k8s.io/apimachinery/pkg/runtime/serializer"
1818
"k8s.io/apimachinery/pkg/runtime/serializer/json"
@@ -322,52 +322,41 @@ func (c *Config) configPath() string {
322322

323323
func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
324324
var config *tarmakv1alpha1.Config
325+
var configBytes bytes.Buffer
326+
var result *multierror.Error
325327

326-
mainConfigs, err := c.readConfigFragment("tarmak")
327-
if err != nil {
328-
return nil, fmt.Errorf("faield to read main tarmak configs: %v", err)
329-
}
328+
for _, suffix := range c.flags.ConfigSuffixes {
329+
b, err := c.readConfigFragment(suffix)
330+
if err != nil {
331+
result = multierror.Append(result, fmt.Errorf("failed to read main tarmak configs: %v", err))
332+
continue
333+
}
330334

331-
if len(mainConfigs) == 0 {
332-
return nil, errors.New("failed to read configuration, at least a single configuration file must exist with prefix 'tarmak'")
335+
if _, err := configBytes.Write(b); err != nil {
336+
result = multierror.Append(result, err)
337+
continue
338+
}
333339
}
334340

335-
config = mainConfigs[0].DeepCopy()
336-
for _, m := range mainConfigs[1:] {
337-
config.Clusters = append(config.Clusters, m.Clusters...)
338-
config.Environments = append(config.Environments, m.Environments...)
339-
config.Providers = append(config.Providers, m.Providers...)
341+
if result.ErrorOrNil() != nil {
342+
return nil, result.ErrorOrNil()
340343
}
341344

342-
clustersConfigs, err := c.readConfigFragment("cluster")
345+
configObj, gvk, err := c.codecs.UniversalDecoder(tarmakv1alpha1.SchemeGroupVersion).Decode(configBytes.Bytes(), nil, nil)
343346
if err != nil {
344-
return nil, fmt.Errorf("failed to read cluster configs: %v", err)
345-
}
346-
for _, clusterConfig := range clustersConfigs {
347-
config.Clusters = append(config.Clusters, clusterConfig.Clusters...)
347+
return nil, fmt.Errorf("failed to decode config bytes from file: %v", err)
348348
}
349349

350-
environmentsConfigs, err := c.readConfigFragment("environment")
351-
if err != nil {
352-
return nil, fmt.Errorf("failed to read environments configs: %v", err)
353-
}
354-
for _, environmentConfig := range environmentsConfigs {
355-
config.Environments = append(config.Environments, environmentConfig.Environments...)
356-
}
357-
358-
providersConfigs, err := c.readConfigFragment("providers")
359-
if err != nil {
360-
return nil, fmt.Errorf("failed to read providers configs: %v", err)
361-
}
362-
for _, providerConfig := range providersConfigs {
363-
config.Providers = append(config.Providers, providerConfig.Providers...)
350+
config, ok := configObj.(*tarmakv1alpha1.Config)
351+
if !ok {
352+
return nil, fmt.Errorf("got unexpected config type: %v", gvk)
364353
}
365354

366355
c.conf = config
367356
return config, nil
368357
}
369358

370-
func (c *Config) readConfigFragment(fragment string) ([]*tarmakv1alpha1.Config, error) {
359+
func (c *Config) readConfigFragment(fragment string) ([]byte, error) {
371360
dir, err := ioutil.ReadDir(c.tarmak.ConfigPath())
372361
if err != nil {
373362
return nil, nil
@@ -380,33 +369,21 @@ func (c *Config) readConfigFragment(fragment string) ([]*tarmakv1alpha1.Config,
380369
}
381370
}
382371

372+
var buff bytes.Buffer
383373
var result *multierror.Error
384-
var fragConfigs []*tarmakv1alpha1.Config
385374
for _, f := range fragFiles {
386375
b, err := ioutil.ReadFile(filepath.Join(c.tarmak.ConfigPath(), f.Name()))
387376
if err != nil {
388377
result = multierror.Append(result, err)
389378
continue
390379
}
391380

392-
configObj, gvk, err := c.codecs.UniversalDecoder(tarmakv1alpha1.SchemeGroupVersion).Decode(b, nil, nil)
393-
if err != nil {
394-
err = fmt.Errorf("faild to decode config file '%s': %v", f.Name(), err)
381+
if _, err := buff.Write(b); err != nil {
395382
result = multierror.Append(result, err)
396-
continue
397383
}
398-
399-
config, ok := configObj.(*tarmakv1alpha1.Config)
400-
if !ok {
401-
err := fmt.Errorf("got unexpected config type: %v", gvk)
402-
result = multierror.Append(result, err)
403-
continue
404-
}
405-
406-
fragConfigs = append(fragConfigs, config)
407384
}
408385

409-
return fragConfigs, result.ErrorOrNil()
386+
return buff.Bytes(), result.ErrorOrNil()
410387
}
411388

412389
func (c *Config) Contact() string {

0 commit comments

Comments
 (0)