Skip to content

Add 503 status code when there are zero upstream endpoints #3406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/controller/nginx/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/agent/broadcast"
agentgrpc "github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/agent/grpc"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/types"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/dataplane"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/resolver"
"github.com/nginx/nginx-gateway-fabric/internal/controller/status"
Expand Down Expand Up @@ -179,6 +180,16 @@ func buildStreamUpstreamServers(upstream dataplane.Upstream) *pb.UpdateStreamSer
}

func buildUpstreamServers(upstream dataplane.Upstream) []*structpb.Struct {
if len(upstream.Endpoints) == 0 {
return []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
"server": structpb.NewStringValue(types.Nginx503Server),
},
},
}
}

servers := make([]*structpb.Struct, 0, len(upstream.Endpoints))

for _, endpoint := range upstream.Endpoints {
Expand Down
22 changes: 21 additions & 1 deletion internal/controller/nginx/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/agent/broadcast/broadcastfakes"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/types"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/dataplane"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/resolver"
"github.com/nginx/nginx-gateway-fabric/internal/controller/status"
Expand Down Expand Up @@ -179,6 +180,10 @@ func TestUpdateUpstreamServers(t *testing.T) {
},
},
},
{
Name: "empty-upstream",
Endpoints: []resolver.Endpoint{},
},
},
StreamUpstreams: []dataplane.Upstream{
{
Expand Down Expand Up @@ -212,6 +217,20 @@ func TestUpdateUpstreamServers(t *testing.T) {
},
},
},
{
Action: &pb.NGINXPlusAction_UpdateHttpUpstreamServers{
UpdateHttpUpstreamServers: &pb.UpdateHTTPUpstreamServers{
HttpUpstreamName: "empty-upstream",
Servers: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
"server": structpb.NewStringValue(types.Nginx503Server),
},
},
},
},
},
},
{
Action: &pb.NGINXPlusAction_UpdateStreamServers{
UpdateStreamServers: &pb.UpdateStreamServers{
Expand All @@ -234,13 +253,14 @@ func TestUpdateUpstreamServers(t *testing.T) {
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(0))
} else if test.buildUpstreams {
g.Expect(deployment.GetNGINXPlusActions()).To(Equal(expActions))
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(2))
g.Expect(fakeBroadcaster.SendCallCount()).To(Equal(3))
}

if test.expErr {
expErr := errors.Join(
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
fmt.Errorf("couldn't update upstream via the API: %w", testErr),
)

g.Expect(deployment.GetLatestUpstreamError()).To(Equal(expErr))
Expand Down
5 changes: 2 additions & 3 deletions internal/controller/nginx/config/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/http"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies/upstreamsettings"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/stream"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/types"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/dataplane"
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
)
Expand All @@ -17,8 +18,6 @@ var (
)

const (
// nginx503Server is used as a backend for services that cannot be resolved (have no IP address).
nginx503Server = "unix:/var/run/nginx/nginx-503-server.sock"
// nginx500Server is used as a server for the invalid backend ref upstream.
nginx500Server = "unix:/var/run/nginx/nginx-500-server.sock"
// invalidBackendRef is used as an upstream name for invalid backend references.
Expand Down Expand Up @@ -159,7 +158,7 @@ func (g GeneratorImpl) createUpstream(
StateFile: stateFile,
Servers: []http.UpstreamServer{
{
Address: nginx503Server,
Address: types.Nginx503Server,
},
},
}
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/nginx/config/upstreams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies/upstreamsettings"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/stream"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/types"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/dataplane"
"github.com/nginx/nginx-gateway-fabric/internal/controller/state/resolver"
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestCreateUpstreams(t *testing.T) {
ZoneSize: ossZoneSize,
Servers: []http.UpstreamServer{
{
Address: nginx503Server,
Address: types.Nginx503Server,
},
},
},
Expand Down Expand Up @@ -277,7 +278,7 @@ func TestCreateUpstream(t *testing.T) {
ZoneSize: ossZoneSize,
Servers: []http.UpstreamServer{
{
Address: nginx503Server,
Address: types.Nginx503Server,
},
},
},
Expand All @@ -293,7 +294,7 @@ func TestCreateUpstream(t *testing.T) {
ZoneSize: ossZoneSize,
Servers: []http.UpstreamServer{
{
Address: nginx503Server,
Address: types.Nginx503Server,
},
},
},
Expand Down Expand Up @@ -581,7 +582,7 @@ func TestCreateUpstreamPlus(t *testing.T) {
StateFile: stateDir + "/no-endpoints.conf",
Servers: []http.UpstreamServer{
{
Address: nginx503Server,
Address: types.Nginx503Server,
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/nginx/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

const (
// Nginx503Server is used as a backend for services that cannot be resolved (have no IP address).
Nginx503Server = "unix:/var/run/nginx/nginx-503-server.sock"
)
3 changes: 0 additions & 3 deletions tests/suite/graceful_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@

It("recovers when node is restarted abruptly", func() {
if *plusEnabled {
Skip(fmt.Sprintf("Skipping test when using NGINX Plus due to known issue:" +

Check notice on line 516 in tests/suite/graceful_recovery_test.go

View workflow job for this annotation

GitHub Actions / Functional tests (plus, v1.33.1) / Run Tests

It 05/27/25 23:21:32.254
" https://github.com/nginx/nginx-gateway-fabric/issues/3248"))
}
runRestartNodeAbruptlyTest(teaURL, coffeeURL, files, &ns)
Expand Down Expand Up @@ -585,9 +585,6 @@
"connect() failed (111: Connection refused)",
"could not be resolved (host not found) during usage report",
"server returned 429",
// FIXME(salonichf5) remove this error message check
// when https://github.com/nginx/nginx-gateway-fabric/issues/2090 is completed.
"no live upstreams while connecting to upstream",
}

unexpectedErrors := ""
Expand Down