Skip to content

Commit c5d08fd

Browse files
authored
Merge pull request #95 from jmpfar/master
Adding GetAttachState and SetAttachState to disk api group
2 parents b800876 + 56e6dbf commit c5d08fd

File tree

16 files changed

+3163
-5
lines changed

16 files changed

+3163
-5
lines changed

client/api/disk/v1beta2/api.pb.go

Lines changed: 1028 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/api/disk/v1beta2/api.proto

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
syntax = "proto3";
2+
3+
package v1beta2;
4+
5+
option go_package = "github.com/kubernetes-csi/csi-proxy/client/api/disk/v1beta2";
6+
7+
service Disk {
8+
// ListDiskLocations returns locations <Adapter, Bus, Target, LUN ID> of all
9+
// disk devices enumerated by the host
10+
rpc ListDiskLocations(ListDiskLocationsRequest) returns (ListDiskLocationsResponse) {}
11+
12+
// PartitionDisk initializes and partitions a disk device (if the disk has not
13+
// been partitioned already) and returns the resulting volume device ID
14+
rpc PartitionDisk(PartitionDiskRequest) returns (PartitionDiskResponse) {}
15+
16+
// Rescan refreshes the host's storage cache
17+
rpc Rescan(RescanRequest) returns (RescanResponse) {}
18+
19+
// ListDiskIDs returns a map of DiskID objects where the key is the disk number
20+
rpc ListDiskIDs(ListDiskIDsRequest) returns (ListDiskIDsResponse) {}
21+
22+
// DiskStats returns the stats for the disk
23+
rpc DiskStats(DiskStatsRequest) returns (DiskStatsResponse) {}
24+
25+
// SetAttachState sets the offline/online state of a disk
26+
rpc SetAttachState(SetAttachStateRequest) returns (SetAttachStateResponse) {}
27+
28+
// GetAttachState gets the offline/online state of a disk
29+
rpc GetAttachState(GetAttachStateRequest) returns (GetAttachStateResponse) {}
30+
}
31+
32+
message ListDiskLocationsRequest {
33+
// Intentionally empty
34+
}
35+
36+
message DiskLocation {
37+
string Adapter = 1;
38+
string Bus = 2;
39+
string Target = 3;
40+
string LUNID = 4;
41+
}
42+
43+
message ListDiskLocationsResponse {
44+
// Map of disk device IDs and <adapter, bus, target, lun ID> associated with each disk device
45+
map <string, DiskLocation> disk_locations = 1;
46+
}
47+
48+
message PartitionDiskRequest {
49+
// Disk device ID of the disk to partition
50+
string diskID = 1;
51+
}
52+
53+
message PartitionDiskResponse {
54+
// Intentionally empty
55+
}
56+
57+
message RescanRequest {
58+
// Intentionally empty
59+
}
60+
61+
message RescanResponse {
62+
// Intentionally empty
63+
}
64+
65+
message ListDiskIDsRequest {
66+
// Intentionally empty
67+
}
68+
69+
message DiskIDs {
70+
// Map of Disk ID types and Disk ID values
71+
map <string, string> identifiers = 1;
72+
}
73+
74+
message ListDiskIDsResponse {
75+
// Map of disk device numbers and IDs <page83> associated with each disk device
76+
map <string, DiskIDs> diskIDs = 1;
77+
}
78+
79+
message DiskStatsRequest {
80+
// Disk device ID of the disk to get the size from
81+
string diskID = 1;
82+
}
83+
84+
message DiskStatsResponse {
85+
//Total size of the volume
86+
int64 diskSize = 1;
87+
}
88+
89+
message SetAttachStateRequest {
90+
// Disk device ID (number) of the disk which state will change
91+
string diskID = 1;
92+
93+
// Online state to set for the disk. true for online, false for offline
94+
bool isOnline = 2;
95+
}
96+
97+
message SetAttachStateResponse {
98+
}
99+
100+
message GetAttachStateRequest {
101+
// Disk device ID (number) of the disk
102+
string diskID = 1;
103+
}
104+
105+
message GetAttachStateResponse {
106+
// Online state of the disk. true for online, false for offline
107+
bool isOnline = 1;
108+
}
109+

client/groups/disk/v1beta2/client_generated.go

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integrationtests/disk_test.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,32 @@ package integrationtests
22

33
import (
44
"context"
5+
"fmt"
6+
"math/rand"
57
"strconv"
68
"strings"
79
"testing"
10+
"time"
811

9-
"github.com/kubernetes-csi/csi-proxy/client/api/disk/v1beta1"
10-
v1beta1client "github.com/kubernetes-csi/csi-proxy/client/groups/disk/v1beta1"
12+
"github.com/kubernetes-csi/csi-proxy/client/api/disk/v1beta2"
13+
v1beta2client "github.com/kubernetes-csi/csi-proxy/client/groups/disk/v1beta2"
14+
"github.com/stretchr/testify/assert"
1115
"github.com/stretchr/testify/require"
1216
)
1317

