Skip to content

Commit e520deb

Browse files
authored
Discovery empty response bug fix (#2) (#65)
1 parent f5e8e2b commit e520deb

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

pkg/rear-controller/gateway/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,13 @@ func (g *Gateway) DiscoverFlavours(ctx context.Context, selector *nodecorev1alph
180180
klog.Errorf("Error when searching Flavour: %s", err)
181181
return nil, err
182182
}
183-
flavoursCR = append(flavoursCR, flavour)
183+
// Check if the flavour is nil
184+
if flavour == nil {
185+
klog.Infof("No Flavours found for provider %s", provider)
186+
} else {
187+
klog.Infof("Flavour found for provider %s", provider)
188+
flavoursCR = append(flavoursCR, flavour)
189+
}
184190
}
185191

186192
klog.Infof("Found %d flavours", len(flavoursCR))

pkg/rear-controller/gateway/provider.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,30 @@ func (g *Gateway) getFlavours(w http.ResponseWriter, _ *http.Request) {
5353

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

56+
availableFlavours := make([]nodecorev1alpha1.Flavour, 0)
57+
5658
// Filtering only the available flavours
5759
for i := range flavours {
5860
if !flavours[i].Spec.OptionalFields.Availability {
59-
flavours = append(flavours[:i], flavours[i+1:]...)
61+
availableFlavours = append(availableFlavours, flavours[i])
6062
}
6163
}
6264

63-
klog.Infof("Available Flavours: %d", len(flavours))
64-
if len(flavours) == 0 {
65+
klog.Infof("Available Flavours: %d", len(availableFlavours))
66+
if len(availableFlavours) == 0 {
6567
klog.Infof("No available Flavours found")
66-
http.Error(w, "No Flavours found", http.StatusNotFound)
68+
// Return content for empty list
69+
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
70+
encodeResponseStatusCode(w, emptyList, http.StatusNoContent)
6771
return
6872
}
6973

7074
// Select the flavour with the max CPU
7175
max := resource.MustParse("0")
7276
index := 0
73-
for i := range flavours {
74-
if flavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
75-
max = flavours[i].Spec.Characteristics.Cpu
77+
for i := range availableFlavours {
78+
if availableFlavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
79+
max = availableFlavours[i].Spec.Characteristics.Cpu
7680
index = i
7781
}
7882
}
@@ -118,17 +122,21 @@ func (g *Gateway) getFlavoursBySelector(w http.ResponseWriter, r *http.Request)
118122

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

125+
availableFlavours := make([]nodecorev1alpha1.Flavour, 0)
126+
121127
// Filtering only the available flavours
122128
for i := range flavours {
123-
if !flavours[i].Spec.OptionalFields.Availability {
124-
flavours = append(flavours[:i], flavours[i+1:]...)
129+
if flavours[i].Spec.OptionalFields.Availability {
130+
availableFlavours = append(availableFlavours, flavours[i])
125131
}
126132
}
127133

128-
klog.Infof("Available Flavours: %d", len(flavours))
129-
if len(flavours) == 0 {
134+
klog.Infof("Available Flavours: %d", len(availableFlavours))
135+
if len(availableFlavours) == 0 {
130136
klog.Infof("No available Flavours found")
131-
http.Error(w, "No Flavours found", http.StatusNotFound)
137+
// Return content for empty list
138+
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
139+
encodeResponseStatusCode(w, emptyList, http.StatusNoContent)
132140
return
133141
}
134142

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

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

151159
if len(flavoursSelected) == 0 {
152160
klog.Infof("No matching Flavours found")
153-
http.Error(w, "No Flavours found", http.StatusNotFound)
161+
// Return content for empty list
162+
emptyList := make([]*nodecorev1alpha1.Flavour, 0)
163+
encodeResponse(w, emptyList)
154164
return
155165
}
156166

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

161171
for i := range flavoursSelected {
162-
if flavours[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
163-
max = flavours[i].Spec.Characteristics.Cpu
172+
if flavoursSelected[i].Spec.Characteristics.Cpu.Cmp(max) == 1 {
173+
max = flavoursSelected[i].Spec.Characteristics.Cpu
164174
index = i
165175
}
166176
}

pkg/rear-controller/gateway/services.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ func searchFlavourWithSelector(ctx context.Context, selector *models.Selector, a
4747

4848
defer resp.Body.Close()
4949

50-
// Check if the response status code is 200 (OK)
51-
if resp.StatusCode != http.StatusOK {
50+
switch resp.StatusCode {
51+
case http.StatusOK:
52+
klog.Infof("Received OK response status code: %d", resp.StatusCode)
53+
case http.StatusNoContent:
54+
klog.Infof("Received No Content response status code: %d", resp.StatusCode)
55+
return nil, nil
56+
default:
5257
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
5358
}
5459

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

7681
// Check if the response status code is 200 (OK)
77-
if resp.StatusCode != http.StatusOK {
82+
switch resp.StatusCode {
83+
case http.StatusOK:
84+
break
85+
case http.StatusNoContent:
86+
return nil, nil
87+
default:
7888
return nil, fmt.Errorf("received non-OK response status code: %d", resp.StatusCode)
7989
}
8090

pkg/rear-controller/gateway/utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ func handleError(w http.ResponseWriter, err error, statusCode int) {
6969

7070
// encodeResponse encodes the response as JSON and writes it to the response writer.
7171
func encodeResponse(w http.ResponseWriter, data interface{}) {
72+
encodeResponseStatusCode(w, data, http.StatusOK)
73+
}
74+
75+
func encodeResponseStatusCode(w http.ResponseWriter, data interface{}, statusCode int) {
7276
resp, err := json.Marshal(data)
7377
if err != nil {
7478
handleError(w, err, http.StatusInternalServerError)
7579
}
7680

7781
w.Header().Set("Content-Type", "application/json")
78-
w.WriteHeader(http.StatusOK)
82+
w.WriteHeader(statusCode)
7983
_, _ = w.Write(resp)
8084
}

0 commit comments

Comments
 (0)