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

Reduce CSI proxy CPU usage #197

Merged
merged 2 commits into from
Feb 7, 2022
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
31 changes: 12 additions & 19 deletions pkg/os/disk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"unsafe"

shared "github.com/kubernetes-csi/csi-proxy/pkg/shared/disk"
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -64,13 +64,6 @@ func New() DiskAPI {
return DiskAPI{}
}

func runExec(command string) ([]byte, error) {
cmd := exec.Command("powershell", "/c", command)
klog.V(4).Infof("Executing command: %q", cmd.String())
out, err := cmd.CombinedOutput()
return out, err
}

// ListDiskLocations - constructs a map with the disk number as the key and the DiskLocation structure
// as the value. The DiskLocation struct has various fields like the Adapter, Bus, Target and LUNID.
func (DiskAPI) ListDiskLocations() (map[uint32]shared.DiskLocation, error) {
Expand All @@ -80,7 +73,7 @@ func (DiskAPI) ListDiskLocations() (map[uint32]shared.DiskLocation, error) {
// "location": "PCI Slot 3 : Adapter 0 : Port 0 : Target 1 : LUN 0"
// }, ...]
cmd := fmt.Sprintf("ConvertTo-Json @(Get-Disk | select Number, Location)")
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return nil, fmt.Errorf("failed to list disk location. cmd: %q, output: %q, err %v", cmd, string(out), err)
}
Expand Down Expand Up @@ -128,7 +121,7 @@ func (DiskAPI) ListDiskLocations() (map[uint32]shared.DiskLocation, error) {

func (DiskAPI) Rescan() error {
cmd := "Update-HostStorageCache"
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return fmt.Errorf("error updating host storage cache output: %q, err: %v", string(out), err)
}
Expand All @@ -137,7 +130,7 @@ func (DiskAPI) Rescan() error {

func (DiskAPI) IsDiskInitialized(diskNumber uint32) (bool, error) {
cmd := fmt.Sprintf("Get-Disk -Number %d | Where partitionstyle -eq 'raw'", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return false, fmt.Errorf("error checking initialized status of disk %d: %v, %v", diskNumber, out, err)
}
Expand All @@ -150,7 +143,7 @@ func (DiskAPI) IsDiskInitialized(diskNumber uint32) (bool, error) {

func (DiskAPI) InitializeDisk(diskNumber uint32) error {
cmd := fmt.Sprintf("Initialize-Disk -Number %d -PartitionStyle GPT", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return fmt.Errorf("error initializing disk %d: %v, %v", diskNumber, out, err)
}
Expand All @@ -159,7 +152,7 @@ func (DiskAPI) InitializeDisk(diskNumber uint32) error {

func (DiskAPI) BasicPartitionsExist(diskNumber uint32) (bool, error) {
cmd := fmt.Sprintf("Get-Partition | Where DiskNumber -eq %d | Where Type -ne Reserved", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return false, fmt.Errorf("error checking presence of partitions on disk %d: %v, %v", diskNumber, out, err)
}
Expand All @@ -172,7 +165,7 @@ func (DiskAPI) BasicPartitionsExist(diskNumber uint32) (bool, error) {

func (DiskAPI) CreateBasicPartition(diskNumber uint32) error {
cmd := fmt.Sprintf("New-Partition -DiskNumber %d -UseMaximumSize", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return fmt.Errorf("error creating parition on disk %d: %v, %v", diskNumber, out, err)
}
Expand Down Expand Up @@ -238,7 +231,7 @@ func (DiskAPI) GetDiskPage83ID(disk syscall.Handle) (string, error) {

func (imp DiskAPI) GetDiskNumberWithID(page83ID string) (uint32, error) {
cmd := "ConvertTo-Json @(Get-Disk | Select Path)"
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return 0, fmt.Errorf("Could not query disk paths")
}
Expand Down Expand Up @@ -298,7 +291,7 @@ func (imp DiskAPI) ListDiskIDs() (map[uint32]shared.DiskIDs, error) {
// "SerialNumber": null
// }, ]
cmd := "ConvertTo-Json @(Get-Disk | Select Path, SerialNumber)"
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return nil, fmt.Errorf("Could not query disk paths")
}
Expand Down Expand Up @@ -329,7 +322,7 @@ func (imp DiskAPI) ListDiskIDs() (map[uint32]shared.DiskIDs, error) {

func (imp DiskAPI) GetDiskStats(diskNumber uint32) (int64, error) {
cmd := fmt.Sprintf("(Get-Disk -Number %d).Size", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil || len(out) == 0 {
return -1, fmt.Errorf("error getting size of disk. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}
Expand All @@ -351,7 +344,7 @@ func (imp DiskAPI) GetDiskStats(diskNumber uint32) (int64, error) {

func (imp DiskAPI) SetDiskState(diskNumber uint32, isOnline bool) error {
cmd := fmt.Sprintf("(Get-Disk -Number %d) | Set-Disk -IsOffline $%t", diskNumber, !isOnline)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return fmt.Errorf("error setting disk attach state. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}
Expand All @@ -361,7 +354,7 @@ func (imp DiskAPI) SetDiskState(diskNumber uint32, isOnline bool) error {

func (imp DiskAPI) GetDiskState(diskNumber uint32) (bool, error) {
cmd := fmt.Sprintf("(Get-Disk -Number %d) | Select-Object -ExpandProperty IsOffline", diskNumber)
out, err := runExec(cmd)
out, err := utils.RunPowershellCmd(cmd)
if err != nil {
return false, fmt.Errorf("error getting disk state. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/os/filesystem/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package filesystem
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/kubernetes-csi/csi-proxy/pkg/utils"
)

// Implements the Filesystem OS API calls. All code here should be very simple
Expand Down Expand Up @@ -49,9 +50,9 @@ func (filesystemAPI) PathExists(path string) (bool, error) {
}

func pathValid(path string) (bool, error) {
cmd := exec.Command("powershell", "/c", `Test-Path $Env:remotepath`)
cmd.Env = append(os.Environ(), fmt.Sprintf("remotepath=%s", path))
output, err := cmd.CombinedOutput()
cmd := `Test-Path $Env:remotepath`
cmdEnv := fmt.Sprintf("remotepath=%s", path)
output, err := utils.RunPowershellCmd(cmd, cmdEnv)
if err != nil {
return false, fmt.Errorf("returned output: %s, error: %v", string(output), err)
}
Expand Down
59 changes: 14 additions & 45 deletions pkg/os/iscsi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package iscsi
import (
"encoding/json"
"fmt"
"os"
"os/exec"

"github.com/kubernetes-csi/csi-proxy/pkg/utils"
)

// Implements the iSCSI OS API calls. All code here should be very simple
Expand All @@ -22,12 +22,8 @@ func (APIImplementor) AddTargetPortal(portal *TargetPortal) error {
cmdLine := fmt.Sprintf(
`New-IscsiTargetPortal -TargetPortalAddress ${Env:iscsi_tp_address} ` +
`-TargetPortalPortNumber ${Env:iscsi_tp_port}`)
cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port))

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error adding target portal. cmd %s, output: %s, err: %v", cmdLine, string(out), err)
}
Expand All @@ -42,12 +38,8 @@ func (APIImplementor) DiscoverTargetPortal(portal *TargetPortal) ([]string, erro
`ConvertTo-Json -InputObject @(Get-IscsiTargetPortal -TargetPortalAddress ` +
`${Env:iscsi_tp_address} -TargetPortalPortNumber ${Env:iscsi_tp_port} | ` +
`Get-IscsiTarget | Select-Object -ExpandProperty NodeAddress)`)
cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port))

out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error discovering target portal. cmd: %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -66,8 +58,7 @@ func (APIImplementor) ListTargetPortals() ([]TargetPortal, error) {
`ConvertTo-Json -InputObject @(Get-IscsiTargetPortal | ` +
`Select-Object TargetPortalAddress, TargetPortalPortNumber)`)

cmd := exec.Command("powershell.exe", "/c", cmdLine)
out, err := cmd.CombinedOutput()
out, err := utils.RunPowershellCmd(cmdLine)
if err != nil {
return nil, fmt.Errorf("error listing target portals. cmd %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -87,12 +78,8 @@ func (APIImplementor) RemoveTargetPortal(portal *TargetPortal) error {
`-TargetPortalPortNumber ${Env:iscsi_tp_port} | Remove-IscsiTargetPortal ` +
`-Confirm:$false`)

cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port))

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error removing target portal. cmd %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -111,24 +98,19 @@ func (APIImplementor) ConnectTarget(portal *TargetPortal, iqn string,
` -AuthenticationType ${Env:iscsi_auth_type}`)

if chapUser != "" {
cmdLine += fmt.Sprintf(` -ChapUsername ${Env:iscsi_chap_user}`)
cmdLine += ` -ChapUsername ${Env:iscsi_chap_user}`
}

if chapSecret != "" {
cmdLine += fmt.Sprintf(` -ChapSecret ${Env:iscsi_chap_secret}`)
cmdLine += ` -ChapSecret ${Env:iscsi_chap_secret}`
}

cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port),
fmt.Sprintf("iscsi_target_iqn=%s", iqn),
fmt.Sprintf("iscsi_auth_type=%s", authType),
fmt.Sprintf("iscsi_chap_user=%s", chapUser),
fmt.Sprintf("iscsi_chap_secret=%s", chapSecret),
)

out, err := cmd.CombinedOutput()
fmt.Sprintf("iscsi_chap_secret=%s", chapSecret))
if err != nil {
return fmt.Errorf("error connecting to target portal. cmd %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -144,13 +126,9 @@ func (APIImplementor) DisconnectTarget(portal *TargetPortal, iqn string) error {
` | Get-IscsiTarget | Where-Object { $_.NodeAddress -eq ${Env:iscsi_target_iqn} }) ` +
`-Confirm:$false`)

cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port),
fmt.Sprintf("iscsi_target_iqn=%s", iqn))

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error disconnecting from target portal. cmd %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -169,13 +147,9 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
`$ids = $c | Get-Disk | Select -ExpandProperty Number | Out-String -Stream; ` +
`ConvertTo-Json -InputObject @($ids)`)

cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_tp_address=%s", portal.Address),
fmt.Sprintf("iscsi_tp_port=%d", portal.Port),
fmt.Sprintf("iscsi_target_iqn=%s", iqn))

out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error getting target disks. cmd %s, output: %s, err: %w", cmdLine, string(out), err)
}
Expand All @@ -190,13 +164,8 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
}

func (APIImplementor) SetMutualChapSecret(mutualChapSecret string) error {
cmdLine := fmt.Sprintf(
`Set-IscsiChapSecret -ChapSecret ${Env:iscsi_mutual_chap_secret}`)
cmd := exec.Command("powershell.exe", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("iscsi_mutual_chap_secret=%s", mutualChapSecret))

out, err := cmd.CombinedOutput()
cmdLine := `Set-IscsiChapSecret -ChapSecret ${Env:iscsi_mutual_chap_secret}`
out, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("iscsi_mutual_chap_secret=%s", mutualChapSecret))
if err != nil {
return fmt.Errorf("error setting mutual chap secret. cmd %s,"+
" output: %s, err: %v", cmdLine, string(out), err)
Expand Down
34 changes: 11 additions & 23 deletions pkg/os/smb/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package smb

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

"github.com/kubernetes-csi/csi-proxy/pkg/utils"
)

type API interface {
Expand All @@ -23,12 +23,9 @@ func New() SmbAPI {
}

func (SmbAPI) IsSmbMapped(remotePath string) (bool, error) {
cmdLine := fmt.Sprintf(`$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status `)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("smbremotepath=%s", remotePath))

out, err := cmd.CombinedOutput()
cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status `
cmdEnv := fmt.Sprintf("smbremotepath=%s", remotePath)
out, err := utils.RunPowershellCmd(cmdLine, cmdEnv)
if err != nil {
return false, fmt.Errorf("error checking smb mapping. cmd %s, output: %s, err: %v", remotePath, string(out), err)
}
Expand All @@ -54,13 +51,8 @@ func (SmbAPI) NewSmbLink(remotePath, localPath string) error {
remotePath = remotePath + "\\"
}

cmdLine := fmt.Sprintf(`New-Item -ItemType SymbolicLink $Env:smblocalPath -Target $Env:smbremotepath`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("smbremotepath=%s", remotePath),
fmt.Sprintf("smblocalpath=%s", localPath),
)
output, err := cmd.CombinedOutput()
cmdLine := `New-Item -ItemType SymbolicLink $Env:smblocalPath -Target $Env:smbremotepath`
output, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("smbremotepath=%s", remotePath), fmt.Sprintf("smblocalpath=%s", localPath))
if err != nil {
return fmt.Errorf("error linking %s to %s. output: %s, err: %v", remotePath, localPath, string(output), err)
}
Expand All @@ -75,21 +67,17 @@ func (SmbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
`;$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Env:smbuser, $PWord` +
`;New-SmbGlobalMapping -RemotePath $Env:smbremotepath -Credential $Credential -RequirePrivacy $true`)

cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("smbuser=%s", username),
if output, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("smbuser=%s", username),
fmt.Sprintf("smbpassword=%s", password),
fmt.Sprintf("smbremotepath=%s", remotePath))
if output, err := cmd.CombinedOutput(); err != nil {
fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
return fmt.Errorf("NewSmbGlobalMapping failed. output: %q, err: %v", string(output), err)
}
return nil
}

func (SmbAPI) RemoveSmbGlobalMapping(remotePath string) error {
cmd := exec.Command("powershell", "/c", `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`)
cmd.Env = append(os.Environ(), fmt.Sprintf("smbremotepath=%s", remotePath))
if output, err := cmd.CombinedOutput(); err != nil {
cmd := `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`
if output, err := utils.RunPowershellCmd(cmd, fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
return fmt.Errorf("UnmountSmbShare failed. output: %q, err: %v", string(output), err)
}
return nil
Expand Down
Loading