Skip to content

Commit

Permalink
refactor(main): unmount override container
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu committed Oct 24, 2023
1 parent 17a0e3e commit 0fd86df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
35 changes: 30 additions & 5 deletions pkg/apply/processor/install.go
Expand Up @@ -140,17 +140,42 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error {
imageTypes.Insert(string(v2.AppImage))
}
}
syncMounts := make([]v2.MountImage, 0)
for _, img := range c.NewImages {
var ctrName string
mount := cluster.FindImage(img)
var ctrName string
if mount != nil {
if !ForceOverride {
continue
}
logger.Debug("trying to override app %s", img)
if err := c.Buildah.Delete(mount.Name); err != nil {
allContainers, err := c.Buildah.ListContainers()
if err != nil {
return err
}
imgID, err := c.Buildah.InspectImage(img)
if err != nil {
return err
}
for _, ctr := range allContainers {
ctrImageID, err := c.Buildah.InspectImage(ctr.ImageName)
if err != nil {
logger.Warn("container get image error: %+v", err)
continue
}
if ctrImageID.FromImageID == imgID.FromImageID {
logger.Debug("trying to delete app %s %s", ctr.ImageName, ctr.ContainerName)
if err := c.Buildah.Delete(ctr.ContainerName); err != nil {
logger.Warn("delete container error: %+v", err)
}
}
}
if cluster.Status.Mounts != nil {
for _, mot := range cluster.Status.Mounts {
if mot.ImageName != mount.ImageName || mot.Type != mount.Type {
syncMounts = append(syncMounts, mot)
}
}
}
}
ctrName = rand.Generator(8)
cluster.Spec.Image = stringsutil.Merge(cluster.Spec.Image, img)
Expand All @@ -168,8 +193,8 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error {
return err
}
mount.Env = maps.Merge(mount.Env, c.ExtraEnvs)

cluster.SetMountImage(mount)
cluster.Status.Mounts = syncMounts
cluster.Status.Mounts = append(cluster.Status.Mounts, *mount)
c.NewMounts = append(c.NewMounts, *mount)
}

Expand Down
16 changes: 0 additions & 16 deletions pkg/types/v1beta1/utils.go
Expand Up @@ -126,22 +126,6 @@ func (c *Cluster) FindImage(name string) *MountImage {
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
}
}
c.Status.Mounts = append(c.Status.Mounts, *mount)
}
}

func (c *Cluster) ReplaceRootfsImage() {
i1, i2 := -1, -1
var v1, v2 string
Expand Down

0 comments on commit 0fd86df

Please sign in to comment.