@@ -3,8 +3,8 @@ package iscsi
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "os"
7
- "os/exec "
6
+
7
+ "github.com/kubernetes-csi/csi-proxy/pkg/utils "
8
8
)
9
9
10
10
// Implements the iSCSI OS API calls. All code here should be very simple
@@ -22,12 +22,8 @@ func (APIImplementor) AddTargetPortal(portal *TargetPortal) error {
22
22
cmdLine := fmt .Sprintf (
23
23
`New-IscsiTargetPortal -TargetPortalAddress ${Env:iscsi_tp_address} ` +
24
24
`-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 ),
28
26
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
29
-
30
- out , err := cmd .CombinedOutput ()
31
27
if err != nil {
32
28
return fmt .Errorf ("error adding target portal. cmd %s, output: %s, err: %v" , cmdLine , string (out ), err )
33
29
}
@@ -42,12 +38,8 @@ func (APIImplementor) DiscoverTargetPortal(portal *TargetPortal) ([]string, erro
42
38
`ConvertTo-Json -InputObject @(Get-IscsiTargetPortal -TargetPortalAddress ` +
43
39
`${Env:iscsi_tp_address} -TargetPortalPortNumber ${Env:iscsi_tp_port} | ` +
44
40
`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 ),
48
42
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
49
-
50
- out , err := cmd .CombinedOutput ()
51
43
if err != nil {
52
44
return nil , fmt .Errorf ("error discovering target portal. cmd: %s, output: %s, err: %w" , cmdLine , string (out ), err )
53
45
}
@@ -66,8 +58,7 @@ func (APIImplementor) ListTargetPortals() ([]TargetPortal, error) {
66
58
`ConvertTo-Json -InputObject @(Get-IscsiTargetPortal | ` +
67
59
`Select-Object TargetPortalAddress, TargetPortalPortNumber)` )
68
60
69
- cmd := exec .Command ("powershell.exe" , "/c" , cmdLine )
70
- out , err := cmd .CombinedOutput ()
61
+ out , err := utils .RunPowershellCmd (cmdLine )
71
62
if err != nil {
72
63
return nil , fmt .Errorf ("error listing target portals. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
73
64
}
@@ -87,12 +78,8 @@ func (APIImplementor) RemoveTargetPortal(portal *TargetPortal) error {
87
78
`-TargetPortalPortNumber ${Env:iscsi_tp_port} | Remove-IscsiTargetPortal ` +
88
79
`-Confirm:$false` )
89
80
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 ),
93
82
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ))
94
-
95
- out , err := cmd .CombinedOutput ()
96
83
if err != nil {
97
84
return fmt .Errorf ("error removing target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
98
85
}
@@ -111,24 +98,19 @@ func (APIImplementor) ConnectTarget(portal *TargetPortal, iqn string,
111
98
` -AuthenticationType ${Env:iscsi_auth_type}` )
112
99
113
100
if chapUser != "" {
114
- cmdLine += fmt . Sprintf ( ` -ChapUsername ${Env:iscsi_chap_user}` )
101
+ cmdLine += ` -ChapUsername ${Env:iscsi_chap_user}`
115
102
}
116
103
117
104
if chapSecret != "" {
118
- cmdLine += fmt . Sprintf ( ` -ChapSecret ${Env:iscsi_chap_secret}` )
105
+ cmdLine += ` -ChapSecret ${Env:iscsi_chap_secret}`
119
106
}
120
107
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 ),
124
109
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
125
110
fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ),
126
111
fmt .Sprintf ("iscsi_auth_type=%s" , authType ),
127
112
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 ))
132
114
if err != nil {
133
115
return fmt .Errorf ("error connecting to target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
134
116
}
@@ -144,13 +126,9 @@ func (APIImplementor) DisconnectTarget(portal *TargetPortal, iqn string) error {
144
126
` | Get-IscsiTarget | Where-Object { $_.NodeAddress -eq ${Env:iscsi_target_iqn} }) ` +
145
127
`-Confirm:$false` )
146
128
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 ),
150
130
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
151
131
fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ))
152
-
153
- out , err := cmd .CombinedOutput ()
154
132
if err != nil {
155
133
return fmt .Errorf ("error disconnecting from target portal. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
156
134
}
@@ -169,13 +147,9 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
169
147
`$ids = $c | Get-Disk | Select -ExpandProperty Number | Out-String -Stream; ` +
170
148
`ConvertTo-Json -InputObject @($ids)` )
171
149
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 ),
175
151
fmt .Sprintf ("iscsi_tp_port=%d" , portal .Port ),
176
152
fmt .Sprintf ("iscsi_target_iqn=%s" , iqn ))
177
-
178
- out , err := cmd .CombinedOutput ()
179
153
if err != nil {
180
154
return nil , fmt .Errorf ("error getting target disks. cmd %s, output: %s, err: %w" , cmdLine , string (out ), err )
181
155
}
@@ -190,13 +164,8 @@ func (APIImplementor) GetTargetDisks(portal *TargetPortal, iqn string) ([]string
190
164
}
191
165
192
166
func (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 ))
200
169
if err != nil {
201
170
return fmt .Errorf ("error setting mutual chap secret. cmd %s," +
202
171
" output: %s, err: %v" , cmdLine , string (out ), err )
0 commit comments