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

Commit 1a8e382

Browse files
committed
Attempts to read separate cluster config from directory
1 parent 8127e3d commit 1a8e382

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

pkg/tarmak/config/config.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"regexp"
1111
"strings"
1212

13+
"github.com/hashicorp/go-multierror"
1314
"github.com/sirupsen/logrus"
1415
"k8s.io/apimachinery/pkg/runtime"
1516
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -336,10 +337,56 @@ func (c *Config) ReadConfig() (*tarmakv1alpha1.Config, error) {
336337
return nil, fmt.Errorf("got unexpected config type: %v", gvk)
337338
}
338339

340+
_, err = c.readClusters()
341+
if err != nil {
342+
return nil, fmt.Errorf("failed to read cluster configs: %v", err)
343+
}
344+
339345
c.conf = config
340346
return config, nil
341347
}
342348

349+
func (c *Config) readClusters() ([]*clusterv1alpha1.Cluster, error) {
350+
dir, err := ioutil.ReadDir(c.tarmak.ConfigPath())
351+
if err != nil {
352+
return nil, nil
353+
}
354+
355+
var clusterFiles []os.FileInfo
356+
for _, f := range dir {
357+
if !f.IsDir() && strings.HasPrefix(f.Name(), "cluster") && strings.HasSuffix(f.Name(), ".yaml") {
358+
clusterFiles = append(clusterFiles, f)
359+
}
360+
}
361+
362+
var result *multierror.Error
363+
var clusterConfigs []*clusterv1alpha1.Cluster
364+
for _, f := range clusterFiles {
365+
b, err := ioutil.ReadFile(filepath.Join(c.tarmak.ConfigPath(), f.Name()))
366+
if err != nil {
367+
result = multierror.Append(result, err)
368+
continue
369+
}
370+
371+
configObj, gvk, err := c.codecs.UniversalDeserializer().Decode(b, nil, nil)
372+
if err != nil {
373+
err = fmt.Errorf("failed to decode cluster config: %v", err)
374+
result = multierror.Append(result, err)
375+
continue
376+
}
377+
378+
clusterConfig, ok := configObj.(*clusterv1alpha1.Cluster)
379+
if !ok {
380+
result = multierror.Append(result, fmt.Errorf("got unexpected config type: %v", gvk))
381+
continue
382+
}
383+
384+
clusterConfigs = append(clusterConfigs, clusterConfig)
385+
}
386+
387+
return clusterConfigs, result.ErrorOrNil()
388+
}
389+
343390
func (c *Config) Contact() string {
344391
return c.conf.Contact
345392
}

0 commit comments

Comments
 (0)