fix update volume bugs, and volume type mem bugs

This commit is contained in:
凡羊羊 2019-12-13 11:31:08 +08:00
parent f0306df2cf
commit e9f5b0f456
3 changed files with 21 additions and 44 deletions

View File

@ -1446,49 +1446,27 @@ func (s *ServiceAction) UpdVolume(sid string, req *api_model.UpdVolumeReq) error
tx.Rollback() tx.Rollback()
} }
}() }()
switch req.VolumeType { v, err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).GetVolumeByServiceIDAndName(sid, req.VolumeName)
case "config-file": if err != nil {
if req.VolumePath != "" { tx.Rollback()
v, err := db.GetManager().TenantServiceVolumeDaoTransactions(tx). return err
GetVolumeByServiceIDAndName(sid, req.VolumeName) }
if err != nil { v.VolumePath = req.VolumePath
tx.Rollback() if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil {
return err tx.Rollback()
} return err
v.VolumePath = req.VolumePath }
if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil { if req.VolumeType == "config-file" {
tx.Rollback() configfile, err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).GetByVolumeName(sid, req.VolumeName)
return err
}
}
if req.FileContent != "" {
configfile, err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).
GetByVolumeName(sid, req.VolumeName)
if err != nil {
tx.Rollback()
return err
}
configfile.FileContent = req.FileContent
if err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).UpdateModel(configfile); err != nil {
tx.Rollback()
return err
}
}
case dbmodel.ShareFileVolumeType.String(), dbmodel.LocalVolumeType.String():
v, err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).
GetVolumeByServiceIDAndName(sid, req.VolumeName)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
v.VolumePath = req.VolumePath configfile.FileContent = req.FileContent
if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil { if err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).UpdateModel(configfile); err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
default:
tx.Rollback()
return fmt.Errorf("unsupported volume type")
} }
tx.Commit() tx.Commit()
return nil return nil

View File

@ -284,9 +284,9 @@ type V2DelVolumeDependencyStruct struct {
// UpdVolumeReq is a value struct holding request for updating volume. // UpdVolumeReq is a value struct holding request for updating volume.
type UpdVolumeReq struct { type UpdVolumeReq struct {
VolumeName string `json:"volume_name" validate:"required"` VolumeName string `json:"volume_name" validate:"required"`
VolumeType string `json:"volume_type" validate:"volume_type|required|in:share-file,local,memoryfs,config-file,alicloud-disk"` VolumeType string `json:"volume_type" validate:"volume_type|required"`
FileContent string `json:"file_content"` FileContent string `json:"file_content"`
VolumePath string `json:"volume_path"` VolumePath string `json:"volume_path" validate:"volume_path|required"`
} }
// VolumeWithStatusResp volume status // VolumeWithStatusResp volume status

View File

@ -32,21 +32,20 @@ type MemoryFSVolume struct {
// CreateVolume memory fs volume create volume // CreateVolume memory fs volume create volume
func (v *MemoryFSVolume) CreateVolume(define *Define) error { func (v *MemoryFSVolume) CreateVolume(define *Define) error {
volumeMountName := fmt.Sprintf("mnt%d", v.svm.ID) volumeMountName := fmt.Sprintf("manual%d", v.svm.ID)
volumeMountPath := v.svm.VolumePath volumeMountPath := v.svm.VolumePath
volumeReadOnly := false volumeReadOnly := false
if volumeMountPath != "" { if volumeMountPath == "" {
logrus.Warningf("service[%s]'s mount path is empty, skip it", v.version.ServiceID) logrus.Warningf("service[%s]'s mount path is empty, skip create memoryfs", v.version.ServiceID)
return nil return nil
} }
for _, m := range define.volumeMounts { for _, m := range define.volumeMounts {
if m.MountPath == volumeMountPath { if m.MountPath == volumeMountPath {
logrus.Warningf("found the same mount path: %s, skip it", volumeMountPath) logrus.Warningf("service[%s]'s found the same mount path: %s, skip create memoryfs", v.version.ServiceID, volumeMountPath)
return nil return nil
} }
} }
name := fmt.Sprintf("manual%d", v.svm.ID) vo := corev1.Volume{Name: volumeMountName} // !!!: volumeMount name of k8s model must equal to volume name of k8s model
vo := corev1.Volume{Name: name}
vo.EmptyDir = &corev1.EmptyDirVolumeSource{ vo.EmptyDir = &corev1.EmptyDirVolumeSource{
Medium: corev1.StorageMediumMemory, Medium: corev1.StorageMediumMemory,
} }