Skip to content

Commit

Permalink
redfish: update based on test results
Browse files Browse the repository at this point in the history
Signed-off-by: Huamin Chen <[email protected]>
  • Loading branch information
rootfs committed Jul 17, 2023
1 parent e686407 commit 134f6a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var (
// redfish cred file path
redfishCredFilePath string
redfishProbeIntervalInSeconds = getConfig("REDFISH_PROBE_INTERVAL_IN_SECONDS", "60")
redfishSkipSSLVerify = getBoolConfig("REDFISH_SKIP_SSL_VERIFY", false)
redfishSkipSSLVerify = getBoolConfig("REDFISH_SKIP_SSL_VERIFY", true)

////////////////////////////////////
ModelServerEnable = getBoolConfig("MODEL_SERVER_ENABLE", false)
Expand Down
2 changes: 1 addition & 1 deletion pkg/power/platform/source/redfish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestRedFishClient_IsPowerSupported(t *testing.T) {
if err := json.NewEncoder(w).Encode(system); err != nil {
fmt.Println(err)
}
} else if r.URL.Path == "/redfish/v1/Chassis/1/Power#/PowerControl" || r.URL.Path == "/redfish/v1/Chassis/2/Power#/PowerControl" {
} else if r.URL.Path == "/redfish/v1/Chassis/1/Power" || r.URL.Path == "/redfish/v1/Chassis/2/Power" {
power := RedfishPowerModel{
Name: "Test Power",
PowerControl: []PowerControl{
Expand Down
36 changes: 31 additions & 5 deletions pkg/power/platform/source/redfish_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/sustainable-computing-io/kepler/pkg/config"
"k8s.io/klog/v2"
)

func getRedfishModel(access RedfishAccessInfo, endpoint string, model interface{}) error {
Expand All @@ -49,6 +51,15 @@ func getRedfishModel(access RedfishAccessInfo, endpoint string, model interface{
defer cancel()
req = req.WithContext(ctx)

// add additional header: 'OData-Version': '4.0'
req.Header.Add("OData-Version", "4.0")
// add accept header: 'application/json'
req.Header.Add("Accept", "application/json")
// set User-Agent header
req.Header.Set("User-Agent", "kepler")
// set keep-alive header
req.Header.Set("Connection", "keep-alive")
// set basic auth
req.SetBasicAuth(username, password)

// Send the request and check the response
Expand All @@ -63,19 +74,33 @@ func getRedfishModel(access RedfishAccessInfo, endpoint string, model interface{
return fmt.Errorf("server returned status: %v", resp.Status)
}

dec := json.NewDecoder(resp.Body)
dec.DisallowUnknownFields()

var returnErr error
// Decode the response body into the provided model struct
err = json.NewDecoder(resp.Body).Decode(model)
if err != nil {
return err
decErr := dec.Decode(model)

// process the error and only return significant ones
if decErr != nil {
if strings.HasPrefix(decErr.Error(), "json: unknown field ") {
// ignore unknown field error
fieldName := strings.TrimPrefix(decErr.Error(), "json: unknown field ")
klog.V(6).Infof("Request body contains unknown field %s", fieldName)
} else {
returnErr = decErr
klog.V(5).Infof("Failed to decode response: %v", decErr)
}
}

return nil
return returnErr
}

func getRedfishSystem(access RedfishAccessInfo) (*RedfishSystemModel, error) {
var system RedfishSystemModel
err := getRedfishModel(access, "/redfish/v1/Systems", &system)
if err != nil {
klog.V(1).Infof("Failed to get system: %v", err)
return nil, err
}

Expand All @@ -84,8 +109,9 @@ func getRedfishSystem(access RedfishAccessInfo) (*RedfishSystemModel, error) {

func getRedfishPower(access RedfishAccessInfo, system string) (*RedfishPowerModel, error) {
var power RedfishPowerModel
err := getRedfishModel(access, "/redfish/v1/Chassis/"+system+"/Power%23/PowerControl", &power)
err := getRedfishModel(access, "/redfish/v1/Chassis/"+system+"/Power#/PowerControl", &power)
if err != nil {
klog.V(1).Infof("Failed to get power: %v", err)
return nil, err
}

Expand Down

0 comments on commit 134f6a1

Please sign in to comment.