@@ -28,9 +28,10 @@ import (
2828 "time"
2929
3030 metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
31- machinev1beta1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
3231 "golang.org/x/oauth2"
3332 corev1 "k8s.io/api/core/v1"
33+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
34+ "k8s.io/apimachinery/pkg/runtime/schema"
3435 "k8s.io/client-go/kubernetes"
3536 ctrl "sigs.k8s.io/controller-runtime"
3637 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -166,15 +167,13 @@ func (f *FTIClient) AddResource(instance *v1alpha1.ComposableResource) (deviceID
166167 return "" , "" , err
167168 }
168169
169- if response .StatusCode != http .StatusOK {
170- errBody := & fticmapi.ErrorBody {}
171- if err := json .Unmarshal (body , errBody ); err != nil {
172- clientLog .Error (err , "failed to unmarshal CM scaleup error response body into errBody" , "ComposableResource" , instance .Name )
173- return "" , "" , fmt .Errorf ("failed to unmarshal CM scaleup error response body into errBody. Original error: %w" , err )
174- }
175-
176- err = fmt .Errorf ("failed to process CM scaleup request. http returned status: '%d', cm return code: '%s', error message: '%s'" , errBody .Status , errBody .Detail .Code , errBody .Detail .Message )
177- clientLog .Error (err , "failed to process CM scaleup request" , "ComposableResource" , instance .Name )
170+ if response .StatusCode < 200 || response .StatusCode >= 300 {
171+ err = fmt .Errorf ("failed to process CM scaleup request. http returned status: %d" , response .StatusCode )
172+ clientLog .Error (err , "failed to process CM scaleup request" ,
173+ "ComposableResource" , instance .Name ,
174+ "httpStatusCode" , response .StatusCode ,
175+ "rawBody" , string (body ),
176+ )
178177 return "" , "" , err
179178 }
180179
@@ -245,15 +244,13 @@ func (f *FTIClient) RemoveResource(instance *v1alpha1.ComposableResource) error
245244 return err
246245 }
247246
248- if response .StatusCode != http .StatusOK {
249- errBody := & fticmapi.ErrorBody {}
250- if err := json .Unmarshal (body , errBody ); err != nil {
251- clientLog .Error (err , "failed to unmarshal CM scaledown error response body into errBody" , "ComposableResource" , instance .Name )
252- return fmt .Errorf ("failed to unmarshal CM scaledown error response body into errBody. Original error: %w" , err )
253- }
254-
255- err = fmt .Errorf ("failed to process CM scaledown request. http returned status: %d, cm return code: %s, error message: %s" , errBody .Status , errBody .Detail .Code , errBody .Detail .Message )
256- clientLog .Error (err , "failed to process CM scaledown request" , "ComposableResource" , instance .Name )
247+ if response .StatusCode < 200 || response .StatusCode >= 300 {
248+ err = fmt .Errorf ("failed to process CM scaledown request. http returned status: %d" , response .StatusCode )
249+ clientLog .Error (err , "failed to process CM scaledown request" ,
250+ "ComposableResource" , instance .Name ,
251+ "httpStatusCode" , response .StatusCode ,
252+ "rawBody" , string (body ),
253+ )
257254 return err
258255 }
259256
@@ -288,15 +285,20 @@ func (f *FTIClient) CheckResource(instance *v1alpha1.ComposableResource) error {
288285
289286 for _ , device := range resourceSpec .Devices {
290287 if device .DeviceUUID == instance .Status .DeviceID {
291- if device .Detail .ResourceOPStatus [:1 ] == "0" {
288+ resourceOPStatus := string (device .Detail .ResourceOPStatus )
289+ if len (resourceOPStatus ) == 0 {
290+ return fmt .Errorf ("the target gpu '%s' on machine '%s' has empty status in CM" , instance .Status .DeviceID , machineID )
291+ }
292+
293+ if resourceOPStatus [:1 ] == "0" {
292294 // The target device exists and has no error, return OK.
293295 return nil
294- } else if device . Detail . ResourceOPStatus [:1 ] == "1" {
296+ } else if resourceOPStatus [:1 ] == "1" {
295297 return fmt .Errorf ("the target gpu '%s' is showing a Warning status in CM" , instance .Status .DeviceID )
296- } else if device . Detail . ResourceOPStatus [:1 ] == "2" {
298+ } else if resourceOPStatus [:1 ] == "2" {
297299 return fmt .Errorf ("the target gpu '%s' is showing a Critical status in CM" , instance .Status .DeviceID )
298300 } else {
299- return fmt .Errorf ("the target gpu '%s' has unknown status '%s' in CM" , instance .Status .DeviceID , device . Detail . ResourceOPStatus )
301+ return fmt .Errorf ("the target gpu '%s' has unknown status '%s' in CM" , instance .Status .DeviceID , resourceOPStatus )
300302 }
301303 }
302304 }
@@ -364,15 +366,20 @@ func (f *FTIClient) getNodeMachineID(nodeName string) (string, error) {
364366 return "" , fmt .Errorf ("failed to get annotation 'machine.openshift.io/machine' from Node %s, now is '%s'" , node .Name , machineInfo )
365367 }
366368
367- machine := & machinev1beta1.Metal3Machine {}
369+ machine := & unstructured.Unstructured {}
370+ machine .SetGroupVersionKind (schema.GroupVersionKind {
371+ Group : "machine.openshift.io" ,
372+ Version : "v1beta1" ,
373+ Kind : "Machine" ,
374+ })
368375 if err := f .client .Get (f .ctx , client.ObjectKey {Namespace : machineInfoParts [0 ], Name : machineInfoParts [1 ]}, machine ); err != nil {
369376 return "" , err
370377 }
371378
372379 bmhInfo := machine .GetAnnotations ()["metal3.io/BareMetalHost" ]
373380 bmhInfoParts := strings .Split (bmhInfo , "/" )
374381 if len (bmhInfoParts ) != 2 {
375- return "" , fmt .Errorf ("failed to get annotation 'metal3.io/BareMetalHost' from Machine %s, now is '%s'" , machine .Name , bmhInfo )
382+ return "" , fmt .Errorf ("failed to get annotation 'metal3.io/BareMetalHost' from Machine %s, now is '%s'" , machine .GetName () , bmhInfo )
376383 }
377384
378385 bmh := & metal3v1alpha1.BareMetalHost {}
@@ -411,13 +418,13 @@ func (f *FTIClient) getMachineInfo(machineID string) (*fticmapi.Data, error) {
411418 return nil , err
412419 }
413420
414- if response .StatusCode != http . StatusOK {
415- errBody := & fticmapi. ErrorBody {}
416- if err := json . Unmarshal ( body , errBody ); err != nil {
417- return nil , fmt . Errorf ( "failed to unmarshal CM get error response body into errBody. Original error: %w " , err )
418- }
419-
420- err = fmt . Errorf ( "failed to process CM get request. http returned status: '%d', cm return code: '%s', error message: '%s'" , errBody . Status , errBody . Detail . Code , errBody . Detail . Message )
421+ if response .StatusCode < 200 || response . StatusCode >= 300 {
422+ err = fmt . Errorf ( "failed to process CM get request. http returned status: %d" , response . StatusCode )
423+ clientLog . Error ( err , "failed to process CM get request" ,
424+ "machineID " , machineID ,
425+ "httpStatusCode" , response . StatusCode ,
426+ "rawBody" , string ( body ),
427+ )
421428 return nil , err
422429 }
423430
@@ -459,26 +466,19 @@ func checkAddingResources(machineData *fticmapi.Data, composableResourceList *v1
459466}
460467
461468func checkRemovingResources (machineData * fticmapi.Data , instance * v1alpha1.ComposableResource ) (string , int , error ) {
462- var specUUID string
463- var deviceCount int
464469 for _ , resourceSpec := range machineData .Cluster .Machine .ResourceSpecs {
465- if ! isSpecMatch ( resourceSpec , instance ) {
470+ if resourceSpec . Type != instance . Spec . Type {
466471 continue
467472 }
468473
469- specUUID = resourceSpec .SpecUUID
470- deviceCount = resourceSpec .DeviceCount
471-
472474 for _ , device := range resourceSpec .Devices {
473475 if device .DeviceUUID == instance .Status .DeviceID {
474476 if device .Status == removeFailed {
475- return specUUID , deviceCount , fmt .Errorf ("%s" , device .StatusReason )
477+ return resourceSpec . SpecUUID , resourceSpec . DeviceCount , fmt .Errorf ("%s" , device .StatusReason )
476478 }
477- return specUUID , deviceCount , nil
479+ return resourceSpec . SpecUUID , resourceSpec . DeviceCount , nil
478480 }
479481 }
480-
481- break
482482 }
483483
484484 return "" , 0 , nil
0 commit comments