diff --git a/pkg/compose/jobs.go b/pkg/compose/jobs.go index eb93a1249..d8b027de4 100644 --- a/pkg/compose/jobs.go +++ b/pkg/compose/jobs.go @@ -68,15 +68,22 @@ func (s *composeService) jobsClient() (jobsv0.Jobs, error) { return s.jobsAPI, s.jobsAPIErr } -// jobsStatus is what the engine offers for jobs. +// jobsStatus is what the engine offers for jobs, as reachable through its API endpoint. type jobsStatus int const ( jobsAvailable jobsStatus = iota jobsEngineTooOld jobsExtensionOff + // jobsHTTP2Unsupported is an engine API endpoint that does not speak HTTP/2 + // (an HTTP/1.1-only proxy, as in front of Docker Desktop's engine): gRPC + // cannot reach the extension, whether it is enabled or not. + jobsHTTP2Unsupported ) +// jobsHTTP2Reason says why jobs are unusable through an endpoint without HTTP/2. +const jobsHTTP2Reason = "the engine API endpoint does not support HTTP/2" + // errJobsExtensionNotReady reports a jobs extension that is enabled but does // not answer yet (or failed to activate): not missing support, a failure. var errJobsExtensionNotReady = errors.New("the engine's jobs extension is enabled but not ready") @@ -84,8 +91,9 @@ var errJobsExtensionNotReady = errors.New("the engine's jobs extension is enable // jobsAvailability reports what the engine offers for jobs. The API version // only says the extension is compiled in: it is disabled by default and the // engine cannot list its extensions, so a call scoped to the project tells -// (Unimplemented: off). Unavailable (enabled, not ready) is an error, never a -// status, so no caller degrades on it. +// (Unimplemented: off). An Unavailable from the engine (enabled, not ready) is +// an error, never a status, so no caller degrades on it. The one Unavailable +// that is a status is a transport without HTTP/2 (jobsHTTP2Unsupported). func (s *composeService) jobsAvailability(ctx context.Context, projectName string) (jobsStatus, error) { version, err := s.RuntimeAPIVersion(ctx) if err != nil { @@ -120,12 +128,23 @@ func (s *composeService) probeJobs(ctx context.Context, projectName string) (job case codes.Unimplemented: return jobsExtensionOff, nil case codes.Unavailable: + if isHTTP2Unsupported(err) { + return jobsHTTP2Unsupported, nil + } return jobsAvailable, fmt.Errorf("%w: %s", errJobsExtensionNotReady, status.Convert(err).Message()) default: return jobsAvailable, err } } +// isHTTP2Unsupported tells whether a gRPC error means the engine API endpoint +// answered the HTTP/2 preface with HTTP/1.1. gRPC gives it no code of its own, +// and words a dropped connection the same way: only the HTTP/1.1 hint x/net/http2 +// adds to its error proves the missing HTTP/2 (see the TestIsHTTP2Unsupported* tests). +func isHTTP2Unsupported(err error) bool { + return strings.Contains(status.Convert(err).Message(), "looked like an HTTP/1.1 header") +} + // rejectUnsupportedScheduledJobs refuses a project declaring active scheduled // jobs when the engine cannot run them: silently not scheduling them would // break the user's expectations, unlike manual jobs which simply wait for an @@ -145,6 +164,9 @@ func (s *composeService) rejectUnsupportedScheduledJobs(ctx context.Context, pro case jobsExtensionOff: return fmt.Errorf("scheduled jobs need the engine's jobs extension, which is not enabled (start dockerd with %s): %s", jobsv0.EnableFlag, strings.Join(names, ", ")) + case jobsHTTP2Unsupported: + return fmt.Errorf("scheduled jobs need the engine's jobs extension, which is unreachable because %s: %s", + jobsHTTP2Reason, strings.Join(names, ", ")) } return nil } @@ -491,6 +513,8 @@ func (s *composeService) RunJob(ctx context.Context, project *types.Project, nam return 0, fmt.Errorf("%w: this version of the engine (API version %s) predates jobs", api.ErrJobsNotSupported, version) case jobsExtensionOff: return 0, fmt.Errorf("%w: the jobs extension is not enabled (start dockerd with %s)", api.ErrJobsNotSupported, jobsv0.EnableFlag) + case jobsHTTP2Unsupported: + return 0, fmt.Errorf("%w: %s, which the jobs extension needs", api.ErrJobsNotSupported, jobsHTTP2Reason) } // Past the availability switch so a degraded run, a one-off service, keeps // honouring -d and dry-run. diff --git a/pkg/compose/jobs_test.go b/pkg/compose/jobs_test.go index b127dee33..4a285f9b8 100644 --- a/pkg/compose/jobs_test.go +++ b/pkg/compose/jobs_test.go @@ -20,6 +20,9 @@ import ( "bytes" "context" "errors" + "net" + "net/http" + "net/http/httptest" "os" "slices" "strings" @@ -31,8 +34,11 @@ import ( "github.com/moby/moby/client" "github.com/sirupsen/logrus" "go.uber.org/mock/gomock" + "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/emptypb" "gotest.tools/v3/assert" jobsv0 "github.com/docker/compose/v5/internal/jobsapi" @@ -756,6 +762,9 @@ var ( // jobsExtensionNotReadyErr is what an engine whose jobs extension is // enabled but still reconciling (or failed to activate) answers. jobsExtensionNotReadyErr = status.Error(codes.Unavailable, "the jobs extension is not active") + // jobsNoHTTP2Err is what the HTTP/1.1-only Docker Desktop socket yields. + jobsNoHTTP2Err = status.Error(codes.Unavailable, `connection error: desc = "error reading server preface: `+ + `http2: failed reading the frame payload: http2: frame too large, note that the frame header looked like an HTTP/1.1 header"`) ) const ( @@ -771,6 +780,10 @@ const ( // jobsExtensionNotReady prefixes the distinct error of an enabled but not // ready extension. jobsExtensionNotReady = "the engine's jobs extension is enabled but not ready: the jobs extension is not active" + // scheduledJobsNoHTTP2Refused is what a project declaring the scheduled + // jobs of scheduledJobsProject gets through such an endpoint. + scheduledJobsNoHTTP2Refused = "scheduled jobs need the engine's jobs extension, which is unreachable because " + + "the engine API endpoint does not support HTTP/2: backup, sync" ) func TestJobsAvailability(t *testing.T) { @@ -807,6 +820,13 @@ func TestJobsAvailability(t *testing.T) { assert.ErrorContains(t, err, jobsExtensionNotReady) }) + t.Run("an endpoint without HTTP/2 is its own status, not an extension failure", func(t *testing.T) { + fake := &fakeJobsClient{listErr: jobsNoHTTP2Err} + got, err := newJobsTestService(t, apiVersionJobs, fake).jobsAvailability(t.Context(), "prj") + assert.NilError(t, err) + assert.Equal(t, got, jobsHTTP2Unsupported) + }) + t.Run("any other error is returned as is", func(t *testing.T) { boom := errors.New("connection refused") fake := &fakeJobsClient{listErr: boom} @@ -816,7 +836,7 @@ func TestJobsAvailability(t *testing.T) { }) t.Run("a definitive answer is probed once per process", func(t *testing.T) { - for _, listErr := range []error{nil, jobsExtensionOffErr} { + for _, listErr := range []error{nil, jobsExtensionOffErr, jobsNoHTTP2Err} { fake := &fakeJobsClient{listErr: listErr} tested := newJobsTestService(t, apiVersionJobs, fake) first, err := tested.jobsAvailability(t.Context(), "prj") @@ -842,6 +862,61 @@ func TestJobsAvailability(t *testing.T) { }) } +// jobsListError dials addr as a gRPC client and returns the error of the jobs +// service's List call, which must be Unavailable. +func jobsListError(t *testing.T, addr string) error { + t.Helper() + conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) + assert.NilError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + err = conn.Invoke(t.Context(), "/jobs.Jobs/List", &emptypb.Empty{}, &emptypb.Empty{}) + assert.Equal(t, status.Code(err), codes.Unavailable, "%v", err) + return err +} + +// isHTTP2Unsupported relies on the text grpc-go gives to a failed HTTP/2 +// handshake. A plain HTTP/1.1 server, like the Docker Desktop API socket, pins +// that text: a grpc-go upgrade changing it fails here instead of silently +// turning the endpoint back into an "extension not ready" failure. +func TestIsHTTP2UnsupportedAgainstHTTP1Endpoint(t *testing.T) { + srv := httptest.NewServer(http.NotFoundHandler()) // plain HTTP: it cannot speak HTTP/2 + t.Cleanup(srv.Close) + + err := jobsListError(t, strings.TrimPrefix(srv.URL, "http://")) + assert.Assert(t, isHTTP2Unsupported(err), "%v", err) +} + +// gRPC words a failure to read the HTTP/2 preface "error reading server +// preface: ..." whatever the read-side cause. Only a server answering HTTP/1.1 proves the +// endpoint has no HTTP/2: an endpoint that drops the connection (a daemon +// restarting, a proxy not ready) is a transient failure, which must stay one, +// never be cached as "jobs unsupported". Which text a real dropped connection +// yields depends on timing (a write that loses the race says "broken pipe"), so +// the synthetic causes below are the deterministic guard, the real one a check +// that a real drop does not classify. +func TestIsHTTP2UnsupportedIgnoresDroppedConnections(t *testing.T) { + lis, err := net.Listen("tcp", "127.0.0.1:0") + assert.NilError(t, err) + t.Cleanup(func() { _ = lis.Close() }) + go func() { + for { + c, err := lis.Accept() + if err != nil { + return + } + _ = c.Close() // accepted, then dropped before any HTTP/2 frame + } + }() + + err = jobsListError(t, lis.Addr().String()) + assert.Assert(t, !isHTTP2Unsupported(err), "%v", err) + + for _, cause := range []string{"EOF", "read: connection reset by peer"} { + dropped := status.Error(codes.Unavailable, `connection error: desc = "error reading server preface: `+cause+`"`) + assert.Assert(t, !isHTTP2Unsupported(dropped), cause) + } +} + func TestRejectUnsupportedScheduledJobs(t *testing.T) { t.Run("an engine with the extension accepts them", func(t *testing.T) { err := newJobsTestService(t, apiVersionJobs, &fakeJobsClient{}).rejectUnsupportedScheduledJobs(t.Context(), scheduledJobsProject()) @@ -883,6 +958,7 @@ func TestScheduledJobsRefusedWithoutJobsEngine(t *testing.T) { "API predates jobs": {version: noJobsEngine, refused: scheduledJobsRefused}, "extension off": {version: apiVersionJobs, listErr: jobsExtensionOffErr, refused: scheduledJobsExtensionOffRefused}, "extension not ready": {version: apiVersionJobs, listErr: jobsExtensionNotReadyErr, refused: jobsExtensionNotReady}, + "no HTTP/2": {version: apiVersionJobs, listErr: jobsNoHTTP2Err, refused: scheduledJobsNoHTTP2Refused}, } { t.Run(name, func(t *testing.T) { tested := newJobsTestService(t, tc.version, &fakeJobsClient{listErr: tc.listErr}) @@ -924,6 +1000,12 @@ func TestRunJobWithoutJobsEngine(t *testing.T) { assert.ErrorIs(t, err, errJobsExtensionNotReady) assert.Assert(t, !errors.Is(err, api.ErrJobsNotSupported), "no sentinel: the CLI must not degrade to a one-off") }) + + t.Run("endpoint without HTTP/2", func(t *testing.T) { + err := run(t, apiVersionJobs, jobsNoHTTP2Err) + assert.ErrorIs(t, err, api.ErrJobsNotSupported) + assert.ErrorContains(t, err, "the engine API endpoint does not support HTTP/2, which the jobs extension needs") + }) } // captureWarnings captures what the code under test tells the user. @@ -996,11 +1078,13 @@ func TestDownJobsWithoutJobsEngine(t *testing.T) { assert.Equal(t, out.String(), "") }) - t.Run("an extension that is off leaves nothing to remove, silently", func(t *testing.T) { - out := captureWarnings(t) - tested := newJobsTestService(t, apiVersionJobs, &fakeJobsClient{listErr: jobsExtensionOffErr}) - assert.Equal(t, len(tested.ensureJobsDown(t.Context(), project)), 0) - assert.Equal(t, out.String(), "") + t.Run("an extension that is off, or out of reach without HTTP/2, leaves nothing to remove, silently", func(t *testing.T) { + for _, listErr := range []error{jobsExtensionOffErr, jobsNoHTTP2Err} { + out := captureWarnings(t) + tested := newJobsTestService(t, apiVersionJobs, &fakeJobsClient{listErr: listErr}) + assert.Equal(t, len(tested.ensureJobsDown(t.Context(), project)), 0) + assert.Equal(t, out.String(), "") + } }) t.Run("an extension enabled but not ready leaves nothing to remove, with a warning", func(t *testing.T) { @@ -1063,11 +1147,13 @@ func TestActualJobs(t *testing.T) { assert.Equal(t, out.String(), "") }) - t.Run("an extension that is off lists nothing, silently", func(t *testing.T) { - out := captureWarnings(t) - tested := newJobsTestService(t, apiVersionJobs, &fakeJobsClient{listErr: jobsExtensionOffErr}) - assert.Equal(t, len(tested.actualJobs(t.Context(), "prj")), 0) - assert.Equal(t, out.String(), "") + t.Run("an extension that is off, or out of reach without HTTP/2, lists nothing, silently", func(t *testing.T) { + for _, listErr := range []error{jobsExtensionOffErr, jobsNoHTTP2Err} { + out := captureWarnings(t) + tested := newJobsTestService(t, apiVersionJobs, &fakeJobsClient{listErr: listErr}) + assert.Equal(t, len(tested.actualJobs(t.Context(), "prj")), 0) + assert.Equal(t, out.String(), "") + } }) t.Run("an extension enabled but not ready lists nothing, with a warning", func(t *testing.T) {