diff --git a/pkg/apply/processor/install.go b/pkg/apply/processor/install.go index 82590235693..b834486372b 100644 --- a/pkg/apply/processor/install.go +++ b/pkg/apply/processor/install.go @@ -140,17 +140,46 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { imageTypes.Insert(string(v2.AppImage)) } } + syncMounts := make([]v2.MountImage, 0) + imageKeys := sets.NewString() + if cluster.Status.Mounts != nil { + for _, mot := range cluster.Status.Mounts { + if imageKeys.Has(mot.ImageName) { + continue + } + imageKeys.Insert(mot.ImageName) + syncMounts = append(syncMounts, mot) + } + } + cluster.Status.Mounts = syncMounts for _, img := range c.NewImages { + index, mount := cluster.FindImage(img) var ctrName string - mount := cluster.FindImage(img) 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(mount.ImageName) + 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) + } + } + } } ctrName = rand.Generator(8) cluster.Spec.Image = stringsutil.Merge(cluster.Spec.Image, img) @@ -168,8 +197,11 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { return err } mount.Env = maps.Merge(mount.Env, c.ExtraEnvs) - - cluster.SetMountImage(mount) + if index >= 0 { + cluster.Status.Mounts[index] = *mount + continue + } + 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() {