From 98bd9bec1d9da6e1b2293a666abbdf84baea7227 Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Sat, 30 Nov 2024 20:49:29 -0800 Subject: [PATCH] Deleted combined entries while iterating over the list This doesn't work because of https://github.com/mikefarah/yq/issues/2027 Signed-off-by: Jan Dubois --- pkg/limatmpl/assemble.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/limatmpl/assemble.go b/pkg/limatmpl/assemble.go index 53d906eb522d..4964f30e7e83 100644 --- a/pkg/limatmpl/assemble.go +++ b/pkg/limatmpl/assemble.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" "github.com/coreos/go-semver/semver" @@ -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. @@ -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 @@ -307,6 +312,7 @@ func (tmpl *Template) combineAdditionalDisks() { to = i } else { diskIdx[disk.Name] = src + src += 1 continue } } @@ -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 @@ -356,6 +362,7 @@ func (tmpl *Template) combineMounts() { to = i } else { mountPointIdx[mountPoint] = src + src += 1 continue } } @@ -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 @@ -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 != "" { @@ -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".