Skip to content

Commit

Permalink
Add v1alpha1 system API
Browse files Browse the repository at this point in the history
  • Loading branch information
ksubrmnn committed Oct 13, 2020
1 parent b3cbf0c commit 01f4d41
Show file tree
Hide file tree
Showing 17 changed files with 828 additions and 0 deletions.
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)

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))
}
return lines[1], nil
}
Loading

0 comments on commit 01f4d41

Please sign in to comment.