From 286c18103b5d4ee8299719e5394cc79fe886c662 Mon Sep 17 00:00:00 2001 From: sealos-ci-robot <109538726+sealos-ci-robot@users.noreply.github.com> Date: Mon, 30 Oct 2023 17:49:47 +0800 Subject: [PATCH] refactor(main): unmount override container (#4161) (#4205) Signed-off-by: cuisongliu Co-authored-by: cuisongliu --- pkg/apply/processor/install.go | 28 +++++++++++++++++++++++++--- pkg/types/v1beta1/utils.go | 24 ++++-------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/pkg/apply/processor/install.go b/pkg/apply/processor/install.go index 82590235693..22da4a45392 100644 --- a/pkg/apply/processor/install.go +++ b/pkg/apply/processor/install.go @@ -17,6 +17,7 @@ package processor import ( "context" "fmt" + "sort" "strings" "golang.org/x/sync/errgroup" @@ -140,9 +141,27 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { imageTypes.Insert(string(v2.AppImage)) } } + // This code ensures that `mount` always contains the latest `MountImage` instances from `cluster.Status.Mounts` + // and that each `ImageName` is represented by only one corresponding instance in `mounts`. + mountIndexes := make(map[string]int) + for i := range cluster.Status.Mounts { + mountIndexes[cluster.Status.Mounts[i].ImageName] = i + } + + indexes := make([]int, 0) + for i := range mountIndexes { + indexes = append(indexes, mountIndexes[i]) + } + sort.Ints(indexes) + mounts := make([]v2.MountImage, 0) + for i := range indexes { + mounts = append(mounts, cluster.Status.Mounts[i]) + } + cluster.Status.Mounts = mounts + for _, img := range c.NewImages { + index, mount := cluster.FindImage(img) var ctrName string - mount := cluster.FindImage(img) if mount != nil { if !ForceOverride { continue @@ -168,8 +187,11 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { return err } mount.Env = maps.Merge(mount.Env, c.ExtraEnvs) - - cluster.SetMountImage(mount) + // This code ensures that `cluster.Status.Mounts` always contains the latest `MountImage` instances + if index >= 0 { + cluster.Status.Mounts = append(cluster.Status.Mounts[:index], cluster.Status.Mounts[index+1:]...) + } + cluster.Status.Mounts = append(cluster.Status.Mounts, *mount) c.NewMounts = append(c.NewMounts, *mount) } diff --git a/pkg/types/v1beta1/utils.go b/pkg/types/v1beta1/utils.go index ab6086ef88c..a27a40ef456 100644 --- a/pkg/types/v1beta1/utils.go +++ b/pkg/types/v1beta1/utils.go @@ -117,29 +117,13 @@ func (c *Cluster) GetRootfsImage() *MountImage { return nil } -func (c *Cluster) FindImage(name string) *MountImage { - for _, img := range c.Status.Mounts { +func (c *Cluster) FindImage(name string) (int, *MountImage) { + for i, img := range c.Status.Mounts { if img.ImageName == name { - return &img - } - } - return nil -} - -func (c *Cluster) SetMountImage(mount *MountImage) { - if mount == nil { - return - } - - if c.Status.Mounts != nil { - for i, img := range c.Status.Mounts { - if img.Name == mount.Name && img.Type == mount.Type { - c.Status.Mounts[i] = *mount.DeepCopy() - return - } + return i, &img } - c.Status.Mounts = append(c.Status.Mounts, *mount) } + return -1, nil } func (c *Cluster) ReplaceRootfsImage() {