generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 63
Migrate system API group to library model #244
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-csi:master
from
alexander-ding:enh/migrate-v2
Oct 13, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/kubernetes-csi/csi-proxy/pkg/utils" | ||
| ) | ||
|
|
||
| // 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 | ||
| // pkg/system/system.go so that logic can be easily unit-tested | ||
| // without requiring specific OS environments. | ||
|
|
||
| type API interface { | ||
| GetBIOSSerialNumber() (string, error) | ||
| GetService(name string) (*ServiceInfo, error) | ||
| StartService(name string) error | ||
| StopService(name string, force bool) error | ||
| } | ||
|
|
||
| type systemAPI struct{} | ||
|
|
||
| func New() API { | ||
| return systemAPI{} | ||
| } | ||
|
|
||
| func (systemAPI) 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 host uuid: %q", string(result)) | ||
| } | ||
| return lines[1], nil | ||
| } | ||
|
|
||
| func (systemAPI) GetService(name string) (*ServiceInfo, error) { | ||
| script := `Get-Service -Name $env:ServiceName | Select-Object DisplayName, Status, StartType | ` + | ||
| `ConvertTo-JSON` | ||
| cmdEnv := fmt.Sprintf("ServiceName=%s", name) | ||
| out, err := utils.RunPowershellCmd(script, cmdEnv) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error querying service name=%s. cmd: %s, output: %s, error: %v", name, script, string(out), err) | ||
| } | ||
|
|
||
| var serviceInfo ServiceInfo | ||
| err = json.Unmarshal(out, &serviceInfo) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &serviceInfo, nil | ||
| } | ||
|
|
||
| func (systemAPI) StartService(name string) error { | ||
| script := `Start-Service -Name $env:ServiceName` | ||
| cmdEnv := fmt.Sprintf("ServiceName=%s", name) | ||
| out, err := utils.RunPowershellCmd(script, cmdEnv) | ||
| if err != nil { | ||
| return fmt.Errorf("error starting service name=%s. cmd: %s, output: %s, error: %v", name, script, string(out), err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (systemAPI) StopService(name string, force bool) error { | ||
| script := `Stop-Service -Name $env:ServiceName -Force:$([System.Convert]::ToBoolean($env:Force))` | ||
| out, err := utils.RunPowershellCmd(script, fmt.Sprintf("ServiceName=%s", name), fmt.Sprintf("Force=%t", force)) | ||
| if err != nil { | ||
| return fmt.Errorf("error stopping service name=%s. cmd: %s, output: %s, error: %v", name, script, string(out), err) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package api | ||
|
|
||
| type ServiceInfo struct { | ||
| // Service display name | ||
| DisplayName string `json:"DisplayName"` | ||
|
|
||
| // Service start type | ||
| StartType uint32 `json:"StartType"` | ||
|
|
||
| // Service status | ||
| Status uint32 `json:"Status"` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package system | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| systemapi "github.com/kubernetes-csi/csi-proxy/pkg/system/api" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| type System struct { | ||
| hostAPI systemapi.API | ||
| } | ||
|
|
||
| type Interface interface { | ||
| GetBIOSSerialNumber(context.Context, *GetBIOSSerialNumberRequest) (*GetBIOSSerialNumberResponse, error) | ||
| GetService(context.Context, *GetServiceRequest) (*GetServiceResponse, error) | ||
| StartService(context.Context, *StartServiceRequest) (*StartServiceResponse, error) | ||
| StopService(context.Context, *StopServiceRequest) (*StopServiceResponse, error) | ||
| } | ||
|
|
||
| // check that System implements Interface | ||
| var _ Interface = &System{} | ||
|
|
||
| func New(hostAPI systemapi.API) (*System, error) { | ||
| return &System{ | ||
| hostAPI: hostAPI, | ||
| }, nil | ||
| } | ||
|
|
||
| func (s *System) GetBIOSSerialNumber(context context.Context, request *GetBIOSSerialNumberRequest) (*GetBIOSSerialNumberResponse, error) { | ||
| klog.V(4).Infof("calling GetBIOSSerialNumber") | ||
| response := &GetBIOSSerialNumberResponse{} | ||
| serialNumber, err := s.hostAPI.GetBIOSSerialNumber() | ||
| if err != nil { | ||
| klog.Errorf("failed GetBIOSSerialNumber: %v", err) | ||
| return response, err | ||
| } | ||
|
|
||
| response.SerialNumber = serialNumber | ||
| return response, nil | ||
| } | ||
|
|
||
| func (s *System) GetService(context context.Context, request *GetServiceRequest) (*GetServiceResponse, error) { | ||
| klog.V(4).Infof("calling GetService name=%s", request.Name) | ||
| response := &GetServiceResponse{} | ||
| info, err := s.hostAPI.GetService(request.Name) | ||
| if err != nil { | ||
| klog.Errorf("failed GetService: %v", err) | ||
| return response, err | ||
| } | ||
|
|
||
| response.DisplayName = info.DisplayName | ||
| response.StartType = Startype(info.StartType) | ||
| response.Status = ServiceStatus(info.Status) | ||
| return response, nil | ||
| } | ||
|
|
||
| func (s *System) StartService(context context.Context, request *StartServiceRequest) (*StartServiceResponse, error) { | ||
| klog.V(4).Infof("calling StartService name=%s", request.Name) | ||
| response := &StartServiceResponse{} | ||
| err := s.hostAPI.StartService(request.Name) | ||
| if err != nil { | ||
| klog.Errorf("failed StartService: %v", err) | ||
| return response, err | ||
| } | ||
|
|
||
| return response, nil | ||
| } | ||
|
|
||
| func (s *System) StopService(context context.Context, request *StopServiceRequest) (*StopServiceResponse, error) { | ||
| klog.V(4).Infof("calling StopService name=%s", request.Name) | ||
| response := &StopServiceResponse{} | ||
| err := s.hostAPI.StopService(request.Name, request.Force) | ||
| if err != nil { | ||
| klog.Errorf("failed StopService: %v", err) | ||
| return response, err | ||
| } | ||
|
|
||
| return response, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package system | ||
|
|
||
| type GetBIOSSerialNumberRequest struct { | ||
| } | ||
|
|
||
| type GetBIOSSerialNumberResponse struct { | ||
| SerialNumber string | ||
| } | ||
|
|
||
| type StartServiceRequest struct { | ||
| // Service name (as listed in System\CCS\Services keys) | ||
| Name string | ||
| } | ||
|
|
||
| type StartServiceResponse struct { | ||
| // Intentionally empty | ||
| } | ||
|
|
||
| type StopServiceRequest struct { | ||
| // Service name (as listed in System\CCS\Services keys) | ||
| Name string | ||
|
|
||
| // Forces stopping of services that has dependent services | ||
| Force bool | ||
| } | ||
|
|
||
| type StopServiceResponse struct { | ||
| // Intentionally empty | ||
| } | ||
|
|
||
| type ServiceStatus uint32 | ||
|
|
||
| const ( | ||
| SERVICE_STATUS_UNKNOWN ServiceStatus = iota | ||
| SERVICE_STATUS_STOPPED | ||
| SERVICE_STATUS_START_PENDING | ||
| SERVICE_STATUS_STOP_PENDING | ||
| SERVICE_STATUS_RUNNING | ||
| SERVICE_STATUS_CONTINUE_PENDING | ||
| SERVICE_STATUS_PAUSE_PENDING | ||
| SERVICE_STATUS_PAUSED | ||
| ) | ||
|
|
||
| type Startype uint32 | ||
|
|
||
| const ( | ||
| START_TYPE_BOOT Startype = iota | ||
| START_TYPE_SYSTEM | ||
| START_TYPE_AUTOMATIC | ||
| START_TYPE_MANUAL | ||
| START_TYPE_DISABLED | ||
| ) | ||
|
|
||
| type GetServiceRequest struct { | ||
| // Service name (as listed in System\CCS\Services keys) | ||
| Name string | ||
| } | ||
|
|
||
| type GetServiceResponse struct { | ||
| // Service display name | ||
| DisplayName string | ||
|
|
||
| // Service start type. | ||
| // Used to control whether a service will start on boot, and if so on which | ||
| // boot phase. | ||
| StartType Startype | ||
|
|
||
| // Service status, e.g. stopped, running, paused | ||
| Status ServiceStatus | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.