Skip to content

Commit

Permalink
bugfix: fixed issue with not querying component correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
davidallendj committed Dec 9, 2024
1 parent 9140cb0 commit dd37cb2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
21 changes: 17 additions & 4 deletions cmd/cloud-init-server/instance_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,29 @@ instance-data:

func InstanceDataHandler(smd smdclient.SMDClientInterface, clusterName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// id should be the ip address that the request originated from. Check the headers to see if the request has been forwarded and the remote IP is preserved
// ip should be the ip address that the request originated from. Check the headers to see if the request has been forwarded and the remote IP is preserved
// Check for the first ip in the X-Forwarded-For header if it exists
var id string
var ip string
if r.Header.Get("X-Forwarded-For") != "" {
// If it exists, use that
id = strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]
ip = strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]
} else {
id = r.RemoteAddr
portIndex := strings.LastIndex(r.RemoteAddr, ":")
if portIndex > 0 {
ip = r.RemoteAddr[:portIndex]
} else {
ip = r.RemoteAddr
}
}
// Get the component information from the SMD client
id, err := smd.IDfromIP(ip)
if err != nil {
fmt.Print(err)
w.WriteHeader(http.StatusUnprocessableEntity)
return
} else {
fmt.Printf("xname %s with ip %s found\n", id, ip)
}
smdComponent, err := smd.ComponentInformation(id)
if err != nil {
// If the component information is not available, return a 404
Expand Down
12 changes: 9 additions & 3 deletions internal/smdclient/SMDclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ func (s *SMDClient) getSMD(ep string, smd interface{}) error {
}
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
log.Info().Msgf("body: %s", string(body))
if err != nil {
log.Error().Err(err).Msg("failed to read response body")
return err
}
if err := json.Unmarshal(body, smd); err != nil {
log.Error().Err(err).Str("body", string(body)).Msg("failed to unmarshal SMD response")
return ErrUnmarshal
}
return nil
Expand All @@ -154,7 +160,7 @@ func (s *SMDClient) getSMD(ep string, smd interface{}) error {
// with the corresponding node information, including MAC addresses, IP addresses, and descriptions.
func (s *SMDClient) PopulateNodes() {
var ethIfaceArray []sm.CompEthInterfaceV2
ep := "/hsm/v2/Inventory/EthernetInterfaces/"
ep := "/Inventory/EthernetInterfaces/"
if err := s.getSMD(ep, &ethIfaceArray); err != nil {
log.Error().Err(err).Msg("Failed to get SMD data")
return
Expand Down Expand Up @@ -263,7 +269,7 @@ func (s *SMDClient) GroupMembership(id string) ([]string, error) {

func (s *SMDClient) ComponentInformation(id string) (base.Component, error) {
var node base.Component
ep := "/hsm/v2/State/Components/" + id
ep := "/State/Components/" + id
err := s.getSMD(ep, &node)
if err != nil {
return node, err
Expand Down

0 comments on commit dd37cb2

Please sign in to comment.