local-server/pkg/clustersconfig/dir.go

235 lines
4.6 KiB
Go
Raw Normal View History

2018-06-17 07:32:44 +00:00
package clustersconfig
import (
2019-02-28 08:27:09 +00:00
"fmt"
2018-06-17 07:32:44 +00:00
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v2"
)
2019-02-28 08:27:09 +00:00
// Debug enables debug logs from this package.
var Debug = false
func FromDir(
read func(path string) ([]byte, error),
assemble func(path string) ([]byte, error),
listBase func(path string) ([]string, error),
listMerged func(path string) ([]string, error),
) (*Config, error) {
2019-02-28 08:27:09 +00:00
load := func(dir, name string, out any) (err error) {
ba, err := assemble(filepath.Join(dir, name))
2018-06-17 07:32:44 +00:00
if err != nil {
return
2018-06-17 07:32:44 +00:00
}
err = yaml.UnmarshalStrict(ba, out)
if err != nil {
return
2019-03-01 01:37:51 +00:00
}
return nil
2018-06-17 07:32:44 +00:00
}
2019-10-09 05:58:28 +00:00
config := &Config{
2023-05-15 14:22:04 +00:00
Addons: make(map[string][]*Template),
StaticPods: make(map[string][]*Template),
2019-10-09 05:58:28 +00:00
}
2019-02-28 08:27:09 +00:00
2018-06-17 07:32:44 +00:00
// load clusters
names, err := listBase("clusters")
2018-06-17 07:32:44 +00:00
if err != nil {
2019-03-01 01:37:51 +00:00
return nil, fmt.Errorf("failed to list clusters: %v", err)
2018-06-17 07:32:44 +00:00
}
for _, name := range names {
name, _ = strings.CutSuffix(name, ".yaml")
2018-06-17 07:32:44 +00:00
cluster := &Cluster{Name: name}
if err := load("clusters", name, cluster); err != nil {
return nil, err
}
config.Clusters = append(config.Clusters, cluster)
}
// load hosts
names, err = listBase("hosts")
2018-06-17 07:32:44 +00:00
if err != nil {
2019-03-01 01:37:51 +00:00
return nil, fmt.Errorf("failed to list hosts: %v", err)
2018-06-17 07:32:44 +00:00
}
for _, name := range names {
name, _ = strings.CutSuffix(name, ".yaml")
2018-06-17 07:32:44 +00:00
o := &Host{Name: name}
if err := load("hosts", name, o); err != nil {
return nil, err
}
config.Hosts = append(config.Hosts, o)
}
// load config templates
loadTemplates := func(dir string, templates *[]*Template) error {
names, err := listMerged(dir)
2018-06-17 07:32:44 +00:00
if err != nil {
2019-03-01 01:37:51 +00:00
return fmt.Errorf("failed to list %s: %v", dir, err)
2018-06-17 07:32:44 +00:00
}
for _, fullName := range names {
name, _ := strings.CutSuffix(fullName, ".yaml")
2018-06-17 07:32:44 +00:00
2019-02-28 08:27:09 +00:00
if hasTemplate(name, *templates) {
continue
}
ba, err := read(path.Join(dir, fullName))
2019-02-28 08:27:09 +00:00
if err != nil {
return err
}
*templates = append(*templates, &Template{
Name: name,
Template: string(ba),
})
2018-06-17 07:32:44 +00:00
}
return nil
}
loadTemplates("configs", &config.Configs)
2019-10-09 05:58:28 +00:00
// cluster addons
2019-02-28 08:27:09 +00:00
for _, cluster := range config.Clusters {
2023-05-15 12:47:53 +00:00
addonSets := cluster.Addons
if len(addonSets) == 0 {
2019-02-28 08:27:09 +00:00
continue
2018-07-07 01:22:35 +00:00
}
2023-05-15 12:47:53 +00:00
for _, addonSet := range addonSets {
if _, ok := config.Addons[addonSet]; ok {
continue
}
2018-07-07 01:22:35 +00:00
2023-05-15 12:47:53 +00:00
templates := make([]*Template, 0)
if err = loadTemplates(path.Join("addons", addonSet), &templates); err != nil {
return nil, err
}
2019-02-28 08:27:09 +00:00
2023-05-15 12:47:53 +00:00
config.Addons[addonSet] = templates
}
2018-07-07 01:22:35 +00:00
}
2023-05-15 14:54:29 +00:00
// cluster static pods
for _, host := range config.Hosts {
2023-05-15 14:22:04 +00:00
bpSet := host.StaticPods
2019-10-09 05:58:28 +00:00
if bpSet == "" {
continue
}
2023-05-15 14:22:04 +00:00
if _, ok := config.StaticPods[bpSet]; ok {
2019-10-09 05:58:28 +00:00
continue
}
templates := make([]*Template, 0)
2023-05-15 14:54:29 +00:00
if err = loadTemplates(path.Join("static-pods", bpSet), &templates); err != nil {
2019-10-09 05:58:28 +00:00
return nil, err
}
2023-05-15 14:22:04 +00:00
config.StaticPods[bpSet] = templates
2019-10-09 05:58:28 +00:00
}
2018-07-07 01:22:35 +00:00
// load SSL configuration
if ba, err := read("ssl-config.json"); err == nil {
2018-06-17 07:32:44 +00:00
config.SSLConfig = string(ba)
} else if !os.IsNotExist(err) {
return nil, err
}
if ba, err := read("cert-requests.yaml"); err == nil {
2018-06-17 07:32:44 +00:00
reqs := make([]*CertRequest, 0)
if err = yaml.Unmarshal(ba, &reqs); err != nil {
return nil, err
}
config.CertRequests = reqs
} else if !os.IsNotExist(err) {
return nil, err
}
return config, nil
}
2019-02-28 08:27:09 +00:00
func hasTemplate(name string, templates []*Template) bool {
for _, tmpl := range templates {
if tmpl.Name == name {
return true
}
}
return false
}
2018-06-17 07:32:44 +00:00
type dirStore struct {
path string
}
2018-07-07 01:22:35 +00:00
// listDir
func (b *dirStore) listDir(prefix string) (subDirs []string, err error) {
entries, err := ioutil.ReadDir(filepath.Join(b.path, prefix))
if err != nil {
return
}
subDirs = make([]string, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
continue
}
name := entry.Name()
if len(name) == 0 || name[0] == '.' {
continue
}
subDirs = append(subDirs, name)
}
return
}
2018-06-17 07:32:44 +00:00
// Names is part of the kvStore interface
func (b *dirStore) List(prefix string) ([]string, error) {
files, err := filepath.Glob(filepath.Join(b.path, filepath.Join(path.Split(prefix)), "*.yaml"))
if err != nil {
return nil, err
}
names := make([]string, 0, len(files))
for _, f := range files {
f2 := strings.TrimSuffix(f, ".yaml")
f2 = filepath.Base(f2)
if f2[0] == '.' { // ignore hidden files
continue
}
names = append(names, f2)
}
return names, nil
}
// Load is part of the DataBackend interface
2019-02-28 08:27:09 +00:00
func (b *dirStore) Get(key string) (ba []byte, err error) {
ba, err = ioutil.ReadFile(filepath.Join(b.path, filepath.Join(path.Split(key))+".yaml"))
if os.IsNotExist(err) {
return nil, nil
}
return
2018-06-17 07:32:44 +00:00
}