@@ -12,6 +12,7 @@ import (
1212
1313 "github.com/hashicorp/go-multierror"
1414 "github.com/sirupsen/logrus"
15+ //"gopkg.in/yaml.v2"
1516 "k8s.io/apimachinery/pkg/runtime"
1617 "k8s.io/apimachinery/pkg/runtime/serializer"
1718 "k8s.io/apimachinery/pkg/runtime/serializer/json"
@@ -320,71 +321,92 @@ func (c *Config) configPath() string {
320321}
321322
322323func (c * Config ) ReadConfig () (* tarmakv1alpha1.Config , error ) {
323- path := c . configPath ()
324+ var config * tarmakv1alpha1. Config
324325
325- configBytes , err := ioutil . ReadFile ( path )
326+ mainConfigs , err := c . readConfigFragment ( "tarmak" )
326327 if err != nil {
327- return nil , err
328+ return nil , fmt . Errorf ( "faield to read main tarmak configs: %v" , err )
328329 }
329330
330- configObj , gvk , err := c .codecs .UniversalDecoder (tarmakv1alpha1 .SchemeGroupVersion ).Decode (configBytes , nil , nil )
331- if err != nil {
332- return nil , err
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'" )
333333 }
334334
335- config , ok := configObj .(* tarmakv1alpha1.Config )
336- if ! ok {
337- return nil , fmt .Errorf ("got unexpected config type: %v" , gvk )
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 ... )
338340 }
339341
340- _ , err = c .readClusters ( )
342+ clustersConfigs , err : = c .readConfigFragment ( "cluster" )
341343 if err != nil {
342344 return nil , fmt .Errorf ("failed to read cluster configs: %v" , err )
343345 }
346+ for _ , clusterConfig := range clustersConfigs {
347+ config .Clusters = append (config .Clusters , clusterConfig .Clusters ... )
348+ }
349+
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 ... )
364+ }
344365
345366 c .conf = config
346367 return config , nil
347368}
348369
349- func (c * Config ) readClusters ( ) ([]* clusterv1alpha1. Cluster , error ) {
370+ func (c * Config ) readConfigFragment ( fragment string ) ([]* tarmakv1alpha1. Config , error ) {
350371 dir , err := ioutil .ReadDir (c .tarmak .ConfigPath ())
351372 if err != nil {
352373 return nil , nil
353374 }
354375
355- var clusterFiles []os.FileInfo
376+ var fragFiles []os.FileInfo
356377 for _ , f := range dir {
357- if ! f .IsDir () && strings .HasPrefix (f .Name (), "cluster" ) && strings .HasSuffix (f .Name (), ".yaml" ) {
358- clusterFiles = append (clusterFiles , f )
378+ if ! f .IsDir () && strings .HasPrefix (f .Name (), fragment ) && strings .HasSuffix (f .Name (), ".yaml" ) {
379+ fragFiles = append (fragFiles , f )
359380 }
360381 }
361382
362383 var result * multierror.Error
363- var clusterConfigs []* clusterv1alpha1. Cluster
364- for _ , f := range clusterFiles {
384+ var fragConfigs []* tarmakv1alpha1. Config
385+ for _ , f := range fragFiles {
365386 b , err := ioutil .ReadFile (filepath .Join (c .tarmak .ConfigPath (), f .Name ()))
366387 if err != nil {
367388 result = multierror .Append (result , err )
368389 continue
369390 }
370391
371- configObj , gvk , err := c .codecs .UniversalDeserializer ( ).Decode (b , nil , nil )
392+ configObj , gvk , err := c .codecs .UniversalDecoder ( tarmakv1alpha1 . SchemeGroupVersion ).Decode (b , nil , nil )
372393 if err != nil {
373- err = fmt .Errorf ("failed to decode cluster config: %v" , err )
394+ err = fmt .Errorf ("faild to decode config file '%s' : %v" , f . Name () , err )
374395 result = multierror .Append (result , err )
375396 continue
376397 }
377398
378- clusterConfig , ok := configObj .(* clusterv1alpha1. Cluster )
399+ config , ok := configObj .(* tarmakv1alpha1. Config )
379400 if ! ok {
380- result = multierror .Append (result , fmt .Errorf ("got unexpected config type: %v" , gvk ))
401+ err := fmt .Errorf ("got unexpected config type: %v" , gvk )
402+ result = multierror .Append (result , err )
381403 continue
382404 }
383405
384- clusterConfigs = append (clusterConfigs , clusterConfig )
406+ fragConfigs = append (fragConfigs , config )
385407 }
386408
387- return clusterConfigs , result .ErrorOrNil ()
409+ return fragConfigs , result .ErrorOrNil ()
388410}
389411
390412func (c * Config ) Contact () string {
0 commit comments