1418
// This test is meant to run on GCE where the page83 ID of the first disk contains
1519
// the host name
1620
// Skip on Github Actions as it is expected to fail
17-
func TestDiskAPIGroupV1Beta1(t *testing.T) {
21+
func TestDiskAPIGroup(t *testing.T) {
1822
t.Run("ListDiskIDs", func(t *testing.T) {
1923
skipTestOnCondition(t, isRunningOnGhActions())
20-
client, err := v1beta1client.NewClient()
24+
client, err := v1beta2client.NewClient()
2125
require.Nil(t, err)
2226
defer client.Close()
2327

2428
diskNumber := 0
2529
id := "page83"
26-
listRequest := &v1beta1.ListDiskIDsRequest{}
30+
listRequest := &v1beta2.ListDiskIDsRequest{}
2731
diskIDsResponse, err := client.ListDiskIDs(context.TODO(), listRequest)
2832
require.Nil(t, err)
2933

@@ -53,4 +57,61 @@ func TestDiskAPIGroupV1Beta1(t *testing.T) {
5357
}
5458
}
5559
})
60+
61+
t.Run("Get/SetAttachState", func(t *testing.T) {
62+
skipTestOnCondition(t, isRunningOnGhActions())
63+
client, err := v1beta2client.NewClient()
64+
require.NoError(t, err)
65+
66+
defer client.Close()
67+
68+
s1 := rand.NewSource(time.Now().UTC().UnixNano())
69+
r1 := rand.New(s1)
70+
71+
testPluginPath := fmt.Sprintf("C:\\var\\lib\\kubelet\\plugins\\testplugin-%d.csi.io\\", r1.Intn(100))
72+
mountPath := fmt.Sprintf("%smount-%d", testPluginPath, r1.Intn(100))
73+
vhdxPath := fmt.Sprintf("%sdisk-%d.vhdx", testPluginPath, r1.Intn(100))
74+
75+
defer diskCleanup(t, vhdxPath, mountPath, testPluginPath)
76+
diskNum := diskInit(t, vhdxPath, mountPath, testPluginPath)
77+
78+
out, err := runPowershellCmd(fmt.Sprintf("Get-Disk -Number %s | Set-Disk -IsOffline $true", diskNum))
79+
require.NoError(t, err, "failed setting disk offline, out=%v", out)
80+
81+
getReq := &v1beta2.GetAttachStateRequest{DiskID: diskNum}
82+
getResp, err := client.GetAttachState(context.TODO(), getReq)
83+
84+
if assert.NoError(t, err) {
85+
assert.False(t, getResp.IsOnline, "Expected disk to be offline")
86+
}
87+
88+
setReq := &v1beta2.SetAttachStateRequest{DiskID: diskNum, IsOnline: true}
89+
_, err = client.SetAttachState(context.TODO(), setReq)
90+
assert.NoError(t, err)
91+
92+
out, err = runPowershellCmd(fmt.Sprintf("Get-Disk -Number %s | Select-Object -ExpandProperty IsOffline", diskNum))
93+
assert.NoError(t, err)
94+
95+
result, err := strconv.ParseBool(strings.TrimSpace(out))
96+
assert.NoError(t, err)
97+
assert.False(t, result, "Expected disk to be online")
98+
99+
getReq = &v1beta2.GetAttachStateRequest{DiskID: diskNum}
100+
getResp, err = client.GetAttachState(context.TODO(), getReq)
101+
102+
if assert.NoError(t, err) {
103+
assert.True(t, getResp.IsOnline, "Expected disk is online")
104+
}
105+
106+
setReq = &v1beta2.SetAttachStateRequest{DiskID: diskNum, IsOnline: false}
107+
_, err = client.SetAttachState(context.TODO(), setReq)
108+
assert.NoError(t, err)
109+
110+
out, err = runPowershellCmd(fmt.Sprintf("Get-Disk -Number %s | Select-Object -ExpandProperty IsOffline", diskNum))
111+
assert.NoError(t, err)
112+
113+
result, err = strconv.ParseBool(strings.TrimSpace(out))
114+
assert.NoError(t, err)
115+
assert.True(t, result, "Expected disk to be offline")
116+
})
56117
}

internal/os/disk/api.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,29 @@ func (imp APIImplementor) DiskStats(diskID string) (int64, error) {
318318

319319
return diskSize, nil
320320
}
321+
322+
func (imp APIImplementor) SetAttachState(diskID string, isOnline bool) error {
323+
cmd := fmt.Sprintf("(Get-Disk -Number %s) | Set-Disk -IsOffline $%t", diskID, !isOnline)
324+
out, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
325+
if err != nil {
326+
return fmt.Errorf("error setting disk attach state. cmd: %s, output: %s, error: %v", cmd, string(out), err)
327+
}
328+
329+
return nil
330+
}
331+
332+
func (imp APIImplementor) GetAttachState(diskID string) (bool, error) {
333+
cmd := fmt.Sprintf("(Get-Disk -Number %s) | Select-Object -ExpandProperty IsOffline", diskID)
334+
out, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
335+
if err != nil {
336+
return false, fmt.Errorf("error getting disk state. cmd: %s, output: %s, error: %v", cmd, string(out), err)
337+
}
338+
339+
sout := strings.TrimSpace(string(out))
340+
isOffline, err := strconv.ParseBool(sout)
341+
if err != nil {
342+
return false, fmt.Errorf("error parsing disk state. output: %s, error: %v", sout, err)
343+
}
344+
345+
return !isOffline, nil
346+
}

internal/server/disk/api_group_generated.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/disk/internal/types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,24 @@ type DiskStatsRequest struct {
5858
type DiskStatsResponse struct {
5959
DiskSize int64
6060
}
61+
62+
type SetAttachStateRequest struct {
63+
// Disk device ID of the disk which state will change
64+
DiskID string
65+
66+
// Online state to set for the disk. true for online, false for offline
67+
IsOnline bool
68+
}
69+
70+
type SetAttachStateResponse struct {
71+
}
72+
73+
type GetAttachStateRequest struct {
74+
// Disk device ID of the disk
75+
DiskID string
76+
}
77+
78+
type GetAttachStateResponse struct {
79+
// Online state of the disk. true for online, false for offline
80+
IsOnline bool
81+
}

internal/server/disk/internal/types_generated.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)