Skip to content

Commit

Permalink
refactor(main): unmount override container (#4161) (#4208)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4fed20a)

Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu committed Oct 30, 2023
1 parent 5cd4716 commit f39b233
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
28 changes: 25 additions & 3 deletions pkg/apply/processor/install.go
Expand Up @@ -17,6 +17,7 @@ package processor
import (
"context"
"fmt"
"sort"
"strings"

"github.com/labring/sealos/pkg/utils/rand"
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down
33 changes: 5 additions & 28 deletions pkg/types/v1beta1/cluster_args.go
Expand Up @@ -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() {
Expand Down

0 comments on commit f39b233

Please sign in to comment.