Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
fix merging multuple configs with kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jun 25, 2019
1 parent 8985af0 commit 0f10bf5
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
2 changes: 2 additions & 0 deletions fixtures/kubernetes.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
kubernetes:
version: 1.14.1
commands:
- echo 1.14.1 > /etc/kubernetes_version
26 changes: 23 additions & 3 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func GetKeys(m map[string]File) []string {
//ToCloudInit will apply all phases and produce a CloudInit object from the results
func (sys *Config) ToCloudInit() cloudinit.CloudInit {
cloud := sys.Extra
log.Debugf("Extra: %+v", cloud)

files, commands, err := sys.ApplyPhases()
if err != nil {
Expand All @@ -101,6 +102,8 @@ func (sys *Config) ToCloudInit() cloudinit.CloudInit {
for path, content := range files {
cloud.AddFile(path, content.Content)
}
yml, _ := yaml.Marshal(sys)
cloud.AddFile("/etc/konfigadm.yml", string(yml))
cloud.AddFile(fmt.Sprintf("/usr/bin/%s.sh", konfigadm), ToScript(commands))
cloud.AddCommand(fmt.Sprintf("/usr/bin/%s.sh", konfigadm))
return *cloud
Expand All @@ -117,7 +120,9 @@ func ToScript(commands []Command) string {

func (sys *Config) Init() {
sys.Services = make(map[string]Service)
sys.Extra = &cloudinit.CloudInit{}
if sys.Extra == nil {
sys.Extra = &cloudinit.CloudInit{}
}
sys.Environment = make(map[string]string)
sys.Files = make(map[string]string)
sys.Filesystem = make(map[string]File)
Expand Down Expand Up @@ -159,6 +164,17 @@ func (sys Config) String() {

//ImportConfig merges to configs together, everything but containerRuntime and Kubernetes configs are merged
func (sys *Config) ImportConfig(c2 Config) {
if c2.Extra != nil {
if strings.TrimSpace(fmt.Sprintf("%+v", sys.Extra)) == "{}" {
sys.Extra = c2.Extra
} else if strings.TrimSpace(fmt.Sprintf("%+v", c2.Extra)) != "#cloud-config\n{}" {
log.Warnf("More than 1 extra cloud-init section found , merging clout-init is not supported and will override")
sys.Extra = c2.Extra
}
}
if c2.Cleanup != nil {
sys.Cleanup = c2.Cleanup
}
sys.Commands = append(sys.Commands, c2.Commands...)
sys.PreCommands = append(sys.PreCommands, c2.PreCommands...)
sys.PostCommands = append(sys.PostCommands, c2.PostCommands...)
Expand Down Expand Up @@ -190,6 +206,10 @@ func (sys *Config) ImportConfig(c2 Config) {
pkgs := append(*sys.Packages, *c2.Packages...)
sys.Packages = &pkgs
sys.Timezone = c2.Timezone
sys.ContainerRuntime = c2.ContainerRuntime
sys.Kubernetes = c2.Kubernetes
if c2.ContainerRuntime != nil {
sys.ContainerRuntime = c2.ContainerRuntime
}
if c2.Kubernetes != nil {
sys.Kubernetes = c2.Kubernetes
}
}
1 change: 0 additions & 1 deletion test/apply_test.go

This file was deleted.

81 changes: 81 additions & 0 deletions test/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package test

import (
"strings"
"testing"

_ "github.com/moshloop/konfigadm/pkg"
"github.com/moshloop/konfigadm/pkg/types"

"github.com/onsi/gomega"
)

func init() {

}

type Fixture struct {
files []string
vars []string
flags []types.Flag
t *testing.T
g *gomega.WithT
}

func (f *Fixture) WithVars(vars ...string) *Fixture {
f.vars = vars
return f
}

func (f *Fixture) WithFlags(flags ...types.Flag) *Fixture {
f.flags = flags
return f
}

func (f *Fixture) Build() (*types.Config, *gomega.WithT) {
cfg, err := types.NewConfig(f.files...).
WithFlags(f.flags...).
WithVars(f.vars...).
Build()
if err != nil {
f.t.Error(err)
}
return cfg, f.g
}

func NewFixture(t *testing.T, files ...string) *Fixture {
return &Fixture{
files: files,
t: t,
g: gomega.NewWithT(t),
}
}

var PATH = "../fixtures/"

func TestImportKubernetesIntoPackages(t *testing.T) {
cfg, g := NewFixture(t, PATH+"packages.yml", PATH+"kubernetes.yml").
WithFlags(types.DEBIAN).
Build()
g.Expect(*cfg.Packages).NotTo(gomega.BeEmpty())
}

func TestImportKubernetesWithCommand(t *testing.T) {
cfg, g := NewFixture(t, PATH+"kubernetes.yml").
WithFlags(types.DEBIAN, types.DEBIAN_LIKE).
Build()
cfg.Extra.FileEncoding = ""
count := strings.Count(cfg.ToCloudInit().String(), "echo 1.14.1 > /etc/kubernetes_version")
g.Expect(count).To(gomega.BeEquivalentTo(1))
g.Expect(*cfg.Packages).NotTo(gomega.BeEmpty())

}

func TestImportThree(t *testing.T) {
cfg, g := NewFixture(t, PATH+"packages.yml", PATH+"kubernetes.yml", PATH+"ssh.yml").
WithFlags(types.DEBIAN, types.DEBIAN_LIKE).
Build()
cfg.Extra.FileEncoding = ""
cloudinit := cfg.ToCloudInit().String()
g.Expect(cloudinit).To(gomega.ContainSubstring("kubeadm"))
}
18 changes: 18 additions & 0 deletions test/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/ory/dockertest"
yaml "gopkg.in/yaml.v3"
)

// uses a sensible default on windows (tcp/http) and linux/osx (socket)
Expand Down Expand Up @@ -139,6 +140,23 @@ func (c konfigadm) Apply(config ...string) bool {
return false
}

func TestYamlRoundTrip(t *testing.T) {
for _, f := range fixtures {
t.Run(f.in, func(t *testing.T) {
_, container := setup(t)
defer container.Delete()
stdout, err := container.Exec(binary, "minify", "-c", cwd+"/fixtures/"+f.in)
if err != nil {
t.Errorf("Minify failed %s:\n %s\n", f.in, stdout)
}
var data map[string]interface{}
if err := yaml.Unmarshal([]byte(stdout), &data); err != nil {
t.Errorf("Failed to unmarshall: %s\n%s", err, stdout)
}
})
}
}

func TestFull(t *testing.T) {
for _, f := range fixtures {
t.Run(f.in, func(t *testing.T) {
Expand Down

0 comments on commit 0f10bf5

Please sign in to comment.