mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feat(region,host): clean no persistent rootfs (#23353)
This commit is contained in:
@@ -361,4 +361,7 @@ type ContainerRootfs struct {
|
||||
Type ContainerVolumeMountType `json:"type"`
|
||||
Disk *ContainerVolumeMountDisk `json:"disk"`
|
||||
//CephFS *ContainerVolumeMountCephFS `json:"ceph_fs"`
|
||||
|
||||
// 是否持久化
|
||||
Persistent bool `json:"persistent"`
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ type ContainerRootfs struct {
|
||||
Type apis.ContainerVolumeMountType `json:"type"`
|
||||
Disk *ContainerVolumeMountDisk `json:"disk"`
|
||||
// CephFS *ContainerVolumeMountCephFS `json:"ceph_fs"`
|
||||
// 是否持久化
|
||||
Persistent bool `default:"false" list:"user" create:"admin_optional" update:"admin"`
|
||||
}
|
||||
|
||||
type ContainerVolumeMount struct {
|
||||
|
||||
@@ -149,6 +149,7 @@ func (d disk) ToHostRootFs(rootFs *apis.ContainerRootfs) (*hostapi.ContainerRoot
|
||||
Id: disk.Id,
|
||||
SubDirectory: disk.SubDirectory,
|
||||
},
|
||||
Persistent: rootFs.Persistent,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -654,21 +654,40 @@ func (s *sPodGuestInstance) ConvertRootFsToVolumeMount(rootFs *hostapi.Container
|
||||
|
||||
func (s *sPodGuestInstance) mountRootFs(ctrId string, rootFs *hostapi.ContainerRootfs) error {
|
||||
drv := volume_mount.GetDriver(rootFs.Type)
|
||||
vol := &hostapi.ContainerVolumeMount{
|
||||
Type: rootFs.Type,
|
||||
Disk: rootFs.Disk,
|
||||
}
|
||||
vol := s.ConvertRootFsToVolumeMount(rootFs)
|
||||
if err := drv.Mount(s, ctrId, vol); err != nil {
|
||||
return errors.Wrapf(err, "mount root fs %s, ctrId %s", jsonutils.Marshal(rootFs), ctrId)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sPodGuestInstance) umountRootFs(ctrId string, rootFs *hostapi.ContainerRootfs) error {
|
||||
func (s *sPodGuestInstance) clearContainerRootFs(ctrId string, rootFs *hostapi.ContainerRootfs) error {
|
||||
if rootFs == nil {
|
||||
return nil
|
||||
}
|
||||
drv := volume_mount.GetDriver(rootFs.Type)
|
||||
vol := &hostapi.ContainerVolumeMount{
|
||||
Type: rootFs.Type,
|
||||
Disk: rootFs.Disk,
|
||||
vol := s.ConvertRootFsToVolumeMount(rootFs)
|
||||
hostPath, err := drv.GetRuntimeMountHostPath(s, ctrId, vol)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "get runtime mount path")
|
||||
}
|
||||
if !rootFs.Persistent {
|
||||
if err := volume_mount.RemoveDir(hostPath); err != nil {
|
||||
return errors.Wrapf(err, "remove %q", hostPath)
|
||||
}
|
||||
log.Infof("clear rootfs of container %s/%s: %q", s.GetName(), ctrId, hostPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sPodGuestInstance) umountRootFs(ctrId string, rootFs *hostapi.ContainerRootfs) error {
|
||||
if rootFs == nil {
|
||||
return nil
|
||||
}
|
||||
drv := volume_mount.GetDriver(rootFs.Type)
|
||||
vol := s.ConvertRootFsToVolumeMount(rootFs)
|
||||
if err := s.clearContainerRootFs(ctrId, rootFs); err != nil {
|
||||
return errors.Wrapf(err, "clear rootfs of container %s", ctrId)
|
||||
}
|
||||
if err := drv.Unmount(s, ctrId, vol); err != nil {
|
||||
return errors.Wrapf(err, "unmount root fs %s, ctrId %s", jsonutils.Marshal(rootFs), ctrId)
|
||||
@@ -1478,6 +1497,11 @@ func (s *sPodGuestInstance) StopContainer(ctx context.Context, userCred mcclient
|
||||
// 后期可以添加主动通知刷新
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
if ctr := s.GetContainerById(ctrId); ctr != nil {
|
||||
if err := s.clearContainerRootFs(ctrId, ctr.Spec.Rootfs); err != nil {
|
||||
return nil, errors.Wrapf(err, "clear container rootfs %s", jsonutils.Marshal(ctr.Spec.Rootfs))
|
||||
}
|
||||
}
|
||||
if err := s.startStat.RemoveContainerFile(ctrId); err != nil {
|
||||
return nil, errors.Wrap(err, "startStat.RemoveContainerFile")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user