Skip to content

Commit

Permalink
Discovery empty response bug fix (#2) (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreacv98 authored May 17, 2024
1 parent f5e8e2b commit e520deb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
8 changes: 7 additions & 1 deletion pkg/rear-controller/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ func (g *Gateway) DiscoverFlavours(ctx context.Context, selector *nodecorev1alph
klog.Errorf("Error when searching Flavour: %s", err)
return nil, err
}
flavoursCR = append(flavoursCR, flavour)
// Check if the flavour is nil
if flavour == nil {
klog.Infof("No Flavours found for provider %s", provider)
} else {
klog.Infof("Flavour found for provider %s", provider)
flavoursCR = append(flavoursCR, flavour)
}
}

klog.Infof("Found %d flavours", len(flavoursCR))
Expand Down
42 changes: 26 additions & 16 deletions pkg/rear-controller/gateway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,30 @@ func (g *Gateway) getFlavours(w http.ResponseWriter, _ *http.Request) {

klog.Infof("Found %d Flavours in the cluster", len(flavours))

availableFlavours := make([]nodecorev1alpha1.Flavour, 0)

// Filtering only the available flavours
for i := range flavours {
if !flavours[i].Spec.OptionalFields.Availability {
flavours = append(flavours[:i], flavours[i+1:]...)
availableFlavours = append(availableFlavours, flavours[i])
}
}

klog.Infof("Available Flavours: %d", len(flavours))
if len(flavours) == 0 {
klog.Infof("Available Flavours: %d", len(availableFlavours))
if len(availableFlavours) == 0 {
klog.Infof("No available Flavours found")
http.Error(w, "No Flavours found", http.StatusNotFound)
// Return content for empty list
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
encodeResponseStatusCode(w, emptyList, http.StatusNoContent)
return
}

// Select the flavour with the max CPU
max := resource.MustParse("0")
index := 0
for i := range flavours {
if flavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
max = flavours[i].Spec.Characteristics.Cpu
for i := range availableFlavours {
if availableFlavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
max = availableFlavours[i].Spec.Characteristics.Cpu
index = i
}
}
Expand Down Expand Up @@ -118,17 +122,21 @@ func (g *Gateway) getFlavoursBySelector(w http.ResponseWriter, r *http.Request)

klog.Infof("Found %d Flavours in the cluster", len(flavours))

availableFlavours := make([]nodecorev1alpha1.Flavour, 0)

// Filtering only the available flavours
for i := range flavours {
if !flavours[i].Spec.OptionalFields.Availability {
flavours = append(flavours[:i], flavours[i+1:]...)
if flavours[i].Spec.OptionalFields.Availability {
availableFlavours = append(availableFlavours, flavours[i])
}
}

klog.Infof("Available Flavours: %d", len(flavours))
if len(flavours) == 0 {
klog.Infof("Available Flavours: %d", len(availableFlavours))
if len(availableFlavours) == 0 {
klog.Infof("No available Flavours found")
http.Error(w, "No Flavours found", http.StatusNotFound)
// Return content for empty list
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
encodeResponseStatusCode(w, emptyList, http.StatusNoContent)
return
}

Expand All @@ -140,7 +148,7 @@ func (g *Gateway) getFlavoursBySelector(w http.ResponseWriter, r *http.Request)
}

klog.Infof("Filtering Flavours by selector...")
flavoursSelected, err := common.FilterFlavoursBySelector(flavours, selector)
flavoursSelected, err := common.FilterFlavoursBySelector(availableFlavours, selector)
if err != nil {
http.Error(w, "Error getting the Flavours by selector", http.StatusInternalServerError)
return
Expand All @@ -150,7 +158,9 @@ func (g *Gateway) getFlavoursBySelector(w http.ResponseWriter, r *http.Request)

if len(flavoursSelected) == 0 {
klog.Infof("No matching Flavours found")
http.Error(w, "No Flavours found", http.StatusNotFound)
// Return content for empty list
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
encodeResponse(w, emptyList)
return
}

Expand All @@ -159,8 +169,8 @@ func (g *Gateway) getFlavoursBySelector(w http.ResponseWriter, r *http.Request)
index := 0

for i := range flavoursSelected {
if flavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
max = flavours[i].Spec.Characteristics.Cpu
if flavoursSelected[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
max = flavoursSelected[i].Spec.Characteristics.Cpu
index = i
}
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/rear-controller/gateway/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ func searchFlavourWithSelector(ctx context.Context, selector *models.Selector, a

defer resp.Body.Close()

// Check if the response status code is 200 (OK)
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusOK:
klog.Infof("Received OK response status code: %d", resp.StatusCode)
case http.StatusNoContent:
klog.Infof("Received No Content response status code: %d", resp.StatusCode)
return nil, nil
default:
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
}

Expand All @@ -74,7 +79,12 @@ func searchFlavour(ctx context.Context, addr string) (*nodecorev1alpha1.Flavour,
defer resp.Body.Close()

// Check if the response status code is 200 (OK)
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusOK:
break
case http.StatusNoContent:
return nil, nil
default:
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/rear-controller/gateway/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ func handleError(w http.ResponseWriter, err error, statusCode int) {

// encodeResponse encodes the response as JSON and writes it to the response writer.
func encodeResponse(w http.ResponseWriter, data interface{}) {
encodeResponseStatusCode(w, data, http.StatusOK)
}

func encodeResponseStatusCode(w http.ResponseWriter, data interface{}, statusCode int) {
resp, err := json.Marshal(data)
if err != nil {
handleError(w, err, http.StatusInternalServerError)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.WriteHeader(statusCode)
_, _ = w.Write(resp)
}

0 comments on commit e520deb

Please sign in to comment.