mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Return MetricsError with ErrCodeNotSupported code
GetMetrics function expects MetricsError error with ErrCodeNotSupported code when driver for the volume does not support metrics Updated csi_metrics to return error when underlying csi driver does not have GET_VOLUME_STATS capability
This commit is contained in:
parent
886c5d261f
commit
528521cfae
@ -61,9 +61,11 @@ func (mc *metricsCsi) GetMetrics() (*volume.Metrics, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// if plugin doesnot support volume status, return.
|
// if plugin doesnot support volume status, return.
|
||||||
if !volumeStatsSet {
|
if !volumeStatsSet {
|
||||||
return nil, nil
|
return nil, volume.NewNotSupportedErrorWithDriverName(
|
||||||
|
string(mc.csiClientGetter.driverName))
|
||||||
}
|
}
|
||||||
// Get Volumestatus
|
// Get Volumestatus
|
||||||
metrics, err := csiClient.NodeGetVolumeStats(ctx, mc.volumeID, mc.targetPath)
|
metrics, err := csiClient.NodeGetVolumeStats(ctx, mc.volumeID, mc.targetPath)
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
|
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
"k8s.io/kubernetes/pkg/volume/csi/fake"
|
"k8s.io/kubernetes/pkg/volume/csi/fake"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -93,6 +94,49 @@ func TestGetMetrics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test GetMetrics with a volume that does not support stats
|
||||||
|
func TestGetMetricsDriverNotSupportStats(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
volumeID string
|
||||||
|
targetPath string
|
||||||
|
expectSuccess bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "volume created by simple driver",
|
||||||
|
expectSuccess: true,
|
||||||
|
volumeID: "foobar",
|
||||||
|
targetPath: "/mnt/foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
metricsGetter := &metricsCsi{volumeID: tc.volumeID, targetPath: tc.targetPath}
|
||||||
|
metricsGetter.csiClient = &csiDriverClient{
|
||||||
|
driverName: "com.simple.SimpleDriver",
|
||||||
|
nodeV1ClientCreator: func(addr csiAddr) (csipbv1.NodeClient, io.Closer, error) {
|
||||||
|
nodeClient := fake.NewNodeClientWithVolumeStats(false /* VolumeStatsCapable */)
|
||||||
|
fakeCloser := fake.NewCloser(t)
|
||||||
|
nodeClient.SetNodeVolumeStatsResp(getRawVolumeInfo())
|
||||||
|
return nodeClient, fakeCloser, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
metrics, err := metricsGetter.GetMetrics()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("for %s: expected error, but got nil error", tc.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !volume.IsNotSupported(err) {
|
||||||
|
t.Fatalf("for %s, expected not supported error but got: %v", tc.name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if metrics != nil {
|
||||||
|
t.Fatalf("for %s, expected nil metrics, but got: %v", tc.name, metrics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func getRawVolumeInfo() *csipbv1.NodeGetVolumeStatsResponse {
|
func getRawVolumeInfo() *csipbv1.NodeGetVolumeStatsResponse {
|
||||||
return &csipbv1.NodeGetVolumeStatsResponse{
|
return &csipbv1.NodeGetVolumeStatsResponse{
|
||||||
Usage: []*csipbv1.VolumeUsage{
|
Usage: []*csipbv1.VolumeUsage{
|
||||||
|
@ -35,7 +35,16 @@ func NewNotSupportedError() *MetricsError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNoPathDefined creates a new MetricsError with code NoPathDefined.
|
// NewNotSupportedErrorWithDriverName creates a new MetricsError with code NotSupported.
|
||||||
|
// driver name is added to the error message.
|
||||||
|
func NewNotSupportedErrorWithDriverName(name string) *MetricsError {
|
||||||
|
return &MetricsError{
|
||||||
|
Code: ErrCodeNotSupported,
|
||||||
|
Msg: fmt.Sprintf("metrics are not supported for %s volumes", name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNoPathDefinedError creates a new MetricsError with code NoPathDefined.
|
||||||
func NewNoPathDefinedError() *MetricsError {
|
func NewNoPathDefinedError() *MetricsError {
|
||||||
return &MetricsError{
|
return &MetricsError{
|
||||||
Code: ErrCodeNoPathDefined,
|
Code: ErrCodeNoPathDefined,
|
||||||
|
Loading…
Reference in New Issue
Block a user