Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v1alpha1 system API #81

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions client/api/system/v1alpha1/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions client/api/system/v1alpha1/api.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

package v1alpha1;

option go_package = "github.com/kubernetes-csi/csi-proxy/client/api/system/v1alpha1";

service System {
// GetBIOSSerialNumber returns the device's serial number
rpc GetBIOSSerialNumber(GetBIOSSerialNumberRequest) returns (GetBIOSSerialNumberResponse) {}
}

message GetBIOSSerialNumberRequest {
// Intentionally empty
}

message GetBIOSSerialNumberResponse {
// Serial number
string serial_number = 1;
}
56 changes: 56 additions & 0 deletions client/groups/system/v1alpha1/client_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/csi-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
diskapi "github.com/kubernetes-csi/csi-proxy/internal/os/disk"
filesystemapi "github.com/kubernetes-csi/csi-proxy/internal/os/filesystem"
smbapi "github.com/kubernetes-csi/csi-proxy/internal/os/smb"
sysapi "github.com/kubernetes-csi/csi-proxy/internal/os/system"
volumeapi "github.com/kubernetes-csi/csi-proxy/internal/os/volume"
"github.com/kubernetes-csi/csi-proxy/internal/server"
disksrv "github.com/kubernetes-csi/csi-proxy/internal/server/disk"
filesystemsrv "github.com/kubernetes-csi/csi-proxy/internal/server/filesystem"
smbsrv "github.com/kubernetes-csi/csi-proxy/internal/server/smb"
syssrv "github.com/kubernetes-csi/csi-proxy/internal/server/system"
srvtypes "github.com/kubernetes-csi/csi-proxy/internal/server/types"
volumesrv "github.com/kubernetes-csi/csi-proxy/internal/server/volume"
"golang.org/x/sys/windows"
Expand Down Expand Up @@ -77,11 +79,17 @@ func apiGroups() ([]srvtypes.APIGroup, error) {
return []srvtypes.APIGroup{}, err
}

syssrv, err := syssrv.NewServer(sysapi.New())
if err != nil {
return []srvtypes.APIGroup{}, err
}

return []srvtypes.APIGroup{
fssrv,
disksrv,
volumesrv,
smbsrv,
syssrv,
}, nil
}

Expand Down
33 changes: 33 additions & 0 deletions integrationtests/system_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package integrationtests

import (
"context"
"os/exec"
"strings"
"testing"

"github.com/kubernetes-csi/csi-proxy/client/api/system/v1alpha1"
v1alpha1client "github.com/kubernetes-csi/csi-proxy/client/groups/system/v1alpha1"
"github.com/stretchr/testify/require"
)

func TestGetBIOSSerialNumber(t *testing.T) {
t.Run("GetBIOSSerialNumber", func(t *testing.T) {
client, err := v1alpha1client.NewClient()
require.Nil(t, err)
defer client.Close()

request := &v1alpha1.GetBIOSSerialNumberRequest{}
response, err := client.GetBIOSSerialNumber(context.TODO(), request)
require.Nil(t, err)
require.NotNil(t, response)

result, err := exec.Command("wmic", "bios", "get", "serialnumber").Output()
require.Nil(t, err)

t.Logf("The serial number is %s", response.SerialNumber)
ksubrmnn marked this conversation as resolved.
Show resolved Hide resolved

resultString := string(result)
require.True(t, strings.Contains(resultString, response.SerialNumber))
})
}
39 changes: 39 additions & 0 deletions internal/os/system/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package system

import (
"fmt"
"os/exec"
"strings"
)

// Implements the System OS API calls. All code here should be very simple
// pass-through to the OS APIs. Any logic around the APIs should go in
// internal/server/system/server.go so that logic can be easily unit-tested
// without requiring specific OS environments.

type APIImplementor struct{}

func New() APIImplementor {
return APIImplementor{}
}

func (APIImplementor) GetBIOSSerialNumber() (string, error) {
// Taken from Kubernetes vSphere cloud provider
// https://github.com/kubernetes/kubernetes/blob/103e926604de6f79161b78af3e792d0ed282bc06/staging/src/k8s.io/legacy-cloud-providers/vsphere/vsphere_util_windows.go#L28
result, err := exec.Command("wmic", "bios", "get", "serialnumber").Output()
if err != nil {
return "", err
}
lines := strings.FieldsFunc(string(result), func(r rune) bool {
switch r {
case '\n', '\r':
return true
default:
return false
}
})
if len(lines) != 2 {
return "", fmt.Errorf("received unexpected value retrieving vm uuid: %q", string(result))
Copy link
Contributor

@ddebroy ddebroy Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove vm from the error string in a separate PR? The host machine where csi-proxy is running need not be a VM.

Copy link
Contributor

@ddebroy ddebroy Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted #94 to address ^

}
return lines[1], nil
}
Loading