Skip to content

Commit

Permalink
fix(server): return 404 if get backing image and backup not found
Browse files Browse the repository at this point in the history
ref: longhorn/longhorn 6266

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin committed Jul 11, 2023
1 parent 6f809b3 commit 872307f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api/backingimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/pkg/errors"
"github.com/rancher/go-rancher/api"
"github.com/rancher/go-rancher/client"

"github.com/longhorn/longhorn-manager/datastore"
)

func (s *Server) BackingImageList(rw http.ResponseWriter, req *http.Request) (err error) {
Expand Down Expand Up @@ -36,6 +38,10 @@ func (s *Server) BackingImageGet(rw http.ResponseWriter, req *http.Request) erro

bi, err := s.m.GetBackingImage(id)
if err != nil {
if datastore.ErrorIsNotFound(err) {
rw.WriteHeader(http.StatusNotFound)
return nil
}
return errors.Wrapf(err, "failed to get backing image '%s'", id)
}

Expand Down
10 changes: 10 additions & 0 deletions api/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/rancher/go-rancher/api"
"github.com/rancher/go-rancher/client"
"github.com/sirupsen/logrus"

"github.com/longhorn/longhorn-manager/datastore"
)

func (s *Server) BackupTargetList(w http.ResponseWriter, req *http.Request) error {
Expand Down Expand Up @@ -47,6 +49,10 @@ func (s *Server) BackupVolumeGet(w http.ResponseWriter, req *http.Request) error

bv, err := s.m.GetBackupVolume(volName)
if err != nil {
if datastore.ErrorIsNotFound(err) {
w.WriteHeader(http.StatusNotFound)
return nil
}
return errors.Wrapf(err, "failed to get backup volume '%s'", volName)
}
apiContext.Write(toBackupVolumeResource(bv, apiContext))
Expand Down Expand Up @@ -97,6 +103,10 @@ func (s *Server) BackupGet(w http.ResponseWriter, req *http.Request) error {

backup, err := s.m.GetBackup(input.Name, volName)
if err != nil {
if datastore.ErrorIsNotFound(err) {
w.WriteHeader(http.StatusNotFound)
return nil
}
return errors.Wrapf(err, "failed to get backup '%v' of volume '%v'", input.Name, volName)
}
if backup == nil {
Expand Down
5 changes: 5 additions & 0 deletions api/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"

bsutil "github.com/longhorn/backupstore/util"
"github.com/longhorn/longhorn-manager/datastore"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/util"
Expand Down Expand Up @@ -272,6 +273,10 @@ func (s *Server) SnapshotCRGet(w http.ResponseWriter, req *http.Request) (err er

snapRO, err := s.m.GetSnapshotCR(input.Name)
if err != nil {
if datastore.ErrorIsNotFound(err) {
w.WriteHeader(http.StatusNotFound)
return nil
}
return err
}

Expand Down
12 changes: 10 additions & 2 deletions csi/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,11 @@ func (cs *ControllerServer) cleanupSnapshot(sourceVolumeName, id string) error {
if err != nil {
return err
}
if volume != nil {
snapshot, err := cs.apiClient.SnapshotCR.ById(id)
if err != nil {
return err
}
if volume != nil && snapshot != nil {
if _, err := cs.apiClient.Volume.ActionSnapshotCRDelete(volume, &longhornclient.SnapshotCRInput{
Name: id,
}); err != nil {
Expand All @@ -996,7 +1000,11 @@ func (cs *ControllerServer) cleanupBackupVolume(sourceVolumeName, id string) err
if err != nil {
return err
}
if backupVolume != nil && backupVolume.Name != "" {
backup, err := cs.apiClient.Backup.ById(backupName)
if err != nil {
return err
}
if backupVolume != nil && backupVolume.Name != "" && backup != nil {
if _, err = cs.apiClient.BackupVolume.ActionBackupDelete(backupVolume, &longhornclient.BackupInput{
Name: backupName,
}); err != nil {
Expand Down

0 comments on commit 872307f

Please sign in to comment.