Skip to content

Commit

Permalink
Deleted combined entries while iterating over the list
Browse files Browse the repository at this point in the history
This doesn't work because of mikefarah/yq#2027

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
  • Loading branch information
jandubois committed Dec 1, 2024
1 parent 1489975 commit 98bd9be
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions pkg/limatmpl/assemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"

"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -266,6 +267,10 @@ func (tmpl *Template) copyListEntryComments(list string, dstIdx, srcIdx int, fie
tmpl.copyComments(dst, src, isMapElement)
}

func (tmpl *Template) deleteListEntry(list string, idx int) {
tmpl.expr.WriteString(fmt.Sprintf("| del($a.%s[%d], $b.%s[%d])\n", list, idx, list, idx))
}

// TODO: Maybe instead of hard-coding all the yaml names of LimaYAML struct fields we should
// TODO: retrieve them using reflection from the Go type tags to avoid possible typos.

Expand All @@ -291,10 +296,10 @@ func (tmpl *Template) combineListEntries() error {
// combineAdditionalDisks merges the additionalDisks lists. The shared key is the disk name.
func (tmpl *Template) combineAdditionalDisks() {
const additionalDisks = "additionalDisks"
var deleteExpr string

diskIdx := make(map[string]int, len(tmpl.Config.AdditionalDisks))
for src, disk := range tmpl.Config.AdditionalDisks {
for src := 0; src < len(tmpl.Config.AdditionalDisks); {
disk := tmpl.Config.AdditionalDisks[src]
var from, to int
if disk.Name == "*" {
// copy to **all** previous entries
Expand All @@ -307,6 +312,7 @@ func (tmpl *Template) combineAdditionalDisks() {
to = i
} else {
diskIdx[disk.Name] = src
src += 1
continue
}
}
Expand All @@ -330,18 +336,18 @@ func (tmpl *Template) combineAdditionalDisks() {
tmpl.copyListEntryComments(additionalDisks, dst, src, "")
}
}
deleteExpr = fmt.Sprintf("| del($a.%s[%d])", additionalDisks, src) + deleteExpr
tmpl.Config.AdditionalDisks = slices.Delete(tmpl.Config.AdditionalDisks, src, src+1)
tmpl.deleteListEntry(additionalDisks, src)
}
tmpl.expr.WriteString(deleteExpr)
}

// combineMounts merges the mounts lists. The shared key is the mount point.
func (tmpl *Template) combineMounts() {
const mounts = "mounts"
var deleteExpr string

mountPointIdx := make(map[string]int, len(tmpl.Config.Mounts))
for src, mount := range tmpl.Config.Mounts {
for src := 0; src < len(tmpl.Config.Mounts); {
mount := tmpl.Config.Mounts[src]
mountPoint := mount.Location
if mount.MountPoint != nil {
mountPoint = *mount.MountPoint
Expand All @@ -356,6 +362,7 @@ func (tmpl *Template) combineMounts() {
to = i
} else {
mountPointIdx[mountPoint] = src
src += 1
continue
}
}
Expand Down Expand Up @@ -413,18 +420,19 @@ func (tmpl *Template) combineMounts() {
tmpl.copyListEntryComments(mounts, dst, src, "virtiofs")
}
}
deleteExpr = fmt.Sprintf("| del($a.%s[%d])", mounts, src) + deleteExpr
tmpl.Config.Mounts = slices.Delete(tmpl.Config.Mounts, src, src+1)
tmpl.deleteListEntry(mounts, src)
}
tmpl.expr.WriteString(deleteExpr)
}

// combineNetworks merges the mounts lists. The shared key is the interface name.
func (tmpl *Template) combineNetworks() {
const networks = "networks"
var deleteExpr string

interfaceIdx := make(map[string]int, len(tmpl.Config.Networks))
for src, nw := range tmpl.Config.Networks {
for src := 0; src < len(tmpl.Config.Networks); {
nw := tmpl.Config.Networks[src]
logrus.Infof("Processing network[%d] %q", src, nw.Interface)
var from, to int
if nw.Interface == "*" {
from = 0
Expand All @@ -437,11 +445,13 @@ func (tmpl *Template) combineNetworks() {
if nw.Interface != "" {
interfaceIdx[nw.Interface] = src
}
src += 1
continue
}
}
for dst := from; dst <= to; dst++ {
dest := &tmpl.Config.Networks[dst]
logrus.Infof("Merging network[%d] %q into network[%d] %q", src, nw.Interface, dst, dest.Interface)
// Lima and Socket are mutually exclusive. Only copy base values if both are still unset.
if dest.Lima == "" && dest.Socket == "" {
if nw.Lima != "" {
Expand Down Expand Up @@ -469,9 +479,10 @@ func (tmpl *Template) combineNetworks() {
tmpl.copyListEntryComments(networks, dst, src, "")
}
}
deleteExpr = fmt.Sprintf("| del($a.%s[%d])", networks, src) + deleteExpr
logrus.Infof("Deleting interface %d", src)
tmpl.Config.Networks = slices.Delete(tmpl.Config.Networks, src, src+1)
tmpl.deleteListEntry(networks, src)
}
tmpl.expr.WriteString(deleteExpr)
}

// updateScript replaces one "file" property with the actual script and then renames the field to "script".
Expand Down

0 comments on commit 98bd9be

Please sign in to comment.