Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute parseable yaml files during runStage #130

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/rancher/yip

go 1.22
go 1.22.0

toolchain go1.22.7

require (
Expand Down
57 changes: 39 additions & 18 deletions pkg/executor/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ func (l opList) uniqueNames() {
}
}

type loaderError struct {
path string
}

func (e loaderError) Error() string {
return fmt.Sprintf("error loading path '%s'", e.path)
}

func newLoaderError(path string) loaderError {
return loaderError{
path,
}
}

func (e *DefaultExecutor) applyStage(stage schema.Stage, fs vfs.FS, console plugins.Console) error {
var errs error
for _, p := range e.conditionals {
Expand Down Expand Up @@ -167,10 +181,10 @@ func (e *DefaultExecutor) genOpFromSchema(file, stage string, config schema.YipC
return results
}

func (e *DefaultExecutor) dirOps(stage, dir string, fs vfs.FS, console plugins.Console) ([]*op, error) {
results := []*op{}
func (e *DefaultExecutor) dirOps(stage, dir string, fs vfs.FS, console plugins.Console) (results []*op, loaderError error, err error) {
results = []*op{}
prev := []*op{}
err := vfs.Walk(fs, dir,
err = vfs.Walk(fs, dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -189,9 +203,11 @@ func (e *DefaultExecutor) dirOps(stage, dir string, fs vfs.FS, console plugins.C

config, err := schema.Load(path, fs, schema.FromFile, e.modifier)
if err != nil {
return err

e.logger.Warnf("failed to load file '%s': %s", path, err.Error())
loaderError = multierror.Append(loaderError, newLoaderError(path))
return nil
kkaempf marked this conversation as resolved.
Show resolved Hide resolved
}

ops := e.genOpFromSchema(path, stage, *config, fs, console)
// mark lexicographic order dependency from previous blocks
if len(prev) > 0 && len(ops) > 0 {
Expand All @@ -209,7 +225,8 @@ func (e *DefaultExecutor) dirOps(stage, dir string, fs vfs.FS, console plugins.C
results = append(results, ops...)
return nil
})
return results, err

return results, loaderError, err
}

func writeDAG(dag [][]dag.GraphEntry) {
Expand All @@ -227,17 +244,17 @@ func writeDAG(dag [][]dag.GraphEntry) {
}

func (e *DefaultExecutor) Graph(stage string, fs vfs.FS, console plugins.Console, source string) ([][]dag.GraphEntry, error) {
g, err := e.prepareDAG(stage, source, fs, console)
g, loaderError, err := e.prepareDAG(stage, source, fs, console)
if err != nil {
return nil, err
}
return g.Analyze(), err
return g.Analyze(), loaderError
}

func (e *DefaultExecutor) Analyze(stage string, fs vfs.FS, console plugins.Console, args ...string) {
var errs error
for _, source := range args {
g, err := e.prepareDAG(stage, source, fs, console)
g, _, err := e.prepareDAG(stage, source, fs, console)
if err != nil {
errs = multierror.Append(errs, err)
continue
Expand All @@ -255,35 +272,35 @@ func (e *DefaultExecutor) Analyze(stage string, fs vfs.FS, console plugins.Conso
}
}

func (e *DefaultExecutor) prepareDAG(stage, uri string, fs vfs.FS, console plugins.Console) (*dag.Graph, error) {
func (e *DefaultExecutor) prepareDAG(stage, uri string, fs vfs.FS, console plugins.Console) (g *dag.Graph, loaderError error, err error) {
f, err := fs.Stat(uri)

g := dag.DAG(dag.EnableInit)
g = dag.DAG(dag.EnableInit)
var ops opList
switch {
case err == nil && f.IsDir():
ops, err = e.dirOps(stage, uri, fs, console)
ops, loaderError, err = e.dirOps(stage, uri, fs, console)
if err != nil {
return nil, err
return nil, loaderError, err
}
case err == nil:
config, err := schema.Load(uri, fs, schema.FromFile, e.modifier)
if err != nil {
return nil, err
return nil, nil, err
}

ops = e.genOpFromSchema(uri, stage, *config, fs, console)
case utils.IsUrl(uri):
config, err := schema.Load(uri, fs, schema.FromUrl, e.modifier)
if err != nil {
return nil, err
return nil, nil, err
}

ops = e.genOpFromSchema(uri, stage, *config, fs, console)
default:
config, err := schema.Load(uri, fs, nil, e.modifier)
if err != nil {
return nil, err
return nil, nil, err
}

ops = e.genOpFromSchema("<STDIN>", stage, *config, fs, console)
Expand All @@ -295,11 +312,11 @@ func (e *DefaultExecutor) prepareDAG(stage, uri string, fs vfs.FS, console plugi
g.Add(o.name, append(o.options, dag.WithCallback(o.fn), dag.WithDeps(append(o.after, o.deps...)...))...)
}

return g, nil
return g, loaderError, nil
}

func (e *DefaultExecutor) runStage(stage, uri string, fs vfs.FS, console plugins.Console) (err error) {
g, err := e.prepareDAG(stage, uri, fs, console)
g, loaderError, err := e.prepareDAG(stage, uri, fs, console)
if err != nil {
return err
}
Expand All @@ -321,6 +338,10 @@ func (e *DefaultExecutor) runStage(stage, uri string, fs vfs.FS, console plugins
}
}

if loaderError != nil {
err = multierror.Append(err, loaderError)
}

return err
}

Expand Down
77 changes: 59 additions & 18 deletions pkg/executor/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,25 +487,25 @@ stages:
stages:
initramfs:
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
`,
})
Expect(err).Should(BeNil())
Expand All @@ -531,28 +531,28 @@ stages:
"/some/yip/01_first.yaml": `
stages:
initramfs:
- name: Create Kairos User
- name: Create User
users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
- name: Create Kairos User
passwd: hunter2
- name: Create User
users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
- users:
kairos:
user1:
groups:
- sudo
passwd: kairos
passwd: hunter2
`,
})
Expect(err).Should(BeNil())
Expand Down Expand Up @@ -748,5 +748,46 @@ stages:
g, _ := def.Graph("default", vfs.OSFS, testConsole, temp)
Expect(len(g)).To(Equal(4))
})
It("loads a graph with malformed yaml files", func() {
davidcassany marked this conversation as resolved.
Show resolved Hide resolved
def := NewExecutor()
testConsole := console.NewStandardConsole()

fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"/some/yip/01_first.yaml": `
name: "Rootfs Layout Settings"
stages:
rootfs.before:
- name: "before roots"
commands:
- echo "rootfs.before"
rootfs:
- name: "rootfs"
commands:
- echo "rootfs"
- name: "rootfs 2"
commands:
- echo "2"
initramfs:
- name: "initramfs"
commands:
- echo "initramfs"
`,
"/some/yip/02_malformed.yaml": `
name: "second Rootfs Layout Settings"
stages:
rootfs.before:
# bad indentation
- name: "second before roots"
commands:
- echo "second.rootfs.before"
`,
})
Expect(err).Should(BeNil())
defer cleanup()

g, err := def.Graph("rootfs", fs, testConsole, "/some/yip")
Expect(err).Should(HaveOccurred())
Expect(len(g)).To(Equal(3))
})
})
})