Skip to content

Commit

Permalink
Avoid nested error in PvcNotFoundError
Browse files Browse the repository at this point in the history
Signed-off-by: Zvi Cahana <zvic@il.ibm.com>
  • Loading branch information
zcahana committed Sep 19, 2021
1 parent fc7ce50 commit c1ee7fb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
18 changes: 6 additions & 12 deletions pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,19 @@ type templateService struct {
}

type PvcNotFoundError struct {
Err error
Reason string
}

func (e PvcNotFoundError) Error() string {
if e.Err == nil {
return ""
}
return e.Err.Error()
return e.Reason
}

type DataVolumeNotFoundError struct {
Err error
Reason string
}

func (e DataVolumeNotFoundError) Error() string {
if e.Err == nil {
return ""
}
return e.Err.Error()
return e.Reason
}

func isFeatureStateEnabled(fs *v1.FeatureState) bool {
Expand Down Expand Up @@ -534,7 +528,7 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, t
return nil, err
} else if !exists {
logger.Errorf("didn't find PVC %v", claimName)
return nil, PvcNotFoundError{Err: fmt.Errorf("didn't find PVC %v", claimName)}
return nil, PvcNotFoundError{Reason: fmt.Sprintf("didn't find PVC %v", claimName)}
} else if isBlock {
devicePath := filepath.Join(string(filepath.Separator), "dev", volume.Name)
device := k8sv1.VolumeDevice{
Expand Down Expand Up @@ -602,7 +596,7 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, t
return nil, err
} else if !exists {
logger.Errorf("didn't find PVC associated with DataVolume: %v", claimName)
return nil, PvcNotFoundError{Err: fmt.Errorf("didn't find PVC associated with DataVolume: %v", claimName)}
return nil, PvcNotFoundError{Reason: fmt.Sprintf("didn't find PVC associated with DataVolume: %v", claimName)}
} else if isBlock {
devicePath := filepath.Join(string(filepath.Separator), "dev", volume.Name)
device := k8sv1.VolumeDevice{
Expand Down
3 changes: 1 addition & 2 deletions pkg/virt-controller/services/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package services

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -2088,7 +2087,7 @@ var _ = Describe("Template", func() {

_, err := svc.RenderLaunchManifest(&vmi)
Expect(err).To(HaveOccurred(), "Render manifest results in an error")
Expect(err).To(BeAssignableToTypeOf(PvcNotFoundError{Err: errors.New("")}), "Render manifest results in an PvsNotFoundError")
Expect(err).To(BeAssignableToTypeOf(PvcNotFoundError{}), "Render manifest results in an PvsNotFoundError")
})
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-controller/watch/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ func (c *VMIController) volumeReadyToAttachToNode(namespace string, volume virtv
// First, ensure DataVolume exists
_, err := dataVolumeFunc(name, namespace)
if err != nil {
return false, false, services.DataVolumeNotFoundError{Err: err}
return false, false, services.DataVolumeNotFoundError{Reason: err.Error()}
}
}

Expand All @@ -1652,7 +1652,7 @@ func (c *VMIController) volumeReadyToAttachToNode(namespace string, volume virtv
}
}
} else {
return false, false, services.PvcNotFoundError{Err: fmt.Errorf("didn't find PVC %v", name)}
return false, false, services.PvcNotFoundError{Reason: fmt.Sprintf("didn't find PVC %v", name)}
}
return ready, wffc, nil
}
Expand Down

0 comments on commit c1ee7fb

Please sign in to comment.