Skip to content

Commit 3cbe049

Browse files
committed
Add 503 status code when there are zero upstream endpoints
1 parent f9fa98c commit 3cbe049

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/mode/static/nginx/agent/agent.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ func buildStreamUpstreamServers(upstream dataplane.Upstream) *pb.UpdateStreamSer
179179
}
180180

181181
func buildUpstreamServers(upstream dataplane.Upstream) []*structpb.Struct {
182+
if len(upstream.Endpoints) == 0 {
183+
return []*structpb.Struct{
184+
{
185+
Fields: map[string]*structpb.Value{
186+
"server": structpb.NewStringValue("unix:/var/run/nginx/nginx-503-server.sock"),
187+
},
188+
},
189+
}
190+
}
191+
182192
servers := make([]*structpb.Struct, 0, len(upstream.Endpoints))
183193

184194
for _, endpoint := range upstream.Endpoints {

internal/mode/static/nginx/agent/agent_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ func TestUpdateUpstreamServers(t *testing.T) {
179179
},
180180
},
181181
},
182+
{
183+
Name: "empty-upstream",
184+
Endpoints: []resolver.Endpoint{},
185+
},
182186
},
183187
StreamUpstreams: []dataplane.Upstream{
184188
{
@@ -212,6 +216,20 @@ func TestUpdateUpstreamServers(t *testing.T) {
212216
},
213217
},
214218
},
219+
{
220+
Action: &pb.NGINXPlusAction_UpdateHttpUpstreamServers{
221+
UpdateHttpUpstreamServers: &pb.UpdateHTTPUpstreamServers{
222+
HttpUpstreamName: "empty-upstream",
223+
Servers: []*structpb.Struct{
224+
{
225+
Fields: map[string]*structpb.Value{
226+
"server": structpb.NewStringValue("unix:/var/run/nginx/nginx-503-server.sock"),
227+
},
228+
},
229+
},
230+
},
231+
},
232+
},
215233
{
216234
Action: &pb.NGINXPlusAction_UpdateStreamServers{
217235
UpdateStreamServers: &pb.UpdateStreamServers{
@@ -234,13 +252,14 @@ func TestUpdateUpstreamServers(t *testing.T) {
234252
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(0))
235253
} else if test.buildUpstreams {
236254
g.Expect(deployment.GetNGINXPlusActions()).To(Equal(expActions))
237-
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(2))
255+
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(3))
238256
}
239257

240258
if test.expErr {
241259
expErr := errors.Join(
242260
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
243261
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
262+
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
244263
)
245264

246265
g.Expect(deployment.GetLatestUpstreamError()).To(Equal(expErr))

0 commit comments

Comments
 (0)