66
77 "github.com/kubernetes-csi/csi-proxy/pkg/cim"
88 "github.com/kubernetes-csi/csi-proxy/pkg/server/system/impl"
9- "github.com/microsoft/wmi/server2019/root/cimv2"
109 "github.com/pkg/errors"
1110 "k8s.io/klog/v2"
1211)
@@ -27,8 +26,8 @@ type ServiceInfo struct {
2726 Status uint32 `json:"Status"`
2827}
2928
30- type stateCheckFunc func (ServiceInterface , string ) (bool , string , error )
31- type stateTransitionFunc func (ServiceInterface ) error
29+ type stateCheckFunc func (cim. ServiceInterface , string ) (bool , string , error )
30+ type stateTransitionFunc func (cim. ServiceInterface ) error
3231
3332const (
3433 // startServiceErrorCodeAccepted indicates the request is accepted
@@ -83,24 +82,13 @@ func serviceState(status string) uint32 {
8382 return stateMappings [status ]
8483}
8584
86- type ServiceInterface interface {
87- GetPropertyName () (string , error )
88- GetPropertyDisplayName () (string , error )
89- GetPropertyState () (string , error )
90- GetPropertyStartMode () (string , error )
91- GetDependents () ([]ServiceInterface , error )
92- StartService () (result uint32 , err error )
93- StopService () (result uint32 , err error )
94- Refresh () error
95- }
96-
9785type ServiceManager interface {
98- WaitUntilServiceState (ServiceInterface , stateTransitionFunc , stateCheckFunc , time.Duration , time.Duration ) (string , error )
86+ WaitUntilServiceState (cim. ServiceInterface , stateTransitionFunc , stateCheckFunc , time.Duration , time.Duration ) (string , error )
9987 GetDependentsForService (string ) ([]string , error )
10088}
10189
10290type ServiceFactory interface {
103- GetService (name string ) (ServiceInterface , error )
91+ GetService (name string ) (cim. ServiceInterface , error )
10492}
10593
10694type APIImplementor struct {
@@ -109,7 +97,7 @@ type APIImplementor struct {
10997}
11098
11199func New () APIImplementor {
112- serviceFactory := Win32ServiceFactory {}
100+ serviceFactory := cim. Win32ServiceFactory {}
113101 return APIImplementor {
114102 serviceFactory : serviceFactory ,
115103 serviceManager : ServiceManagerImpl {
@@ -119,36 +107,36 @@ func New() APIImplementor {
119107}
120108
121109func (APIImplementor ) GetBIOSSerialNumber () (string , error ) {
122- bios , err := cim .QueryBIOSElement ([] string { "SerialNumber" } )
110+ bios , err := cim .QueryBIOSElement (cim . BIOSSelectorList )
123111 if err != nil {
124112 return "" , fmt .Errorf ("failed to get BIOS element: %w" , err )
125113 }
126114
127- sn , err := bios . GetPropertySerialNumber ( )
115+ sn , err := cim . GetBIOSSerialNumber ( bios )
128116 if err != nil {
129117 return "" , fmt .Errorf ("failed to get BIOS serial number property: %w" , err )
130118 }
131119
132120 return sn , nil
133121}
134122
135- func (APIImplementor ) GetService (name string ) (* ServiceInfo , error ) {
136- service , err := cim . QueryServiceByName (name , [] string { "DisplayName" , "State" , "StartMode" } )
123+ func (impl APIImplementor ) GetService (name string ) (* ServiceInfo , error ) {
124+ service , err := impl . serviceFactory . GetService (name )
137125 if err != nil {
138126 return nil , fmt .Errorf ("failed to get service %s: %w" , name , err )
139127 }
140128
141- displayName , err := service . GetPropertyDisplayName ( )
129+ displayName , err := cim . GetServiceDisplayName ( service )
142130 if err != nil {
143131 return nil , fmt .Errorf ("failed to get displayName property of service %s: %w" , name , err )
144132 }
145133
146- state , err := service . GetPropertyState ( )
134+ state , err := cim . GetServiceState ( service )
147135 if err != nil {
148136 return nil , fmt .Errorf ("failed to get state property of service %s: %w" , name , err )
149137 }
150138
151- startMode , err := service . GetPropertyStartMode ( )
139+ startMode , err := cim . GetServiceStartMode ( service )
152140 if err != nil {
153141 return nil , fmt .Errorf ("failed to get startMode property of service %s: %w" , name , err )
154142 }
@@ -161,14 +149,14 @@ func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
161149}
162150
163151func (impl APIImplementor ) StartService (name string ) error {
164- startService := func (service ServiceInterface ) error {
152+ startService := func (service cim. ServiceInterface ) error {
165153 retVal , err := service .StartService ()
166154 if err != nil || (retVal != startServiceErrorCodeAccepted && retVal != startServiceErrorCodeAlreadyRunning ) {
167155 return fmt .Errorf ("error starting service name %s. return value: %d, error: %v" , name , retVal , err )
168156 }
169157 return nil
170158 }
171- serviceRunningCheck := func (service ServiceInterface , state string ) (bool , string , error ) {
159+ serviceRunningCheck := func (service cim. ServiceInterface , state string ) (bool , string , error ) {
172160 err := service .Refresh ()
173161 if err != nil {
174162 return false , "" , err
@@ -202,7 +190,7 @@ func (impl APIImplementor) StartService(name string) error {
202190
203191func (impl APIImplementor ) stopSingleService (name string ) (bool , error ) {
204192 var dependentRunning bool
205- stopService := func (service ServiceInterface ) error {
193+ stopService := func (service cim. ServiceInterface ) error {
206194 retVal , err := service .StopService ()
207195 if err != nil || (retVal != stopServiceErrorCodeAccepted && retVal != stopServiceErrorCodeStopPending ) {
208196 if retVal == stopServiceErrorCodeDependentRunning {
@@ -213,7 +201,7 @@ func (impl APIImplementor) stopSingleService(name string) (bool, error) {
213201 }
214202 return nil
215203 }
216- serviceStoppedCheck := func (service ServiceInterface , state string ) (bool , string , error ) {
204+ serviceStoppedCheck := func (service cim. ServiceInterface , state string ) (bool , string , error ) {
217205 err := service .Refresh ()
218206 if err != nil {
219207 return false , "" , err
@@ -266,47 +254,11 @@ func (impl APIImplementor) StopService(name string, force bool) error {
266254 return nil
267255}
268256
269- type Win32Service struct {
270- * cimv2.Win32_Service
271- }
272-
273- func (s * Win32Service ) GetDependents () ([]ServiceInterface , error ) {
274- collection , err := s .GetAssociated ("Win32_DependentService" , "Win32_Service" , "Dependent" , "Antecedent" )
275- if err != nil {
276- return nil , err
277- }
278-
279- var result []ServiceInterface
280- for _ , coll := range collection {
281- service , err := cimv2 .NewWin32_ServiceEx1 (coll )
282- if err != nil {
283- return nil , err
284- }
285-
286- result = append (result , & Win32Service {
287- service ,
288- })
289- }
290- return result , nil
291- }
292-
293- type Win32ServiceFactory struct {
294- }
295-
296- func (impl Win32ServiceFactory ) GetService (name string ) (ServiceInterface , error ) {
297- service , err := cim .QueryServiceByName (name , nil )
298- if err != nil {
299- return nil , err
300- }
301-
302- return & Win32Service {Win32_Service : service }, nil
303- }
304-
305257type ServiceManagerImpl struct {
306258 serviceFactory ServiceFactory
307259}
308260
309- func (impl ServiceManagerImpl ) WaitUntilServiceState (service ServiceInterface , stateTransition stateTransitionFunc , stateCheck stateCheckFunc , interval time.Duration , timeout time.Duration ) (string , error ) {
261+ func (impl ServiceManagerImpl ) WaitUntilServiceState (service cim. ServiceInterface , stateTransition stateTransitionFunc , stateCheck stateCheckFunc , interval time.Duration , timeout time.Duration ) (string , error ) {
310262 done , state , err := stateCheck (service , "" )
311263 if err != nil {
312264 return state , err
@@ -346,7 +298,7 @@ func (impl ServiceManagerImpl) WaitUntilServiceState(service ServiceInterface, s
346298
347299func (impl ServiceManagerImpl ) GetDependentsForService (name string ) ([]string , error ) {
348300 var serviceNames []string
349- var servicesToCheck []ServiceInterface
301+ var servicesToCheck []cim. ServiceInterface
350302 servicesByName := map [string ]string {}
351303
352304 service , err := impl .serviceFactory .GetService (name )
@@ -360,12 +312,12 @@ func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, e
360312 service = servicesToCheck [i ]
361313 i += 1
362314
363- serviceName , err := service . GetPropertyName ( )
315+ serviceName , err := cim . GetServiceName ( service )
364316 if err != nil {
365317 return serviceNames , err
366318 }
367319
368- currentState , err := service . GetPropertyState ( )
320+ currentState , err := cim . GetServiceState ( service )
369321 if err != nil {
370322 return serviceNames , err
371323 }
0 commit comments