feat: cluster addons

This commit is contained in:
Mikaël Cluseau
2018-07-07 12:22:35 +11:00
parent 6c20c29106
commit 975f002935
78 changed files with 3226 additions and 123 deletions

View File

@ -45,8 +45,11 @@ func (s *DirStore) GetOrCreate(tag, item string, create func(io.Writer) error) (
return
}
partFile := fullPath + ".part"
os.Remove(partFile)
var out *os.File
out, err = os.OpenFile(fullPath+".part", os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_EXCL, 0600)
out, err = os.OpenFile(partFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_EXCL, 0600)
if err != nil {
return
}

View File

@ -16,13 +16,14 @@ type Config struct {
Groups []*Group
Clusters []*Cluster
Configs []*Template
StaticPods []*Template `yaml:"static_pods"`
StaticPods []*Template `yaml:"static_pods"`
Addons map[string][]*Template
SSLConfig string `yaml:"ssl_config"`
CertRequests []*CertRequest `yaml:"cert_requests"`
}
func FromBytes(data []byte) (*Config, error) {
config := &Config{}
config := &Config{Addons: make(map[string][]*Template)}
if err := yaml.Unmarshal(data, config); err != nil {
return nil, err
}
@ -192,6 +193,7 @@ type Vars map[string]interface{}
type Cluster struct {
Name string
Domain string
Addons string
Subnets struct {
Services string
Pods string

View File

@ -11,7 +11,7 @@ import (
)
func FromDir(dirPath string) (*Config, error) {
config := &Config{}
config := &Config{Addons: make(map[string][]*Template)}
store := dirStore{dirPath}
load := func(dir, name string, out interface{}) error {
@ -95,6 +95,23 @@ func FromDir(dirPath string) (*Config, error) {
return nil, err
}
{
addonSets, err := store.listDir("addons")
if err != nil {
return nil, err
}
for _, addonSet := range addonSets {
templates := make([]*Template, 0)
if err = loadTemplates(path.Join("addons", addonSet), &templates); err != nil {
return nil, err
}
config.Addons[addonSet] = templates
}
}
// load SSL configuration
if ba, err := ioutil.ReadFile(filepath.Join(dirPath, "ssl-config.json")); err == nil {
config.SSLConfig = string(ba)
@ -121,6 +138,32 @@ type dirStore struct {
path string
}
// 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
}
// 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"))

View File

@ -2,12 +2,54 @@ package config
import (
"fmt"
"io/ioutil"
"io"
"os"
yaml "gopkg.in/yaml.v2"
)
// Load a config from a file.
func Load(file string) (config *Config, err error) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
config, err = Read(f)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %v", file, err)
}
return
}
// Read a config from a reader.
func Read(reader io.Reader) (config *Config, err error) {
config = &Config{}
err = yaml.NewDecoder(reader).Decode(config)
if err != nil {
return nil, err
}
return
}
// Parse the config in data.
func Parse(data []byte) (config *Config, err error) {
config = &Config{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
return
}
// Config represent this system's configuration
type Config struct {
Vars []VarDefault
@ -83,19 +125,3 @@ type NetworkDef struct {
Optional bool
Script string
}
func Load(file string) (config *Config, err error) {
config = &Config{}
configData, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", file, err)
}
err = yaml.Unmarshal(configData, config)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %v", file, err)
}
return
}