From e9f5b0f4565cb32fffaa77b8d88743e67c6dafd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E7=BE=8A=E7=BE=8A?= Date: Fri, 13 Dec 2019 11:31:08 +0800 Subject: [PATCH] fix update volume bugs, and volume type mem bugs --- api/handler/service.go | 50 ++++++++++------------------------ api/model/volume.go | 4 +-- worker/appm/volume/memoryfs.go | 11 ++++---- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/api/handler/service.go b/api/handler/service.go index 492aef133..b069eccb7 100644 --- a/api/handler/service.go +++ b/api/handler/service.go @@ -1446,49 +1446,27 @@ func (s *ServiceAction) UpdVolume(sid string, req *api_model.UpdVolumeReq) error tx.Rollback() } }() - switch req.VolumeType { - case "config-file": - if req.VolumePath != "" { - v, err := db.GetManager().TenantServiceVolumeDaoTransactions(tx). - GetVolumeByServiceIDAndName(sid, req.VolumeName) - if err != nil { - tx.Rollback() - return err - } - v.VolumePath = req.VolumePath - if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil { - tx.Rollback() - 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) + v, err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).GetVolumeByServiceIDAndName(sid, req.VolumeName) + if err != nil { + tx.Rollback() + return err + } + v.VolumePath = req.VolumePath + if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil { + tx.Rollback() + return err + } + if req.VolumeType == "config-file" { + configfile, err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).GetByVolumeName(sid, req.VolumeName) if err != nil { tx.Rollback() return err } - v.VolumePath = req.VolumePath - if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).UpdateModel(v); err != nil { + configfile.FileContent = req.FileContent + if err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).UpdateModel(configfile); err != nil { tx.Rollback() return err } - default: - tx.Rollback() - return fmt.Errorf("unsupported volume type") } tx.Commit() return nil diff --git a/api/model/volume.go b/api/model/volume.go index 4d272cae2..713fd38b3 100644 --- a/api/model/volume.go +++ b/api/model/volume.go @@ -284,9 +284,9 @@ type V2DelVolumeDependencyStruct struct { // UpdVolumeReq is a value struct holding request for updating volume. type UpdVolumeReq struct { 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"` - VolumePath string `json:"volume_path"` + VolumePath string `json:"volume_path" validate:"volume_path|required"` } // VolumeWithStatusResp volume status diff --git a/worker/appm/volume/memoryfs.go b/worker/appm/volume/memoryfs.go index 3cd21141f..b1fa5c305 100644 --- a/worker/appm/volume/memoryfs.go +++ b/worker/appm/volume/memoryfs.go @@ -32,21 +32,20 @@ type MemoryFSVolume struct { // CreateVolume memory fs volume create volume 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 volumeReadOnly := false - if volumeMountPath != "" { - logrus.Warningf("service[%s]'s mount path is empty, skip it", v.version.ServiceID) + if volumeMountPath == "" { + logrus.Warningf("service[%s]'s mount path is empty, skip create memoryfs", v.version.ServiceID) return nil } for _, m := range define.volumeMounts { 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 } } - name := fmt.Sprintf("manual%d", v.svm.ID) - vo := corev1.Volume{Name: name} + vo := corev1.Volume{Name: volumeMountName} // !!!: volumeMount name of k8s model must equal to volume name of k8s model vo.EmptyDir = &corev1.EmptyDirVolumeSource{ Medium: corev1.StorageMediumMemory, }