Skip to content

Commit ce7604e

Browse files
authored
use only the nguid part of the volumeHandle for lightbits volumes (#33)
1 parent 9821e19 commit ce7604e

File tree

2 files changed

+106
-7
lines changed

2 files changed

+106
-7
lines changed

pkg/metal/core.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,21 +349,47 @@ func (p *Provider) ListMachines(ctx context.Context, req *driver.ListMachinesReq
349349
//
350350
// RESPONSE PARAMETERS (driver.GetVolumeIDsResponse)
351351
// VolumeIDs []string VolumeIDs is a repeated list of VolumeIDs.
352-
func (p *Provider) GetVolumeIDs(ctx context.Context, req *driver.GetVolumeIDsRequest) (*driver.GetVolumeIDsResponse, error) {
352+
func (p *Provider) GetVolumeIDs(_ context.Context, req *driver.GetVolumeIDsRequest) (*driver.GetVolumeIDsResponse, error) {
353353
// Log messages to track start and end of request
354354
klog.V(2).Infof("GetVolumeIDs request has been received for %q", req.PVSpecs)
355-
volumeIDs := []string{}
356-
specs := req.PVSpecs
357-
for i := range specs {
358-
spec := specs[i]
359-
if spec.CSI == nil {
355+
356+
var (
357+
volumeIDs []string
358+
)
359+
360+
for _, spec := range req.PVSpecs {
361+
if spec == nil || spec.CSI == nil {
360362
// Not a CSI volume
361363
continue
362364
}
363365

364-
volumeIDs = append(volumeIDs, spec.CSI.VolumeHandle)
366+
switch spec.CSI.Driver {
367+
case "csi.lightbitslabs.com":
368+
fields := map[string]string{}
369+
for _, part := range strings.Split(spec.CSI.VolumeHandle, "|") {
370+
k, v, ok := strings.Cut(part, ":")
371+
if !ok {
372+
continue
373+
}
374+
fields[k] = v
375+
}
376+
377+
nguid, ok := fields["nguid"]
378+
if ok {
379+
volumeIDs = append(volumeIDs, nguid)
380+
continue
381+
}
382+
383+
klog.Errorf("invalid lightbits volumeHandle (missing nguid): %s", spec.CSI.VolumeHandle)
384+
385+
fallthrough
386+
default:
387+
volumeIDs = append(volumeIDs, spec.CSI.VolumeHandle)
388+
}
365389
}
390+
366391
klog.V(2).Infof("GetVolumeIDs request has been processed successfully for %q", req.PVSpecs)
392+
367393
return &driver.GetVolumeIDsResponse{VolumeIDs: volumeIDs}, nil
368394
}
369395

pkg/metal/core_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Package provider contains the cloud provider specific implementations to manage machines
2+
package provider
3+
4+
import (
5+
"context"
6+
"testing"
7+
8+
"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
9+
"github.com/google/go-cmp/cmp"
10+
corev1 "k8s.io/api/core/v1"
11+
)
12+
13+
func TestProvider_GetVolumeIDs(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
req *driver.GetVolumeIDsRequest
17+
want *driver.GetVolumeIDsResponse
18+
wantErr error
19+
}{
20+
{
21+
name: "valid lightbits volume",
22+
req: &driver.GetVolumeIDsRequest{
23+
PVSpecs: []*corev1.PersistentVolumeSpec{
24+
{
25+
26+
PersistentVolumeSource: corev1.PersistentVolumeSource{
27+
CSI: &corev1.CSIPersistentVolumeSource{
28+
Driver: "csi.lightbitslabs.com",
29+
VolumeHandle: "mgmt:10.131.44.1:443,10.131.44.2:443,10.131.44.3:443|nguid:d22572da-a225-4578-ab1a-9318ac5155c3|proj:cd4eac58-46a5-4a31-b59f-2ec207baa817|scheme:grpcs",
30+
},
31+
},
32+
},
33+
},
34+
},
35+
want: &driver.GetVolumeIDsResponse{
36+
VolumeIDs: []string{"d22572da-a225-4578-ab1a-9318ac5155c3"},
37+
},
38+
},
39+
{
40+
name: "invalid lightbits volume",
41+
req: &driver.GetVolumeIDsRequest{
42+
PVSpecs: []*corev1.PersistentVolumeSpec{
43+
{
44+
45+
PersistentVolumeSource: corev1.PersistentVolumeSource{
46+
CSI: &corev1.CSIPersistentVolumeSource{
47+
Driver: "csi.lightbitslabs.com",
48+
VolumeHandle: "mgmt:10.131.44.1:443,10.131.44.2:443,10.131.44.3:443|proj:cd4eac58-46a5-4a31-b59f-2ec207baa817|scheme:grpcs",
49+
},
50+
},
51+
},
52+
},
53+
},
54+
want: &driver.GetVolumeIDsResponse{
55+
VolumeIDs: []string{"mgmt:10.131.44.1:443,10.131.44.2:443,10.131.44.3:443|proj:cd4eac58-46a5-4a31-b59f-2ec207baa817|scheme:grpcs"},
56+
},
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
p := &Provider{}
62+
63+
got, err := p.GetVolumeIDs(context.Background(), tt.req)
64+
65+
if diff := cmp.Diff(tt.wantErr, err); diff != "" {
66+
t.Errorf("err diff = %s", diff)
67+
}
68+
if diff := cmp.Diff(tt.want, got); diff != "" {
69+
t.Errorf("diff = %s", diff)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)