Skip to content

Commit

Permalink
use only the nguid part of the volumeHandle for lightbits volumes (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwennrich authored May 29, 2024
1 parent 9821e19 commit ce7604e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
40 changes: 33 additions & 7 deletions pkg/metal/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,47 @@ func (p *Provider) ListMachines(ctx context.Context, req *driver.ListMachinesReq
//
// RESPONSE PARAMETERS (driver.GetVolumeIDsResponse)
// VolumeIDs []string VolumeIDs is a repeated list of VolumeIDs.
func (p *Provider) GetVolumeIDs(ctx context.Context, req *driver.GetVolumeIDsRequest) (*driver.GetVolumeIDsResponse, error) {
func (p *Provider) GetVolumeIDs(_ context.Context, req *driver.GetVolumeIDsRequest) (*driver.GetVolumeIDsResponse, error) {
// Log messages to track start and end of request
klog.V(2).Infof("GetVolumeIDs request has been received for %q", req.PVSpecs)
volumeIDs := []string{}
specs := req.PVSpecs
for i := range specs {
spec := specs[i]
if spec.CSI == nil {

var (
volumeIDs []string
)

for _, spec := range req.PVSpecs {
if spec == nil || spec.CSI == nil {
// Not a CSI volume
continue
}

volumeIDs = append(volumeIDs, spec.CSI.VolumeHandle)
switch spec.CSI.Driver {
case "csi.lightbitslabs.com":
fields := map[string]string{}
for _, part := range strings.Split(spec.CSI.VolumeHandle, "|") {
k, v, ok := strings.Cut(part, ":")
if !ok {
continue
}
fields[k] = v
}

nguid, ok := fields["nguid"]
if ok {
volumeIDs = append(volumeIDs, nguid)
continue
}

klog.Errorf("invalid lightbits volumeHandle (missing nguid): %s", spec.CSI.VolumeHandle)

fallthrough
default:
volumeIDs = append(volumeIDs, spec.CSI.VolumeHandle)
}
}

klog.V(2).Infof("GetVolumeIDs request has been processed successfully for %q", req.PVSpecs)

return &driver.GetVolumeIDsResponse{VolumeIDs: volumeIDs}, nil
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/metal/core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Package provider contains the cloud provider specific implementations to manage machines
package provider

import (
"context"
"testing"

"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
)

func TestProvider_GetVolumeIDs(t *testing.T) {
tests := []struct {
name string
req *driver.GetVolumeIDsRequest
want *driver.GetVolumeIDsResponse
wantErr error
}{
{
name: "valid lightbits volume",
req: &driver.GetVolumeIDsRequest{
PVSpecs: []*corev1.PersistentVolumeSpec{
{

PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "csi.lightbitslabs.com",
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",
},
},
},
},
},
want: &driver.GetVolumeIDsResponse{
VolumeIDs: []string{"d22572da-a225-4578-ab1a-9318ac5155c3"},
},
},
{
name: "invalid lightbits volume",
req: &driver.GetVolumeIDsRequest{
PVSpecs: []*corev1.PersistentVolumeSpec{
{

PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "csi.lightbitslabs.com",
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",
},
},
},
},
},
want: &driver.GetVolumeIDsResponse{
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"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Provider{}

got, err := p.GetVolumeIDs(context.Background(), tt.req)

if diff := cmp.Diff(tt.wantErr, err); diff != "" {
t.Errorf("err diff = %s", diff)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("diff = %s", diff)
}
})
}
}

0 comments on commit ce7604e

Please sign in to comment.