Skip to content

Commit 3371227

Browse files
milasndeloof
authored andcommitted
chore(desktop): revised feature detection for file shares
Unfortunately, the feature flag mechanism for experimental features isn't adequate. To avoid some edge cases where Compose might try to use Synchronized file shares with Desktop when the feature isn't available, we need to query a new endpoint. Before we move any of this out of experimental, we need to improve the integration here with Desktop & Compose so that we get all the necessary feature/experiment state up-front to reduce the quantity of IPC calls needed up-front. For now, there's some intentional redundancy to avoid making this extra call if we can avoid it. The actual endpoint is very cheap/ fast, but every IPC call is a potential point of of failure, so it's worth it. Signed-off-by: Milas Bowman <[email protected]>
1 parent e9dc820 commit 3371227

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

internal/desktop/client.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ func (c *Client) FeatureFlags(ctx context.Context) (FeatureFlagResponse, error)
123123
return ret, nil
124124
}
125125

126+
type GetFileSharesConfigResponse struct {
127+
Active bool `json:"active"`
128+
Compose struct {
129+
ManageBindMounts bool `json:"manageBindMounts"`
130+
}
131+
}
132+
133+
func (c *Client) GetFileSharesConfig(ctx context.Context) (*GetFileSharesConfigResponse, error) {
134+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/mutagen/file-shares/config"), http.NoBody)
135+
if err != nil {
136+
return nil, err
137+
}
138+
resp, err := c.client.Do(req)
139+
if err != nil {
140+
return nil, err
141+
}
142+
defer func() {
143+
_ = resp.Body.Close()
144+
}()
145+
146+
if resp.StatusCode != http.StatusOK {
147+
return nil, newHTTPStatusCodeError(resp)
148+
}
149+
150+
var ret GetFileSharesConfigResponse
151+
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
152+
return nil, err
153+
}
154+
return &ret, nil
155+
}
156+
126157
type CreateFileShareRequest struct {
127158
HostPath string `json:"hostPath"`
128159
Labels map[string]string `json:"labels,omitempty"`

pkg/compose/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
152152
}
153153

154154
err := func() error {
155-
if s.experiments.AutoFileShares() && s.isDesktopIntegrationActive() {
155+
if s.manageDesktopFileSharesEnabled(ctx) {
156156
// collect all the bind mount paths and try to set up file shares in
157157
// Docker Desktop for them
158158
var paths []string

pkg/compose/desktop.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package compose
1818

1919
import (
20+
"context"
21+
2022
"github.com/docker/compose/v2/internal/desktop"
2123
"github.com/docker/compose/v2/internal/experimental"
24+
"github.com/sirupsen/logrus"
2225
)
2326

2427
func (s *composeService) SetDesktopClient(cli *desktop.Client) {
@@ -28,3 +31,18 @@ func (s *composeService) SetDesktopClient(cli *desktop.Client) {
2831
func (s *composeService) SetExperiments(experiments *experimental.State) {
2932
s.experiments = experiments
3033
}
34+
35+
func (s *composeService) manageDesktopFileSharesEnabled(ctx context.Context) bool {
36+
// there's some slightly redundancy here to avoid fetching the config if
37+
// we can already tell the feature state - in practice, we
38+
if !s.isDesktopIntegrationActive() || !s.experiments.AutoFileShares() {
39+
return false
40+
}
41+
42+
fileSharesConfig, err := s.desktopCli.GetFileSharesConfig(ctx)
43+
if err != nil {
44+
logrus.Debugf("Failed to retrieve file shares config: %v", err)
45+
return false
46+
}
47+
return fileSharesConfig.Active && fileSharesConfig.Compose.ManageBindMounts
48+
}

pkg/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (s *composeService) ensureVolumesDown(ctx context.Context, project *types.P
145145
})
146146
}
147147

148-
if s.experiments.AutoFileShares() && s.isDesktopIntegrationActive() {
148+
if s.manageDesktopFileSharesEnabled(ctx) {
149149
ops = append(ops, func() error {
150150
desktop.RemoveFileSharesForProject(ctx, s.desktopCli, project.Name)
151151
return nil

0 commit comments

Comments
 (0)