Skip to content

Commit

Permalink
Discovery empty response bug fix (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreacv98 authored May 16, 2024
1 parent f5e8e2b commit 75bbf79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 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
20 changes: 15 additions & 5 deletions pkg/rear-controller/gateway/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ 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 {
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
switch resp.StatusCode {
case http.StatusOK:

Check failure on line 51 in pkg/rear-controller/gateway/services.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/fluidos-project/node) (gci)
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)
}

if err := json.NewDecoder(resp.Body).Decode(&flavour); err != nil {
Expand All @@ -74,8 +79,13 @@ 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 {
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
switch resp.StatusCode {
case http.StatusOK:

Check failure on line 83 in pkg/rear-controller/gateway/services.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/fluidos-project/node) (gci)
break
case http.StatusNoContent:
return nil, nil
default:
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
}

if err := json.NewDecoder(resp.Body).Decode(&flavour); err != nil {
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 75bbf79

Please sign in to comment.