@@ -3,8 +3,8 @@ package iscsi
33import (
44 "encoding/json"
55 "fmt"
6- "os"
7- "os/exec "
6+
7+ "github.com/kubernetes-csi/csi-proxy/pkg/utils "
88)
99
1010// Implements the iSCSI OS API calls. All code here should be very simple
@@ -22,12 +22,8 @@ func (APIImplementor) AddTargetPortal(portal *TargetPortal) error {
2222 cmdLine := fmt .Sprintf (
2323 `New-IscsiTargetPortal -TargetPortalAddress ${Env:iscsi_tp_address} ` +
2424 `-TargetPortalPortNumber ${Env:iscsi_tp_port}` )
25- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
26- cmd .Env = append (os .Environ (),
27- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
25+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
2826 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
29-
30- out , err := cmd .CombinedOutput ()
3127 if err != nil {
3228 return fmt .Errorf ("error adding target portal. cmd %s, output: %s, err: %v" , cmdLine , string (out ), err )
3329 }
@@ -42,12 +38,8 @@ func (APIImplementor) DiscoverTargetPortal(portal *TargetPortal) ([]string, erro
4238 `ConvertTo-Json -InputObject @(Get-IscsiTargetPortal -TargetPortalAddress ` +
4339 `${Env:iscsi_tp_address} -TargetPortalPortNumber ${Env:iscsi_tp_port} | ` +
4440 `Get-IscsiTarget | Select-Object -ExpandProperty NodeAddress)` )
45- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
46- cmd .Env = append (os .Environ (),
47- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
41+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
4842 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
49-
50- out , err := cmd .CombinedOutput ()
5143 if err != nil {
5244 return nil , fmt .Errorf ("error discovering target portal. cmd: %s, output: %s, err: %w" , cmdLine , string (out ), err )
5345 }
@@ -66,8 +58,7 @@ func (APIImplementor) ListTargetPortals() ([]TargetPortal, error) {
6658 `ConvertTo-Json -InputObject @(Get-IscsiTargetPortal | ` +
6759 `Select-Object TargetPortalAddress, TargetPortalPortNumber)` )
6860
69- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
70- out , err := cmd .CombinedOutput ()
61+ out , err := utils .RunPowershellCmd (cmdLine )
7162 if err != nil {
7263 return nil , fmt .Errorf ("error listing target portals. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
7364 }
@@ -87,12 +78,8 @@ func (APIImplementor) RemoveTargetPortal(portal *TargetPortal) error {
8778 `-TargetPortalPortNumber ${Env:iscsi_tp_port} | Remove-IscsiTargetPortal ` +
8879 `-Confirm:$false` )
8980
90- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
91- cmd .Env = append (os .Environ (),
92- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
81+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
9382 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
94-
95- out , err := cmd .CombinedOutput ()
9683 if err != nil {
9784 return fmt .Errorf ("error removing target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
9885 }
@@ -111,24 +98,19 @@ func (APIImplementor) ConnectTarget(portal *TargetPortal, iqn string,
11198 ` -AuthenticationType ${Env:iscsi_auth_type}` )
11299
113100 if chapUser != "" {
114- cmdLine += fmt . Sprintf ( ` -ChapUsername ${Env:iscsi_chap_user}` )
101+ cmdLine += ` -ChapUsername ${Env:iscsi_chap_user}`
115102 }
116103
117104 if chapSecret != "" {
118- cmdLine += fmt . Sprintf ( ` -ChapSecret ${Env:iscsi_chap_secret}` )
105+ cmdLine += ` -ChapSecret ${Env:iscsi_chap_secret}`
119106 }
120107
121- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
122- cmd .Env = append (os .Environ (),
123- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
108+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
124109 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
125110 fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ),
126111 fmt .Sprintf ("iscsi_auth_type=%s" , authType ),
127112 fmt .Sprintf ("iscsi_chap_user=%s" , chapUser ),
128- fmt .Sprintf ("iscsi_chap_secret=%s" , chapSecret ),
129- )
130-
131- out , err := cmd .CombinedOutput ()
113+ fmt .Sprintf ("iscsi_chap_secret=%s" , chapSecret ))
132114 if err != nil {
133115 return fmt .Errorf ("error connecting to target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
134116 }
@@ -144,13 +126,9 @@ func (APIImplementor) DisconnectTarget(portal *TargetPortal, iqn string) error {
144126 ` | Get-IscsiTarget | Where-Object { $_.NodeAddress -eq ${Env:iscsi_target_iqn} }) ` +
145127 `-Confirm:$false` )
146128
147- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
148- cmd .Env = append (os .Environ (),
149- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
129+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
150130 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
151131 fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ))
152-
153- out , err := cmd .CombinedOutput ()
154132 if err != nil {
155133 return fmt .Errorf ("error disconnecting from target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
156134 }
@@ -169,13 +147,9 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
169147 `$ids = $c | Get-Disk | Select -ExpandProperty Number | Out-String -Stream; ` +
170148 `ConvertTo-Json -InputObject @($ids)` )
171149
172- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
173- cmd .Env = append (os .Environ (),
174- fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
150+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_tp_address=%s" , portal .Address ),
175151 fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
176152 fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ))
177-
178- out , err := cmd .CombinedOutput ()
179153 if err != nil {
180154 return nil , fmt .Errorf ("error getting target disks. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
181155 }
@@ -190,13 +164,8 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
190164}
191165
192166func (APIImplementor ) SetMutualChapSecret (mutualChapSecret string ) error {
193- cmdLine := fmt .Sprintf (
194- `Set-IscsiChapSecret -ChapSecret ${Env:iscsi_mutual_chap_secret}` )
195- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
196- cmd .Env = append (os .Environ (),
197- fmt .Sprintf ("iscsi_mutual_chap_secret=%s" , mutualChapSecret ))
198-
199- out , err := cmd .CombinedOutput ()
167+ cmdLine := `Set-IscsiChapSecret -ChapSecret ${Env:iscsi_mutual_chap_secret}`
168+ out , err := utils .RunPowershellCmd (cmdLine , fmt .Sprintf ("iscsi_mutual_chap_secret=%s" , mutualChapSecret ))
200169 if err != nil {
201170 return fmt .Errorf ("error setting mutual chap secret. cmd %s," +
202171 " output: %s, err: %v" , cmdLine , string (out ), err )
0 commit comments