mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-06-03 01:44:53 +00:00
art: Refactor out config sections
Now there is a `install` section in the config that has the fields that previously where in `c3os` but were actually only used during install phase. Also the k3s and c3os config were moved to the provider instead that in the global config.
This commit is contained in:
parent
3af29389d3
commit
a5437561eb
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
retry "github.com/avast/retry-go"
|
retry "github.com/avast/retry-go"
|
||||||
"github.com/c3os-io/c3os/internal/machine"
|
"github.com/c3os-io/c3os/internal/machine"
|
||||||
@ -16,34 +17,19 @@ import (
|
|||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type C3OS struct {
|
type Install struct {
|
||||||
NetworkToken string `yaml:"network_token,omitempty"`
|
Auto bool `yaml:"auto,omitempty"`
|
||||||
Offline bool `yaml:"offline,omitempty"`
|
|
||||||
Reboot bool `yaml:"reboot,omitempty"`
|
Reboot bool `yaml:"reboot,omitempty"`
|
||||||
Device string `yaml:"device,omitempty"`
|
Device string `yaml:"device,omitempty"`
|
||||||
Poweroff bool `yaml:"poweroff,omitempty"`
|
Poweroff bool `yaml:"poweroff,omitempty"`
|
||||||
Role string `yaml:"role,omitempty"`
|
|
||||||
NetworkID string `yaml:"network_id,omitempty"`
|
|
||||||
DNS bool `yaml:"dns,omitempty"`
|
|
||||||
LogLevel string `yaml:"loglevel,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type K3s struct {
|
|
||||||
Env map[string]string `yaml:"env,omitempty"`
|
|
||||||
ReplaceEnv bool `yaml:"replace_env,omitempty"`
|
|
||||||
ReplaceArgs bool `yaml:"replace_args,omitempty"`
|
|
||||||
Args []string `yaml:"args,omitempty"`
|
|
||||||
Enabled bool `yaml:"enabled,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
C3OS *C3OS `yaml:"c3os,omitempty"`
|
Install *Install `yaml:"install,omitempty"`
|
||||||
K3sAgent K3s `yaml:"k3s-agent,omitempty"`
|
|
||||||
K3s K3s `yaml:"k3s,omitempty"`
|
|
||||||
VPN map[string]string `yaml:"vpn,omitempty"`
|
|
||||||
//cloudFileContent string
|
//cloudFileContent string
|
||||||
originalData map[string]interface{}
|
originalData map[string]interface{}
|
||||||
location string
|
location string
|
||||||
|
header string
|
||||||
ConfigURL string `yaml:"config_url,omitempty"`
|
ConfigURL string `yaml:"config_url,omitempty"`
|
||||||
Options map[string]string `yaml:"options,omitempty"`
|
Options map[string]string `yaml:"options,omitempty"`
|
||||||
IgnoreBundleErrors bool `yaml:"ignore_bundles_errors,omitempty"`
|
IgnoreBundleErrors bool `yaml:"ignore_bundles_errors,omitempty"`
|
||||||
@ -60,6 +46,18 @@ type Bundle struct {
|
|||||||
Targets []string `yaml:"targets,omitempty"`
|
Targets []string `yaml:"targets,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasHeader(userdata, head string) (bool, string) {
|
||||||
|
header := strings.SplitN(userdata, "\n", 2)[0]
|
||||||
|
|
||||||
|
// Trim trailing whitespaces
|
||||||
|
header = strings.TrimRightFunc(header, unicode.IsSpace)
|
||||||
|
|
||||||
|
if head != "" {
|
||||||
|
return head == header, header
|
||||||
|
}
|
||||||
|
return (header == "#cloud-config") || (header == "#c3os-config") || (header == "#node-config"), header
|
||||||
|
}
|
||||||
|
|
||||||
func (b Bundles) Options() (res [][]machine.BundleOption) {
|
func (b Bundles) Options() (res [][]machine.BundleOption) {
|
||||||
for _, bundle := range b {
|
for _, bundle := range b {
|
||||||
for _, t := range bundle.Targets {
|
for _, t := range bundle.Targets {
|
||||||
@ -76,6 +74,10 @@ func (b Bundles) Options() (res [][]machine.BundleOption) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Config) Unmarshal(o interface{}) error {
|
||||||
|
return yaml.Unmarshal([]byte(c.String()), o)
|
||||||
|
}
|
||||||
|
|
||||||
func (c Config) Data() map[string]interface{} {
|
func (c Config) Data() map[string]interface{} {
|
||||||
return c.originalData
|
return c.originalData
|
||||||
}
|
}
|
||||||
@ -89,16 +91,16 @@ func (c Config) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dat, _ := yaml.Marshal(c.originalData)
|
dat, _ := yaml.Marshal(c.originalData)
|
||||||
|
if c.header != "" {
|
||||||
|
return AddHeader(c.header, string(dat))
|
||||||
|
}
|
||||||
return string(dat)
|
return string(dat)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) IsValid() bool {
|
func (c Config) IsValid() bool {
|
||||||
return c.C3OS != nil ||
|
return c.Install != nil ||
|
||||||
c.K3s.Enabled ||
|
|
||||||
c.K3sAgent.Enabled ||
|
|
||||||
c.ConfigURL != "" ||
|
c.ConfigURL != "" ||
|
||||||
len(c.Bundles) != 0 ||
|
len(c.Bundles) != 0
|
||||||
len(c.VPN) != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Scan(opts ...Option) (c *Config, err error) {
|
func Scan(opts ...Option) (c *Config, err error) {
|
||||||
@ -129,11 +131,13 @@ func Scan(opts ...Option) (c *Config, err error) {
|
|||||||
b, err := ioutil.ReadFile(f)
|
b, err := ioutil.ReadFile(f)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
yaml.Unmarshal(b, c)
|
yaml.Unmarshal(b, c)
|
||||||
if c.IsValid() {
|
if exists, header := HasHeader(string(b), ""); c.IsValid() || exists {
|
||||||
// c.cloudFileContent = string(b)
|
|
||||||
c.location = f
|
c.location = f
|
||||||
yaml.Unmarshal(b, &c.originalData)
|
yaml.Unmarshal(b, &c.originalData)
|
||||||
configFound = true
|
configFound = true
|
||||||
|
if exists {
|
||||||
|
c.header = header
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,11 +244,13 @@ func listFiles(dir string) ([]string, error) {
|
|||||||
func ReplaceToken(dir []string, token string) (err error) {
|
func ReplaceToken(dir []string, token string) (err error) {
|
||||||
c, err := Scan(Directories(dir...))
|
c, err := Scan(Directories(dir...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("no config file found: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.C3OS == nil {
|
header := "#node-config"
|
||||||
return errors.New("no config file found")
|
|
||||||
|
if hasHeader, head := HasHeader(c.String(), ""); hasHeader {
|
||||||
|
header = head
|
||||||
}
|
}
|
||||||
|
|
||||||
content := map[interface{}]interface{}{}
|
content := map[interface{}]interface{}{}
|
||||||
@ -282,7 +288,7 @@ func ReplaceToken(dir []string, token string) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ioutil.WriteFile(c.location, d, fi.Mode().Perm())
|
return ioutil.WriteFile(c.location, []byte(AddHeader(header, string(d))), fi.Mode().Perm())
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stage string
|
type Stage string
|
||||||
@ -306,3 +312,28 @@ func SaveCloudConfig(name Stage, yc yip.YipConfig) error {
|
|||||||
func FromString(s string, o interface{}) error {
|
func FromString(s string, o interface{}) error {
|
||||||
return yaml.Unmarshal([]byte(s), o)
|
return yaml.Unmarshal([]byte(s), o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MergeYAML(objs ...interface{}) ([]byte, error) {
|
||||||
|
content := [][]byte{}
|
||||||
|
for _, o := range objs {
|
||||||
|
dat, err := yaml.Marshal(o)
|
||||||
|
if err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
content = append(content, dat)
|
||||||
|
}
|
||||||
|
|
||||||
|
finalData := make(map[string]interface{})
|
||||||
|
|
||||||
|
for _, c := range content {
|
||||||
|
if err := yaml.Unmarshal(c, &finalData); err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return yaml.Marshal(finalData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddHeader(header, data string) string {
|
||||||
|
return fmt.Sprintf("%s\n%s", header, data)
|
||||||
|
}
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
providerConfig "github.com/c3os-io/c3os/internal/provider/config"
|
||||||
|
"github.com/c3os-io/c3os/pkg/config"
|
||||||
. "github.com/c3os-io/c3os/pkg/config"
|
. "github.com/c3os-io/c3os/pkg/config"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
@ -30,7 +32,8 @@ var _ = Describe("Get config", func() {
|
|||||||
Context("directory", func() {
|
Context("directory", func() {
|
||||||
It("reads config file greedly", func() {
|
It("reads config file greedly", func() {
|
||||||
|
|
||||||
var cc string = `baz: bar
|
var cc string = `#c3os-config
|
||||||
|
baz: bar
|
||||||
c3os:
|
c3os:
|
||||||
network_token: foo
|
network_token: foo
|
||||||
`
|
`
|
||||||
@ -47,13 +50,17 @@ fooz:
|
|||||||
c, err := Scan(Directories(d))
|
c, err := Scan(Directories(d))
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(c).ToNot(BeNil())
|
Expect(c).ToNot(BeNil())
|
||||||
Expect(c.C3OS.NetworkToken).To(Equal("foo"))
|
providerCfg := &providerConfig.Config{}
|
||||||
|
err = c.Unmarshal(providerCfg)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(providerCfg.C3OS).ToNot(BeNil())
|
||||||
|
Expect(providerCfg.C3OS.NetworkToken).To(Equal("foo"))
|
||||||
Expect(c.String()).To(Equal(cc))
|
Expect(c.String()).To(Equal(cc))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("replace token in config files", func() {
|
It("replace token in config files", func() {
|
||||||
|
|
||||||
var cc string = `
|
var cc string = `#node-config
|
||||||
c3os:
|
c3os:
|
||||||
network_token: "foo"
|
network_token: "foo"
|
||||||
|
|
||||||
@ -80,6 +87,8 @@ fooz:
|
|||||||
|
|
||||||
err = yaml.Unmarshal(content, &res)
|
err = yaml.Unmarshal(content, &res)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
hasHeader, _ := config.HasHeader(string(content), "#node-config")
|
||||||
|
Expect(hasHeader).To(BeTrue())
|
||||||
|
|
||||||
Expect(res).To(Equal(map[interface{}]interface{}{
|
Expect(res).To(Equal(map[interface{}]interface{}{
|
||||||
"c3os": map[interface{}]interface{}{"network_token": "baz"},
|
"c3os": map[interface{}]interface{}{"network_token": "baz"},
|
||||||
@ -89,7 +98,7 @@ fooz:
|
|||||||
|
|
||||||
It("merges with bootargs", func() {
|
It("merges with bootargs", func() {
|
||||||
|
|
||||||
var cc string = `
|
var cc string = `#c3os-config
|
||||||
c3os:
|
c3os:
|
||||||
network_token: "foo"
|
network_token: "foo"
|
||||||
|
|
||||||
@ -107,7 +116,12 @@ bb:
|
|||||||
c, err := Scan(Directories(d), MergeBootLine, WithBootCMDLineFile(filepath.Join(d, "b")))
|
c, err := Scan(Directories(d), MergeBootLine, WithBootCMDLineFile(filepath.Join(d, "b")))
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(c.Options["foo"]).To(Equal("bar"))
|
Expect(c.Options["foo"]).To(Equal("bar"))
|
||||||
Expect(c.C3OS.NetworkToken).To(Equal("foo"))
|
|
||||||
|
providerCfg := &providerConfig.Config{}
|
||||||
|
err = c.Unmarshal(providerCfg)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(providerCfg.C3OS).ToNot(BeNil())
|
||||||
|
Expect(providerCfg.C3OS.NetworkToken).To(Equal("foo"))
|
||||||
_, exists := c.Data()["zz"]
|
_, exists := c.Data()["zz"]
|
||||||
Expect(exists).To(BeFalse())
|
Expect(exists).To(BeFalse())
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user