From e520deb322a3c2d54defe43f8f4250af6104f00b Mon Sep 17 00:00:00 2001 From: Andrea Colli-Vignarelli <48754766+andreacv98@users.noreply.github.com> Date: Fri, 17 May 2024 10:09:54 +0200 Subject: [PATCH] Discovery empty response bug fix (#2) (#65) --- pkg/rear-controller/gateway/client.go | 8 ++++- pkg/rear-controller/gateway/provider.go | 42 +++++++++++++++---------- pkg/rear-controller/gateway/services.go | 16 ++++++++-- pkg/rear-controller/gateway/utils.go | 6 +++- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/pkg/rear-controller/gateway/client.go b/pkg/rear-controller/gateway/client.go index be837f7..169578b 100644 --- a/pkg/rear-controller/gateway/client.go +++ b/pkg/rear-controller/gateway/client.go @@ -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)) diff --git a/pkg/rear-controller/gateway/provider.go b/pkg/rear-controller/gateway/provider.go index d07f579..3424c07 100644 --- a/pkg/rear-controller/gateway/provider.go +++ b/pkg/rear-controller/gateway/provider.go @@ -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 } } @@ -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 } @@ -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 @@ -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 } @@ -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 } } diff --git a/pkg/rear-controller/gateway/services.go b/pkg/rear-controller/gateway/services.go index e80aee2..e739063 100644 --- a/pkg/rear-controller/gateway/services.go +++ b/pkg/rear-controller/gateway/services.go @@ -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) } @@ -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) } diff --git a/pkg/rear-controller/gateway/utils.go b/pkg/rear-controller/gateway/utils.go index 1f80e28..e71689f 100644 --- a/pkg/rear-controller/gateway/utils.go +++ b/pkg/rear-controller/gateway/utils.go @@ -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) }