diff --git a/pkg/cloud/entity/persistentvolume.go b/pkg/cloud/entity/persistentvolume.go new file mode 100644 index 0000000..3e2d120 --- /dev/null +++ b/pkg/cloud/entity/persistentvolume.go @@ -0,0 +1,15 @@ +package entity + +import ( + lcorev1 "k8s.io/api/core/v1" +) + +type PersistentVolume struct { + *lcorev1.PersistentVolume +} + +func NewPersistentVolume(ppv *lcorev1.PersistentVolume) *PersistentVolume { + return &PersistentVolume{ + PersistentVolume: ppv, + } +} diff --git a/pkg/cloud/errors/error_code.go b/pkg/cloud/errors/error_code.go index 6122148..870a831 100644 --- a/pkg/cloud/errors/error_code.go +++ b/pkg/cloud/errors/error_code.go @@ -6,6 +6,9 @@ import lsdkErrs "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error" const ( EcK8sPvcFailedToGet = lsdkErrs.ErrorCode("K8sPvcFailedToGet") EcK8sPvcNotFound = lsdkErrs.ErrorCode("K8sPvcNotFound") + + EcK8sPvFailedToGet = lsdkErrs.ErrorCode("K8sPvFailedToGet") + EcK8sPvNotFound = lsdkErrs.ErrorCode("K8sPvNotFound") ) // StorageClass error group diff --git a/pkg/cloud/errors/k8s.go b/pkg/cloud/errors/k8s.go index ca276a7..5120169 100644 --- a/pkg/cloud/errors/k8s.go +++ b/pkg/cloud/errors/k8s.go @@ -27,6 +27,25 @@ var ( "pvcName": pname, }) } + + ErrK8sPvFailedToGet = func(pname string, perr error) IError { + return NewError(new(lsdkErr.SdkError). + WithErrorCode(EcK8sPvFailedToGet). + WithMessage(lfmt.Sprintf("Failed to get PV %s", pname))). + WithErrors(perr). + WithParameters(map[string]interface{}{ + "pvcName": pname, + }) + } + + ErrK8sPvNotFound = func(pname string) IError { + return NewError(new(lsdkErr.SdkError). + WithErrorCode(EcK8sPvNotFound). + WithMessage(lfmt.Sprintf("PV %s not found", pname))). + WithParameters(map[string]interface{}{ + "pvcName": pname, + }) + } ) // StorageClass error group diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 180d893..2b24818 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -237,8 +237,8 @@ func (s *controllerService) CreateVolume(pctx lctx.Context, preq *lcsi.CreateVol return newCreateVolumeResponse(newVol, cvr, respCtx), nil } -func (s *controllerService) DeleteVolume(_ lctx.Context, preq *lcsi.DeleteVolumeRequest) (*lcsi.DeleteVolumeResponse, error) { - llog.V(4).InfoS("[INFO] - DeleteVolume: called", "request", *preq) +func (s *controllerService) DeleteVolume(pctx lctx.Context, preq *lcsi.DeleteVolumeRequest) (*lcsi.DeleteVolumeResponse, error) { + llog.InfoS("[INFO] - DeleteVolume: called", "request", *preq) if err := validateDeleteVolumeRequest(preq); err != nil { llog.ErrorS(err, "[ERROR] - DeleteVolume: Invalid request", "request", *preq) diff --git a/pkg/k8s/ik8s.go b/pkg/k8s/ik8s.go index 1b3ec89..f7c0561 100644 --- a/pkg/k8s/ik8s.go +++ b/pkg/k8s/ik8s.go @@ -10,8 +10,11 @@ import ( type IKubernetes interface { GetPersistentVolumeClaimByName(pctx lctx.Context, pnamespace, pname string) (*lsentity.PersistentVolumeClaim, lserr.IError) GetStorageClassByName(pctx lctx.Context, pname string) (*lsentity.StorageClass, lserr.IError) + GetPersistentVolume(pctx lctx.Context, pname string) (*lsentity.PersistentVolume, lserr.IError) // Event recorder PersistentVolumeClaimEventWarning(pctx lctx.Context, pnamespace, pname, preason, pmessage string) PersistentVolumeClaimEventNormal(pctx lctx.Context, pnamespace, pname, preason, pmessage string) + PersistentVolumeEventWarning(pctx lctx.Context, pname, preason, pmessage string) + PersistentVolumeEventNormal(pctx lctx.Context, pname, preason, pmessage string) } diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 33280ec..f371cac 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -50,6 +50,20 @@ func (s *kubernetes) GetStorageClassByName(pctx lctx.Context, pname string) (*ls return lsentity.NewStorageClass(sc), nil } +func (s *kubernetes) GetPersistentVolume(pctx lctx.Context, pname string) (*lsentity.PersistentVolume, lserr.IError) { + pv, err := s.CoreV1().PersistentVolumes().Get(pctx, pname, lmetav1.GetOptions{}) + if err != nil { + return nil, lserr.ErrK8sPvFailedToGet(pname, err) + } + + if pv == nil { + return nil, lserr.ErrK8sPvNotFound(pname) + } + + return lsentity.NewPersistentVolume(pv), nil + +} + func (s *kubernetes) PersistentVolumeClaimEventWarning(pctx lctx.Context, pnamespace, pname, preason, pmessage string) { if pnamespace == "" || pname == "" { return @@ -73,3 +87,27 @@ func (s *kubernetes) PersistentVolumeClaimEventNormal(pctx lctx.Context, pnamesp } s.EventRecorder.Event(pvc.PersistentVolumeClaim, lcoreV1.EventTypeNormal, preason, pmessage) } + +func (s *kubernetes) PersistentVolumeEventWarning(pctx lctx.Context, pname, preason, pmessage string) { + if pname == "" { + return + } + + pvc, err := s.GetPersistentVolume(pctx, pname) + if err != nil || pvc == nil { + return + } + s.EventRecorder.Event(pvc.PersistentVolume, lcoreV1.EventTypeWarning, preason, pmessage) +} + +func (s *kubernetes) PersistentVolumeEventNormal(pctx lctx.Context, pname, preason, pmessage string) { + if pname == "" { + return + } + + pvc, err := s.GetPersistentVolume(pctx, pname) + if err != nil || pvc == nil { + return + } + s.EventRecorder.Event(pvc.PersistentVolume, lcoreV1.EventTypeNormal, preason, pmessage) +}