From f39b23394b9d06774cf096de7d342201d4b264ca Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Tue, 31 Oct 2023 00:07:09 +0800 Subject: [PATCH] refactor(main): unmount override container (#4161) (#4208) (cherry picked from commit 4fed20a298b95f89782d84ad9349f456efdd0646) Signed-off-by: cuisongliu --- pkg/apply/processor/install.go | 28 +++++++++++++++++++++++--- pkg/types/v1beta1/cluster_args.go | 33 +++++-------------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/pkg/apply/processor/install.go b/pkg/apply/processor/install.go index ccd2444fe44..ddcfc0d0c7a 100644 --- a/pkg/apply/processor/install.go +++ b/pkg/apply/processor/install.go @@ -17,6 +17,7 @@ package processor import ( "context" "fmt" + "sort" "strings" "github.com/labring/sealos/pkg/utils/rand" @@ -139,9 +140,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 @@ -167,8 +186,11 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { return err } mount.Env = maps.MergeMap(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) } runtime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig()) diff --git a/pkg/types/v1beta1/cluster_args.go b/pkg/types/v1beta1/cluster_args.go index 194a5b70791..7d03d9508af 100644 --- a/pkg/types/v1beta1/cluster_args.go +++ b/pkg/types/v1beta1/cluster_args.go @@ -135,36 +135,13 @@ func (c *Cluster) GetRootfsImage() *MountImage { return image } -func (c *Cluster) FindImage(targetImage string) *MountImage { - var image *MountImage - if c.Status.Mounts != nil { - for _, img := range c.Status.Mounts { - if img.ImageName == targetImage { - image = &img - break - } - } - } - return image -} - -func (c *Cluster) SetMountImage(targetMount *MountImage) { - tgMount := targetMount.DeepCopy() - if c.Status.Mounts != nil { - if tgMount != nil { - hasMount := false - for i, img := range c.Status.Mounts { - if img.Name == tgMount.Name && img.Type == tgMount.Type { - c.Status.Mounts[i] = *tgMount - hasMount = true - break - } - } - if !hasMount { - c.Status.Mounts = append(c.Status.Mounts, *tgMount) - } +func (c *Cluster) FindImage(name string) (int, *MountImage) { + for i, img := range c.Status.Mounts { + if img.ImageName == name { + return i, &img } } + return -1, nil } func (c *Cluster) ReplaceRootfsImage() {